Преглед изворни кода

Populate execution_time for existing jobs

Jeremy Stretch пре 2 недеља
родитељ
комит
09cd3f2dfd
1 измењених фајлова са 15 додато и 0 уклоњено
  1. 15 0
      netbox/core/migrations/0025_add_job_execution_time.py

+ 15 - 0
netbox/core/migrations/0025_add_job_execution_time.py

@@ -1,4 +1,15 @@
 from django.db import migrations, models
+from django.db.models import DurationField, ExpressionWrapper, F
+
+
+def populate_execution_time(apps, schema_editor):
+    """
+    Populate execution_time for existing jobs which have both a start and completion time recorded.
+    """
+    Job = apps.get_model("core", "Job")
+    Job.objects.filter(started__isnull=False, completed__isnull=False).update(
+        execution_time=ExpressionWrapper(F("completed") - F("started"), output_field=DurationField())
+    )
 
 
 class Migration(migrations.Migration):
@@ -13,4 +24,8 @@ class Migration(migrations.Migration):
             name="execution_time",
             field=models.DurationField(blank=True, editable=False, null=True),
         ),
+        migrations.RunPython(
+            code=populate_execution_time,
+            reverse_code=migrations.RunPython.noop,
+        ),
     ]