From 00f702b3dae1dc7c369692cfc94541ed879e0961 Mon Sep 17 00:00:00 2001 From: Thomas Fransolet Date: Fri, 10 Apr 2026 17:04:46 +0200 Subject: [PATCH] Update for quotas (use it in ia translator) --- ManagerService/Controllers/AiController.cs | 14 ++++++++++++++ .../Controllers/InstanceController.cs | 17 +++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/ManagerService/Controllers/AiController.cs b/ManagerService/Controllers/AiController.cs index 7da302c..21920a3 100644 --- a/ManagerService/Controllers/AiController.cs +++ b/ManagerService/Controllers/AiController.cs @@ -46,6 +46,20 @@ namespace ManagerService.Controllers if (instance == null || !instance.IsAssistant) return Forbid(); + var monthKey = DateTime.UtcNow.ToString("yyyy-MM"); + if (instance.AiUsageMonthKey != monthKey) + { + instance.AiRequestsThisMonth = 0; + instance.AiUsageMonthKey = monthKey; + } + + var quota = instance.AiRequestsPerMonth; + if (quota > 0 && instance.AiRequestsThisMonth >= quota) + return StatusCode(429, "Quota IA mensuel dépassé"); + + instance.AiRequestsThisMonth++; + _context.SaveChanges(); + var result = await _assistantService.TranslateAsync(request); return Ok(result); } diff --git a/ManagerService/Controllers/InstanceController.cs b/ManagerService/Controllers/InstanceController.cs index 510ddad..c1c32de 100644 --- a/ManagerService/Controllers/InstanceController.cs +++ b/ManagerService/Controllers/InstanceController.cs @@ -305,12 +305,25 @@ namespace ManagerService.Controllers var monthKey = DateTime.UtcNow.ToString("yyyy-MM"); var aiUsed = instance.AiUsageMonthKey == monthKey ? instance.AiRequestsThisMonth : 0; + var storageQuota = instance.StorageQuotaBytes; + var aiQuota = instance.AiRequestsPerMonth; + + if ((storageQuota == 0 || aiQuota == 0) && instance.SubscriptionPlanId != null) + { + var plan = _myInfoMateDbContext.SubscriptionPlans.FirstOrDefault(p => p.Id == instance.SubscriptionPlanId); + if (plan != null) + { + if (storageQuota == 0) storageQuota = plan.StorageQuotaBytes; + if (aiQuota == 0) aiQuota = plan.AiRequestsPerMonth; + } + } + return new OkObjectResult(new InstanceQuotaDTO { storageUsedBytes = storageUsed, - storageQuotaBytes = instance.StorageQuotaBytes, + storageQuotaBytes = storageQuota, aiRequestsUsed = aiUsed, - aiRequestsPerMonth = instance.AiRequestsPerMonth + aiRequestsPerMonth = aiQuota }); } catch (Exception ex)