Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -3,10 +3,15 @@ from typing import List, Optional
|
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from database import engine
|
| 5 |
import pandas as pd
|
|
|
|
| 6 |
from sqlalchemy import text
|
| 7 |
|
| 8 |
app = FastAPI(title="WB AI Analytics API - Neuron_AI Edition")
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# --- Схемы данных ---
|
| 11 |
class ProductAnalytics(BaseModel):
|
| 12 |
nm_id: int
|
|
@@ -304,7 +309,7 @@ def get_top_products(
|
|
| 304 |
"""Топ товаров с сортировкой"""
|
| 305 |
query = text(f"SELECT * FROM wb_vitrine_for_ai ORDER BY {sort_by} DESC LIMIT :limit")
|
| 306 |
df = pd.read_sql(query, engine, params={"limit": limit})
|
| 307 |
-
return df.to_dict(orient="records")
|
| 308 |
|
| 309 |
@app.get("/analytics/product/{nm_id}", response_model=ProductAnalytics)
|
| 310 |
def get_product_details(nm_id: int):
|
|
@@ -313,7 +318,7 @@ def get_product_details(nm_id: int):
|
|
| 313 |
df = pd.read_sql(query, engine, params={"nm_id": nm_id})
|
| 314 |
if df.empty:
|
| 315 |
raise HTTPException(status_code=404, detail="Товар не найден")
|
| 316 |
-
return df.to_dict(orient="records")[0]
|
| 317 |
|
| 318 |
@app.get("/analytics/product/{nm_id}/stocks", response_model=List[StockDistribution])
|
| 319 |
def get_product_stocks(nm_id: int):
|
|
@@ -328,7 +333,7 @@ def get_product_stocks(nm_id: int):
|
|
| 328 |
WHERE nm_id = :nm_id AND amount > 0
|
| 329 |
""")
|
| 330 |
df = pd.read_sql(query, engine, params={"nm_id": nm_id})
|
| 331 |
-
return df.to_dict(orient="records")
|
| 332 |
|
| 333 |
@app.get("/analytics/product/{nm_id}/stock-summary", response_model=StockSummary)
|
| 334 |
def get_stock_summary(nm_id: int):
|
|
@@ -344,7 +349,7 @@ def get_stock_summary(nm_id: int):
|
|
| 344 |
(SELECT COUNT(DISTINCT warehouse_id) FROM wb_stocks_fbs WHERE nm_id = :nm_id AND amount > 0) as fbs_warehouses
|
| 345 |
""")
|
| 346 |
df = pd.read_sql(query, engine, params={"nm_id": nm_id})
|
| 347 |
-
return df.to_dict(orient="records")[0]
|
| 348 |
|
| 349 |
@app.get("/analytics/low-stock", response_model=List[LowStockAlert])
|
| 350 |
def get_low_stock_alerts(days_threshold: int = 7):
|
|
@@ -365,7 +370,7 @@ def get_low_stock_alerts(days_threshold: int = 7):
|
|
| 365 |
ORDER BY days_of_stock ASC
|
| 366 |
""")
|
| 367 |
df = pd.read_sql(query, engine, params={"days_threshold": days_threshold})
|
| 368 |
-
return df.to_dict(orient="records")
|
| 369 |
|
| 370 |
@app.get("/analytics/out-of-stock")
|
| 371 |
def get_out_of_stock():
|
|
@@ -377,7 +382,7 @@ def get_out_of_stock():
|
|
| 377 |
ORDER BY orders_7d DESC
|
| 378 |
""")
|
| 379 |
df = pd.read_sql(query, engine)
|
| 380 |
-
return df.to_dict(orient="records")
|
| 381 |
|
| 382 |
@app.get("/analytics/product/{nm_id}/sales", response_model=SalesStats)
|
| 383 |
def get_product_sales(nm_id: int):
|
|
@@ -396,7 +401,7 @@ def get_product_sales(nm_id: int):
|
|
| 396 |
WHERE product_nm_id = :nm_id
|
| 397 |
""")
|
| 398 |
df = pd.read_sql(query, engine, params={"nm_id": nm_id})
|
| 399 |
-
return df.to_dict(orient="records")[0]
|
| 400 |
|
| 401 |
@app.get("/analytics/profitability")
|
| 402 |
def get_profitability_report(limit: int = 50):
|
|
@@ -417,7 +422,7 @@ def get_profitability_report(limit: int = 50):
|
|
| 417 |
LIMIT :limit
|
| 418 |
""")
|
| 419 |
df = pd.read_sql(query, engine, params={"limit": limit})
|
| 420 |
-
return df.to_dict(orient="records")
|
| 421 |
|
| 422 |
@app.get("/analytics/returns")
|
| 423 |
def get_returns_report():
|
|
@@ -440,7 +445,7 @@ def get_returns_report():
|
|
| 440 |
ORDER BY return_rate_pct DESC
|
| 441 |
""")
|
| 442 |
df = pd.read_sql(query, engine)
|
| 443 |
-
return df.to_dict(orient="records")
|
| 444 |
|
| 445 |
@app.get("/analytics/regions")
|
| 446 |
def get_regional_stats():
|
|
@@ -457,7 +462,7 @@ def get_regional_stats():
|
|
| 457 |
LIMIT 20
|
| 458 |
""")
|
| 459 |
df = pd.read_sql(query, engine)
|
| 460 |
-
return df.to_dict(orient="records")
|
| 461 |
|
| 462 |
# ==================== FUNNEL (ВОРОНКА) ====================
|
| 463 |
|
|
@@ -484,7 +489,7 @@ def get_funnel_summary():
|
|
| 484 |
FROM mv_funnel_with_finance
|
| 485 |
""")
|
| 486 |
df = pd.read_sql(query, engine)
|
| 487 |
-
return df.to_dict(orient="records")[0]
|
| 488 |
|
| 489 |
@app.get("/funnel/product/{nm_id}", response_model=FunnelProduct)
|
| 490 |
def get_funnel_product(nm_id: int):
|
|
@@ -504,7 +509,7 @@ def get_funnel_product(nm_id: int):
|
|
| 504 |
df = pd.read_sql(query, engine, params={"nm_id": nm_id})
|
| 505 |
if df.empty:
|
| 506 |
raise HTTPException(status_code=404, detail="Товар не найден в воронке")
|
| 507 |
-
return df.to_dict(orient="records")[0]
|
| 508 |
|
| 509 |
@app.get("/funnel/top", response_model=List[FunnelProduct])
|
| 510 |
def get_funnel_top(
|
|
@@ -526,7 +531,7 @@ def get_funnel_top(
|
|
| 526 |
LIMIT :limit
|
| 527 |
""")
|
| 528 |
df = pd.read_sql(query, engine, params={"limit": limit})
|
| 529 |
-
return df.to_dict(orient="records")
|
| 530 |
|
| 531 |
@app.get("/funnel/dynamics-alerts", response_model=List[FunnelAlert])
|
| 532 |
def get_dynamics_alerts(
|
|
@@ -546,7 +551,7 @@ def get_dynamics_alerts(
|
|
| 546 |
LIMIT :limit
|
| 547 |
""")
|
| 548 |
df = pd.read_sql(query, engine, params={"threshold": threshold, "limit": limit})
|
| 549 |
-
return df.to_dict(orient="records")
|
| 550 |
|
| 551 |
@app.get("/funnel/rising-stars", response_model=List[FunnelAlert])
|
| 552 |
def get_rising_stars(
|
|
@@ -566,7 +571,7 @@ def get_rising_stars(
|
|
| 566 |
LIMIT :limit
|
| 567 |
""")
|
| 568 |
df = pd.read_sql(query, engine, params={"threshold": threshold, "limit": limit})
|
| 569 |
-
return df.to_dict(orient="records")
|
| 570 |
|
| 571 |
@app.get("/funnel/conversion-leaks", response_model=List[ConversionLeak])
|
| 572 |
def get_conversion_leaks(limit: int = 20):
|
|
@@ -594,7 +599,7 @@ def get_conversion_leaks(limit: int = 20):
|
|
| 594 |
LIMIT :limit
|
| 595 |
""")
|
| 596 |
df = pd.read_sql(query, engine, params={"limit": limit})
|
| 597 |
-
return df.to_dict(orient="records")
|
| 598 |
|
| 599 |
@app.get("/funnel/high-demand-low-stock")
|
| 600 |
def get_high_demand_low_stock(limit: int = 20):
|
|
@@ -611,7 +616,7 @@ def get_high_demand_low_stock(limit: int = 20):
|
|
| 611 |
LIMIT :limit
|
| 612 |
""")
|
| 613 |
df = pd.read_sql(query, engine, params={"limit": limit})
|
| 614 |
-
return df.to_dict(orient="records")
|
| 615 |
|
| 616 |
@app.get("/funnel/wishlist-leaders")
|
| 617 |
def get_wishlist_leaders(limit: int = 20):
|
|
@@ -628,7 +633,7 @@ def get_wishlist_leaders(limit: int = 20):
|
|
| 628 |
LIMIT :limit
|
| 629 |
""")
|
| 630 |
df = pd.read_sql(query, engine, params={"limit": limit})
|
| 631 |
-
return df.to_dict(orient="records")
|
| 632 |
|
| 633 |
@app.get("/funnel/cancel-analysis")
|
| 634 |
def get_cancel_analysis(limit: int = 20):
|
|
@@ -646,7 +651,7 @@ def get_cancel_analysis(limit: int = 20):
|
|
| 646 |
LIMIT :limit
|
| 647 |
""")
|
| 648 |
df = pd.read_sql(query, engine, params={"limit": limit})
|
| 649 |
-
return df.to_dict(orient="records")
|
| 650 |
|
| 651 |
@app.get("/funnel/category-benchmark")
|
| 652 |
def get_category_benchmark(subject_id: int = None, limit: int = 20):
|
|
@@ -708,7 +713,7 @@ def get_category_benchmark(subject_id: int = None, limit: int = 20):
|
|
| 708 |
LIMIT :limit
|
| 709 |
""")
|
| 710 |
df = pd.read_sql(query, engine, params={"limit": limit})
|
| 711 |
-
return df.to_dict(orient="records")
|
| 712 |
|
| 713 |
@app.get("/funnel/rating-impact")
|
| 714 |
def get_rating_impact(limit: int = 20):
|
|
@@ -726,7 +731,7 @@ def get_rating_impact(limit: int = 20):
|
|
| 726 |
LIMIT :limit
|
| 727 |
""")
|
| 728 |
df = pd.read_sql(query, engine, params={"limit": limit})
|
| 729 |
-
return df.to_dict(orient="records")
|
| 730 |
|
| 731 |
# ==================== AI INSIGHTS ====================
|
| 732 |
|
|
@@ -757,7 +762,7 @@ def get_insights(nm_id: int):
|
|
| 757 |
ORDER BY analyzed_at DESC
|
| 758 |
""")
|
| 759 |
df = pd.read_sql(query, engine, params={"nm_id": nm_id})
|
| 760 |
-
return df.to_dict(orient="records")
|
| 761 |
|
| 762 |
|
| 763 |
@app.get("/analytics/sales-trend")
|
|
@@ -778,7 +783,7 @@ def get_sales_trend(days: int = 30):
|
|
| 778 |
""")
|
| 779 |
df = pd.read_sql(query, engine, params={"days": days})
|
| 780 |
df['date'] = df['date'].astype(str)
|
| 781 |
-
return df.to_dict(orient="records")
|
| 782 |
|
| 783 |
@app.get("/analytics/conversion-rate")
|
| 784 |
def get_conversion_rate(limit: int = 30):
|
|
@@ -800,7 +805,7 @@ def get_conversion_rate(limit: int = 30):
|
|
| 800 |
LIMIT :limit
|
| 801 |
""")
|
| 802 |
df = pd.read_sql(query, engine, params={"limit": limit})
|
| 803 |
-
return df.to_dict(orient="records")
|
| 804 |
|
| 805 |
@app.get("/analytics/avg-check-trend")
|
| 806 |
def get_avg_check_trend(days: int = 30):
|
|
@@ -817,7 +822,7 @@ def get_avg_check_trend(days: int = 30):
|
|
| 817 |
""")
|
| 818 |
df = pd.read_sql(query, engine, params={"days": days})
|
| 819 |
df['date'] = df['date'].astype(str)
|
| 820 |
-
return df.to_dict(orient="records")
|
| 821 |
|
| 822 |
@app.get("/analytics/regions-revenue")
|
| 823 |
def get_regions_revenue(limit: int = 15):
|
|
@@ -836,7 +841,7 @@ def get_regions_revenue(limit: int = 15):
|
|
| 836 |
LIMIT :limit
|
| 837 |
""")
|
| 838 |
df = pd.read_sql(query, engine, params={"limit": limit})
|
| 839 |
-
return df.to_dict(orient="records")
|
| 840 |
|
| 841 |
|
| 842 |
# ==================== ADS (РЕКЛАМА) ====================
|
|
@@ -862,7 +867,7 @@ def get_ads_summary():
|
|
| 862 |
FROM dm_ad_spend_product_7d
|
| 863 |
""")
|
| 864 |
df = pd.read_sql(query, engine)
|
| 865 |
-
return df.to_dict(orient="records")[0]
|
| 866 |
|
| 867 |
@app.get("/ads/trend")
|
| 868 |
def get_ads_trend(days: int = 30):
|
|
@@ -876,7 +881,7 @@ def get_ads_trend(days: int = 30):
|
|
| 876 |
ORDER BY day ASC
|
| 877 |
""")
|
| 878 |
df = pd.read_sql(query, engine, params={"days": days})
|
| 879 |
-
return df.to_dict(orient="records")
|
| 880 |
|
| 881 |
@app.get("/ads/products/top")
|
| 882 |
def get_ads_products_top(
|
|
@@ -896,7 +901,7 @@ def get_ads_products_top(
|
|
| 896 |
LIMIT :limit
|
| 897 |
""")
|
| 898 |
df = pd.read_sql(query, engine, params={"limit": limit})
|
| 899 |
-
return df.to_dict(orient="records")
|
| 900 |
|
| 901 |
@app.get("/ads/products/losers")
|
| 902 |
def get_ads_products_losers(limit: int = 20):
|
|
@@ -913,7 +918,7 @@ def get_ads_products_losers(limit: int = 20):
|
|
| 913 |
LIMIT :limit
|
| 914 |
""")
|
| 915 |
df = pd.read_sql(query, engine, params={"limit": limit})
|
| 916 |
-
return df.to_dict(orient="records")
|
| 917 |
|
| 918 |
@app.get("/ads/products/rising")
|
| 919 |
def get_ads_products_rising(limit: int = 20):
|
|
@@ -930,7 +935,7 @@ def get_ads_products_rising(limit: int = 20):
|
|
| 930 |
LIMIT :limit
|
| 931 |
""")
|
| 932 |
df = pd.read_sql(query, engine, params={"limit": limit})
|
| 933 |
-
return df.to_dict(orient="records")
|
| 934 |
|
| 935 |
@app.get("/ads/products/falling")
|
| 936 |
def get_ads_products_falling(limit: int = 20):
|
|
@@ -947,7 +952,7 @@ def get_ads_products_falling(limit: int = 20):
|
|
| 947 |
LIMIT :limit
|
| 948 |
""")
|
| 949 |
df = pd.read_sql(query, engine, params={"limit": limit})
|
| 950 |
-
return df.to_dict(orient="records")
|
| 951 |
|
| 952 |
@app.get("/ads/product/{nm_id}")
|
| 953 |
def get_ads_product(nm_id: int):
|
|
@@ -968,7 +973,7 @@ def get_ads_product(nm_id: int):
|
|
| 968 |
df = pd.read_sql(query, engine, params={"nm_id": nm_id})
|
| 969 |
if df.empty:
|
| 970 |
raise HTTPException(status_code=404, detail="Товар не найден в рекламных данных")
|
| 971 |
-
return df.to_dict(orient="records")[0]
|
| 972 |
|
| 973 |
@app.get("/ads/campaigns")
|
| 974 |
def get_ads_campaigns(
|
|
@@ -987,7 +992,7 @@ def get_ads_campaigns(
|
|
| 987 |
LIMIT :limit
|
| 988 |
""")
|
| 989 |
df = pd.read_sql(query, engine, params={"limit": limit})
|
| 990 |
-
return df.to_dict(orient="records")
|
| 991 |
|
| 992 |
@app.get("/ads/campaigns/inefficient")
|
| 993 |
def get_ads_campaigns_inefficient(limit: int = 20):
|
|
@@ -1008,7 +1013,7 @@ def get_ads_campaigns_inefficient(limit: int = 20):
|
|
| 1008 |
LIMIT :limit
|
| 1009 |
""")
|
| 1010 |
df = pd.read_sql(query, engine, params={"limit": limit})
|
| 1011 |
-
return df.to_dict(orient="records")
|
| 1012 |
|
| 1013 |
@app.get("/ads/campaign/{campaign_id}")
|
| 1014 |
def get_ads_campaign(campaign_id: int):
|
|
@@ -1027,7 +1032,7 @@ def get_ads_campaign(campaign_id: int):
|
|
| 1027 |
df = pd.read_sql(query, engine, params={"campaign_id": campaign_id})
|
| 1028 |
if df.empty:
|
| 1029 |
raise HTTPException(status_code=404, detail="Кампания не найдена")
|
| 1030 |
-
return df.to_dict(orient="records")[0]
|
| 1031 |
|
| 1032 |
@app.get("/ads/categories")
|
| 1033 |
def get_ads_categories(
|
|
@@ -1044,7 +1049,7 @@ def get_ads_categories(
|
|
| 1044 |
ORDER BY {sort_by} DESC
|
| 1045 |
""")
|
| 1046 |
df = pd.read_sql(query, engine)
|
| 1047 |
-
return df.to_dict(orient="records")
|
| 1048 |
|
| 1049 |
@app.get("/ads/category-benchmark")
|
| 1050 |
def get_ads_category_benchmark(nm_id: int):
|
|
@@ -1078,7 +1083,7 @@ def get_ads_category_benchmark(nm_id: int):
|
|
| 1078 |
df = pd.read_sql(query, engine, params={"nm_id": nm_id})
|
| 1079 |
if df.empty:
|
| 1080 |
raise HTTPException(status_code=404, detail="Товар не найден")
|
| 1081 |
-
return df.to_dict(orient="records")[0]
|
| 1082 |
|
| 1083 |
@app.get("/ads/alerts")
|
| 1084 |
def get_ads_alerts(limit: int = 30):
|
|
@@ -1109,7 +1114,7 @@ def get_ads_alerts(limit: int = 30):
|
|
| 1109 |
LIMIT :limit
|
| 1110 |
""")
|
| 1111 |
df = pd.read_sql(query, engine, params={"limit": limit})
|
| 1112 |
-
return df.to_dict(orient="records")
|
| 1113 |
|
| 1114 |
@app.get("/dashboard/ads-summary")
|
| 1115 |
def get_dashboard_ads_summary():
|
|
@@ -1126,7 +1131,7 @@ def get_dashboard_ads_summary():
|
|
| 1126 |
FROM dm_ad_spend_product_7d
|
| 1127 |
""")
|
| 1128 |
df = pd.read_sql(query, engine)
|
| 1129 |
-
return df.to_dict(orient="records")[0]
|
| 1130 |
|
| 1131 |
# ==================== DASHBOARD ====================
|
| 1132 |
|
|
@@ -1145,7 +1150,7 @@ def get_dashboard_summary():
|
|
| 1145 |
(SELECT COUNT(*) FROM wb_vitrine_for_ai WHERE total_stock = 0) as out_of_stock_count
|
| 1146 |
""")
|
| 1147 |
df = pd.read_sql(query, engine)
|
| 1148 |
-
return df.to_dict(orient="records")[0]
|
| 1149 |
|
| 1150 |
@app.get("/dashboard/funnel-summary")
|
| 1151 |
def get_dashboard_funnel_summary():
|
|
@@ -1163,4 +1168,4 @@ def get_dashboard_funnel_summary():
|
|
| 1163 |
FROM mv_funnel_with_finance
|
| 1164 |
""")
|
| 1165 |
df = pd.read_sql(query, engine)
|
| 1166 |
-
return df.to_dict(orient="records")[0]
|
|
|
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from database import engine
|
| 5 |
import pandas as pd
|
| 6 |
+
import numpy as np
|
| 7 |
from sqlalchemy import text
|
| 8 |
|
| 9 |
app = FastAPI(title="WB AI Analytics API - Neuron_AI Edition")
|
| 10 |
|
| 11 |
+
def clean_dataframe(df):
|
| 12 |
+
"""Заменяет inf и NaN на None для корректной валидации Pydantic и JSON-сериализации"""
|
| 13 |
+
return df.replace([np.inf, -np.inf, np.nan], None)
|
| 14 |
+
|
| 15 |
# --- Схемы данных ---
|
| 16 |
class ProductAnalytics(BaseModel):
|
| 17 |
nm_id: int
|
|
|
|
| 309 |
"""Топ товаров с сортировкой"""
|
| 310 |
query = text(f"SELECT * FROM wb_vitrine_for_ai ORDER BY {sort_by} DESC LIMIT :limit")
|
| 311 |
df = pd.read_sql(query, engine, params={"limit": limit})
|
| 312 |
+
return clean_dataframe(df).to_dict(orient="records")
|
| 313 |
|
| 314 |
@app.get("/analytics/product/{nm_id}", response_model=ProductAnalytics)
|
| 315 |
def get_product_details(nm_id: int):
|
|
|
|
| 318 |
df = pd.read_sql(query, engine, params={"nm_id": nm_id})
|
| 319 |
if df.empty:
|
| 320 |
raise HTTPException(status_code=404, detail="Товар не найден")
|
| 321 |
+
return clean_dataframe(df).to_dict(orient="records")[0]
|
| 322 |
|
| 323 |
@app.get("/analytics/product/{nm_id}/stocks", response_model=List[StockDistribution])
|
| 324 |
def get_product_stocks(nm_id: int):
|
|
|
|
| 333 |
WHERE nm_id = :nm_id AND amount > 0
|
| 334 |
""")
|
| 335 |
df = pd.read_sql(query, engine, params={"nm_id": nm_id})
|
| 336 |
+
return clean_dataframe(df).to_dict(orient="records")
|
| 337 |
|
| 338 |
@app.get("/analytics/product/{nm_id}/stock-summary", response_model=StockSummary)
|
| 339 |
def get_stock_summary(nm_id: int):
|
|
|
|
| 349 |
(SELECT COUNT(DISTINCT warehouse_id) FROM wb_stocks_fbs WHERE nm_id = :nm_id AND amount > 0) as fbs_warehouses
|
| 350 |
""")
|
| 351 |
df = pd.read_sql(query, engine, params={"nm_id": nm_id})
|
| 352 |
+
return clean_dataframe(df).to_dict(orient="records")[0]
|
| 353 |
|
| 354 |
@app.get("/analytics/low-stock", response_model=List[LowStockAlert])
|
| 355 |
def get_low_stock_alerts(days_threshold: int = 7):
|
|
|
|
| 370 |
ORDER BY days_of_stock ASC
|
| 371 |
""")
|
| 372 |
df = pd.read_sql(query, engine, params={"days_threshold": days_threshold})
|
| 373 |
+
return clean_dataframe(df).to_dict(orient="records")
|
| 374 |
|
| 375 |
@app.get("/analytics/out-of-stock")
|
| 376 |
def get_out_of_stock():
|
|
|
|
| 382 |
ORDER BY orders_7d DESC
|
| 383 |
""")
|
| 384 |
df = pd.read_sql(query, engine)
|
| 385 |
+
return clean_dataframe(df).to_dict(orient="records")
|
| 386 |
|
| 387 |
@app.get("/analytics/product/{nm_id}/sales", response_model=SalesStats)
|
| 388 |
def get_product_sales(nm_id: int):
|
|
|
|
| 401 |
WHERE product_nm_id = :nm_id
|
| 402 |
""")
|
| 403 |
df = pd.read_sql(query, engine, params={"nm_id": nm_id})
|
| 404 |
+
return clean_dataframe(df).to_dict(orient="records")[0]
|
| 405 |
|
| 406 |
@app.get("/analytics/profitability")
|
| 407 |
def get_profitability_report(limit: int = 50):
|
|
|
|
| 422 |
LIMIT :limit
|
| 423 |
""")
|
| 424 |
df = pd.read_sql(query, engine, params={"limit": limit})
|
| 425 |
+
return clean_dataframe(df).to_dict(orient="records")
|
| 426 |
|
| 427 |
@app.get("/analytics/returns")
|
| 428 |
def get_returns_report():
|
|
|
|
| 445 |
ORDER BY return_rate_pct DESC
|
| 446 |
""")
|
| 447 |
df = pd.read_sql(query, engine)
|
| 448 |
+
return clean_dataframe(df).to_dict(orient="records")
|
| 449 |
|
| 450 |
@app.get("/analytics/regions")
|
| 451 |
def get_regional_stats():
|
|
|
|
| 462 |
LIMIT 20
|
| 463 |
""")
|
| 464 |
df = pd.read_sql(query, engine)
|
| 465 |
+
return clean_dataframe(df).to_dict(orient="records")
|
| 466 |
|
| 467 |
# ==================== FUNNEL (ВОРОНКА) ====================
|
| 468 |
|
|
|
|
| 489 |
FROM mv_funnel_with_finance
|
| 490 |
""")
|
| 491 |
df = pd.read_sql(query, engine)
|
| 492 |
+
return clean_dataframe(df).to_dict(orient="records")[0]
|
| 493 |
|
| 494 |
@app.get("/funnel/product/{nm_id}", response_model=FunnelProduct)
|
| 495 |
def get_funnel_product(nm_id: int):
|
|
|
|
| 509 |
df = pd.read_sql(query, engine, params={"nm_id": nm_id})
|
| 510 |
if df.empty:
|
| 511 |
raise HTTPException(status_code=404, detail="Товар не найден в воронке")
|
| 512 |
+
return clean_dataframe(df).to_dict(orient="records")[0]
|
| 513 |
|
| 514 |
@app.get("/funnel/top", response_model=List[FunnelProduct])
|
| 515 |
def get_funnel_top(
|
|
|
|
| 531 |
LIMIT :limit
|
| 532 |
""")
|
| 533 |
df = pd.read_sql(query, engine, params={"limit": limit})
|
| 534 |
+
return clean_dataframe(df).to_dict(orient="records")
|
| 535 |
|
| 536 |
@app.get("/funnel/dynamics-alerts", response_model=List[FunnelAlert])
|
| 537 |
def get_dynamics_alerts(
|
|
|
|
| 551 |
LIMIT :limit
|
| 552 |
""")
|
| 553 |
df = pd.read_sql(query, engine, params={"threshold": threshold, "limit": limit})
|
| 554 |
+
return clean_dataframe(df).to_dict(orient="records")
|
| 555 |
|
| 556 |
@app.get("/funnel/rising-stars", response_model=List[FunnelAlert])
|
| 557 |
def get_rising_stars(
|
|
|
|
| 571 |
LIMIT :limit
|
| 572 |
""")
|
| 573 |
df = pd.read_sql(query, engine, params={"threshold": threshold, "limit": limit})
|
| 574 |
+
return clean_dataframe(df).to_dict(orient="records")
|
| 575 |
|
| 576 |
@app.get("/funnel/conversion-leaks", response_model=List[ConversionLeak])
|
| 577 |
def get_conversion_leaks(limit: int = 20):
|
|
|
|
| 599 |
LIMIT :limit
|
| 600 |
""")
|
| 601 |
df = pd.read_sql(query, engine, params={"limit": limit})
|
| 602 |
+
return clean_dataframe(df).to_dict(orient="records")
|
| 603 |
|
| 604 |
@app.get("/funnel/high-demand-low-stock")
|
| 605 |
def get_high_demand_low_stock(limit: int = 20):
|
|
|
|
| 616 |
LIMIT :limit
|
| 617 |
""")
|
| 618 |
df = pd.read_sql(query, engine, params={"limit": limit})
|
| 619 |
+
return clean_dataframe(df).to_dict(orient="records")
|
| 620 |
|
| 621 |
@app.get("/funnel/wishlist-leaders")
|
| 622 |
def get_wishlist_leaders(limit: int = 20):
|
|
|
|
| 633 |
LIMIT :limit
|
| 634 |
""")
|
| 635 |
df = pd.read_sql(query, engine, params={"limit": limit})
|
| 636 |
+
return clean_dataframe(df).to_dict(orient="records")
|
| 637 |
|
| 638 |
@app.get("/funnel/cancel-analysis")
|
| 639 |
def get_cancel_analysis(limit: int = 20):
|
|
|
|
| 651 |
LIMIT :limit
|
| 652 |
""")
|
| 653 |
df = pd.read_sql(query, engine, params={"limit": limit})
|
| 654 |
+
return clean_dataframe(df).to_dict(orient="records")
|
| 655 |
|
| 656 |
@app.get("/funnel/category-benchmark")
|
| 657 |
def get_category_benchmark(subject_id: int = None, limit: int = 20):
|
|
|
|
| 713 |
LIMIT :limit
|
| 714 |
""")
|
| 715 |
df = pd.read_sql(query, engine, params={"limit": limit})
|
| 716 |
+
return clean_dataframe(df).to_dict(orient="records")
|
| 717 |
|
| 718 |
@app.get("/funnel/rating-impact")
|
| 719 |
def get_rating_impact(limit: int = 20):
|
|
|
|
| 731 |
LIMIT :limit
|
| 732 |
""")
|
| 733 |
df = pd.read_sql(query, engine, params={"limit": limit})
|
| 734 |
+
return clean_dataframe(df).to_dict(orient="records")
|
| 735 |
|
| 736 |
# ==================== AI INSIGHTS ====================
|
| 737 |
|
|
|
|
| 762 |
ORDER BY analyzed_at DESC
|
| 763 |
""")
|
| 764 |
df = pd.read_sql(query, engine, params={"nm_id": nm_id})
|
| 765 |
+
return clean_dataframe(df).to_dict(orient="records")
|
| 766 |
|
| 767 |
|
| 768 |
@app.get("/analytics/sales-trend")
|
|
|
|
| 783 |
""")
|
| 784 |
df = pd.read_sql(query, engine, params={"days": days})
|
| 785 |
df['date'] = df['date'].astype(str)
|
| 786 |
+
return clean_dataframe(df).to_dict(orient="records")
|
| 787 |
|
| 788 |
@app.get("/analytics/conversion-rate")
|
| 789 |
def get_conversion_rate(limit: int = 30):
|
|
|
|
| 805 |
LIMIT :limit
|
| 806 |
""")
|
| 807 |
df = pd.read_sql(query, engine, params={"limit": limit})
|
| 808 |
+
return clean_dataframe(df).to_dict(orient="records")
|
| 809 |
|
| 810 |
@app.get("/analytics/avg-check-trend")
|
| 811 |
def get_avg_check_trend(days: int = 30):
|
|
|
|
| 822 |
""")
|
| 823 |
df = pd.read_sql(query, engine, params={"days": days})
|
| 824 |
df['date'] = df['date'].astype(str)
|
| 825 |
+
return clean_dataframe(df).to_dict(orient="records")
|
| 826 |
|
| 827 |
@app.get("/analytics/regions-revenue")
|
| 828 |
def get_regions_revenue(limit: int = 15):
|
|
|
|
| 841 |
LIMIT :limit
|
| 842 |
""")
|
| 843 |
df = pd.read_sql(query, engine, params={"limit": limit})
|
| 844 |
+
return clean_dataframe(df).to_dict(orient="records")
|
| 845 |
|
| 846 |
|
| 847 |
# ==================== ADS (РЕКЛАМА) ====================
|
|
|
|
| 867 |
FROM dm_ad_spend_product_7d
|
| 868 |
""")
|
| 869 |
df = pd.read_sql(query, engine)
|
| 870 |
+
return clean_dataframe(df).to_dict(orient="records")[0]
|
| 871 |
|
| 872 |
@app.get("/ads/trend")
|
| 873 |
def get_ads_trend(days: int = 30):
|
|
|
|
| 881 |
ORDER BY day ASC
|
| 882 |
""")
|
| 883 |
df = pd.read_sql(query, engine, params={"days": days})
|
| 884 |
+
return clean_dataframe(df).to_dict(orient="records")
|
| 885 |
|
| 886 |
@app.get("/ads/products/top")
|
| 887 |
def get_ads_products_top(
|
|
|
|
| 901 |
LIMIT :limit
|
| 902 |
""")
|
| 903 |
df = pd.read_sql(query, engine, params={"limit": limit})
|
| 904 |
+
return clean_dataframe(df).to_dict(orient="records")
|
| 905 |
|
| 906 |
@app.get("/ads/products/losers")
|
| 907 |
def get_ads_products_losers(limit: int = 20):
|
|
|
|
| 918 |
LIMIT :limit
|
| 919 |
""")
|
| 920 |
df = pd.read_sql(query, engine, params={"limit": limit})
|
| 921 |
+
return clean_dataframe(df).to_dict(orient="records")
|
| 922 |
|
| 923 |
@app.get("/ads/products/rising")
|
| 924 |
def get_ads_products_rising(limit: int = 20):
|
|
|
|
| 935 |
LIMIT :limit
|
| 936 |
""")
|
| 937 |
df = pd.read_sql(query, engine, params={"limit": limit})
|
| 938 |
+
return clean_dataframe(df).to_dict(orient="records")
|
| 939 |
|
| 940 |
@app.get("/ads/products/falling")
|
| 941 |
def get_ads_products_falling(limit: int = 20):
|
|
|
|
| 952 |
LIMIT :limit
|
| 953 |
""")
|
| 954 |
df = pd.read_sql(query, engine, params={"limit": limit})
|
| 955 |
+
return clean_dataframe(df).to_dict(orient="records")
|
| 956 |
|
| 957 |
@app.get("/ads/product/{nm_id}")
|
| 958 |
def get_ads_product(nm_id: int):
|
|
|
|
| 973 |
df = pd.read_sql(query, engine, params={"nm_id": nm_id})
|
| 974 |
if df.empty:
|
| 975 |
raise HTTPException(status_code=404, detail="Товар не найден в рекламных данных")
|
| 976 |
+
return clean_dataframe(df).to_dict(orient="records")[0]
|
| 977 |
|
| 978 |
@app.get("/ads/campaigns")
|
| 979 |
def get_ads_campaigns(
|
|
|
|
| 992 |
LIMIT :limit
|
| 993 |
""")
|
| 994 |
df = pd.read_sql(query, engine, params={"limit": limit})
|
| 995 |
+
return clean_dataframe(df).to_dict(orient="records")
|
| 996 |
|
| 997 |
@app.get("/ads/campaigns/inefficient")
|
| 998 |
def get_ads_campaigns_inefficient(limit: int = 20):
|
|
|
|
| 1013 |
LIMIT :limit
|
| 1014 |
""")
|
| 1015 |
df = pd.read_sql(query, engine, params={"limit": limit})
|
| 1016 |
+
return clean_dataframe(df).to_dict(orient="records")
|
| 1017 |
|
| 1018 |
@app.get("/ads/campaign/{campaign_id}")
|
| 1019 |
def get_ads_campaign(campaign_id: int):
|
|
|
|
| 1032 |
df = pd.read_sql(query, engine, params={"campaign_id": campaign_id})
|
| 1033 |
if df.empty:
|
| 1034 |
raise HTTPException(status_code=404, detail="Кампания не найдена")
|
| 1035 |
+
return clean_dataframe(df).to_dict(orient="records")[0]
|
| 1036 |
|
| 1037 |
@app.get("/ads/categories")
|
| 1038 |
def get_ads_categories(
|
|
|
|
| 1049 |
ORDER BY {sort_by} DESC
|
| 1050 |
""")
|
| 1051 |
df = pd.read_sql(query, engine)
|
| 1052 |
+
return clean_dataframe(df).to_dict(orient="records")
|
| 1053 |
|
| 1054 |
@app.get("/ads/category-benchmark")
|
| 1055 |
def get_ads_category_benchmark(nm_id: int):
|
|
|
|
| 1083 |
df = pd.read_sql(query, engine, params={"nm_id": nm_id})
|
| 1084 |
if df.empty:
|
| 1085 |
raise HTTPException(status_code=404, detail="Товар не найден")
|
| 1086 |
+
return clean_dataframe(df).to_dict(orient="records")[0]
|
| 1087 |
|
| 1088 |
@app.get("/ads/alerts")
|
| 1089 |
def get_ads_alerts(limit: int = 30):
|
|
|
|
| 1114 |
LIMIT :limit
|
| 1115 |
""")
|
| 1116 |
df = pd.read_sql(query, engine, params={"limit": limit})
|
| 1117 |
+
return clean_dataframe(df).to_dict(orient="records")
|
| 1118 |
|
| 1119 |
@app.get("/dashboard/ads-summary")
|
| 1120 |
def get_dashboard_ads_summary():
|
|
|
|
| 1131 |
FROM dm_ad_spend_product_7d
|
| 1132 |
""")
|
| 1133 |
df = pd.read_sql(query, engine)
|
| 1134 |
+
return clean_dataframe(df).to_dict(orient="records")[0]
|
| 1135 |
|
| 1136 |
# ==================== DASHBOARD ====================
|
| 1137 |
|
|
|
|
| 1150 |
(SELECT COUNT(*) FROM wb_vitrine_for_ai WHERE total_stock = 0) as out_of_stock_count
|
| 1151 |
""")
|
| 1152 |
df = pd.read_sql(query, engine)
|
| 1153 |
+
return clean_dataframe(df).to_dict(orient="records")[0]
|
| 1154 |
|
| 1155 |
@app.get("/dashboard/funnel-summary")
|
| 1156 |
def get_dashboard_funnel_summary():
|
|
|
|
| 1168 |
FROM mv_funnel_with_finance
|
| 1169 |
""")
|
| 1170 |
df = pd.read_sql(query, engine)
|
| 1171 |
+
return clean_dataframe(df).to_dict(orient="records")[0]
|