From 8c73a51730b5edd088f4d58c5e743ca4c60f928f Mon Sep 17 00:00:00 2001 From: Seth Foster Date: Wed, 7 Apr 2021 15:54:43 -0400 Subject: [PATCH 1/4] Fix elapsed time on job showing incorrect value - job elapsed time showed 0.0, during and after the job run --- awx/main/models/unified_jobs.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/awx/main/models/unified_jobs.py b/awx/main/models/unified_jobs.py index 76a2895bf8..6b9f28941e 100644 --- a/awx/main/models/unified_jobs.py +++ b/awx/main/models/unified_jobs.py @@ -842,15 +842,16 @@ class UnifiedJob( if 'finished' not in update_fields: update_fields.append('finished') + if self.elapsed is None: + self.elapsed = 0.0 + if 'elapsed' not in update_fields: + update_fields.append('elapsed') + # If we have a start and finished time, and haven't already calculated # out the time that elapsed, do so. - if self.started and self.finished and not self.elapsed: + if self.started and self.finished and self.elapsed == 0.0: td = self.finished - self.started - elapsed = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10 ** 6) / (10 ** 6 * 1.0) - else: - elapsed = 0.0 - if self.elapsed != elapsed: - self.elapsed = str(elapsed) + self.elapsed = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10 ** 6) / (10 ** 6 * 1.0) if 'elapsed' not in update_fields: update_fields.append('elapsed') From 09c176847d2193336f598d3aae1c3a61fadc9b8e Mon Sep 17 00:00:00 2001 From: Seth Foster Date: Wed, 7 Apr 2021 17:30:46 -0400 Subject: [PATCH 2/4] string --- awx/main/models/unified_jobs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/awx/main/models/unified_jobs.py b/awx/main/models/unified_jobs.py index 6b9f28941e..a8d20e1788 100644 --- a/awx/main/models/unified_jobs.py +++ b/awx/main/models/unified_jobs.py @@ -851,7 +851,7 @@ class UnifiedJob( # out the time that elapsed, do so. if self.started and self.finished and self.elapsed == 0.0: td = self.finished - self.started - self.elapsed = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10 ** 6) / (10 ** 6 * 1.0) + self.elapsed = str((td.microseconds + (td.seconds + td.days * 24 * 3600) * 10 ** 6) / (10 ** 6 * 1.0)) if 'elapsed' not in update_fields: update_fields.append('elapsed') From f05ffa521aef7488dcbcb78209919d0be1754a11 Mon Sep 17 00:00:00 2001 From: Seth Foster Date: Thu, 8 Apr 2021 11:36:40 -0400 Subject: [PATCH 3/4] the returned self.elapsed should be decimal, instead of float, to match django model field type --- awx/main/models/unified_jobs.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/awx/main/models/unified_jobs.py b/awx/main/models/unified_jobs.py index a8d20e1788..d33f54ce1a 100644 --- a/awx/main/models/unified_jobs.py +++ b/awx/main/models/unified_jobs.py @@ -4,6 +4,7 @@ # Python from io import StringIO import datetime +import decimal import codecs import json import logging @@ -842,8 +843,9 @@ class UnifiedJob( if 'finished' not in update_fields: update_fields.append('finished') + dq = decimal.Decimal('1.000') if self.elapsed is None: - self.elapsed = 0.0 + self.elapsed = decimal.Decimal(0.0).quantize(dq) if 'elapsed' not in update_fields: update_fields.append('elapsed') @@ -851,7 +853,8 @@ class UnifiedJob( # out the time that elapsed, do so. if self.started and self.finished and self.elapsed == 0.0: td = self.finished - self.started - self.elapsed = str((td.microseconds + (td.seconds + td.days * 24 * 3600) * 10 ** 6) / (10 ** 6 * 1.0)) + elapsed = decimal.Decimal(td.total_seconds()) + self.elapsed = elapsed.quantize(dq) if 'elapsed' not in update_fields: update_fields.append('elapsed') From 89e28d6d4adea6404ed887ec5f61ae0520269680 Mon Sep 17 00:00:00 2001 From: Seth Foster Date: Thu, 8 Apr 2021 13:38:35 -0400 Subject: [PATCH 4/4] fix awx_collections tests by comparing Decimal to float, instead of comparing strings --- awx/main/models/unified_jobs.py | 2 -- awx_collection/test/awx/test_ad_hoc_wait.py | 6 ++++-- awx_collection/test/awx/test_job.py | 6 ++++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/awx/main/models/unified_jobs.py b/awx/main/models/unified_jobs.py index d33f54ce1a..6bc85e3162 100644 --- a/awx/main/models/unified_jobs.py +++ b/awx/main/models/unified_jobs.py @@ -846,8 +846,6 @@ class UnifiedJob( dq = decimal.Decimal('1.000') if self.elapsed is None: self.elapsed = decimal.Decimal(0.0).quantize(dq) - if 'elapsed' not in update_fields: - update_fields.append('elapsed') # If we have a start and finished time, and haven't already calculated # out the time that elapsed, do so. diff --git a/awx_collection/test/awx/test_ad_hoc_wait.py b/awx_collection/test/awx/test_ad_hoc_wait.py index 943268184a..5718783015 100644 --- a/awx_collection/test/awx/test_ad_hoc_wait.py +++ b/awx_collection/test/awx/test_ad_hoc_wait.py @@ -13,9 +13,10 @@ def test_ad_hoc_command_wait_successful(run_module, admin_user): command = AdHocCommand.objects.create(status='successful', started=now(), finished=now()) result = run_module('tower_ad_hoc_command_wait', dict(command_id=command.id), admin_user) result.pop('invocation', None) + result['elapsed'] = float(result['elapsed']) assert result.pop('finished', '')[:10] == str(command.finished)[:10] assert result.pop('started', '')[:10] == str(command.started)[:10] - assert result == {"status": "successful", "changed": False, "elapsed": str(command.elapsed), "id": command.id} + assert result == {"status": "successful", "changed": False, "elapsed": command.elapsed, "id": command.id} @pytest.mark.django_db @@ -23,13 +24,14 @@ def test_ad_hoc_command_wait_failed(run_module, admin_user): command = AdHocCommand.objects.create(status='failed', started=now(), finished=now()) result = run_module('tower_ad_hoc_command_wait', dict(command_id=command.id), admin_user) result.pop('invocation', None) + result['elapsed'] = float(result['elapsed']) assert result.pop('finished', '')[:10] == str(command.finished)[:10] assert result.pop('started', '')[:10] == str(command.started)[:10] assert result == { "status": "failed", "failed": True, "changed": False, - "elapsed": str(command.elapsed), + "elapsed": command.elapsed, "id": command.id, "msg": "The ad hoc command - 1, failed", } diff --git a/awx_collection/test/awx/test_job.py b/awx_collection/test/awx/test_job.py index 012c1fd9d1..107d7c1616 100644 --- a/awx_collection/test/awx/test_job.py +++ b/awx_collection/test/awx/test_job.py @@ -13,9 +13,10 @@ def test_job_wait_successful(run_module, admin_user): job = Job.objects.create(status='successful', started=now(), finished=now()) result = run_module('tower_job_wait', dict(job_id=job.id), admin_user) result.pop('invocation', None) + result['elapsed'] = float(result['elapsed']) assert result.pop('finished', '')[:10] == str(job.finished)[:10] assert result.pop('started', '')[:10] == str(job.started)[:10] - assert result == {"status": "successful", "changed": False, "elapsed": str(job.elapsed), "id": job.id} + assert result == {"status": "successful", "changed": False, "elapsed": job.elapsed, "id": job.id} @pytest.mark.django_db @@ -23,9 +24,10 @@ def test_job_wait_failed(run_module, admin_user): job = Job.objects.create(status='failed', started=now(), finished=now()) result = run_module('tower_job_wait', dict(job_id=job.id), admin_user) result.pop('invocation', None) + result['elapsed'] = float(result['elapsed']) assert result.pop('finished', '')[:10] == str(job.finished)[:10] assert result.pop('started', '')[:10] == str(job.started)[:10] - assert result == {"status": "failed", "failed": True, "changed": False, "elapsed": str(job.elapsed), "id": job.id, "msg": "Job with id 1 failed"} + assert result == {"status": "failed", "failed": True, "changed": False, "elapsed": job.elapsed, "id": job.id, "msg": "Job with id 1 failed"} @pytest.mark.django_db