|
| 1 | +import decimal |
1 | 2 | import logging |
2 | 3 |
|
| 4 | +from django.conf import settings |
3 | 5 | from django.db import transaction |
4 | 6 |
|
5 | 7 | from backend.apps.clusters.models import ( |
@@ -60,12 +62,26 @@ def organization(self) -> Organization | None: |
60 | 62 | @property |
61 | 63 | def job_template(self) -> JobTemplate: |
62 | 64 | external_job_template = self.data.summary_fields.job_template |
63 | | - return JobTemplate.create_or_update( |
| 65 | + job_template = JobTemplate.create_or_update( |
64 | 66 | cluster=self.cluster, |
65 | 67 | external_id=external_job_template.id if external_job_template else -1, |
66 | 68 | name=external_job_template.name if external_job_template else self.data.name, |
67 | 69 | description=external_job_template.description if external_job_template else self.data.description, |
68 | 70 | ) |
| 71 | + job_count = Job.objects.filter(job_template=job_template).count() |
| 72 | + # If no jobs exist for this template, it's safe to calculate the manual execution time |
| 73 | + if job_count == 0: |
| 74 | + logger.info("No jobs exist for this template. Calculating manual execution time.") |
| 75 | + elapsed = self.data.elapsed |
| 76 | + if elapsed is not None: |
| 77 | + manual_execution_time = int(decimal.Decimal(elapsed / 60 * 2).quantize(decimal.Decimal(1), rounding=decimal.ROUND_UP)) |
| 78 | + if manual_execution_time < settings.DEFAULT_TIME_TAKEN_TO_MANUALLY_EXECUTE_MINUTES: |
| 79 | + manual_execution_time = settings.DEFAULT_TIME_TAKEN_TO_MANUALLY_EXECUTE_MINUTES |
| 80 | + if manual_execution_time > 1000000: |
| 81 | + manual_execution_time = 1000000 |
| 82 | + job_template.time_taken_manually_execute_minutes = manual_execution_time |
| 83 | + job_template.save(update_fields=['time_taken_manually_execute_minutes']) |
| 84 | + return job_template |
69 | 85 |
|
70 | 86 | @property |
71 | 87 | def launched_by(self) -> AAPUser | None: |
|
0 commit comments