diff options
author | Lukas Metzger | 2020-05-27 14:17:31 +0200 |
---|---|---|
committer | Lukas Metzger | 2020-05-27 14:17:31 +0200 |
commit | 509fe8d753af4c3915d05ee38a919cda7d574f63 (patch) | |
tree | cd0926733b6972215e95c8bb41865d208e9c0304 | |
parent | Added favicon (diff) | |
download | bwlp-statistics-509fe8d753af4c3915d05ee38a919cda7d574f63.tar.gz bwlp-statistics-509fe8d753af4c3915d05ee38a919cda7d574f63.tar.xz bwlp-statistics-509fe8d753af4c3915d05ee38a919cda7d574f63.zip |
Added VMs page
-rw-r--r-- | index.py | 3 | ||||
-rw-r--r-- | pages/vms.py | 139 |
2 files changed, 141 insertions, 1 deletions
@@ -9,13 +9,14 @@ import plotly.graph_objects as go import plotly.express as px from app import app -from pages import satellites, total, machines, locations +from pages import satellites, total, machines, locations, vms pages = [ {'name': 'Satellites', 'link': '/', 'id': 'satellites', 'layout': satellites.layout}, {'name': 'Total', 'link': '/total', 'id': 'total', 'layout': total.layout}, {'name': 'Machines', 'link': '/machines', 'id': 'machines', 'layout': machines.layout}, {'name': 'Locations', 'link': '/locations', 'id': 'locations', 'layout': locations.layout}, + {'name': 'VMs', 'link': '/vms', 'id': 'vms', 'layout': vms.layout}, ] pio.templates['custom'] = dict( diff --git a/pages/vms.py b/pages/vms.py new file mode 100644 index 0000000..1c75545 --- /dev/null +++ b/pages/vms.py @@ -0,0 +1,139 @@ +#!/usr/bin/env python3 + +import dash_core_components as dcc +import dash_html_components as html +import dash_bootstrap_components as dbc +from plotly.subplots import make_subplots +from dash.dependencies import Input, Output + +import plotly.graph_objects as go + +import datetime as dt +from natsort import natsorted + +import db +from app import app + +def layout(): + return dbc.Container(fluid=True, children=[ + dbc.Row([ + dbc.Col(width=12, lg=1, children=[ + dcc.Dropdown( + id='vms-days', + options=[{'label': '{} days'.format(d), 'value': d} for d in [7, 30, 90]], + value=7, + clearable=False + ) + ]), + dbc.Col(width=12, lg=3, children=[ + dcc.DatePickerSingle( + id='vms-date', + date=get_newest_date(), + display_format='DD-MM-YYYY', + max_date_allowed=get_newest_date(), + initial_visible_month=get_newest_date(), + first_day_of_week=1 + ), + ]), + dbc.Col(width=12, lg=6, children=[ + dcc.Dropdown( + id='vms-satellite', + options=[{'label': s, 'value': s} for s in get_satellites()], + value=None, + placeholder='All Satellites' + ) + ]) + ]), + dcc.Loading(dcc.Graph(id='vms-figure')) + ]) + +@app.callback(Output('vms-figure', 'figure'), + [Input('vms-days', 'value'), + Input('vms-date', 'date'), + Input('vms-satellite', 'value')]) +def make_content(days, date, satellite): + if satellite == None: + return make_content_all(days, date) + else: + return make_content_sat(days, date, satellite) + +def make_content_all(days, date): + dbcon= db.getConnection() + cursor = dbcon.cursor() + + stmt = """ + SELECT r.ip, COUNT(DISTINCT v.vm) as count + FROM reports r + JOIN perVM v ON r.id = v.report + WHERE r.date = (SELECT date FROM reports WHERE date >= %s ORDER BY date ASC LIMIT 1) + AND v.days = %s + GROUP BY r.ip + ORDER BY count DESC + """ + cursor.execute(stmt, (date, days)) + + data = cursor.fetchall() + + db.closeConnection(dbcon) + + figure = go.Figure() + figure.add_trace(go.Bar( + x=[item['ip'] for item in data], + y=[item['count'] for item in data] + )) + figure.update_layout( + title_text = 'VMs per Location' + ) + + return figure + +def make_content_sat(days, date, satellite): + dbcon= db.getConnection() + cursor = dbcon.cursor() + + stmt = """ + SELECT r.ip, v.vm, v.sessions + FROM reports r + JOIN perVM v ON r.id = v.report + WHERE r.date = (SELECT date FROM reports WHERE date >= %s ORDER BY date ASC LIMIT 1) + AND v.days = %s AND r.ip = %s + ORDER BY v.sessions DESC + """ + cursor.execute(stmt, (date, days, satellite)) + + data = cursor.fetchall() + + db.closeConnection(dbcon) + + figure = go.Figure() + figure.add_trace(go.Bar( + x=[item['vm'][0:9] for item in data], + y=[item['sessions'] for item in data] + )) + figure.update_layout( + title_text = 'Sessions per VM', + ) + + return figure + +def get_satellites(): + dbcon = db.getConnection() + cursor = dbcon.cursor() + + cursor.execute("""SELECT DISTINCT ip FROM reports""") + + data = [item['ip'] for item in cursor.fetchall()] + + db.closeConnection(dbcon) + return data + +def get_newest_date(): + dbcon = db.getConnection() + cursor = dbcon.cursor() + + cursor.execute("""SELECT date FROM reports ORDER BY date DESC LIMIT 1""") + + data = cursor.fetchall() + + db.closeConnection(dbcon) + return data[0]['date'] |