code quality run
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
import logging
|
||||
from typing import Any, Dict, List
|
||||
|
||||
from sqlalchemy import func
|
||||
from sqlalchemy.orm import Session
|
||||
import logging
|
||||
from typing import List, Dict, Any
|
||||
|
||||
from models.database_models import User, Product, Stock
|
||||
from models.api_models import StatsResponse, MarketplaceStatsResponse
|
||||
from models.api_models import MarketplaceStatsResponse, StatsResponse
|
||||
from models.database_models import Product, Stock, User
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -25,26 +26,37 @@ class StatsService:
|
||||
# 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_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()
|
||||
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_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()
|
||||
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()
|
||||
@@ -57,10 +69,12 @@ class StatsService:
|
||||
"unique_marketplaces": unique_marketplaces,
|
||||
"unique_shops": unique_shops,
|
||||
"total_stock_entries": total_stock_entries,
|
||||
"total_inventory_quantity": total_inventory
|
||||
"total_inventory_quantity": total_inventory,
|
||||
}
|
||||
|
||||
logger.info(f"Generated comprehensive stats: {total_products} products, {unique_marketplaces} marketplaces")
|
||||
logger.info(
|
||||
f"Generated comprehensive stats: {total_products} products, {unique_marketplaces} marketplaces"
|
||||
)
|
||||
return stats_data
|
||||
|
||||
def get_marketplace_breakdown_stats(self, db: Session) -> List[Dict[str, Any]]:
|
||||
@@ -74,25 +88,31 @@ class StatsService:
|
||||
List of dictionaries containing marketplace statistics
|
||||
"""
|
||||
# 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()
|
||||
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()
|
||||
)
|
||||
|
||||
stats_list = [
|
||||
{
|
||||
"marketplace": stat.marketplace,
|
||||
"total_products": stat.total_products,
|
||||
"unique_shops": stat.unique_shops,
|
||||
"unique_brands": stat.unique_brands
|
||||
} for stat in marketplace_stats
|
||||
"unique_brands": stat.unique_brands,
|
||||
}
|
||||
for stat in marketplace_stats
|
||||
]
|
||||
|
||||
logger.info(f"Generated marketplace breakdown stats for {len(stats_list)} marketplaces")
|
||||
logger.info(
|
||||
f"Generated marketplace breakdown stats for {len(stats_list)} marketplaces"
|
||||
)
|
||||
return stats_list
|
||||
|
||||
def get_product_count(self, db: Session) -> int:
|
||||
@@ -101,31 +121,42 @@ class StatsService:
|
||||
|
||||
def get_unique_brands_count(self, db: Session) -> int:
|
||||
"""Get count of unique brands"""
|
||||
return db.query(Product.brand).filter(
|
||||
Product.brand.isnot(None),
|
||||
Product.brand != ""
|
||||
).distinct().count()
|
||||
return (
|
||||
db.query(Product.brand)
|
||||
.filter(Product.brand.isnot(None), Product.brand != "")
|
||||
.distinct()
|
||||
.count()
|
||||
)
|
||||
|
||||
def get_unique_categories_count(self, db: Session) -> int:
|
||||
"""Get count of unique categories"""
|
||||
return db.query(Product.google_product_category).filter(
|
||||
Product.google_product_category.isnot(None),
|
||||
Product.google_product_category != ""
|
||||
).distinct().count()
|
||||
return (
|
||||
db.query(Product.google_product_category)
|
||||
.filter(
|
||||
Product.google_product_category.isnot(None),
|
||||
Product.google_product_category != "",
|
||||
)
|
||||
.distinct()
|
||||
.count()
|
||||
)
|
||||
|
||||
def get_unique_marketplaces_count(self, db: Session) -> int:
|
||||
"""Get count of unique marketplaces"""
|
||||
return db.query(Product.marketplace).filter(
|
||||
Product.marketplace.isnot(None),
|
||||
Product.marketplace != ""
|
||||
).distinct().count()
|
||||
return (
|
||||
db.query(Product.marketplace)
|
||||
.filter(Product.marketplace.isnot(None), Product.marketplace != "")
|
||||
.distinct()
|
||||
.count()
|
||||
)
|
||||
|
||||
def get_unique_shops_count(self, db: Session) -> int:
|
||||
"""Get count of unique shops"""
|
||||
return db.query(Product.shop_name).filter(
|
||||
Product.shop_name.isnot(None),
|
||||
Product.shop_name != ""
|
||||
).distinct().count()
|
||||
return (
|
||||
db.query(Product.shop_name)
|
||||
.filter(Product.shop_name.isnot(None), Product.shop_name != "")
|
||||
.distinct()
|
||||
.count()
|
||||
)
|
||||
|
||||
def get_stock_statistics(self, db: Session) -> Dict[str, int]:
|
||||
"""
|
||||
@@ -142,25 +173,35 @@ class StatsService:
|
||||
|
||||
return {
|
||||
"total_stock_entries": total_stock_entries,
|
||||
"total_inventory_quantity": total_inventory
|
||||
"total_inventory_quantity": total_inventory,
|
||||
}
|
||||
|
||||
def get_brands_by_marketplace(self, db: Session, marketplace: str) -> List[str]:
|
||||
"""Get unique brands for a specific marketplace"""
|
||||
brands = db.query(Product.brand).filter(
|
||||
Product.marketplace == marketplace,
|
||||
Product.brand.isnot(None),
|
||||
Product.brand != ""
|
||||
).distinct().all()
|
||||
brands = (
|
||||
db.query(Product.brand)
|
||||
.filter(
|
||||
Product.marketplace == marketplace,
|
||||
Product.brand.isnot(None),
|
||||
Product.brand != "",
|
||||
)
|
||||
.distinct()
|
||||
.all()
|
||||
)
|
||||
return [brand[0] for brand in brands]
|
||||
|
||||
def get_shops_by_marketplace(self, db: Session, marketplace: str) -> List[str]:
|
||||
"""Get unique shops for a specific marketplace"""
|
||||
shops = db.query(Product.shop_name).filter(
|
||||
Product.marketplace == marketplace,
|
||||
Product.shop_name.isnot(None),
|
||||
Product.shop_name != ""
|
||||
).distinct().all()
|
||||
shops = (
|
||||
db.query(Product.shop_name)
|
||||
.filter(
|
||||
Product.marketplace == marketplace,
|
||||
Product.shop_name.isnot(None),
|
||||
Product.shop_name != "",
|
||||
)
|
||||
.distinct()
|
||||
.all()
|
||||
)
|
||||
return [shop[0] for shop in shops]
|
||||
|
||||
def get_products_by_marketplace(self, db: Session, marketplace: str) -> int:
|
||||
|
||||
Reference in New Issue
Block a user