Fix some issues

This commit is contained in:
Urtzi Alfaro
2026-01-11 19:38:54 +01:00
parent ce4f3aff8c
commit 54163843ec
17 changed files with 282 additions and 67 deletions

View File

@@ -13,6 +13,7 @@ import redis.asyncio as redis
from shared.auth.decorators import get_current_user_dep
from app.core.config import settings
from app.core.database import database_manager
from app.services.subscription_limit_service import SubscriptionLimitService
router = APIRouter(prefix="/usage-forecast", tags=["usage-forecast"])
@@ -180,82 +181,84 @@ async def get_usage_forecast(
"""
# Initialize services
redis_client = await get_redis_client()
limit_service = SubscriptionLimitService()
limit_service = SubscriptionLimitService(database_manager=database_manager)
try:
# Get current usage summary
# Get current usage summary (includes limits)
usage_summary = await limit_service.get_usage_summary(tenant_id)
subscription = await limit_service.get_active_subscription(tenant_id)
if not subscription:
if not usage_summary or 'error' in usage_summary:
raise HTTPException(
status_code=404,
detail=f"No active subscription found for tenant {tenant_id}"
)
# Extract usage data
usage = usage_summary.get('usage', {})
# Define metrics to forecast
metric_configs = [
{
'key': 'users',
'label': 'Users',
'current': usage_summary['users'],
'limit': subscription.max_users,
'current': usage.get('users', {}).get('current', 0),
'limit': usage.get('users', {}).get('limit'),
'unit': ''
},
{
'key': 'locations',
'label': 'Locations',
'current': usage_summary['locations'],
'limit': subscription.max_locations,
'current': usage.get('locations', {}).get('current', 0),
'limit': usage.get('locations', {}).get('limit'),
'unit': ''
},
{
'key': 'products',
'label': 'Products',
'current': usage_summary['products'],
'limit': subscription.max_products,
'current': usage.get('products', {}).get('current', 0),
'limit': usage.get('products', {}).get('limit'),
'unit': ''
},
{
'key': 'recipes',
'label': 'Recipes',
'current': usage_summary['recipes'],
'limit': subscription.max_recipes,
'current': usage.get('recipes', {}).get('current', 0),
'limit': usage.get('recipes', {}).get('limit'),
'unit': ''
},
{
'key': 'suppliers',
'label': 'Suppliers',
'current': usage_summary['suppliers'],
'limit': subscription.max_suppliers,
'current': usage.get('suppliers', {}).get('current', 0),
'limit': usage.get('suppliers', {}).get('limit'),
'unit': ''
},
{
'key': 'training_jobs',
'label': 'Training Jobs',
'current': usage_summary.get('training_jobs_today', 0),
'limit': subscription.max_training_jobs_per_day,
'current': usage.get('training_jobs_today', {}).get('current', 0),
'limit': usage.get('training_jobs_today', {}).get('limit'),
'unit': '/day'
},
{
'key': 'forecasts',
'label': 'Forecasts',
'current': usage_summary.get('forecasts_today', 0),
'limit': subscription.max_forecasts_per_day,
'current': usage.get('forecasts_today', {}).get('current', 0),
'limit': usage.get('forecasts_today', {}).get('limit'),
'unit': '/day'
},
{
'key': 'api_calls',
'label': 'API Calls',
'current': usage_summary.get('api_calls_this_hour', 0),
'limit': subscription.max_api_calls_per_hour,
'current': usage.get('api_calls_this_hour', {}).get('current', 0),
'limit': usage.get('api_calls_this_hour', {}).get('limit'),
'unit': '/hour'
},
{
'key': 'storage',
'label': 'File Storage',
'current': int(usage_summary.get('file_storage_used_gb', 0)),
'limit': subscription.max_storage_gb,
'current': int(usage.get('file_storage_used_gb', {}).get('current', 0)),
'limit': usage.get('file_storage_used_gb', {}).get('limit'),
'unit': ' GB'
}
]

View File

@@ -143,7 +143,7 @@ service.add_router(plans.router, tags=["subscription-plans"]) # Public endpoint
service.add_router(internal_demo.router, tags=["internal-demo"]) # Internal demo data cloning
service.add_router(subscription.router, tags=["subscription"])
service.add_router(internal_demo.router, tags=["internal-demo"]) # Internal demo data cloning
service.add_router(usage_forecast.router, tags=["usage-forecast"]) # Usage forecasting & predictive analytics
service.add_router(usage_forecast.router, prefix="/api/v1", tags=["usage-forecast"]) # Usage forecasting & predictive analytics
service.add_router(internal_demo.router, tags=["internal-demo"]) # Internal demo data cloning
# Register settings router BEFORE tenants router to ensure proper route matching
service.add_router(tenant_settings.router, prefix="/api/v1/tenants", tags=["tenant-settings"])