Skip to content

Commit

Permalink
Add first draft for progress in reports
Browse files Browse the repository at this point in the history
  • Loading branch information
JCWasmx86 committed Nov 26, 2023
1 parent d52a6d3 commit b74f17d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
4 changes: 4 additions & 0 deletions netbox/core/models/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
16 changes: 15 additions & 1 deletion netbox/extras/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions netbox/extras/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion netbox/templates/extras/htmx/report_result.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,15 @@ <h5 class="card-header">{% trans "Report Results" %}</h5>
</div>
</div>
{% elif job.started %}
{% include 'extras/inc/result_pending.html' %}
{% if emit_progress %}
{% if active_progress_message %}
<p><code>{{ active_test }}</code>: {{ active_progress_message }}</p>
{% endif %}

<div class="progress">
<div class="progress-bar" role="progressbar" style="width: {{ active_progress }}%;" aria-valuenow="{{ active_progress }}" aria-valuemin="0" aria-valuemax="100">{{ active_progress|floatformat:2 }}%</div>
</div>
{% else %}
{% include 'extras/inc/result_pending.html' %}
{% endif %}
{% endif %}

0 comments on commit b74f17d

Please sign in to comment.