Commit 957de63d authored by Vladislav Rykov's avatar Vladislav Rykov
Browse files

real time alerts+automation added, email alerts enriched

parent 82ef861e
...@@ -13,3 +13,4 @@ mail = Mail(app) ...@@ -13,3 +13,4 @@ mail = Mail(app)
from app import views from app import views
from app import views_admin from app import views_admin
from app.helpers import maintainer from app.helpers import maintainer
from app.helpers import notification_manager
...@@ -66,6 +66,41 @@ def create_function(cur, appkey, devid, nfid, expr): ...@@ -66,6 +66,41 @@ def create_function(cur, appkey, devid, nfid, expr):
return (True, ) return (True, )
# expr has a form of list [variable, operand, value]
# new listen/notify function
@with_psql
def create_function_rt(cur, appkey, devid, nfid, expr, action_type, action):
query = """
CREATE OR REPLACE FUNCTION
nf_{}_{}_{}()
RETURNS trigger AS
$BODY$
BEGIN
""".format(appkey, devid, nfid)
#PERFORM pg_notify('nf_channel', '{{"appkey":"{}", "devid":{}, "nfid":"{}", "data": row_to_json(NEW.*) }}');
#PERFORM pg_notify('nf_channel', json_build_object('appkey','{}','devid',{},'nfid','{}','data',NEW.data));
query += """
IF (NEW.data->>'{}')::{} {} {} THEN
PERFORM pg_notify('nf_channel', '{{"appkey":"{}", "devid":{}, "nfid":"{}", "action_type":"{}", "action":"{}", "message":' || row_to_json(NEW) || '}}');
END IF;
""".format( expr[0], get_type(expr[2]), expr[1], expr[2],
appkey, devid, nfid, action_type, action)
query += """
RETURN NEW;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
"""
print(query)
cur.execute(query)
return (True, )
@with_psql @with_psql
def delete_function(cur, appkey, devid, nfid): def delete_function(cur, appkey, devid, nfid):
query = """ query = """
......
...@@ -4,10 +4,10 @@ from flask_mail import Message ...@@ -4,10 +4,10 @@ from flask_mail import Message
import app.dao.application.application as ad import app.dao.application.application as ad
def send_mail(app, alert, alert_evt): def send_mail(app, alert, fired):
with app.app_context(): with app.app_context():
ap = ad.get(alert[1]) ap = ad.get(fired['appkey'])
msg = Message('Alert '+alert[3], recipients=[alert[6]]) msg = Message('Alert '+alert[3], recipients=[alert[6]])
context = {'alert':alert, 'username':ap[1][2], 'appname':ap[1][0], 'timestamp':alert_evt[3]} context = {'alert':alert, 'username':ap[1][2], 'appname':ap[1][0], 'message':fired['message']}
msg.html = render_template('mailing/alert.html', **context) msg.html = render_template('mailing/alert.html', **context)
mail.send(msg) mail.send(msg)
import pgpubsub import pgpubsub
from app import app from app import app
from uwsgidecorators import thread from uwsgidecorators import thread
import json
import app.dao.notification.notification as nf
import app.dao.pend.pend as pend
import app.helpers.mailer as mailer
import app.helpers.misc as misc
@thread @thread
def listening(): def listening():
...@@ -12,9 +16,21 @@ def listening(): ...@@ -12,9 +16,21 @@ def listening():
host = app.config['DB_HOST'], host = app.config['DB_HOST'],
port = app.config['DB_PORT'] port = app.config['DB_PORT']
) )
ps.listen('test') ps.listen('nf_channel')
while True: while True:
for e in ps.events(): for e in ps.events():
print(e.payload) d = json.loads(e.payload)
if d['action_type'] == 'alert':
# send mail
n = nf.get(d['appkey'], d['devid'], d['nfid'])[1]
print(n)
mailer.send_mail(app, n, d)
elif d['action_type'] == 'automation':
# enqueue confid
# action format: '<devid>#<confid>#<arg>'
action = d['action'].split('#')
base64_args = misc.pend_base64_encode(action[2], action[1])
pend.create(d['appkey'], action[0], base64_args)
listening() listening()
<p> Hello {{ username }}, </p> <p> Hello {{ username }}, </p>
<p> The alert {{ alert[3] }} from your {{ appname }} application has been fired at {{ timestamp }}. </p> <p> The alert {{ alert[3] }} from your {{ appname }} application has been fired at {{ message['timedate'] }} with the next message: {{ message['data'] }}. </p>
<p> Remember the condition of the alert was {{ alert[4] }}. </p> <p> Remember the condition of the alert was {{ alert[4] }}. </p>
<p>Take care,<br>HPC&amp;A IoT Server </p> <p>Take care,<br>HPC&amp;A IoT Server </p>
...@@ -514,7 +514,9 @@ def application_new_alert(appkey): ...@@ -514,7 +514,9 @@ def application_new_alert(appkey):
res = nfs.create(nid, appkey, request.form['devid'], request.form['alertname'], desc, 'alert', request.form['alertemail']) res = nfs.create(nid, appkey, request.form['devid'], request.form['alertname'], desc, 'alert', request.form['alertemail'])
if res[0]: if res[0]:
# create new function and trigger # create new function and trigger
tr.create_function(appkey, request.form['devid'], nid, [request.form['varname'],request.form['operation'],request.form['avalue']]) #tr.create_function(appkey, request.form['devid'], nid, [request.form['varname'],request.form['operation'],request.form['avalue']])
t = tr.create_function_rt(appkey, request.form['devid'], nid, [request.form['varname'],request.form['operation'],request.form['avalue']],'alert',request.form['alertemail'])
print(t)
tr.create(appkey, request.form['devid'], nid) tr.create(appkey, request.form['devid'], nid)
flash('Alert created', 'success') flash('Alert created', 'success')
return redirect(url_for('application_alerts', appkey=appkey)) return redirect(url_for('application_alerts', appkey=appkey))
...@@ -578,7 +580,8 @@ def application_new_automation(appkey): ...@@ -578,7 +580,8 @@ def application_new_automation(appkey):
res = nfs.create(nid, appkey, request.form['devid'], request.form['automationname'], desc, 'automation', action) res = nfs.create(nid, appkey, request.form['devid'], request.form['automationname'], desc, 'automation', action)
if res[0]: if res[0]:
# create new function and trigger # create new function and trigger
tr.create_function(appkey, request.form['devid'], nid, [request.form['varname'],request.form['operation'],request.form['avalue']]) #tr.create_function(appkey, request.form['devid'], nid, [request.form['varname'],request.form['operation'],request.form['avalue']])
t = tr.create_function_rt(appkey, request.form['devid'], nid, [request.form['varname'],request.form['operation'],request.form['avalue']],'automation', action)
tr.create(appkey, request.form['devid'], nid) tr.create(appkey, request.form['devid'], nid)
flash('Automation created', 'success') flash('Automation created', 'success')
return redirect(url_for('application_automation', appkey=appkey)) return redirect(url_for('application_automation', appkey=appkey))
......
...@@ -5,4 +5,6 @@ callable = app ...@@ -5,4 +5,6 @@ callable = app
master = true master = true
processes = 2 processes = 2
threads = 4 threads = 5
py-autoreload = 2
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