#!/usr/bin/env python3 # -*- coding: utf-8 -*- from flask import Flask from flask import render_template, jsonify import mysql.connector import copy app = Flask(__name__) def dbconnect(): return mysql.connector.connect(user='root', password='', host='127.0.0.1', database='monit') @app.route('/') def graph(): return render_template('graph.html') @app.route('/ws') def wsgraph(): return render_template('wsgraph.html') @app.route('/smooth') def smooth(): return render_template('smooth.html') @app.route('/downstream/') def downstream(name=None): valid = ['power', 'snr'] if name not in valid: return 'LOL JEWS' if name == 'power': title = 'Power (dBmV)' elif name == 'snr': title = 'SNR (dB)' output = {'data': [], 'layout': {'title': title}} template = {'x': [], 'y': [], 'type': 'lines', 'name': '', 'uid': ''} cnx = dbconnect() cursor = cnx.cursor() for x in range(0, 8): query = ("select ts, " + name + " from ajax where channel=%s and direction = 0 order by ts desc") cursor.execute(query, (x,)) recetor = copy.deepcopy(template) recetor['uid'] = 'recetor'+str(x+1) recetor['name'] = 'Canal '+str(x+1) for (ts, item) in cursor: recetor['x'].append(ts.strftime('%Y-%m-%d %H:%M:%S')) recetor['y'].append(item) output['data'].append(recetor) cursor.close() cnx.close() return jsonify(output) @app.route('/upstream/') def upstream(name=None): valid = ['power'] if name not in valid: return 'LOL JEWS' if name == 'power': title = 'Power (dBmV)' output = {'data': [], 'layout': {'title': title}} template = {'x': [], 'y': [], 'type': 'lines', 'name': '', 'uid': '', 'line': {'smoothing': 0.85}} cnx = dbconnect() cursor = cnx.cursor() for x in range(9, 11): query = ("select ts, " + name + " from ajax where channel=%s and direction = 1 order by ts desc") cursor.execute(query, (x,)) recetor = copy.deepcopy(template) recetor['uid'] = 'recetor'+str(x) recetor['name'] = 'Canal '+str(x) for (ts, item) in cursor: recetor['x'].append(ts.strftime('%Y-%m-%d %H:%M:%S')) recetor['y'].append(item) output['data'].append(recetor) cursor.close() cnx.close() return jsonify(output)