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

alert implementation finished

parent 7aa3d5d2
from flask import Flask
from flask_mail import Mail
app = Flask(__name__)
......@@ -7,5 +8,7 @@ if app.config['ENV'] == 'production':
else:
app.config.from_object('config.DevelopmentConfig')
mail = Mail(app)
from app import views
from app.helpers import maintainer
......@@ -13,7 +13,7 @@ def create(cur, nid, appkey, devid, name, desc, action_type, action):
return (True,)
@with_psql
def delete(cur, appkey, nid, devid):
def delete(cur, appkey, devid, nid):
query = """
DELETE FROM
notifications
......@@ -29,7 +29,7 @@ def delete(cur, appkey, nid, devid):
return (True,)
@with_psql
def get(cur, appkey, nid):
def get(cur, appkey, devid, nid):
query = """
SELECT * FROM
notifications
......@@ -37,8 +37,10 @@ def get(cur, appkey, nid):
id = %s
AND
app_key = %s
AND
dev_id = %s
"""
cur.execute(query, (nid, appkey))
cur.execute(query, (nid, appkey, devid))
nf = cur.fetchone()
if nf is None:
......
......@@ -13,7 +13,7 @@ def create(cur, nid, appkey, devid):
return (True,)
@with_psql
def delete(cur, appkey, nid):
def delete(cur, appkey, devid, nid):
query = """
DELETE FROM
notifications_queue
......@@ -21,13 +21,15 @@ def delete(cur, appkey, nid):
nf_id = %s
AND
app_key = %s
AND
dev_id = %s
"""
cur.execute(query, (nid, appkey))
cur.execute(query, (nid, appkey, devid))
return (True,)
@with_psql
def get(cur, appkey, nid):
def get(cur, appkey, devid, nid):
query = """
SELECT * FROM
notifications_queue
......@@ -35,8 +37,10 @@ def get(cur, appkey, nid):
nf_id = %s
AND
app_key = %s
AND
dev_id = %s
"""
cur.execute(query, (nid, appkey))
cur.execute(query, (nid, appkey, devid))
nf = cur.fetchone()
if nf is None:
......@@ -60,7 +64,7 @@ def delete_list(cur, appkey):
def get_all(cur):
query = """
SELECT * FROM
notificationis_queue
notifications_queue
"""
cur.execute(query)
......
......@@ -83,6 +83,7 @@ def delete_function(cur, appkey, devid, nfid):
# bool -> bool
# str -> text
def get_type(tstr):
print('before get_type', tstr)
tstr = tstr.strip()
if tstr == 'true':
tstr = 'True'
......@@ -91,7 +92,8 @@ def get_type(tstr):
try:
# int, float, bool
t = type(ast.literal_eval(tstr)).__name__
except:
except Exception as e:
print ('expection', e, ' -> ', ' from ', tstr)
t = 'text'
if t == 'float':
......
from app import mail
from flask import render_template, session
from flask_mail import Message
import app.dao.application.application as ad
def send_mail(app, alert, alert_evt):
with app.app_context():
ap = ad.get(alert[1])
msg = Message('Alert '+alert[3], recipients=[alert[6]])
context = {'alert':alert, 'username':ap[1][2], 'appname':ap[1][0], 'timestamp':alert_evt[3]}
msg.html = render_template('mailing/alert.html', **context)
mail.send(msg)
......@@ -14,6 +14,10 @@ def maintainer():
views.pend_delete_all_ack()
clean_data_folder()
def fire_notifications():
views.fire_notifications(app)
scheduler = BackgroundScheduler()
job = scheduler.add_job(maintainer, 'interval', minutes=app.config['MAINTAINER_INTERVAL'])
scheduler.add_job(maintainer, 'interval', minutes=app.config['MAINTAINER_INTERVAL'])
scheduler.add_job(fire_notifications, 'interval', minutes=app.config['FIRE_NOTIFICATIONS_INTERVAL'])
scheduler.start()
<h4> Hello {{ username }}, </h4>
<p> Hello {{ username }}, </p>
<p> The alert {{ alertname }} from your {{ appname }} application is has been fired at {{ timestamp }}. </p>
<p> Remember the condition of the alert was {{ condition }}. </p>
<p> The alert {{ alert[3] }} from your {{ appname }} application has been fired at {{ timestamp }}. </p>
<p> Remember the condition of the alert was {{ alert[4] }}. </p>
<p>Take care,</p>
<p>HPC&amp;A IoT Server </p>
<p>Take care,<br>HPC&amp;A IoT Server </p>
from app import app
from flask_mail import Mail, Message
from app import app, mail
from flask_mail import Message
from flask import render_template, request, redirect, url_for, session, send_from_directory, flash
from flask_mail import Message
import psycopg2
import app.dao.user.user as ud
......@@ -15,6 +14,7 @@ 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.mailer as mailer
import binascii
import os
......@@ -25,8 +25,6 @@ MAX_PG_ENTRIES_USERS = 10
MAX_PG_ENTRIES_DATA = 10
MAX_PG_ENTRIES_GRAPH_HOURS = 24
mail = Mail(app)
@app.route('/')
def index():
if 'name' in session and len(session['name']) > 0:
......@@ -526,7 +524,6 @@ def dev_data(var, dest, page):
def alerts():
if 'name' in session:
alerts = nfs.get_list(session['appkey'])
print(alerts)
return render_template('public/alerts.html', alert_list=alerts[1])
else:
return redirect(url_for('index'))
......@@ -547,16 +544,16 @@ 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 = ''
desc = dev[1][0]+'.'+request.form['varname']+' '+request.form['operation']+' '+request.form['avalue']
res = nfs.create(nid, session['appkey'], request.form['devid'], request.form['alertname'], desc, 'alert', request.form['alertemail'])
if res[0]:
# 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)
r = tr.create_function(session['appkey'], request.form['devid'], nid, [request.form['varname'],request.form['operation'],request.form['avalue']])
print ('tr.create_function', r)
r = tr.create(session['appkey'], request.form['devid'], nid)
print('tr.create', r)
return redirect(url_for('alerts'))
else:
flash('Error creating new notification: {}'.format(res[1]), 'danger')
......@@ -572,7 +569,7 @@ def alarm_rm():
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'))
res = nfs.delete(session['appkey'], request.args.get('devid'), request.args.get('id'))
if res[0]:
flash('Alert removed', 'success')
......@@ -583,22 +580,22 @@ def alarm_rm():
else:
return redirect(url_for('index'))
@app.route('/mail')
def send_mail():
print (1)
msg = Message('test message',
sender = 'hpcaiotserver@gmail.com',
recipients = ['al373630@uji.es'])
print (2)
msg.body = 'Hello vlad, alert is here!'
print (3)
res = mail.send(msg)
print (res)
return res
def pend_delete_all_ack():
pend.delete_all_ack()
def fire_notifications(app):
fnfs = nq.get_all()
print ('nq.get_all ', fnfs)
if fnfs[0]:
for fnf in fnfs[1]:
nf = nfs.get(fnf[1], fnf[2], fnf[0])
print('nfs.get ', nf)
if nf[1][5] == 'alert':
# send mail
mailer.send_mail(app, nf[1], fnf)
nq.delete(fnf[1], fnf[2], fnf[0])
print('send alert mail')
elif nf[1][5] == 'automation':
# enqueue conf id
print('automation')
......@@ -30,6 +30,10 @@ class Config(object):
MAIL_USE_SSL = True
MAIL_USERNAME = 'hpcaiotserver@gmail.com'
MAIL_PASSWORD = 'HPC&A10T.'
MAIL_DEFAULT_SENDER = 'hpcaiotserver@gmail.com'
# in minutes
FIRE_NOTIFICATIONS_INTERVAL = 1
class ProductionConfig(Config):
pass
......
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