2018-05-04 13:53:29 +02:00
|
|
|
#!/usr/bin/env python3
|
2016-12-09 08:46:37 +01:00
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
|
|
|
|
'''
|
|
|
|
Flask functions and routes for the dashboard page
|
|
|
|
'''
|
2016-12-09 08:50:36 +01:00
|
|
|
import json
|
|
|
|
|
2016-12-09 13:53:57 +01:00
|
|
|
import datetime
|
2016-12-09 08:46:37 +01:00
|
|
|
import flask
|
2017-04-19 11:02:03 +02:00
|
|
|
from flask import Flask, render_template, jsonify, request, Blueprint
|
2016-12-09 08:46:37 +01:00
|
|
|
|
|
|
|
# ============ VARIABLES ============
|
|
|
|
import Flask_config
|
|
|
|
|
|
|
|
app = Flask_config.app
|
|
|
|
cfg = Flask_config.cfg
|
|
|
|
r_serv = Flask_config.r_serv
|
|
|
|
r_serv_log = Flask_config.r_serv_log
|
2017-04-19 11:02:03 +02:00
|
|
|
|
|
|
|
dashboard = Blueprint('dashboard', __name__, template_folder='templates')
|
|
|
|
|
2016-12-09 08:46:37 +01:00
|
|
|
# ============ FUNCTIONS ============
|
|
|
|
|
|
|
|
def event_stream():
|
|
|
|
pubsub = r_serv_log.pubsub()
|
|
|
|
pubsub.psubscribe("Script" + '.*')
|
|
|
|
for msg in pubsub.listen():
|
2018-05-04 13:53:29 +02:00
|
|
|
|
|
|
|
type = msg['type']
|
|
|
|
pattern = msg['pattern']
|
|
|
|
channel = msg['channel']
|
|
|
|
data = msg['data']
|
2018-04-17 16:06:32 +02:00
|
|
|
|
|
|
|
msg = {'channel': channel, 'type': type, 'pattern': pattern, 'data': data}
|
|
|
|
|
|
|
|
level = (msg['channel']).split('.')[1]
|
2016-12-09 08:46:37 +01:00
|
|
|
if msg['type'] == 'pmessage' and level != "DEBUG":
|
|
|
|
yield 'data: %s\n\n' % json.dumps(msg)
|
|
|
|
|
|
|
|
def get_queues(r):
|
|
|
|
# We may want to put the llen in a pipeline to do only one query.
|
|
|
|
newData = []
|
2018-04-17 16:06:32 +02:00
|
|
|
for queue, card in r.hgetall("queues").items():
|
2018-05-04 13:53:29 +02:00
|
|
|
|
2016-12-09 08:46:37 +01:00
|
|
|
key = "MODULE_" + queue + "_"
|
|
|
|
keySet = "MODULE_TYPE_" + queue
|
|
|
|
|
|
|
|
for moduleNum in r.smembers(keySet):
|
2018-04-17 16:06:32 +02:00
|
|
|
|
2018-05-04 13:53:29 +02:00
|
|
|
value = r.get(key + str(moduleNum))
|
2018-04-17 16:06:32 +02:00
|
|
|
|
2016-12-09 08:46:37 +01:00
|
|
|
if value is not None:
|
|
|
|
timestamp, path = value.split(", ")
|
|
|
|
if timestamp is not None:
|
|
|
|
startTime_readable = datetime.datetime.fromtimestamp(int(timestamp))
|
|
|
|
processed_time_readable = str((datetime.datetime.now() - startTime_readable)).split('.')[0]
|
|
|
|
seconds = int((datetime.datetime.now() - startTime_readable).total_seconds())
|
|
|
|
newData.append( (queue, card, seconds, moduleNum) )
|
|
|
|
else:
|
|
|
|
newData.append( (queue, cards, 0, moduleNum) )
|
|
|
|
|
|
|
|
return newData
|
|
|
|
|
|
|
|
# ============ ROUTES ============
|
|
|
|
|
2017-04-19 11:02:03 +02:00
|
|
|
@dashboard.route("/_logs")
|
2016-12-09 08:46:37 +01:00
|
|
|
def logs():
|
|
|
|
return flask.Response(event_stream(), mimetype="text/event-stream")
|
|
|
|
|
|
|
|
|
2017-04-19 11:02:03 +02:00
|
|
|
@dashboard.route("/_stuff", methods=['GET'])
|
2016-12-09 08:46:37 +01:00
|
|
|
def stuff():
|
|
|
|
return jsonify(row1=get_queues(r_serv))
|
|
|
|
|
|
|
|
|
2017-04-19 11:02:03 +02:00
|
|
|
@dashboard.route("/")
|
2016-12-09 08:46:37 +01:00
|
|
|
def index():
|
|
|
|
default_minute = cfg.get("Flask", "minute_processed_paste")
|
|
|
|
threshold_stucked_module = cfg.getint("Module_ModuleInformation", "threshold_stucked_module")
|
|
|
|
return render_template("index.html", default_minute = default_minute, threshold_stucked_module=threshold_stucked_module)
|
2017-04-19 11:02:03 +02:00
|
|
|
|
|
|
|
# ========= REGISTRATION =========
|
|
|
|
app.register_blueprint(dashboard)
|