summaryrefslogtreecommitdiffstats
path: root/dash/pages/machines.py
blob: a2db633a47e38199c44b347ff9e46aeddeb67b61 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#!/usr/bin/env python3

import dash_core_components as dcc
import dash_html_components as html
import dash_bootstrap_components as dbc
import dash_table as dtbl
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='machines-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='machines-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='machines-satellites',
                    options=[{'label': name, 'value': ip} for ip, name in get_satellites()],
                    multi=True,
                    value=[],
                    placeholder='All Satellites',
                    persistence=True,
                    persistence_type='memory'
                )
            ])
        ]),
        dbc.Row([dbc.Col([
            dcc.Loading(dcc.Graph(id='machines-graph-location'))
        ])]),
        dbc.Row([dbc.Col([
            dcc.Loading(html.Pre(id='machines-sum-location'))
        ])]),
        dbc.Row([
            dbc.Col(width=12, md=6, xl=4, children=[
                dcc.Loading(dcc.Graph(id='machines-graph-ram'))
            ]),
            dbc.Col(width=12, md=6, xl=4, children=[
                dcc.Loading(dcc.Graph(id='machines-graph-realcores'))
            ]),
            dbc.Col(width=12, md=6, xl=4, children=[
                dcc.Loading(dcc.Graph(id='machines-graph-kvmstate'))
            ]),
            dbc.Col(width=12, xl=7, children=[
                dcc.Loading(dcc.Graph(id='machines-graph-systemmodel')),
            ]),
            dbc.Col(width=12, xl=7, children=[
                dcc.Loading(dcc.Graph(id='machines-graph-basesystem')),
            ]),
            dbc.Col(width=12, xl=4, id='machines-graph-systemmodel-locations-container', children=[
                html.H3(id='machines-graph-systemmodel-locations-head', style=dict(fontWeight='normal', fontSize='25px', fontFamily='Open Sans', marginTop='20px')),
                dcc.Loading(dtbl.DataTable(
                    id='machines-graph-systemmodel-locations',
                    columns=[
                        {'id': 'ip', 'name': 'Location', 'type': 'text'},
                        {'id': 'count', 'name': 'Count', 'type': 'numeric'}
                    ],
                    style_cell_conditional=[
                        {
                            'if': {'column_type': 'text'},
                            'textAlign': 'left'
                        }
                    ]
                ))
            ]),
            dbc.Col(width=12, xl=7, children=[
                dcc.Loading(dcc.Graph(id='machines-graph-cpumodel')),
            ]),
            dbc.Col(width=12, xl=4, id='machines-graph-cpumodel-locations-container', children=[
                html.H3(id='machines-graph-cpumodel-locations-head', style=dict(fontWeight='normal', fontSize='25px', fontFamily='Open Sans', marginTop='20px')),
                dcc.Loading(dtbl.DataTable(
                    id='machines-graph-cpumodel-locations',
                    columns=[
                        {'id': 'ip', 'name': 'Location', 'type': 'text'},
                        {'id': 'count', 'name': 'Count', 'type': 'numeric'}
                    ],
                    style_cell_conditional=[
                        {
                            'if': {'column_type': 'text'},
                            'textAlign': 'left'
                        }
                    ]
                ))
            ])
        ])
    ])

@app.callback([Output('machines-graph-location', 'figure'),
               Output('machines-sum-location', 'children')],
              [Input('machines-days', 'value'),
               Input('machines-date', 'date'),
               Input('machines-satellites', 'value')])
def make_graph_location(days, date, satellites):
    dbcon = db.getConnection()
    cursor = dbcon.cursor()

    stmt = """
            SELECT m.value, m.count
            FROM reports r
            JOIN machine m ON r.id = m.report
            WHERE r.date = (SELECT date FROM reports WHERE date >= %s ORDER BY date ASC LIMIT 1)
            AND m.days = %s AND m.property = 'location'
        """
    stmt_end = """ ORDER BY m.count DESC"""

    if len(satellites) > 0:
        formatStrings = ','.join(['%s'] * len(satellites))
        cursor.execute(stmt + ' AND r.ip IN ({})'.format(formatStrings) + stmt_end, tuple([date, days] + satellites))
    else:
        cursor.execute(stmt + stmt_end, (date, days))

    data = cursor.fetchall()

    db.closeConnection(dbcon)

    figure = go.Figure()
    figure.add_trace(go.Bar(
        x=[item['value'][0:9] for item in data],
        y=[item['count'] for item in data]
    ))
    figure.update_layout(
        title_text = 'Locations',
        yaxis_title = 'Machines'
    )

    totalStr = 'Total Locations: {}'.format(len(data))

    return figure, totalStr

@app.callback(Output('machines-graph-cpumodel', 'figure'),
              [Input('machines-days', 'value'),
               Input('machines-date', 'date'),
               Input('machines-satellites', 'value')])
def make_graph_cpumodel(days, date, satellites):
    return make_graph(days, date, satellites, 'cpumodel', 'CPU<br><sub>Click Bar to view Details</sub>', typ='bar')

@app.callback(Output('machines-graph-realcores', 'figure'),
              [Input('machines-days', 'value'),
               Input('machines-date', 'date'),
               Input('machines-satellites', 'value')])
def make_graph_realcores(days, date, satellites):
    return make_graph(days, date, satellites, 'realcores', 'Cores', unit=' Cores')

@app.callback(Output('machines-graph-ram', 'figure'),
              [Input('machines-days', 'value'),
               Input('machines-date', 'date'),
               Input('machines-satellites', 'value')])
def make_graph_ram(days, date, satellites):
    return make_graph(days, date, satellites, 'ram', 'RAM', unit=' GB')

@app.callback(Output('machines-graph-systemmodel', 'figure'),
              [Input('machines-days', 'value'),
               Input('machines-date', 'date'),
               Input('machines-satellites', 'value')])
def make_graph_systemmodel(days, date, satellites):
    return make_graph(days, date, satellites, 'systemmodel', 'System Model<br><sub>Click Bar to view Details</sub>', typ='bar')


#def make_graph_basesystem(days, date, satellites):
#   return make_graph(days, date, satellites, 'basesystem', 'BASE SYSTEMS<br><sub>Click Bar to view Details</sub>', typ='bar')

@app.callback(Output('machines-graph-kvmstate', 'figure'),
              [Input('machines-days', 'value'),
               Input('machines-date', 'date'),
               Input('machines-satellites', 'value')])
def make_graph_kvmstate(days, date, satellites):
    return make_graph(days, date, satellites, 'kvmstate', 'KVM State')

@app.callback([Output('machines-graph-systemmodel-locations-container', 'style'),
               Output('machines-graph-systemmodel-locations', 'data'),
               Output('machines-graph-systemmodel-locations-head', 'children')],
              [Input('machines-days', 'value'),
               Input('machines-date', 'date'),
               Input('machines-graph-systemmodel', 'clickData')])
def make_graph_systemmodel_locations(days, date, clickData):
    return make_graph_locations(days, date, clickData, 'systemmodel')

@app.callback([Output('machines-graph-cpumodel-locations-container', 'style'),
               Output('machines-graph-cpumodel-locations', 'data'),
               Output('machines-graph-cpumodel-locations-head', 'children')],
              [Input('machines-days', 'value'),
               Input('machines-date', 'date'),
               Input('machines-graph-cpumodel', 'clickData')])
def make_graph_cpumodel_locations(days, date, clickData):
    return make_graph_locations(days, date, clickData, 'cpumodel')

def make_graph_locations(days, date, clickData, prop):
    if clickData == None:
        return dict(display='none'), [], ''

    dbcon = db.getConnection()
    cursor = dbcon.cursor()

    stmt = """
            SELECT r.ip, m.count
            FROM reports r
            JOIN machine m ON r.id = m.report
            WHERE r.date = (SELECT date FROM reports WHERE date >= %s ORDER BY date ASC LIMIT 1)
            AND m.days = %s
            AND m.property = %s
            AND m.value = %s
            ORDER BY m.count DESC
        """
    item = clickData['points'][0]['y']

    cursor.execute(stmt, (date, days, prop, item))

    data = cursor.fetchall()

    db.closeConnection(dbcon)

    return dict(display='block'), data, item


def make_graph(days, date, satellites, prop, title, unit='', typ='pie'):
    dbcon = db.getConnection()
    cursor = dbcon.cursor()

    stmt = """
            SELECT m.value, SUM(m.count) AS count
            FROM reports r
            JOIN machine m ON r.id = m.report
            WHERE r.date = (SELECT date FROM reports WHERE date >= %s ORDER BY date ASC LIMIT 1)
            AND m.days = %s AND m.property = %s
        """
    stmt_end = """ GROUP BY m.value"""

    if len(satellites) > 0:
        formatStrings = ','.join(['%s'] * len(satellites))
        cursor.execute(stmt + ' AND r.ip IN ({})'.format(formatStrings) + stmt_end, tuple([date, days, prop] + satellites))
    else:
        cursor.execute(stmt + stmt_end, (date, days, prop))

    data = cursor.fetchall()
    data = natsorted(data, key = lambda x: x['value' if typ=='pie' else 'count'])

    db.closeConnection(dbcon)

    figure = go.Figure()
    if typ == 'pie':
        figure.add_trace(go.Pie(
            labels=[item['value'] + unit for item in data],
            values=[item['count'] for item in data],
            hole=0.3,
            sort=False,
            textinfo='label',
            direction='clockwise'
        ))
    elif typ == 'bar':
        figure.add_trace(go.Bar(
            y=[item['value'] + unit for item in data],
            x=[item['count'] for item in data],
            orientation='h'
        ))
    figure.update_layout(
        title_text = title,
        height = 450 if typ == 'pie' else max(20 * len(data) + 200,300),
        yaxis_automargin=True
    )
    return figure

@app.callback(Output('machines-graph-basesystem', 'figure'),
              [Input('machines-days', 'value'),
               Input('machines-date', 'date'),
               Input('machines-satellites', 'value')])
def make_graph_basesystem(days, date, satellites):
    dbcon = db.getConnection()
    cursor = dbcon.cursor()

    stmt = """
            SELECT b.system, SUM(b.count) AS count
            FROM reports r
            JOIN basesystem b ON r.id = b.report
            WHERE r.date = (SELECT date FROM reports WHERE date >= %s ORDER BY date ASC LIMIT 1)
            AND b.days = %s
        """
    stmt_end = """ GROUP BY b.system"""
    if len(satellites) > 0:
        formatStrings = ','.join(['%s'] * len(satellites))
        cursor.execute(stmt + ' AND r.ip IN ({})'.format(formatStrings) + stmt_end, tuple([date, days] + satellites))
    else:
        print(stmt)
        cursor.execute(stmt + stmt_end, (date, days))

    data = cursor.fetchall()
    data = natsorted(data, key = lambda x: x['count'])
    db.closeConnection(dbcon)

    figure = go.Figure()
    figure.add_trace(go.Bar(
            y=[item['system'] for item in data],
            x=[item['count'] for item in data],
            orientation='h'
    ))
    figure.update_layout(
        title_text = "BASE SYSTEMS",
        height = max(20 * len(data) + 200,300),
        yaxis_automargin=True
    )
    return figure

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

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

    cursor.execute("""
                SELECT DISTINCT r.ip, COALESCE(nm.name, r.ip) AS name FROM reports r
                LEFT OUTER JOIN names nm ON r.ip = nm.ip
                ORDER BY name ASC
            """)

    data = [(item['ip'], item['name']) for item in cursor.fetchall()]

    db.closeConnection(dbcon)
    return data