summaryrefslogtreecommitdiffstats
path: root/dash/pages/vms.py
blob: fae6fb96b28a0d000d27f6f0ec083c5496455b3f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/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,
                    persistence=True,
                    persistence_type='memory'
                )
            ]),
            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(),
                    min_date_allowed=get_oldest_date(),
                    initial_visible_month=get_newest_date(),
                    first_day_of_week=1,
                    persistence=True,
                    persistence_type='memory'
                ),
            ]),
            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',
                    persistence=True,
                    persistence_type='memory'
                )
            ])
        ]),
        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 Satellite'
    )

    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']

def get_oldest_date():
    dbcon = db.getConnection()
    cursor = dbcon.cursor()

    cursor.execute("""SELECT date FROM reports ORDER BY date ASC LIMIT 1""")

    data = cursor.fetchall()

    db.closeConnection(dbcon)
    return data[0]['date']