diff --git a/web/src/beanflows/analytics.py b/web/src/beanflows/analytics.py index f146bed..cc706d7 100644 --- a/web/src/beanflows/analytics.py +++ b/web/src/beanflows/analytics.py @@ -14,19 +14,19 @@ COFFEE_COMMODITY_CODE = 711100 # Metrics safe for user-facing queries (prevents SQL injection in dynamic column refs) ALLOWED_METRICS = frozenset({ - "Production", - "Imports", - "Exports", - "Total_Distribution", - "Ending_Stocks", - "Beginning_Stocks", - "Total_Supply", - "Domestic_Consumption", - "Net_Supply", - "Trade_Balance", - "Supply_Demand_Balance", - "Stock_to_Use_Ratio_pct", - "Production_YoY_pct", + "production", + "imports", + "exports", + "total_distribution", + "ending_stocks", + "beginning_stocks", + "total_supply", + "domestic_consumption", + "net_supply", + "trade_balance", + "supply_demand_balance", + "stock_to_use_ratio_pct", + "production_yoy_pct", }) _conn: duckdb.DuckDBPyConnection | None = None @@ -152,7 +152,7 @@ async def get_stock_to_use_trend(commodity_code: int) -> list[dict]: """Global stock-to-use ratio over time.""" return await fetch_analytics( """ - SELECT market_year, Stock_to_Use_Ratio_pct + SELECT market_year, stock_to_use_ratio_pct FROM serving.commodity_metrics WHERE commodity_code = ? AND country_name = 'Global' @@ -166,7 +166,7 @@ async def get_supply_demand_balance(commodity_code: int) -> list[dict]: """Global supply-demand balance trend.""" return await fetch_analytics( """ - SELECT market_year, Production, Total_Distribution, Supply_Demand_Balance + SELECT market_year, production, total_distribution, supply_demand_balance FROM serving.commodity_metrics WHERE commodity_code = ? AND country_name = 'Global' @@ -189,13 +189,13 @@ async def get_production_yoy_by_country( WHERE commodity_code = ? AND country_code IS NOT NULL ) SELECT country_name, country_code, market_year, - Production, Production_YoY_pct + production, production_yoy_pct FROM serving.commodity_metrics, latest WHERE commodity_code = ? AND country_code IS NOT NULL AND market_year = latest.max_year - AND Production > 0 - ORDER BY ABS(Production_YoY_pct) DESC + AND production > 0 + ORDER BY ABS(production_yoy_pct) DESC LIMIT ? """, [commodity_code, commodity_code, limit], diff --git a/web/src/beanflows/dashboard/routes.py b/web/src/beanflows/dashboard/routes.py index a3e1927..bd6b200 100644 --- a/web/src/beanflows/dashboard/routes.py +++ b/web/src/beanflows/dashboard/routes.py @@ -104,9 +104,9 @@ async def index(): time_series, top_producers, stu_trend, balance, yoy = await asyncio.gather( analytics.get_global_time_series( analytics.COFFEE_COMMODITY_CODE, - ["Production", "Exports", "Imports", "Ending_Stocks", "Total_Distribution"], + ["production", "exports", "imports", "ending_stocks", "total_distribution"], ), - analytics.get_top_countries(analytics.COFFEE_COMMODITY_CODE, "Production", limit=10), + analytics.get_top_countries(analytics.COFFEE_COMMODITY_CODE, "production", limit=10), analytics.get_stock_to_use_trend(analytics.COFFEE_COMMODITY_CODE), analytics.get_supply_demand_balance(analytics.COFFEE_COMMODITY_CODE), analytics.get_production_yoy_by_country(analytics.COFFEE_COMMODITY_CODE, limit=15), @@ -147,11 +147,11 @@ async def countries(): plan = (g.get("subscription") or {}).get("plan", "free") # Get available countries for coffee - all_countries = await analytics.get_top_countries(analytics.COFFEE_COMMODITY_CODE, "Production", limit=50) + all_countries = await analytics.get_top_countries(analytics.COFFEE_COMMODITY_CODE, "production", limit=50) # Parse query params selected_codes = request.args.getlist("country") - metric = request.args.get("metric", "Production") + metric = request.args.get("metric", "production") comparison_data = [] if selected_codes: diff --git a/web/src/beanflows/dashboard/templates/countries.html b/web/src/beanflows/dashboard/templates/countries.html index bb323c1..0e3f9ca 100644 --- a/web/src/beanflows/dashboard/templates/countries.html +++ b/web/src/beanflows/dashboard/templates/countries.html @@ -21,7 +21,7 @@