From f8f45477431a38a44fd553e5b199ec2125a6a3ea Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Fri, 25 Jan 2019 20:35:14 +0100 Subject: [PATCH] Add hourly graph --- db.py | 8 +-- query_discord_music.py => sql_queries.py | 14 +++++- templates/activity.html | 62 ++++++++++++++++++++++++ webserver.py | 9 ++-- 4 files changed, 83 insertions(+), 10 deletions(-) rename query_discord_music.py => sql_queries.py (90%) diff --git a/db.py b/db.py index 9e74aa2b..ae142f39 100644 --- a/db.py +++ b/db.py @@ -21,7 +21,7 @@ from discord import User as DiscordUser from telegram import User as TelegramUser import loldata from dirty import Dirty, DirtyDelta -import query_discord_music +import sql_queries from flask import escape import libgravatar import configparser @@ -716,11 +716,11 @@ class Discord(Base, Mini): @classmethod def mini_get_all(cls, session: Session): - return [dict(row) for row in session.execute(query_discord_music.all_query)] + return [dict(row) for row in session.execute(sql_queries.all_query)] @classmethod def mini_get_single(cls, session: Session, **kwargs): - return session.execute(query_discord_music.one_query, {"royal": kwargs["royal"].id}).fetchone() + return session.execute(sql_queries.one_query, {"royal": kwargs["royal"].id}).fetchone() @classmethod def mini_get_single_from_royal(cls, session: Session, royal: "Royal"): @@ -927,7 +927,7 @@ class VoteQuestion(Base): text = f"{self.question}\n\n" none, yes, no, abstain = 0, 0, 0, 0 if self.message_id is not None: - query = session.execute(query_discord_music.vote_answers, {"message_id": self.message_id}) + query = session.execute(sql_queries.vote_answers, {"message_id": self.message_id}) for record in query: if record["username"] == "royalgamesbot": continue diff --git a/query_discord_music.py b/sql_queries.py similarity index 90% rename from query_discord_music.py rename to sql_queries.py index 3e57d21e..4a0ada5a 100644 --- a/query_discord_music.py +++ b/sql_queries.py @@ -1,4 +1,4 @@ -all_query = """SELECT +all_music_query = """SELECT discord.royal_id, discord.discord_id, discord.name, @@ -59,7 +59,7 @@ LEFT JOIN # TODO: can and should be optimized, but I'm too lazy for that -one_query = """SELECT +one_music_query = """SELECT discord.royal_id, discord.discord_id, discord.name, @@ -141,3 +141,13 @@ LEFT JOIN WHERE votequestion.message_id = :message_id ) answer ON telegram.telegram_id = answer.user_id ORDER BY answer.choice;""" + +activity_by_hour = """SELECT AVG(discord_members_online) online_members_avg, + AVG(discord_members_ingame) ingame_members_avg, + AVG(discord_members_cv) cv_members_avg, + AVG(discord_channels_used) channels_used_avg, + AVG(discord_cv) cv_avg, + extract(hour from timestamp) h +FROM activityreports +GROUP BY h +ORDER BY h;""" \ No newline at end of file diff --git a/templates/activity.html b/templates/activity.html index 0d12e871..6f30d5ce 100644 --- a/templates/activity.html +++ b/templates/activity.html @@ -151,6 +151,68 @@ }, "options":{ + } + }); + +

+ Media dell'attività per ogni ora +

+ + diff --git a/webserver.py b/webserver.py index e39f3294..1f17ef97 100644 --- a/webserver.py +++ b/webserver.py @@ -9,7 +9,7 @@ import datetime # noinspection PyPackageRequirements import telegram import errors -import query_discord_music +import sql_queries import random import re import functools @@ -310,7 +310,7 @@ def page_diario(): @app.route("/music") def page_music(): - songs = fl_g.session.execute(query_discord_music.top_songs) + songs = fl_g.session.execute(sql_queries.top_songs) return render_template("topsongs.html", songs=songs) @@ -320,14 +320,15 @@ def page_music_individual(discord_id: str): if discord is None: abort(404) return - songs = fl_g.session.execute(query_discord_music.single_top_songs, {"discordid": discord.discord_id}) + songs = fl_g.session.execute(sql_queries.single_top_songs, {"discordid": discord.discord_id}) return render_template("topsongs.html", songs=songs, discord=discord) @app.route("/activity") def page_activity(): reports = list(fl_g.session.query(db.ActivityReport).order_by(db.ActivityReport.timestamp.desc()).limit(192).all()) - return render_template("activity.html", activityreports=list(reversed(reports))) + hourly_avg = list(fl_g.session.execute(sql_queries.activity_by_hour)) + return render_template("activity.html", activityreports=list(reversed(reports)), hourly_avg=hourly_avg) @app.route("/ses/identify")