feat: Trends landing page with directorate overview chart (Task E.3)

This commit is contained in:
Andrew Charlwood
2026-02-07 22:19:52 +00:00
parent 6c5f9be776
commit c253e05046
7 changed files with 237 additions and 22 deletions
+4 -11
View File
@@ -1591,18 +1591,19 @@ def get_trend_data(
metric: str = "patients",
directory: Optional[str] = None,
drug: Optional[str] = None,
group_by: str = "drug",
) -> list[dict]:
"""
Query pathway_trends table for time-series data.
Returns list of dicts with: period_end, name (drug or directory), value.
Groups by drug (one line per drug) unless aggregating by directory.
Args:
db_path: Path to pathways.db
metric: "patients", "total_cost", or "cost_pp_pa"
directory: Optional directory filter
drug: Optional drug filter
group_by: "drug" (one line per drug) or "directory" (one line per directory)
Returns:
List of dicts: [{period_end, name, value}, ...]
@@ -1612,7 +1613,6 @@ def get_trend_data(
conn.row_factory = sqlite3.Row
try:
# Check if the table exists
cursor = conn.cursor()
cursor.execute(
"SELECT name FROM sqlite_master WHERE type='table' AND name='pathway_trends'"
@@ -1624,7 +1624,6 @@ def get_trend_data(
if metric not in valid_metrics:
metric = "patients"
# Build query — group by drug (one line per drug over time)
where_clauses = []
params = []
@@ -1637,16 +1636,10 @@ def get_trend_data(
where_sql = " AND ".join(where_clauses) if where_clauses else "1=1"
# Aggregate across directories per drug per period (or per directory if filtering by drug)
if drug:
# One line per directory for a specific drug
group_col = "directory"
else:
# One line per drug (aggregate across directories)
group_col = "drug"
# Determine grouping column
group_col = "directory" if group_by == "directory" else "drug"
if metric == "cost_pp_pa":
# Weighted average for cost_pp_pa
agg = "SUM(cost_pp_pa * patients) / NULLIF(SUM(patients), 0)"
elif metric == "total_cost":
agg = "SUM(total_cost)"