summaryrefslogtreecommitdiffstats
path: root/dash/pages/locations.py
blob: 9125ba00e176b47830457602544577b9bb00cb4e (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
#!/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='locations-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='locations-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='locations-satellite',
                    options=[{'label': s, 'value': s} for s in get_satellites()],
                    value=None,
                    placeholder='All Satellites',
                    persistence=True,
                    persistence_type='memory'
                )
            ])
        ]),
        dcc.Loading(style={'marginTop': '200px'}, children=dbc.Row(id='locations-content'))
    ])

@app.callback(Output('locations-content', 'children'),
              [Input('locations-days', 'value'),
               Input('locations-date', 'date'),
               Input('locations-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, l.locationname, l.totalSessionTime
            FROM reports r
            JOIN perLocation l ON r.id = l.report
            WHERE r.date = (SELECT date FROM reports WHERE date >= %s ORDER BY date ASC LIMIT 1)
                    AND l.days = %s
        """
    cursor.execute(stmt, (date, days))

    data = cursor.fetchall()

    db.closeConnection(dbcon)

    satellites = natsorted(get_satellites())

    figures = []
    for sat in satellites:
        figure = go.Figure()
        figure.add_trace(go.Pie(
            labels=[item['locationname'][0:9] for item in data if item['ip'] == sat],
            values=[item['totalSessionTime'] for item in data if item['ip'] == sat],
            text=[str(int(item['totalSessionTime'] / 3600)) + '  h' for item in data if item['ip'] == sat],
            hole=0.3,
            textinfo='none',
            hoverinfo='label+text+percent',
            direction='clockwise'
        ))
        numElements = len([item['ip'] for item in data if item['ip'] == sat])
        figure.update_layout(
            title_text = '<sub>Sessiontime per Location (Total: {})</sub><br>{}'.format(numElements, sat),
            showlegend=False
        )
        if len([item for item in data if item['ip'] == sat and item['totalSessionTime'] > 0]) > 0:
            figures.append(dbc.Col(width=12, md=6, xl=4, children=dcc.Graph(figure=figure)))

    return figures

def make_content_sat(days, date, satellite):
    return [
            dbc.Col(width=12, children=dcc.Graph(figure=item(days, date, satellite)))
            for item in [make_content_sat_sessiontime, make_content_sat_median, make_content_sat_sessions]
    ]

def make_content_sat_sessiontime(days, date, satellite):
    dbcon= db.getConnection()
    cursor = dbcon.cursor()

    stmt = """
            SELECT l.locationname, l.totalSessionTime
            FROM reports r
            JOIN perLocation l ON r.id = l.report
            WHERE r.date = (SELECT date FROM reports WHERE date >= %s ORDER BY date ASC LIMIT 1)
                    AND l.days = %s AND r.ip = %s
            ORDER BY l.locationname ASC
        """
    cursor.execute(stmt, (date, days, satellite))

    data = cursor.fetchall()

    db.closeConnection(dbcon)

    figure = go.Figure()
    figure.add_trace(go.Bar(
        x=[item['locationname'][0:9] for item in data],
        y=[int(item['totalSessionTime'] / 3600) for item in data]
    ))
    figure.update_layout(
        title_text = 'Total Session Time',
        yaxis_ticksuffix=' h',
    )

    return figure

def make_content_sat_median(days, date, satellite):
    dbcon= db.getConnection()
    cursor = dbcon.cursor()

    stmt = """
            SELECT l.locationname, l.medianSessionLength
            FROM reports r
            JOIN perLocation l ON r.id = l.report
            WHERE r.date = (SELECT date FROM reports WHERE date >= %s ORDER BY date ASC LIMIT 1)
                    AND l.days = %s AND r.ip = %s
            ORDER BY l.locationname ASC
        """
    cursor.execute(stmt, (date, days, satellite))

    data = cursor.fetchall()

    db.closeConnection(dbcon)

    figure = go.Figure()
    figure.add_trace(go.Bar(
        x=[item['locationname'][0:9] for item in data],
        y=[int(item['medianSessionLength'] / 60) for item in data]
    ))
    figure.update_layout(
        title_text = 'Median Session Time',
        yaxis_ticksuffix=' min',
    )

    return figure

def make_content_sat_sessions(days, date, satellite):
    dbcon= db.getConnection()
    cursor = dbcon.cursor()

    stmt = """
            SELECT l.locationname, l.shortSessions, l.longSessions
            FROM reports r
            JOIN perLocation l ON r.id = l.report
            WHERE r.date = (SELECT date FROM reports WHERE date >= %s ORDER BY date ASC LIMIT 1)
                    AND l.days = %s AND r.ip = %s
            ORDER BY l.locationname ASC
        """
    cursor.execute(stmt, (date, days, satellite))

    data = cursor.fetchall()

    db.closeConnection(dbcon)

    figure = go.Figure()
    figure.add_trace(go.Bar(
        x=[item['locationname'][0:9] for item in data],
        y=[item['shortSessions'] for item in data],
        name='Short Sessions'
    ))
    figure.add_trace(go.Bar(
        x=[item['locationname'][0:9] for item in data],
        y=[item['longSessions'] for item in data],
        name='Long Sessions'
    ))
    figure.update_layout(
        title_text = 'Sessions',
        barmode='stack',
        legend=dict(x=0.9, y=1.2)
    )

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