Skip to content

Commit

Permalink
Add compliance check subtotals in machine detail
Browse files Browse the repository at this point in the history
  • Loading branch information
np5 committed Jun 5, 2024
1 parent 9e6acef commit cb87828
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
27 changes: 16 additions & 11 deletions tests/inventory/test_compliance_checks_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ def test_machine_no_compliance_checks(self):
response = self.client.get(self.machine.get_absolute_url())
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "inventory/machine_detail.html")
self.assertContains(response, "Compliance checks (0)")
self.assertContains(response, "<h3>Compliance checks</h3>")

def test_machine_no_tags_no_compliance_checks_in_scope(self):
self._login(
Expand All @@ -528,7 +528,7 @@ def test_machine_no_tags_no_compliance_checks_in_scope(self):
response = self.client.get(self.machine.get_absolute_url())
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "inventory/machine_detail.html")
self.assertContains(response, "Compliance checks (0)")
self.assertContains(response, "<h3>Compliance checks</h3>")

def test_machine_with_tag_no_compliance_checks_in_scope(self):
self._login(
Expand All @@ -543,7 +543,7 @@ def test_machine_with_tag_no_compliance_checks_in_scope(self):
response = self.client.get(self.machine.get_absolute_url())
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "inventory/machine_detail.html")
self.assertContains(response, "Compliance checks (0)")
self.assertContains(response, "<h3>Compliance checks</h3>")

def test_machine_source_mismatch_no_compliance_checks_in_scope(self):
self._login(
Expand All @@ -555,7 +555,7 @@ def test_machine_source_mismatch_no_compliance_checks_in_scope(self):
response = self.client.get(self.machine.get_absolute_url())
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "inventory/machine_detail.html")
self.assertContains(response, "Compliance checks (0)")
self.assertContains(response, "<h3>Compliance checks</h3>")

def test_machine_source_match_platform_missmatch_no_compliance_checks_in_scope(self):
self._login(
Expand All @@ -567,7 +567,7 @@ def test_machine_source_match_platform_missmatch_no_compliance_checks_in_scope(s
response = self.client.get(self.machine.get_absolute_url())
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "inventory/machine_detail.html")
self.assertContains(response, "Compliance checks (0)")
self.assertContains(response, "<h3>Compliance checks</h3>")

def test_machine_source_match_one_compliance_checks_in_scope(self):
self._login(
Expand All @@ -579,7 +579,8 @@ def test_machine_source_match_one_compliance_checks_in_scope(self):
response = self.client.get(self.machine.get_absolute_url())
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "inventory/machine_detail.html")
self.assertContains(response, "Compliance check (1)")
self.assertContains(response, "<h3>Compliance check</h3>")
self.assertContains(response, "0/1 OK, 1 Pending")
self.assertContains(response, cc.compliance_check.name)
cc_redirect_link = reverse("compliance_checks:redirect", args=(cc.compliance_check.pk,))
compliance_check_statuses = response.context["compliance_check_statuses"]
Expand All @@ -600,7 +601,8 @@ def test_machine_no_tags_compliance_checks_one_in_scope_one_out_of_scope_pending
response = self.client.get(self.machine.get_absolute_url())
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "inventory/machine_detail.html")
self.assertContains(response, "Compliance check (1)")
self.assertContains(response, "<h3>Compliance check</h3>")
self.assertContains(response, "0/1 OK, 1 Pending")
self.assertContains(response, cc_without_tag.compliance_check.name)
self.assertNotContains(response, cc_with_tags.compliance_check.name)
cc_redirect_link = reverse("compliance_checks:redirect", args=(cc_without_tag.compliance_check.pk,))
Expand All @@ -623,7 +625,8 @@ def test_machine_no_tags_compliance_checks_one_in_scope_one_out_of_scope_pending
response = self.client.get(self.machine.get_absolute_url())
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "inventory/machine_detail.html")
self.assertContains(response, "Compliance check (1)")
self.assertContains(response, "<h3>Compliance check</h3>")
self.assertContains(response, "0/1 OK, 1 Pending")
self.assertContains(response, cc_without_tag.compliance_check.name)
self.assertNotContains(response, cc_with_tags.compliance_check.name)
cc_redirect_link = reverse("compliance_checks:redirect", args=(cc_without_tag.compliance_check.pk,))
Expand All @@ -646,7 +649,7 @@ def test_machine_no_tags_compliance_checks_one_in_scope_one_out_of_scope_no_sect
response = self.client.get(self.machine.get_absolute_url())
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "inventory/machine_detail.html")
self.assertNotContains(response, "Compliance check (1)")
self.assertNotContains(response, "<h3>Compliance check</h3>")
self.assertNotContains(response, cc_without_tag.compliance_check.name)
cc_redirect_link = reverse("compliance_checks:redirect", args=(cc_without_tag.compliance_check.pk,))
self.assertNotContains(response, cc_redirect_link)
Expand All @@ -673,7 +676,8 @@ def test_machine_tags_once_compliance_check_in_scope_ok_with_link(self):
response = self.client.get(self.machine.get_absolute_url())
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "inventory/machine_detail.html")
self.assertContains(response, "Compliance check (1)")
self.assertContains(response, "<h3>Compliance check</h3>")
self.assertContains(response, "1/1 OK")
self.assertContains(response, cc.compliance_check.name)
cc_redirect_link = reverse("compliance_checks:redirect", args=(cc.compliance_check.pk,))
self.assertContains(response, cc_redirect_link)
Expand Down Expand Up @@ -714,7 +718,8 @@ def test_machine_tags_once_compliance_check_in_scope_two_different_statuses_ok_w
response = self.client.get(self.machine.get_absolute_url())
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "inventory/machine_detail.html")
self.assertContains(response, "Compliance check (1)")
self.assertContains(response, "<h3>Compliance check</h3>")
self.assertContains(response, "1/1 OK")
self.assertContains(response, cc.compliance_check.name)
cc_redirect_link = reverse("compliance_checks:redirect", args=(cc.compliance_check.pk,))
self.assertContains(response, cc_redirect_link)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,12 @@ <h3>Extra facts</h4>
</div><!-- end col-md-6 -->
<div class="col-md-6">
{% if perms.compliance_checks.view_machinestatus %}
<h3>Compliance check{{ compliance_check_statuses|length|pluralize }} ({{ compliance_check_statuses|length }})</h3>
<h3>Compliance check{{ compliance_check_total|pluralize }}</h3>
{% if compliance_check_total %}
<h4>
{{ compliance_check_ok }}/{{ compliance_check_total }} OK{% if compliance_check_failed %}, {{ compliance_check_failed }} Failed{% endif %}{% if compliance_check_pending %}, {{ compliance_check_pending }} Pending{% endif %}{% if compliance_check_unknown %}, {{ compliance_check_unknown }} Unknown{% endif %}
</h4>
{% endif %}
{% if compliance_check_statuses %}
<table class="table table-striped align-middle table-hover">
<thead>
Expand Down
16 changes: 16 additions & 0 deletions zentral/contrib/inventory/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from zentral.conf import settings
from zentral.core.compliance_checks import compliance_check_class_from_model
from zentral.core.compliance_checks.forms import ComplianceCheckForm
from zentral.core.compliance_checks.models import Status
from zentral.core.incidents.models import MachineIncident
from zentral.core.stores.conf import frontend_store, stores
from zentral.core.stores.views import EventsView, FetchEventsView, EventsStoreRedirectView
Expand Down Expand Up @@ -535,14 +536,29 @@ def ms_sort_key(t):

# compliance checks
compliance_check_statuses = []
cc_total = cc_ok = cc_pending = cc_unknown = cc_failed = 0
if self.request.user.has_perm("compliance_checks.view_machinestatus"):
for cc_model, cc_pk, cc_name, status, status_time in machine.compliance_check_statuses():
cc_url = None
cc_cls = compliance_check_class_from_model(cc_model)
if self.request.user.has_perms(cc_cls.required_view_permissions):
cc_url = reverse("compliance_checks:redirect", args=(cc_pk,))
compliance_check_statuses.append((cc_url, cc_name, status, status_time))
cc_total += 1
if status == Status.OK:
cc_ok += 1
elif status == Status.PENDING:
cc_pending += 1
elif status == Status.UNKNOWN:
cc_unknown += 1
elif status == Status.FAILED:
cc_failed += 1
context["compliance_check_statuses"] = compliance_check_statuses
context["compliance_check_total"] = cc_total
context["compliance_check_ok"] = cc_ok
context["compliance_check_failed"] = cc_failed
context["compliance_check_pending"] = cc_pending
context["compliance_check_unknown"] = cc_unknown

# event links
context['show_events_link'] = frontend_store.machine_events
Expand Down

0 comments on commit cb87828

Please sign in to comment.