webhost: stats improvements

This commit is contained in:
Fabian Dill 2022-04-09 19:38:08 +02:00 committed by KonoTyran
parent dae0e233b8
commit 249972c7fd
2 changed files with 22 additions and 16 deletions

View File

@ -1,7 +1,7 @@
from collections import Counter, defaultdict from collections import Counter, defaultdict
from itertools import cycle from itertools import cycle
from datetime import datetime, timedelta, date from datetime import datetime, timedelta, date
from math import pi from math import tau
from bokeh.embed import components from bokeh.embed import components
from bokeh.palettes import Dark2_8 as palette from bokeh.palettes import Dark2_8 as palette
@ -13,7 +13,17 @@ from pony.orm import select
from . import app, cache from . import app, cache
from .models import Room from .models import Room
pi2 = 2 * pi
def get_db_data():
games_played = defaultdict(Counter)
total_games = Counter()
cutoff = date.today()-timedelta(days=30000)
room: Room
for room in select(room for room in Room if room.creation_time >= cutoff):
for slot in room.seed.slots:
total_games[slot.game] += 1
games_played[room.creation_time.date()][slot.game] += 1
return total_games, games_played
@app.route('/stats') @app.route('/stats')
@ -21,24 +31,19 @@ pi2 = 2 * pi
def stats(): def stats():
plot = figure(title="Games played per day", x_axis_type='datetime', x_axis_label="Date", y_axis_label="Played", plot = figure(title="Games played per day", x_axis_type='datetime', x_axis_label="Date", y_axis_label="Played",
sizing_mode="scale_both") sizing_mode="scale_both")
games_played = defaultdict(Counter)
total_games = Counter() total_games, games_played = get_db_data()
room: Room
games = set()
cutoff = date.today()-timedelta(days=30000)
for room in select(room for room in Room if room.creation_time >= cutoff):
for slot in room.seed.slots:
total_games[slot.game] += 1
games.add(slot.game)
games_played[room.creation_time.date()][slot.game] += 1
days = sorted(games_played) days = sorted(games_played)
cyc_palette = cycle(palette) cyc_palette = cycle(palette)
for game in sorted(games):
for game in sorted(total_games):
occurences = [] occurences = []
for day in days: for day in days:
occurences.append(games_played[day][game]) occurences.append(games_played[day][game])
plot.line([datetime.combine(day, datetime.min.time()) for day in days], plot.line([datetime.combine(day, datetime.min.time()) for day in days],
occurences, legend_label=game, line_width=2, color=next(cyc_palette)) occurences, legend_label=game, line_width=2, color=next(cyc_palette))
total = sum(total_games.values()) total = sum(total_games.values())
pie = figure(plot_height=350, title=f"Games played in the last 30 days. (Total: {total})", toolbar_location=None, pie = figure(plot_height=350, title=f"Games played in the last 30 days. (Total: {total})", toolbar_location=None,
tools="hover", tooltips=[("Game:", "@games"), ("Played:", "@count")], tools="hover", tooltips=[("Game:", "@games"), ("Played:", "@count")],
@ -56,11 +61,12 @@ def stats():
data["games"].append(game) data["games"].append(game)
data["count"].append(count) data["count"].append(count)
data["start_angles"].append(current_angle) data["start_angles"].append(current_angle)
angle = count / total * pi2 angle = count / total * tau
current_angle += angle current_angle += angle
data["end_angles"].append(current_angle) data["end_angles"].append(current_angle)
data["colors"] = [element[1] for element in sorted((game, color) for game, color in zip(data["games"], cycle(palette)))] data["colors"] = [element[1] for element in sorted((game, color) for game, color in
zip(data["games"], cycle(palette)))]
pie.wedge(x=0.5, y=0.5, radius=0.5, pie.wedge(x=0.5, y=0.5, radius=0.5,
start_angle="start_angles", end_angle="end_angles", fill_color="colors", start_angle="start_angles", end_angle="end_angles", fill_color="colors",
source=ColumnDataSource(data=data), legend_field="games") source=ColumnDataSource(data=data), legend_field="games")

View File

@ -1,7 +1,7 @@
{% extends 'pageWrapper.html' %} {% extends 'pageWrapper.html' %}
{% block head %} {% block head %}
<title>Site Map</title> <title>Statistics</title>
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename="styles/markdown.css") }}" /> <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename="styles/markdown.css") }}" />
{{ css_resources|indent(4)|safe }} {{ css_resources|indent(4)|safe }}
{{ js_resources|indent(4)|safe }} {{ js_resources|indent(4)|safe }}