Skip to content

Commit

Permalink
Preheat statistics cache
Browse files Browse the repository at this point in the history
  • Loading branch information
flofriday committed Nov 26, 2024
1 parent 556e321 commit 460eedb
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,32 @@
DATABASE = "bettercal.db"


def get_db():
def create_db() -> sqlite3.Connection:
if app.config["TESTING"]:
db = sqlite3.connect(":memory:")
else:
db = sqlite3.connect(DATABASE)
db.execute("PRAGMA journal_mode=WAL;")

db.cursor().executescript(
"""
CREATE TABLE IF NOT EXISTS statistics_daily (
id INTEGER PRIMARY KEY AUTOINCREMENT,
date TEXT DEFAULT (DATE('now')),
token_hash TEXT NOT NULL,
UNIQUE(date, token_hash)
);
"""
)
db.commit()
return db



def get_db() -> sqlite3.Connection:
db = getattr(g, "_database", None)
if db is None:
if app.config["TESTING"]:
db = g._database = sqlite3.connect(":memory:")
else:
db = g._database = sqlite3.connect(DATABASE)
db.execute("PRAGMA journal_mode=WAL;")

db.cursor().executescript(
"""
CREATE TABLE IF NOT EXISTS statistics_daily (
id INTEGER PRIMARY KEY AUTOINCREMENT,
date TEXT DEFAULT (DATE('now')),
token_hash TEXT NOT NULL,
UNIQUE(date, token_hash)
);
"""
)
db.commit()
db = g._database = create_db()
return db


Expand All @@ -43,6 +49,8 @@ def close_connection(exception):
if db is not None:
db.close()

# Preheat the statistics cache
get_chart_data(create_db())

@app.route("/")
def home():
Expand Down

0 comments on commit 460eedb

Please sign in to comment.