added logging
This commit is contained in:
+42
-2
@@ -1,7 +1,7 @@
|
||||
import os
|
||||
import secrets
|
||||
import sqlite3
|
||||
from datetime import date
|
||||
from datetime import date, datetime
|
||||
from decimal import Decimal, InvalidOperation, ROUND_HALF_UP
|
||||
from functools import wraps
|
||||
from pathlib import Path
|
||||
@@ -57,6 +57,7 @@ def load_yaml_config(path):
|
||||
"APP_USERNAME": auth.get("username"),
|
||||
"APP_PASSWORD": auth.get("password"),
|
||||
"URL_PREFIX": normalize_url_prefix(flask_config.get("url_prefix", "/fatima")),
|
||||
"LOG_FILE": flask_config.get("log_file", "/tmp/fatima.log"),
|
||||
}
|
||||
missing = [
|
||||
name for name, value in required.items()
|
||||
@@ -80,6 +81,40 @@ def load_yaml_config(path):
|
||||
return required
|
||||
|
||||
|
||||
def database_summary(database_path):
|
||||
with sqlite3.connect(database_path) as database:
|
||||
amount_count = database.execute("SELECT COUNT(*) FROM date_choices").fetchone()[0]
|
||||
status_count = database.execute("SELECT COUNT(*) FROM date_statuses").fetchone()[0]
|
||||
recorded_count, first_date, last_date = database.execute(
|
||||
"""
|
||||
SELECT COUNT(*), MIN(recorded_date), MAX(recorded_date) FROM (
|
||||
SELECT choice_date AS recorded_date FROM date_choices
|
||||
UNION
|
||||
SELECT status_date AS recorded_date FROM date_statuses
|
||||
)
|
||||
"""
|
||||
).fetchone()
|
||||
return amount_count, status_count, recorded_count, first_date, last_date
|
||||
|
||||
|
||||
def log_database_summary(database_path, log_path):
|
||||
amounts, statuses, recorded_dates, first_date, last_date = database_summary(database_path)
|
||||
date_range = f"{first_date} through {last_date}" if first_date else "no recorded dates"
|
||||
message = (
|
||||
"Fatima database: "
|
||||
f"path={database_path} | "
|
||||
f"amounts={amounts} | statuses={statuses} | "
|
||||
f"recorded_dates={recorded_dates} | range={date_range}"
|
||||
)
|
||||
print(message, flush=True)
|
||||
try:
|
||||
with log_path.open("a", encoding="utf-8") as log_file:
|
||||
timestamp = datetime.now().astimezone().isoformat(timespec="seconds")
|
||||
log_file.write(f"{timestamp} | {message}\n")
|
||||
except OSError as error:
|
||||
print(f"Could not write database summary to {log_path}: {error}", flush=True)
|
||||
|
||||
|
||||
def create_app(test_config=None):
|
||||
app = Flask(__name__)
|
||||
config_path = os.environ.get("APP_CONFIG", Path(app.root_path) / "config.yaml")
|
||||
@@ -87,7 +122,7 @@ def create_app(test_config=None):
|
||||
app.config.setdefault("DATABASE", str(Path(app.instance_path) / "calendar.sqlite"))
|
||||
|
||||
# Environment variables remain useful when deploying without a local file.
|
||||
for name in ("SECRET_KEY", "APP_USERNAME", "APP_PASSWORD"):
|
||||
for name in ("SECRET_KEY", "APP_USERNAME", "APP_PASSWORD", "DATABASE", "LOG_FILE"):
|
||||
if os.environ.get(name):
|
||||
app.config[name] = os.environ[name]
|
||||
if "URL_PREFIX" in os.environ:
|
||||
@@ -95,6 +130,8 @@ def create_app(test_config=None):
|
||||
if test_config:
|
||||
app.config.update(test_config)
|
||||
app.config["URL_PREFIX"] = normalize_url_prefix(app.config["URL_PREFIX"])
|
||||
app.config["DATABASE"] = str(Path(app.config["DATABASE"]).expanduser().resolve())
|
||||
app.config["LOG_FILE"] = str(Path(app.config["LOG_FILE"]).expanduser().resolve())
|
||||
app.wsgi_app = UrlPrefixMiddleware(app.wsgi_app, app.config["URL_PREFIX"])
|
||||
|
||||
Path(app.config["DATABASE"]).parent.mkdir(parents=True, exist_ok=True)
|
||||
@@ -119,6 +156,9 @@ def create_app(test_config=None):
|
||||
"""
|
||||
)
|
||||
|
||||
if not app.config.get("TESTING"):
|
||||
log_database_summary(app.config["DATABASE"], Path(app.config["LOG_FILE"]))
|
||||
|
||||
def get_database():
|
||||
connection = sqlite3.connect(app.config["DATABASE"])
|
||||
connection.row_factory = sqlite3.Row
|
||||
|
||||
Reference in New Issue
Block a user