diff --git a/dashboard/server.py b/dashboard/server.py index f18bcb3..f34c464 100644 --- a/dashboard/server.py +++ b/dashboard/server.py @@ -227,10 +227,55 @@ async def get_analytics( query += " order by label asc" + print(f"Executing Query: {query}") try: response = requests.get(f"http://{DB_HOST}:9000/exec", params={'query': query}, auth=DB_AUTH) if response.status_code == 200: return response.json() + + print(f"DEBUG: Query Failed: {response.text}") + + if use_analytics_table: + print("DEBUG: Analytics query failed, falling back to RAW trades query...") + + selected_metric = metrics_map.get(metric, metrics_map["volume"]) + selected_group = groups_map.get(group_by, groups_map["day"]) + + raw_query = f"select {selected_group} as label" + + if sub_group_by and sub_group_by in groups_map: + raw_query += f", {groups_map[sub_group_by]} as sub_label" + + if metric == 'all': + raw_query += f", count(*) as value_count, sum({t_prefix}price * {t_prefix}quantity) as value_volume from trades" + else: + raw_query += f", {selected_metric} as value from trades" + + if needs_metadata: + raw_query += " t left join metadata m on t.isin = m.isin" + + raw_query += " where 1=1" + + if date_from: raw_query += f" and {t_prefix}timestamp >= '{date_from}'" + if date_to: raw_query += f" and {t_prefix}timestamp <= '{date_to}'" + if isins: + isins_list = ",".join([f"'{i.strip()}'" for i in isins.split(",")]) + raw_query += f" and {t_prefix}isin in ({isins_list})" + if continents and needs_metadata: + cont_list = ",".join([f"'{c.strip()}'" for c in continents.split(",")]) + raw_query += f" and {m_prefix}continent in ({cont_list})" + + raw_query += f" group by {selected_group}" + if sub_group_by and sub_group_by in groups_map: + raw_query += f", {groups_map[sub_group_by]}" + + raw_query += " order by label asc" + + print(f"Executing Fallback Query: {raw_query}") + fb_response = requests.get(f"http://{DB_HOST}:9000/exec", params={'query': raw_query}, auth=DB_AUTH) + if fb_response.status_code == 200: + return fb_response.json() + throw_http_error(response) except Exception as e: raise HTTPException(status_code=500, detail=str(e))