Skip to content

Commit

Permalink
Add diff statistics calculation and display in compliance view template
Browse files Browse the repository at this point in the history
  • Loading branch information
miaow2 committed Oct 27, 2024
1 parent 9012a14 commit 3543f9f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
13 changes: 13 additions & 0 deletions netbox_config_diff/compliance/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,16 @@ def get_remediation_commands(name: str, platform: str, actual_config: str, rende
host.load_running_config(config_text=actual_config)
host.load_generated_config(config_text=rendered_config)
return host.remediation_config_filtered_text(include_tags={}, exclude_tags={})


def get_diff_statistics(diff: str) -> tuple[int, int]:
lines_added = 0
lines_deleted = 0

for line in diff.splitlines():
if line.startswith("+") and not line.startswith("+++"):
lines_added += 1
elif line.startswith("-") and not line.startswith("---"):
lines_deleted += 1

return lines_added, lines_deleted
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ <h5 class="card-header">Error</h5>
<div class="row mb-3">
<div class="col col-md-12">
<div class="card">
<h5 class="card-header">Diff</h5>
<h2 class="card-header">Diff
{% if version|first != "3" and statistics %}
<div class="card-actions">
<span style="color: #1a7f37;">{{ statistics.0 }}</span> line(s) missing,
<span style="color: #d1242f;">{{ statistics.1 }}</span> extra line(s)
</div>
{% endif %}
</h2>
<div class="card-body" id="diffElement"></div>
</div>
</div>
Expand Down
5 changes: 5 additions & 0 deletions netbox_config_diff/views/compliance.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from netbox.views import generic
from utilities.views import ViewTab, register_model_view

from netbox_config_diff.compliance.utils import get_diff_statistics
from netbox_config_diff.filtersets import ConfigComplianceFilterSet, PlatformSettingFilterSet
from netbox_config_diff.forms import (
ConfigComplianceFilterForm,
Expand All @@ -25,10 +26,14 @@ class ConfigComplianceView(generic.ObjectView):
template_name = "netbox_config_diff/configcompliance/data.html"

def get_extra_context(self, request, instance):
statistics = None
if instance.diff:
statistics = get_diff_statistics(instance.diff)
return {
"instance": instance,
"base_template": self.base_template,
"version": VERSION,
"statistics": statistics,
}


Expand Down

0 comments on commit 3543f9f

Please sign in to comment.