From b74f17ddf1d0a908942735ce9974fbc614abd12c Mon Sep 17 00:00:00 2001 From: JCWasmx86 Date: Sun, 26 Nov 2023 17:41:07 +0100 Subject: [PATCH] Add first draft for progress in reports --- netbox/core/models/jobs.py | 4 ++++ netbox/extras/reports.py | 16 +++++++++++++- netbox/extras/views.py | 21 +++++++++++++++++++ .../templates/extras/htmx/report_result.html | 12 ++++++++++- 4 files changed, 51 insertions(+), 2 deletions(-) diff --git a/netbox/core/models/jobs.py b/netbox/core/models/jobs.py index 61b0e64fab..911be2aa1d 100644 --- a/netbox/core/models/jobs.py +++ b/netbox/core/models/jobs.py @@ -133,6 +133,10 @@ def duration(self): return f"{int(minutes)} minutes, {seconds:.2f} seconds" + @property + def connection(self): + return django_rq.get_connection('default') + def delete(self, *args, **kwargs): super().delete(*args, **kwargs) diff --git a/netbox/extras/reports.py b/netbox/extras/reports.py index cc279a49ad..a7679d0f7e 100644 --- a/netbox/extras/reports.py +++ b/netbox/extras/reports.py @@ -200,7 +200,17 @@ def log_failure(self, obj, message): self._results[self.active_test]['failure'] += 1 self.logger.info(f"Failure | {obj}: {message}") self.failed = True - + + def log_progress(self, msg, current, needed): + assert current <= needed + if needed == 0: + percentage = 1.0 + else: + percentage = float(current) / float(needed) + + conn = self.__job.connection + conn.set(f"{self.__job}_{self.active_test}_progress", percentage * 100.0) + conn.set(f"{self.__job}_{self.active_test}_progress_message", msg) # # Run methods # @@ -213,10 +223,14 @@ def run(self, job): # Perform any post-run tasks self.pre_run() + + self.__job = job try: for method_name in self.test_methods: self.active_test = method_name + conn = job.connection + conn.set(f"{job}_active_test", self.active_test) test_method = getattr(self, method_name) test_method() job.data = self._results diff --git a/netbox/extras/views.py b/netbox/extras/views.py index 55b73d29d3..bfee104379 100644 --- a/netbox/extras/views.py +++ b/netbox/extras/views.py @@ -1,3 +1,5 @@ +import django_rq + from django.contrib import messages from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.contenttypes.models import ContentType @@ -1107,9 +1109,28 @@ def get(self, request, job_pk): # If this is an HTMX request, return only the result HTML if is_htmx(request): + conn = django_rq.get_connection('default') + active_test = conn.get(f'{job}_active_test') + + if active_test: + active_test = active_test.decode('utf-8') + active_progress = conn.get(f'{job}_{active_test}_progress') + if active_progress: + active_progress = float(active_progress.decode('utf-8')) + active_progress_message = conn.get(f'{job}_{active_test}_progress_message') + if active_progress_message: + active_progress_message = active_progress_message.decode('utf-8') + else: + active_progress = None + active_progress_message = None + response = render(request, 'extras/htmx/report_result.html', { 'report': report, 'job': job, + 'emit_progress': active_test is not None and active_progress is not None, + 'active_test': active_test, + 'active_progress': active_progress, + 'active_progress_message': active_progress_message, }) if job.completed or not job.started: response.status_code = 286 diff --git a/netbox/templates/extras/htmx/report_result.html b/netbox/templates/extras/htmx/report_result.html index e6b6caf731..d0d95b30e0 100644 --- a/netbox/templates/extras/htmx/report_result.html +++ b/netbox/templates/extras/htmx/report_result.html @@ -77,5 +77,15 @@
{% trans "Report Results" %}
{% elif job.started %} - {% include 'extras/inc/result_pending.html' %} + {% if emit_progress %} + {% if active_progress_message %} +

{{ active_test }}: {{ active_progress_message }}

+ {% endif %} + +
+
{{ active_progress|floatformat:2 }}%
+
+ {% else %} + {% include 'extras/inc/result_pending.html' %} + {% endif %} {% endif %}