feat: SQL-Views und Grafana-Provisionierung
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
33
finance/app/models/views.py
Normal file
33
finance/app/models/views.py
Normal file
@@ -0,0 +1,33 @@
|
||||
from sqlalchemy import text
|
||||
from sqlalchemy.engine import Engine
|
||||
|
||||
VIEWS: dict[str, str] = {
|
||||
"v_balance_history": """
|
||||
SELECT t.booking_date AS day, a.name AS account,
|
||||
SUM(SUM(t.amount)) OVER (PARTITION BY a.id
|
||||
ORDER BY t.booking_date) AS balance
|
||||
FROM transactions t JOIN accounts a ON a.id = t.account_id
|
||||
WHERE t.status = 'confirmed'
|
||||
GROUP BY a.id, a.name, t.booking_date""",
|
||||
"v_balance_total": """
|
||||
SELECT booking_date AS day,
|
||||
SUM(SUM(amount)) OVER (ORDER BY booking_date) AS balance
|
||||
FROM transactions WHERE status = 'confirmed' GROUP BY booking_date""",
|
||||
"v_monthly_by_category": """
|
||||
SELECT date_trunc('month', t.booking_date) AS month,
|
||||
COALESCE(c.name, 'unkategorisiert') AS category,
|
||||
SUM(t.amount) AS total
|
||||
FROM transactions t LEFT JOIN categories c ON c.id = t.category_id
|
||||
WHERE t.status = 'confirmed' GROUP BY 1, 2""",
|
||||
"v_projection": """
|
||||
SELECT s.name AS scenario, p.day, p.balance
|
||||
FROM projection_points p JOIN scenarios s ON s.id = p.scenario_id""",
|
||||
}
|
||||
|
||||
|
||||
def create_views(engine: Engine) -> None:
|
||||
if engine.dialect.name != "postgresql":
|
||||
return
|
||||
with engine.begin() as conn:
|
||||
for name, body in VIEWS.items():
|
||||
conn.execute(text(f"CREATE OR REPLACE VIEW {name} AS {body}"))
|
||||
Reference in New Issue
Block a user