From 451fa198e1a3ad18febe70b0ef57fdd0cc5e949f Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Fri, 24 Sep 2021 20:37:43 +0200 Subject: [PATCH] sfd --- files/routes/static.py | 75 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/files/routes/static.py b/files/routes/static.py index e77e910cc..27de38860 100644 --- a/files/routes/static.py +++ b/files/routes/static.py @@ -4,6 +4,8 @@ from files.helpers.alerts import * from files.classes.award import AWARDS from sqlalchemy import func from os import path +import calendar +import matplotlib.pyplot as plt site = environ.get("DOMAIN").strip() site_name = environ.get("SITE_NAME").strip() @@ -60,6 +62,79 @@ def participation_stats(v): return render_template("admin/content_stats.html", v=v, title="Content Statistics", data=data) + +@app.get("/chart") +@auth_required +def chart(v): + file = cached_chart() + return send_file(f"../{file}") + + +@cache.memoize(timeout=86400) +def cached_chart(): + days = int(request.values.get("days", 25)) + + now = time.gmtime() + midnight_this_morning = time.struct_time((now.tm_year, + now.tm_mon, + now.tm_mday, + 0, + 0, + 0, + now.tm_wday, + now.tm_yday, + 0) + ) + today_cutoff = calendar.timegm(midnight_this_morning) + + day = 3600 * 24 + + day_cutoffs = [today_cutoff - day * i for i in range(days)] + day_cutoffs.insert(0, calendar.timegm(now)) + + daily_times = [time.strftime("%d", time.gmtime(day_cutoffs[i + 1])) for i in range(len(day_cutoffs) - 1)][2:][::-1] + + daily_signups = [g.db.query(User).options(lazyload('*')).filter(User.created_utc < day_cutoffs[i], User.created_utc > day_cutoffs[i + 1]).count() for i in range(len(day_cutoffs) - 1)][2:][::-1] + + post_stats = [g.db.query(Submission).options(lazyload('*')).filter(Submission.created_utc < day_cutoffs[i], Submission.created_utc > day_cutoffs[i + 1], Submission.is_banned == False).count() for i in range(len(day_cutoffs) - 1)][2:][::-1] + + comment_stats = [g.db.query(Comment).options(lazyload('*')).filter(Comment.created_utc < day_cutoffs[i], Comment.created_utc > day_cutoffs[i + 1],Comment.is_banned == False, Comment.author_id != 1).count() for i in range(len(day_cutoffs) - 1)][2:][::-1] + + # create multiple charts + signup_chart = plt.subplot2grid((20, 4), (0, 0), rowspan=5, colspan=4) + posts_chart = plt.subplot2grid((20, 4), (7, 0), rowspan=5, colspan=4) + comments_chart = plt.subplot2grid((20, 4), (14, 0), rowspan=5, colspan=4) + + signup_chart.grid(), posts_chart.grid(), comments_chart.grid() + + signup_chart.plot( + daily_times, + daily_signups, + color='red') + posts_chart.plot( + daily_times, + post_stats, + color='green') + comments_chart.plot( + daily_times, + comment_stats, + color='gold') + + signup_chart.set_ylabel("Signups") + posts_chart.set_ylabel("Posts") + comments_chart.set_ylabel("Comments") + comments_chart.set_xlabel("Time (UTC)") + + signup_chart.legend(loc='upper left', frameon=True) + posts_chart.legend(loc='upper left', frameon=True) + comments_chart.legend(loc='upper left', frameon=True) + + file = "chart.png" + plt.savefig(file) + plt.clf() + return file + + @app.get("/patrons") @app.get("/paypigs") @auth_desired