Update for quotas (use it in ia translator)

This commit is contained in:
Thomas Fransolet 2026-04-10 17:04:46 +02:00
parent e0ff0eeba6
commit 00f702b3da
2 changed files with 29 additions and 2 deletions

View File

@ -46,6 +46,20 @@ namespace ManagerService.Controllers
if (instance == null || !instance.IsAssistant) if (instance == null || !instance.IsAssistant)
return Forbid(); 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); var result = await _assistantService.TranslateAsync(request);
return Ok(result); return Ok(result);
} }

View File

@ -305,12 +305,25 @@ namespace ManagerService.Controllers
var monthKey = DateTime.UtcNow.ToString("yyyy-MM"); var monthKey = DateTime.UtcNow.ToString("yyyy-MM");
var aiUsed = instance.AiUsageMonthKey == monthKey ? instance.AiRequestsThisMonth : 0; 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 return new OkObjectResult(new InstanceQuotaDTO
{ {
storageUsedBytes = storageUsed, storageUsedBytes = storageUsed,
storageQuotaBytes = instance.StorageQuotaBytes, storageQuotaBytes = storageQuota,
aiRequestsUsed = aiUsed, aiRequestsUsed = aiUsed,
aiRequestsPerMonth = instance.AiRequestsPerMonth aiRequestsPerMonth = aiQuota
}); });
} }
catch (Exception ex) catch (Exception ex)