Skip to content

Commit

Permalink
Add cache for statistics
Browse files Browse the repository at this point in the history
The statistics for past days will never change so we can savely
cache it and reduce the time we spent in sqlite dramatically

On my dev machine from 325ms to 14ms and the change might be
even more dramatically on the server which is quite slower.
  • Loading branch information
flofriday committed Nov 26, 2024
1 parent 7635eb2 commit 556e321
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions app/monitoring.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import date
import hashlib
from dataclasses import dataclass
from sqlite3 import Connection
Expand All @@ -24,7 +25,7 @@ def get_statistics(db: Connection) -> statistic:
# Get daily active users
cursor = db.cursor()
cursor.execute(
"""SELECT COUNT(*)
"""SELECT COUNT(*)
FROM statistics_daily
WHERE date == DATE('now');"""
)
Expand All @@ -48,7 +49,30 @@ def get_statistics(db: Connection) -> statistic:
return statistic(daily_users, monthly_users, total_users)


def get_chart_data(db: Connection) -> list[Tuple[str, int]]:
# Only the usage for today will change, so we can cache all past days.
chart_cache: list[Tuple[str, int, int, int]] = []

def get_chart_data(db: Connection) -> list[Tuple[str, int, int, int]]:
global chart_cache
since = chart_cache[-1][0] if len(chart_cache) > 0 else None

# Fetch the newest data
new_data = get_chart_data_since(db, since)
result = chart_cache + new_data

# Update the cache (never store the last day because we don't trust it)
# Well the day is not over so it will probably change.
chart_cache += new_data[:-1]

return result


def get_chart_data_since(db: Connection, since: str|None = None) -> list[Tuple[str, int, int, int]]:
if since is None:
# This is just any day far in the past but, the exact day is just the day before
# it went into production.
since = "2023-07-16"

# Get daily active users
cursor = db.cursor()
cursor.execute(
Expand All @@ -57,17 +81,18 @@ def get_chart_data(db: Connection) -> list[Tuple[str, int]]:
COUNT(*) AS 'daily',
(SELECT COUNT(DISTINCT token_hash)
FROM statistics_daily s2
WHERE s2.date <= s.date
WHERE s2.date <= s.date
AND s2.date >= DATE( s.date, '-30 days')
) AS 'monbthly',
(SELECT COUNT(DISTINCT token_hash)
FROM statistics_daily s3
WHERE s3.date <= s.date
) AS 'total'
FROM statistics_daily s
WHERE day > ?
GROUP BY day
ORDER BY day
"""
ORDER BY day
""", (since,)
)
rows = cursor.fetchall()
return rows

0 comments on commit 556e321

Please sign in to comment.