From 880ea83546eaea4b2b02aed07424807790098035 Mon Sep 17 00:00:00 2001 From: Jacob Brewer Date: Wed, 27 Dec 2023 20:22:04 +0000 Subject: [PATCH] Sorting the report order --- cmd/summary/reports.go | 3 +++ pkg/entities/report.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/cmd/summary/reports.go b/cmd/summary/reports.go index 0d9c8de0..546eb422 100644 --- a/cmd/summary/reports.go +++ b/cmd/summary/reports.go @@ -80,6 +80,9 @@ func reportIDHandler(w http.ResponseWriter, r *http.Request) { return } + // Sort the report resources. + report.SortResources() + rep = report type PageData struct { diff --git a/pkg/entities/report.go b/pkg/entities/report.go index bdb36495..b9abd2e1 100644 --- a/pkg/entities/report.go +++ b/pkg/entities/report.go @@ -3,6 +3,7 @@ package entities import ( "log/slog" "path/filepath" + "sort" "time" ) @@ -117,3 +118,38 @@ func (n *PuppetReport) ReportFilePath() string { n.YamlFile = path return path } + +func (n *PuppetReport) SortResources() { + // Sort the resources. + n.sortResource(n.ResourcesFailed) + n.sortResource(n.ResourcesChanged) + n.sortResource(n.ResourcesSkipped) + n.sortResource(n.ResourcesOK) +} + +func (n *PuppetReport) sortResource(resources []*PuppetResource) { + sort.Slice(resources, func(i, j int) bool { + // Sort by resource file. + if resources[i].File != resources[j].File { + return resources[i].File < resources[j].File + } + + // Sort by resource line. + if resources[i].Line != resources[j].Line { + return resources[i].Line < resources[j].Line + } + + // Sort by resource type. + if resources[i].Type != resources[j].Type { + return resources[i].Type < resources[j].Type + } + + // Sort by resource name. + if resources[i].Name != resources[j].Name { + return resources[i].Name < resources[j].Name + } + + // Resources are equal. + return false + }) +}