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