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

alert internal logic done

parent c18005e8
......@@ -13,7 +13,7 @@ def create(cur, nid, appkey, devid, name, desc, action_type, action):
return (True,)
@with_psql
def delete(cur, appkey, nid):
def delete(cur, appkey, nid, devid):
query = """
DELETE FROM
notifications
......@@ -21,8 +21,10 @@ def delete(cur, appkey, nid):
id = %s
AND
app_key = %s
AND
dev_id = %s
"""
cur.execute(query, (nid, appkey))
cur.execute(query, (nid, appkey, devid))
return (True,)
......
......@@ -44,6 +44,17 @@ def get(cur, appkey, nid):
else:
return (True, nf)
@with_psql
def delete_list(cur, appkey):
query = """
DELETE FROM
notifications_queue
WHERE
app_key = %s
"""
cur.execute(query, (appkey,))
return (True,)
@with_psql
def get_all(cur):
......
......@@ -3,10 +3,7 @@ import ast
# expr has a form of list [variable, operand, value]
@with_psql
def create(cur, appkey, devid, nfid, expr):
res = create_function(appkey, devid, nfid, expr)
if res[0]:
def create(cur, appkey, devid, nfid):
query = """
CREATE TRIGGER tr_{}_{}_{}
AFTER INSERT
......@@ -20,22 +17,19 @@ def create(cur, appkey, devid, nfid, expr):
cur.execute(query)
return (True,)
return res
@with_psql
def delete(cur, appkey, devid, nfid):
res = delete_function(appkey, devid, nfid)
if res[0]:
query = """
DROP TRIGGER
tr_{}_{}_{}
""".format(appkey, devid, nfid)
ON dev_{}_{}
""".format( appkey, devid, nfid,
appkey, devid)
cur.execute(query)
return (True,)
return res
# expr has a form of list [variable, operand, value]
@with_psql
......@@ -48,23 +42,15 @@ def create_function(cur, appkey, devid, nfid, expr):
BEGIN
""".format(appkey, devid, nfid)
if expr[1] == 'CHANGES':
query += """
INSERT INTO
notifications_queue
VALUES
({},{},{})
""".format(nfid, appkey, devid)
else:
query += """
IF (NEW data->>'{}')::{} {} {} THEN
IF (NEW.data->>'{}')::{} {} {} THEN
INSERT INTO
notifications_queue
VALUES
({},{},{});
('{}','{}',{});
END IF;
""".format( expr[0], get_type(expr[2]), expr[1], expr[2],
ndif, appkey, devid)
nfid, appkey, devid)
query += """
RETURN NEW;
......@@ -75,7 +61,6 @@ def create_function(cur, appkey, devid, nfid, expr):
COST 100;
"""
print(query)
cur.execute(query)
return (True, )
......@@ -99,9 +84,13 @@ def delete_function(cur, appkey, devid, nfid):
# str -> text
def get_type(tstr):
tstr = tstr.strip()
if tstr == 'true':
tstr = 'True'
elif tstr == 'false':
tstr = 'False'
try:
# int, float, bool
t = ast.literal_eval(tstr).__name__
t = type(ast.literal_eval(tstr)).__name__
except:
t = 'text'
......
......@@ -25,7 +25,7 @@
<tr>
<th> {{ a[3] }} </th>
<th> {{ a[4] }} </th>
<th> <a href="/alert-rm?id={{ a[0] }}"> <span class="glyphicon glyphicon-remove"</span> </a> </th>
<th> <a href="/alert-rm?id={{ a[0] }}&devid={{ a[2] }}"> <span class="glyphicon glyphicon-remove"</span> </a> </th>
</tr>
{% endfor %}
</tbody>
......
......@@ -72,23 +72,14 @@
}
function onvar() {
if (!document.getElementById("operation")) {
$("#alertform").append("<select class='form-control notifelem' name='operation' id='operation' onchange='onop()' required><option default>CHANGES</option><option>></option><option>>=</option><option><</option><option><=</option><option>==</option></select>");
$("#alertform").append("<select class='form-control notifelem' name='operation' id='operation' required><option default>></option><option>>=</option><option><</option><option><=</option><option>=</option></select>");
$("#alertform").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 == "-") {
$("#operation").remove();
}
}
}
function onop() {
var op_sel = document.getElementById("operation");
var sel_op = op_sel.options[op_sel.selectedIndex].text;
if (sel_op != "CHANGES" && !document.getElementById("avalue")) {
$("#alertform").append("<input type='numeric' class='form-control notifelem' name='avalue' id='avalue' placeholder='Value' required>");
} else {
if (document.getElementById("avalue")) {
$("#avalue").remove();
$("#operation").remove();
}
}
}
......
......@@ -10,6 +10,7 @@ import app.dao.pend.pend as pend
import app.dao.data.data as data
import app.dao.notification.notification as nfs
import app.dao.trigger.trigger as tr
import app.dao.notification_queue.notification_queue as nq
import app.helpers.misc as misc
......@@ -542,22 +543,24 @@ def alert():
# create new notification
nid = misc.rand_str(app.config['NID_LENGTH']).decode('utf-8')
dev = dd.get(session['appkey'], request.form['devid'])
avalue = ''
try:
desc_ext = request.form['alertdesc'] + ' (Application '+session['appname']+' => '+dev[1][0]+'.'+request.form['varname']+' '+request.form['operation']+' '+request.form['avalue']+')'
except:
desc_ext = request.form['alertdesc'] + ' (Application '+session['appname']+' => '+dev[1][0]+'.'+request.form['varname']+' '+request.form['operation']+')'
desc_ext = request.form['alertdesc'] + ' (Application '+session['appname']+' => '+dev[1][0]+'.'+request.form['varname']+' '+request.form['operation']
if request.form['operation'] == 'CHANGES':
desc_ext += ')'
else:
desc_ext += ' '+request.form['avalue']+')'
res = nfs.create(nid, session['appkey'], request.form['devid'], request.form['alertname'], desc_ext, 'alert', request.form['alertemail'])
if res[0]:
print('notification created')
# create new function and trigger
tr.create_function(session['appkey'], request.form['devid'], nid, [request.form['varname'],request.form['operation'],avalue])
tr.create(session['appkey'], request.form['devid'], nid)
return redirect(url_for('alerts'))
else:
flash('Error creating new notification: {}'.format(res[1]), 'danger')
return redirect(url_for('alerts'))
# create new function and trigger
else:
return redirect(url_for('index'))
else:
......@@ -566,7 +569,10 @@ def alert():
@app.route('/alert-rm')
def alarm_rm():
if 'name' in session:
res = nfs.delete(session['appkey'], request.args.get('id'))
nq.delete_list(session['appkey'])
tr.delete(session['appkey'], request.args.get('devid'), request.args.get('id'))
tr.delete_function(session['appkey'], request.args.get('devid'), request.args.get('id'))
res = nfs.delete(session['appkey'], request.args.get('id'), request.args.get('devid'))
if res[0]:
flash('Alert removed', 'success')
......
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