From 6e14fa362bad1f70c82d578c5227092dafc4b79f Mon Sep 17 00:00:00 2001 From: flofriday Date: Mon, 16 Oct 2023 19:50:17 +0200 Subject: [PATCH] Add daily active users chart to the statistics page --- app/__init__.py | 8 ++- app/monitoring.py | 15 +++++ app/static/statistics.js | 34 +++++++++++ app/templates/statistics.html | 103 +++++++++++++++++++++------------- 4 files changed, 118 insertions(+), 42 deletions(-) create mode 100644 app/static/statistics.js diff --git a/app/__init__.py b/app/__init__.py index 347606d..1556f69 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,10 +1,11 @@ +import json from flask import Flask, render_template, send_from_directory, request, g import requests import sqlite3 import app.tiss as tiss from app.format import improve_calendar -from app.monitoring import get_statistics, add_usage +from app.monitoring import get_statistics, add_usage, get_chart_data app = Flask(__name__) @@ -50,7 +51,10 @@ def home(): @app.route("/statistics") def statistic_page(): statistic = get_statistics(get_db()) - return render_template("statistics.html", statistic=statistic) + chart_data = get_chart_data(get_db()) + return render_template( + "statistics.html", statistic=statistic, chart_data=json.dumps(chart_data) + ) @app.route("/static/") diff --git a/app/monitoring.py b/app/monitoring.py index 679c0ac..c63d09b 100644 --- a/app/monitoring.py +++ b/app/monitoring.py @@ -1,6 +1,7 @@ from dataclasses import dataclass from sqlite3 import Connection import hashlib +from typing import Tuple def add_usage(db: Connection, token: str): @@ -49,3 +50,17 @@ def get_statistics(db: Connection) -> statistic: total_rows = cursor.fetchone()[0] return statistic(daily_users, monthly_users, total_users, total_rows) + + +def get_chart_data(db: Connection) -> list[Tuple[str, int]]: + # Get daily active users + cursor = db.cursor() + cursor.execute( + """ + SELECT strftime('%Y-%m-%d', date) AS day, COUNT(DISTINCT token_hash) AS unique_token_count + FROM statistics + GROUP BY day; + """ + ) + rows = cursor.fetchall() + return rows diff --git a/app/static/statistics.js b/app/static/statistics.js new file mode 100644 index 0000000..78eae17 --- /dev/null +++ b/app/static/statistics.js @@ -0,0 +1,34 @@ +const ctx = document.getElementById("dailyChart"); + +Chart.defaults.backgroundColor = "rgba(255, 255, 255, 0.2)"; +Chart.defaults.borderColor = "rgba(255, 255, 255, 0.2)"; +Chart.defaults.color = "#FFF"; + +console.log(daily_data); +let labels = daily_data.map(([label, _]) => label); +let counts = daily_data.map(([_, count]) => count); +new Chart(ctx, { + type: "line", + data: { + labels: labels, + datasets: [ + { + label: "daily active users", + data: counts, + borderWidth: 1, + }, + ], + }, + options: { + scales: { + y: { + beginAtZero: true, + }, + }, + elements: { + point: { + radius: 2, + }, + }, + }, +}); diff --git a/app/templates/statistics.html b/app/templates/statistics.html index 8debe44..22a3d02 100644 --- a/app/templates/statistics.html +++ b/app/templates/statistics.html @@ -1,51 +1,74 @@ - + - - - + + Statistics - - - - - - - - - - + + + + + + + + + + - +
-
-

- Statistics -

+
+

Statistics

-
-
    -
  • Daily active users: {{statistic.daily_users}}
  • -
  • Montly active users: {{statistic.monthly_users}}
  • -
  • Total users: {{statistic.total_users}}
  • -
-
+
    +
  • Daily active users: {{statistic.daily_users}}
  • +
  • Montly active users: {{statistic.monthly_users}}
  • +
  • Total users: {{statistic.total_users}}
  • +
-
-
+ +
+ on + GitHub +
+
+ + - - - \ No newline at end of file + + + + + +