fix fix fi x fix

This commit is contained in:
IGNY8 VPS (Salman)
2026-01-12 15:30:15 +00:00
parent 7d4d309677
commit e8360a6703
11 changed files with 488 additions and 71 deletions

View File

@@ -1896,18 +1896,27 @@ class AutomationService:
"""
Get total credits used by this run so far.
Uses CreditUsageLog (same source as /account/usage/credits endpoint) for accuracy.
Filters by site to only count credits used by this specific automation run.
"""
if not self.run:
return 0
# FIXED: Use CreditUsageLog instead of counting AITaskLog records
# This matches the source of truth used by /account/usage/credits endpoint
# Use CreditUsageLog - the source of truth for credit usage
# Filter by site to get only credits used by this automation run
from igny8_core.business.billing.models import CreditUsageLog
from django.db.models import Sum
filters = {
'account': self.account,
'created_at__gte': self.run.started_at
}
# Filter by site if available for more accurate per-run counting
if self.site:
filters['site'] = self.site
total = CreditUsageLog.objects.filter(
account=self.account,
created_at__gte=self.run.started_at
**filters
).aggregate(total=Sum('credits_used'))['total'] or 0
return total