master plan implemenattion

This commit is contained in:
IGNY8 VPS (Salman)
2025-12-30 08:51:31 +00:00
parent 96aaa4151a
commit 2af7bb725f
10 changed files with 900 additions and 26 deletions

View File

@@ -1643,14 +1643,22 @@ class AutomationService:
raise
def _get_credits_used(self) -> int:
"""Get total credits used by this run so far"""
"""
Get total credits used by this run so far.
Uses CreditUsageLog (same source as /account/usage/credits endpoint) for accuracy.
"""
if not self.run:
return 0
total = AITaskLog.objects.filter(
# FIXED: Use CreditUsageLog instead of counting AITaskLog records
# This matches the source of truth used by /account/usage/credits endpoint
from igny8_core.business.billing.models import CreditUsageLog
from django.db.models import Sum
total = CreditUsageLog.objects.filter(
account=self.account,
created_at__gte=self.run.started_at
).aggregate(total=Count('id'))['total'] or 0
).aggregate(total=Sum('credits_used'))['total'] or 0
return total