Refactoring code for modular approach
This commit is contained in:
@@ -5,6 +5,7 @@ from sqlalchemy import func
|
||||
from sqlalchemy.orm import Session
|
||||
from app.core.database import get_db
|
||||
from app.api.deps import get_current_user
|
||||
from app.services.stats_service import stats_service
|
||||
from app.tasks.background_tasks import process_marketplace_import
|
||||
from middleware.decorators import rate_limit
|
||||
from models.api_models import MarketplaceImportJobResponse, MarketplaceImportRequest, StatsResponse, \
|
||||
@@ -21,66 +22,37 @@ logger = logging.getLogger(__name__)
|
||||
@router.get("/stats", response_model=StatsResponse)
|
||||
def get_stats(db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
|
||||
"""Get comprehensive statistics with marketplace data (Protected)"""
|
||||
try:
|
||||
stats_data = stats_service.get_comprehensive_stats(db=db)
|
||||
|
||||
# Use more efficient queries with proper indexes
|
||||
total_products = db.query(Product).count()
|
||||
|
||||
unique_brands = db.query(Product.brand).filter(
|
||||
Product.brand.isnot(None),
|
||||
Product.brand != ""
|
||||
).distinct().count()
|
||||
|
||||
unique_categories = db.query(Product.google_product_category).filter(
|
||||
Product.google_product_category.isnot(None),
|
||||
Product.google_product_category != ""
|
||||
).distinct().count()
|
||||
|
||||
# New marketplace statistics
|
||||
unique_marketplaces = db.query(Product.marketplace).filter(
|
||||
Product.marketplace.isnot(None),
|
||||
Product.marketplace != ""
|
||||
).distinct().count()
|
||||
|
||||
unique_shops = db.query(Product.shop_name).filter(
|
||||
Product.shop_name.isnot(None),
|
||||
Product.shop_name != ""
|
||||
).distinct().count()
|
||||
|
||||
# Stock statistics
|
||||
total_stock_entries = db.query(Stock).count()
|
||||
total_inventory = db.query(func.sum(Stock.quantity)).scalar() or 0
|
||||
|
||||
return StatsResponse(
|
||||
total_products=total_products,
|
||||
unique_brands=unique_brands,
|
||||
unique_categories=unique_categories,
|
||||
unique_marketplaces=unique_marketplaces,
|
||||
unique_shops=unique_shops,
|
||||
total_stock_entries=total_stock_entries,
|
||||
total_inventory_quantity=total_inventory
|
||||
)
|
||||
return StatsResponse(
|
||||
total_products=stats_data["total_products"],
|
||||
unique_brands=stats_data["unique_brands"],
|
||||
unique_categories=stats_data["unique_categories"],
|
||||
unique_marketplaces=stats_data["unique_marketplaces"],
|
||||
unique_shops=stats_data["unique_shops"],
|
||||
total_stock_entries=stats_data["total_stock_entries"],
|
||||
total_inventory_quantity=stats_data["total_inventory_quantity"]
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"Error getting comprehensive stats: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
|
||||
|
||||
@router.get("/marketplace-stats", response_model=List[MarketplaceStatsResponse])
|
||||
def get_marketplace_stats(db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
|
||||
"""Get statistics broken down by marketplace (Protected)"""
|
||||
try:
|
||||
marketplace_stats = stats_service.get_marketplace_breakdown_stats(db=db)
|
||||
|
||||
# Query to get stats per marketplace
|
||||
marketplace_stats = db.query(
|
||||
Product.marketplace,
|
||||
func.count(Product.id).label('total_products'),
|
||||
func.count(func.distinct(Product.shop_name)).label('unique_shops'),
|
||||
func.count(func.distinct(Product.brand)).label('unique_brands')
|
||||
).filter(
|
||||
Product.marketplace.isnot(None)
|
||||
).group_by(Product.marketplace).all()
|
||||
|
||||
return [
|
||||
MarketplaceStatsResponse(
|
||||
marketplace=stat.marketplace,
|
||||
total_products=stat.total_products,
|
||||
unique_shops=stat.unique_shops,
|
||||
unique_brands=stat.unique_brands
|
||||
) for stat in marketplace_stats
|
||||
]
|
||||
|
||||
return [
|
||||
MarketplaceStatsResponse(
|
||||
marketplace=stat["marketplace"],
|
||||
total_products=stat["total_products"],
|
||||
unique_shops=stat["unique_shops"],
|
||||
unique_brands=stat["unique_brands"]
|
||||
) for stat in marketplace_stats
|
||||
]
|
||||
except Exception as e:
|
||||
logger.error(f"Error getting marketplace stats: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
|
||||
Reference in New Issue
Block a user