Commit 33e4d418 authored by Vladislav Rykov's avatar Vladislav Rykov
Browse files

admin:alerts view completely integrated

parent b655c245
...@@ -54,7 +54,7 @@ ...@@ -54,7 +54,7 @@
{% endif %} {% endif %}
<br> <br>
<div class="col-lg-4"> <div class="col-lg-4">
<a href="/administration/{{ user }}/application/{{ app[1] }}/new-alert"><button class="btn btn-primary" type="submit">New Alert</button></a> <a href="/administration/users/{{ user }}/application/{{ app[1] }}/new-alert"><button class="btn btn-primary" type="submit">New Alert</button></a>
</div> </div>
</div> </div>
...@@ -67,7 +67,7 @@ ...@@ -67,7 +67,7 @@
<script type="text/javascript"> <script type="text/javascript">
function remove_alert(id, devid) { function remove_alert(id, devid) {
$.ajax({ $.ajax({
url:"/administration/{{ name }}/application/{{ app[1] }}/delete-alert?id="+id+"&devid="+devid, url:"/administration/users/{{ user }}/application/{{ app[1] }}/delete-alert?id="+id+"&devid="+devid,
type:"get", type:"get",
success: function() { success: function() {
location.reload(); location.reload();
......
{% extends 'logged_layout.html' %}
{% block title %} Administration - {{ name }} - {{ app[1] }} - New Alert HPC&amp;A IoT {% endblock %}
{% block location %}
<a class="h4 mb-0 text-white text-uppercase d-none d-lg-inline-block" href="/administration">Administration</a>
<p class="h4 mb-0 text-white text-uppercase d-none d-lg-inline-block"> &nbsp;-&nbsp; </p>
<a class="h4 mb-0 text-white text-uppercase d-none d-lg-inline-block" href="/administration/users">Users</a>
<p class="h4 mb-0 text-white text-uppercase d-none d-lg-inline-block"> &nbsp;-&nbsp; </p>
<a class="h4 mb-0 text-white text-uppercase d-none d-lg-inline-block" href="/administration/users/{{ user }}">{{ user }}</a>
<p class="h4 mb-0 text-white text-uppercase d-none d-lg-inline-block"> &nbsp;-&nbsp; </p>
<a class="h4 mb-0 text-white text-uppercase d-none d-lg-inline-block" href="/administration/users/{{ user }}/applications">Applications</a>
<p class="h4 mb-0 text-white text-uppercase d-none d-lg-inline-block"> &nbsp;-&nbsp; </p>
<a class="h4 mb-0 text-white text-uppercase d-none d-lg-inline-block" href="/administration/users/{{ user }}/application/{{ app[1] }}">{{ app[0] }}</a>
{% endblock %}
{% block body %}
<!-- Page content -->
<div class="container-fluid mt--7">
<!-- Table -->
<div class="row">
<div class="col">
<div class="card shadow">
<div class="card-header bg-transparent">
<h3 class="mb-0">New Alert</h3>
</div>
<div class="card-body">
<form action="/administration/users/{{ user }}/application/{{ app[1] }}/new-alert" method="post">
<div class="form-group">
<label>Name:</label><br>
<input type="text" maxlength="30" class="form-control" id="alertname" name="alertname" required><br>
</div>
<h3> Condition: </h3>
<h4> IF </h4>
<select class="form-control" id="devid" name="devid" onchange="return ondev();" required>
<option default value="-">Select Device</option>
{% for d in devs %}
<option value="{{ d[1] }}">{{ d[0] }}</option>
{% endfor %}
</select>
<br>
<select class="form-control" id="varname" name="varname" onchange="return validate_form();" required>
<option value="-">Select variable</option>
</select>
<br>
<select class='form-control notifelem' name='operation' id='operation' required>
<option default>></option>
<option>>=</option>
<option><</option>
<option><=</option>
<option>=</option>
</select>
<br>
<input type='text' class='form-control notifelem' name='avalue' id='avalue' placeholder='Value' title='use n (e.g. 5) for integer notation, n.n (e.g. 5.0) for float, and true or false for boolean' required>
<p> Use n (e.g. 5) for integer notation, n.n (e.g. 5.0) for float, and true or false for boolean. <br>
Keep types consistently for each variable for both, alerts and automations. </p>
<br>
<h4> THEN </h4>
<h5> Send email to: </h5>
<input class="form-control" type="email" id="alertemail" name="alertemail" placeholder="Type email..." required>
<br><br>
<div class="form-group">
<button type="submit" id="submit" class="btn btn-primary">Create Alert</button>
</div>
</form>
</div>
</div>
</div>
</div>
{% endblock %}
{% block script %}
<script type="text/javascript">
$("#submit").prop("disabled", true);
function validate_form() {
var dev_sel = document.getElementById("devid");
var sel_op = dev_sel.options[dev_sel.selectedIndex].value;
var var_sel = document.getElementById("varname");
var sel_var = var_sel.options[var_sel.selectedIndex].value;
if (sel_op == "-" || sel_var == "-") {
$("#submit").prop("disabled", true);
console.log("not validated: "+sel_op+" "+sel_var);
} else {
$("#submit").prop("disabled", false);
console.log("validated: "+sel_op+" "+sel_var);
}
}
function ondev() {
var dev_sel = document.getElementById("devid");
var sel_op = dev_sel.options[dev_sel.selectedIndex].value;
if (sel_op != "-") {
fetch("/administration/users/{{ user }}/application/{{ app[1] }}/device/"+sel_op+"/variables").then(res => res.text()).then(data => $("#varname").html(data));
}
validate_form();
}
</script>
{% endblock %}
...@@ -171,6 +171,64 @@ def administration_users_user_application_alerts(name, appkey): ...@@ -171,6 +171,64 @@ def administration_users_user_application_alerts(name, appkey):
return render_template('new/admin/user-application-alerts.html', alert_list=alerts[1], app=ap[1], user=name) return render_template('new/admin/user-application-alerts.html', alert_list=alerts[1], app=ap[1], user=name)
@app.route('/administration/users/<name>/application/<appkey>/new-alert', methods=['GET', 'POST'])
@restricted(access_level='admin')
def administration_users_user_application_new_alert(name, appkey):
if request.method == 'GET':
ap = ad.get(appkey)
devs = dd.get_list(appkey)
return render_template('new/admin/user-new-alert.html', devs=devs[1], app=ap[1], user=name)
elif request.method == 'POST':
# create new notification
nid = misc.rand_str(app.config['NID_LENGTH']).decode('utf-8')
dev = dd.get(appkey, request.form['devid'])
try:
desc = dev[1][0]+'.'+request.form['varname']+' '+request.form['operation']+' '+request.form['avalue']
res = nfs.create(nid, appkey, request.form['devid'], request.form['alertname'], desc, 'alert', request.form['alertemail'])
if res[0]:
# create new function and trigger
tr.create_function(appkey, request.form['devid'], nid, [request.form['varname'],request.form['operation'],request.form['avalue']])
tr.create(appkey, request.form['devid'], nid)
flash('Alert created', 'success')
return redirect(url_for('administration_users_user_application_alerts', name=name, appkey=appkey))
else:
flash('Error creating new alert: {}'.format(res[1]), 'danger')
return redirect(request.url)
except Exception as e:
flash('Error creating new alert: {}. Make sure you have filled all form fields.'.format(e), 'danger')
return redirect(request.url)
@app.route('/administration/users/<name>/application/<appkey>/delete-<ntype>')
@restricted(access_level='admin')
def administration_users_user_application_notification_remove(name, appkey, ntype):
nq.delete(appkey, request.args.get('devid'), request.args.get('id'))
tr.delete(appkey, request.args.get('devid'), request.args.get('id'))
tr.delete_function(appkey, request.args.get('devid'), request.args.get('id'))
res = nfs.delete(appkey, request.args.get('devid'), request.args.get('id'))
if res[0]:
flash('{} removed'.format(ntype.capitalize()), 'success')
return '', 200
else:
flash('{} cannot be removed : {}'.format(ntype.capitalize(), res[1]), 'danger')
return '', 500
@app.route('/administration/users/<name>/application/<appkey>/device/<devid>/variables')
@restricted(access_level='admin')
def administration_users_user_application_device_variables(name, appkey, devid):
last = data.get_last_n(appkey, devid, 1)
if last[0]:
select = '<select class="form-control" id="varname" name="varname" onchange="validate_form();" required>'
select += '<option value="-">Select Variable</option>'
for k in last[1][0][2]:
select += '<option>'+k+'</option>'
select += '</select>'
return select
@app.route('/administration/users/<name>/application/<appkey>/device/<devid>/data/<var>/<dest>/<page>') @app.route('/administration/users/<name>/application/<appkey>/device/<devid>/data/<var>/<dest>/<page>')
@restricted(access_level='admin') @restricted(access_level='admin')
def administration_users_user_application_device_data(name, appkey, devid, var, dest, page): def administration_users_user_application_device_data(name, appkey, devid, var, dest, page):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment