2018-04-21 18:18:20 -04:00
|
|
|
from flask_restful import Resource
|
|
|
|
from pygal.style import Style
|
|
|
|
from master import ctx
|
|
|
|
import pygal
|
|
|
|
import timeago
|
|
|
|
from math import ceil
|
|
|
|
|
|
|
|
class HistoryGraph(Resource):
|
|
|
|
def get(self, history_count):
|
|
|
|
try:
|
|
|
|
custom_style = Style(
|
|
|
|
background='transparent',
|
|
|
|
plot_background='transparent',
|
2018-04-24 18:01:27 -04:00
|
|
|
foreground='#6c757d',
|
|
|
|
foreground_strong='#6c757d',
|
|
|
|
foreground_subtle='#6c757d',
|
2018-04-21 18:18:20 -04:00
|
|
|
opacity='0.1',
|
|
|
|
opacity_hover='0.2',
|
2018-04-24 18:01:27 -04:00
|
|
|
transition='0ms',
|
|
|
|
colors=('#749363','#007acc'),
|
2018-04-21 18:18:20 -04:00
|
|
|
)
|
|
|
|
|
2018-04-24 18:01:27 -04:00
|
|
|
graph = pygal.Line(
|
2018-04-21 18:18:20 -04:00
|
|
|
stroke_style={'width': 0.4},
|
2018-04-24 18:01:27 -04:00
|
|
|
#show_dots=False,
|
2018-04-21 18:18:20 -04:00
|
|
|
show_legend=False,
|
|
|
|
fill=True,
|
|
|
|
style=custom_style,
|
|
|
|
disable_xml_declaration=True)
|
|
|
|
|
|
|
|
instance_count = [history['time'] for history in ctx.history.instance_history][-history_count:]
|
|
|
|
|
|
|
|
if len(instance_count) > 0:
|
|
|
|
graph.x_labels = [ timeago.format(instance_count[0])]
|
|
|
|
|
2018-04-23 17:03:50 -04:00
|
|
|
instance_counts = [history['count'] for history in ctx.history.instance_history][-history_count:]
|
|
|
|
client_counts = [history['count'] for history in ctx.history.client_history][-history_count:]
|
2018-05-21 17:09:27 -04:00
|
|
|
server_counts = [history['count'] for history in ctx.history.server_history][-history_count:]
|
2018-04-23 17:03:50 -04:00
|
|
|
|
|
|
|
graph.add('Client Count', client_counts)
|
2018-04-24 18:01:27 -04:00
|
|
|
graph.add('Instance Count', instance_counts)
|
|
|
|
return { 'message' : graph.render().replace("<title>Pygal</title>", ""),
|
2018-04-23 17:03:50 -04:00
|
|
|
'data_points' : len(instance_count),
|
|
|
|
'instance_count' : 0 if len(instance_counts) is 0 else instance_counts[-1],
|
2018-05-21 17:09:27 -04:00
|
|
|
'client_count' : 0 if len(client_counts) is 0 else client_counts[-1],
|
|
|
|
'server_count' : 0 if len(server_counts) is 0 else server_counts[-1]
|
2018-04-21 18:18:20 -04:00
|
|
|
}, 200
|
|
|
|
except Exception as e:
|
|
|
|
return { 'message' : str(e) }, 500
|