Commit e575951a authored by Vladislav Rykov's avatar Vladislav Rykov
Browse files

automations & new automation views added

parent 43559947
...@@ -62,6 +62,36 @@ def get_list(cur, appkey): ...@@ -62,6 +62,36 @@ def get_list(cur, appkey):
return (True, cur.fetchall()) return (True, cur.fetchall())
@with_psql
def get_alerts_list(cur, appkey):
query = """
SELECT * FROM
notifications
WHERE
app_key = %s
AND
action_type = 'alert'
"""
cur.execute(query, (appkey,))
return (True, cur.fetchall())
@with_psql
def get_automation_list(cur, appkey):
query = """
SELECT * FROM
notifications
WHERE
app_key = %s
AND
action_type = 'automation'
"""
cur.execute(query, (appkey,))
return (True, cur.fetchall())
@with_psql @with_psql
def get_count(cur): def get_count(cur):
query = """ query = """
......
...@@ -48,6 +48,7 @@ ...@@ -48,6 +48,7 @@
<center> <center>
<a href="/add-dev"><button type="submit" class="btn btn-primary">Add Device</button></a> <a href="/add-dev"><button type="submit" class="btn btn-primary">Add Device</button></a>
<a href="/alerts"><button type="submit" class="btn btn-primary">Alerts</button></a> <a href="/alerts"><button type="submit" class="btn btn-primary">Alerts</button></a>
<a href="/automation"><button type="submit" class="btn btn-primary">Automation</button></a>
<a href="/delete-app"><button type="submit" class="btn btn-danger" onclick="return confirm('Are you sure? It will remove all devices and data.');">Delete Application</button></a> <a href="/delete-app"><button type="submit" class="btn btn-danger" onclick="return confirm('Are you sure? It will remove all devices and data.');">Delete Application</button></a>
</center> </center>
<br> <br>
......
{% extends 'layout.html' %}
{% block title %} Application Automation: {% endblock %}
{% block content %}
<div class="row">
<div class="col-md-3">
<div class="clickback">
<span class="glyphicon glyphicon-arrow-left"></span>
<p><a class="backlink" onclick="history.back(-1)"></a></p>
</div>
</div>
<div class="col-md-6">
{% if auto_list %}
<div>
<table class="table">
<thead>
<th> Name </th>
<th> Condition </th>
<th> Action </th>
<th> </th>
</thead>
<tbody>
{% for a in auto_list %}
<tr>
<th> {{ a[3] }} </th>
<th> {{ a[4] }} </th>
<th> {{ a[6] }} </th>
<th> <a href="/automation-rm?id={{ a[0] }}&devid={{ a[2] }}"> <span class="glyphicon glyphicon-remove"</span> </a> </th>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% else %}
<center> There is no automation for a given application. </center>
{% endif %}
<br>
<center><a href="/new-automation"><button class="btn btn-primary" type="submit">Add New Automation</button></a></center>
</div>
</div>
{% endblock %}
{% extends 'layout.html' %}
{% block title %} New Automation {% endblock %}
{% block content %}
<div class="row">
<div class="col-md-3">
<div class="clickback">
<span class="glyphicon glyphicon-arrow-left"></span>
<p><a class="backlink" onclick="history.back(-1)"></a></p>
</div>
</div>
<div class="col-md-6">
<h2> Add New Automation </h2>
<br><br>
<form id="automationform" action="alert" method="post">
<div class="form-group">
<label>Name:</label><br>
<input type="text" maxlength="30" class="form-control" id="automationname" name="automationname" required><br>
</div>
<h3> Condition: </h3>
<h4> IF </h4>
<select class="form-control" id="devid" name="devid" onchange="ondev()" required>
<option default value="-">Select Device</option>
{% for d in devs %}
<option value="{{ d[1] }}">{{ d[0] }}</option>
{% endfor %}
</select>
</form>
{% if feedback %}
<p class="text-danger float-right">{{ feedback }}</p>
{% endif %}
<br>
<h4> THEN </h4>
<h5> Set config on </h5>
<div id="THEN">
<select class="form-control" id="adevid" name="adevid" onchange="onadev()" required>
<option default value="-">Select Device</option>
{% for d in devs %}
<option value="{{ d[1] }}">{{ d[0] }}</option>
{% endfor %}
</select>
</div>
<br>
<div class="form-group">
<button form="automationform" type="submit" class="btn btn-primary">Create Alert</button>
</div>
</div>
</div>
<script type="text/javascript">
function ondev() {
var dev_sel = document.getElementById("devid");
var sel_op = dev_sel.options[dev_sel.selectedIndex].value;
if (!document.getElementById("varname")) {
if (sel_op != "-") {
fetch(window.origin+'/dev-vars?id='+sel_op).then(res => res.text()).then(data => $("#automationform").append(data));
}
} else {
if (sel_op == "-") {
$("#varname").remove();
if (document.getElementById("operation")) {
$("#operation").remove();
}
if (document.getElementById("avalue")) {
$("#avalue").remove();
}
}
}
}
function onvar() {
if (!document.getElementById("operation")) {
$("#automationform").append("<select class='form-control notifelem' name='operation' id='operation' required><option default>></option><option>>=</option><option><</option><option><=</option><option>=</option></select>");
$("#automationform").append("<input type='numeric' class='form-control notifelem' name='avalue' id='avalue' placeholder='Value' required>");
} else {
var var_sel = document.getElementById("varname");
var sel_var = var_sel.options[var_sel.selectedIndex].value;
if (sel_var == "-") {
$("#avalue").remove();
$("#operation").remove();
}
}
}
function onadev() {
var dev_sel = document.getElementById("adevid");
var sel_op = dev_sel.options[dev_sel.selectedIndex].value;
if (sel_op != "-") {
$("#THEN").append("<input type='number' size='3' min='0' max='255' class='form-control notifelem' id='confid' name='confid' placeholder='Conf ID' required>");
$("#THEN").append("<textarea type='text' maxlength='50' class='form-control notifelem' id='arg' name='arg' rows='2' placeholder='Args' reqiured></textarea>");
} else {
$("#confid").remove();
$("#arg").remove();
}
}
</script>
{% endblock %}
...@@ -523,7 +523,7 @@ def dev_data(var, dest, page): ...@@ -523,7 +523,7 @@ def dev_data(var, dest, page):
@app.route('/alerts') @app.route('/alerts')
def alerts(): def alerts():
if 'name' in session: if 'name' in session:
alerts = nfs.get_list(session['appkey']) alerts = nfs.get_alerts_list(session['appkey'])
return render_template('public/alerts.html', alert_list=alerts[1]) return render_template('public/alerts.html', alert_list=alerts[1])
else: else:
return redirect(url_for('index')) return redirect(url_for('index'))
...@@ -580,6 +580,26 @@ def alarm_rm(): ...@@ -580,6 +580,26 @@ def alarm_rm():
else: else:
return redirect(url_for('index')) return redirect(url_for('index'))
@app.route('/automation', methods=['GET','POST'])
def automation():
if 'name' in session:
if request.method == 'GET':
auto = nfs.get_automation_list(session['appkey'])
return render_template('public/automation.html', auto_list=auto[1])
else:
return redirect(url_for('index'))
@app.route('/new-automation')
def new_automation():
if 'name' in session:
devs = dd.get_list(session['appkey'])
return render_template('public/new-automation.html', devs=devs[1])
else:
return redirect(url_for('index'))
def pend_delete_all_ack(): def pend_delete_all_ack():
pend.delete_all_ack() pend.delete_all_ack()
......
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