-
Notifications
You must be signed in to change notification settings - Fork 0
/
statistic.py
88 lines (65 loc) · 3.31 KB
/
statistic.py
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
import plotly, locale, json, numpy as np, pandas as pd
from datetime import datetime, timedelta, date
from zoneinfo import ZoneInfo
import plotly.graph_objs as go
import plotly.express as px
from plotly.subplots import make_subplots
locale.setlocale(locale.LC_ALL, 'ru_RU.UTF-8')
MSK = ZoneInfo("Europe/Moscow")
def _get_statistic_file_html(data, user):
df = pd.DataFrame(data).T.reset_index().rename(columns={'index':'days'})
fig = go.Figure()
fig.add_trace(go.Bar(x=df['days'], y=df['correct'], name='correct'))
fig.add_trace(go.Bar(x=df['days'], y=df['wrong'], name='wrong'))
fig.add_trace(go.Bar(x=df['days'], y=df['correct']+df['wrong'], name='all'))
fig.update_layout(
legend_orientation="h",
legend=dict(x=0.5, xanchor="center"),
margin=dict(l=0, r=0, t=30, b=0),
title=user.name,
title_x=0.5,
xaxis_title="Day",
yaxis_title="Answer",
)
fig.update_traces(hoverinfo="all",hovertemplate="Day: %{x}<br>Aswers: %{y}")
graphJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
file = open('example.html', 'w+b')
html_doc = '''<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<title>You statistic</title>
</head>
<body>
<div id="plotly_graph"></div>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script>
<script>var graphs = %s;</script>
<script>Plotly.plot('plotly_graph',graphs,{});</script>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>''' % graphJSON
file.write(html_doc.encode('utf-8'))
file.seek(0)
return file
def _today_filter(user):
today = datetime.now(MSK).day
new_data = {k.strftime("%d.%m.%Y"): user.stat[k] for k in user.stat if k.day == today}
return new_data
def get_today_statistic(user):
new_data = _today_filter(user)
return _get_statistic_file_html(new_data, user)
def _month_filter(user):
now = datetime.now(MSK).date()
new_data = {k.strftime("%d.%m.%Y"): user.stat[k] for k in user.stat if now - k <= timedelta(days=30)}
return new_data
def get_month_statistic(user):
new_data = _month_filter(user)
return _get_statistic_file_html(new_data, user)