fixed dashboard - updated analytics to include sectors instead of isin
Some checks failed
Deployment / deploy-docker (push) Has been cancelled

This commit is contained in:
Melchior Reimers
2026-02-03 13:58:47 +01:00
parent 33939c7e46
commit f608f7ca40
3 changed files with 37 additions and 44 deletions

View File

@@ -203,7 +203,7 @@ async def get_custom_analytics(
# Validiere Parameter
valid_x_axis = ["date", "exchange", "isin"]
valid_y_axis = ["volume", "trade_count", "avg_price"]
valid_group_by = ["exchange", "isin", "date"]
valid_group_by = ["exchange", "sector", "date"]
if x_axis not in valid_x_axis:
raise HTTPException(status_code=400, detail=f"Invalid x_axis. Must be one of: {valid_x_axis}")
@@ -212,50 +212,43 @@ async def get_custom_analytics(
if group_by not in valid_group_by:
raise HTTPException(status_code=400, detail=f"Invalid group_by. Must be one of: {valid_group_by}")
# Für Sektor-Gruppierung: direkter JOIN mit metadata (nicht vorberechnet)
if group_by == "sector":
y_axis_map = {
"volume": "sum(t.price * t.quantity)",
"trade_count": "count(*)",
"avg_price": "avg(t.price)"
}
y_metric = y_axis_map[y_axis]
query = f"""
select
date_trunc('day', t.timestamp) as x_value,
coalesce(m.sector, 'Unbekannt') as group_value,
{y_metric} as y_value
from trades t
left join metadata m on t.isin = m.isin
where t.timestamp >= '{date_from}'
and t.timestamp <= '{date_to}'
"""
if exchanges:
exchange_list = ",".join([f"'{e.strip()}'" for e in exchanges.split(",")])
query += f" and t.exchange in ({exchange_list})"
query += f" group by date_trunc('day', t.timestamp), coalesce(m.sector, 'Unbekannt') order by x_value asc, group_value asc"
data = query_questdb(query, timeout=30)
return format_questdb_response(data)
# Für Custom Analytics: x_axis muss "date" sein (wird täglich vorberechnet)
if x_axis != "date":
# Für nicht-date x_axis: gib Fehler zurück, da dies nicht vorberechnet wird
raise HTTPException(
status_code=400,
status_code=400,
detail="x_axis must be 'date' for pre-calculated analytics. Other x_axis values are not supported for performance reasons."
)
y_axis_map = {
"volume": "sum(price * quantity)",
"trade_count": "count(*)",
"avg_price": "avg(price)"
}
x_axis_map = {
"exchange": "exchange",
"isin": "isin"
}
group_by_map = {
"exchange": "exchange",
"isin": "isin",
"date": "date_trunc('day', timestamp)"
}
y_metric = y_axis_map[y_axis]
x_label = x_axis_map[x_axis]
group_by_field = group_by_map[group_by]
query = f"""
select
{x_label} as x_value,
{group_by_field} as group_value,
{y_metric} as y_value
from trades
where timestamp >= '{date_from}'
and timestamp <= '{date_to}'
"""
if exchanges:
exchange_list = ",".join([f"'{e.strip()}'" for e in exchanges.split(",")])
query += f" and exchange in ({exchange_list})"
query += f" group by {x_label}, {group_by_field} order by {x_label} asc, {group_by_field} asc"
data = query_questdb(query, timeout=15)
return format_questdb_response(data)
# Nutze vorberechnete Daten aus analytics_custom
exchange_filter = "all"