Skip to content

Closes #76: Add diff statistics calculation and display in compliance view template #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading