-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch_dashboard_trends.py
More file actions
66 lines (55 loc) · 1.89 KB
/
Copy pathpatch_dashboard_trends.py
File metadata and controls
66 lines (55 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from pathlib import Path
import re
path = Path(r".\app\api\financial_operational.py")
text = path.read_text(encoding="utf-8")
insert_block = r'''
@router.get("/dashboard/trends")
def get_dashboard_trends(
limit: int = Query(24, ge=1, le=120),
):
"""
Return monthly trend summary based on last_payment_date_raw.
"""
conn = get_financial_db()
try:
cur = conn.cursor()
try:
cur.execute(
"""
SELECT
SUBSTR(last_payment_date_raw, 1, 7) AS period,
COUNT(*) AS identity_count,
COALESCE(SUM(lifetime_net_received), 0) AS total_revenue,
COALESCE(AVG(financial_value_score), 0) AS avg_financial_value_score
FROM financial_identity_profile
WHERE last_payment_date_raw IS NOT NULL
AND LENGTH(last_payment_date_raw) >= 7
GROUP BY SUBSTR(last_payment_date_raw, 1, 7)
ORDER BY period DESC
LIMIT ?
""",
(limit,),
)
rows = cur.fetchall()
except sqlite3.OperationalError as e:
raise HTTPException(
status_code=503,
detail=f"Trend query unavailable: {e}",
)
data = [dict(r) for r in rows]
return {
"count": len(data),
"limit": limit,
"data": data,
}
finally:
conn.close()
'''
marker = r'(?=@router\.get\("/scheduling/priority"\))'
if re.search(r'@router\.get\("/dashboard/trends"\)', text):
raise SystemExit("dashboard/trends already exists.")
new_text, count = re.subn(marker, insert_block, text, count=1)
if count != 1:
raise SystemExit(f"Insertion failed. Matched markers: {count}")
path.write_text(new_text, encoding="utf-8")
print("Inserted /dashboard/trends successfully.")