Revert "perf: Optimize average_time_cost calculation in job model"

This reverts commit eafb5ecfb3.
pull/14810/head
w940853815 2025-01-14 18:11:48 +08:00 committed by Bryan
parent e721ec147c
commit c088437fe5
1 changed files with 6 additions and 13 deletions

View File

@ -9,8 +9,7 @@ from datetime import timedelta
from celery import current_task from celery import current_task
from django.conf import settings from django.conf import settings
from django.db import models from django.db import models
from django.db.models import Q, Avg, F, FloatField from django.db.models import Q
from django.db.models.functions import Coalesce, Now, Extract
from django.utils import timezone from django.utils import timezone
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
@ -189,17 +188,11 @@ class Job(JMSOrgBaseModel, PeriodTaskModelMixin):
@property @property
def average_time_cost(self): def average_time_cost(self):
executions = self.executions.filter(status__in=['success', 'failed']) total_cost = 0
if not executions.exists(): finished_count = self.executions.filter(status__in=['success', 'failed']).count()
return 0 for execution in self.executions.filter(status__in=['success', 'failed']).all():
average = executions.annotate( total_cost += execution.time_cost
time_cost=Coalesce(F('date_finished'), Now()) - F('date_start') return total_cost / finished_count if finished_count else 0
).annotate(
time_cost_seconds=Extract('time_cost', 'epoch')
).aggregate(
avg_time=Avg('time_cost_seconds')
)['avg_time']
return average if average is not None else 0
def get_register_task(self): def get_register_task(self):
from ..tasks import run_ops_job from ..tasks import run_ops_job