Skip to content

Commit

Permalink
Sort reports
Browse files Browse the repository at this point in the history
  • Loading branch information
prymitive committed Oct 25, 2024
1 parent d009d5d commit b704144
Show file tree
Hide file tree
Showing 14 changed files with 40 additions and 71 deletions.
1 change: 1 addition & 0 deletions cmd/pint/ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ func actionCI(c *cli.Context) error {
slog.Info("Problems found", logSeverityCounters(bySeverity)...)
}

summary.SortReports()
for _, rep := range reps {
err = rep.Submit(summary)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/pint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func actionLint(c *cli.Context) error {
reps = append(reps, reporter.NewJSONReporter(j))
}

summary.SortReports()
for _, rep := range reps {
err = rep.Submit(summary)
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion cmd/pint/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ func checkRules(ctx context.Context, workers int, isOffline bool, gen *config.Pr
for result := range results {
summary.Report(result)
}
summary.SortReports()
summary.Duration = time.Since(start)
summary.TotalEntries = len(entries)
summary.CheckedEntries = checkedEntriesCount.Load()
Expand Down
8 changes: 4 additions & 4 deletions cmd/pint/tests/0007_alerts.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ cmp stderr stderr.txt
-- stderr.txt --
level=INFO msg="Loading configuration file" path=.pint.hcl
level=INFO msg="Finding all rules to check" paths=["rules"]
rules/0001.yml:1-2 Bug: `url` annotation is required. (alerts/annotation)
rules/0001.yml:1-2 Warning: `severity` label is required. (rule/label)
1 | - alert: Always
2 | expr: up

rules/0001.yml:1-2 Warning: `severity` label is required. (rule/label)
rules/0001.yml:1-2 Bug: `url` annotation is required. (alerts/annotation)
1 | - alert: Always
2 | expr: up

rules/0001.yml:2 Warning: Alert query doesn't have any condition, it will always fire if the metric exists. (alerts/comparison)
2 | expr: up

rules/0001.yml:9-10 Bug: `url` annotation is required. (alerts/annotation)
rules/0001.yml:9-10 Warning: `severity` label is required. (rule/label)
9 | - alert: ServiceIsDown
10 | expr: up == 0

rules/0001.yml:9-10 Warning: `severity` label is required. (rule/label)
rules/0001.yml:9-10 Bug: `url` annotation is required. (alerts/annotation)
9 | - alert: ServiceIsDown
10 | expr: up == 0

Expand Down
4 changes: 2 additions & 2 deletions cmd/pint/tests/0008_recording_rule_prometheus.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ cmp stderr stderr.txt
-- stderr.txt --
level=INFO msg="Loading configuration file" path=.pint.hcl
level=INFO msg="Finding all rules to check" paths=["rules"]
rules/0001.yml:5 Bug: `instance` label should be removed when aggregating `^colo(?:_.+)?:.+$` rules, remove instance from `by()`. (promql/aggregate)
rules/0001.yml:5 Warning: `job` label is required and should be preserved when aggregating `^.+$` rules, use `by(job, ...)`. (promql/aggregate)
5 | expr: sum by (instance) (http_inprogress_requests)

rules/0001.yml:5 Warning: `job` label is required and should be preserved when aggregating `^.+$` rules, use `by(job, ...)`. (promql/aggregate)
rules/0001.yml:5 Bug: `instance` label should be removed when aggregating `^colo(?:_.+)?:.+$` rules, remove instance from `by()`. (promql/aggregate)
5 | expr: sum by (instance) (http_inprogress_requests)

level=INFO msg="Problems found" Bug=1 Warning=1
Expand Down
4 changes: 2 additions & 2 deletions cmd/pint/tests/0160_ci_comment_edit.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ level=WARN msg="Using dummy Prometheus uptime metric results with no gaps" name=
level=WARN msg="No results for Prometheus uptime metric, you might have set uptime config option to a missing metric, please check your config" name=prom metric=up
level=WARN msg="Using dummy Prometheus uptime metric results with no gaps" name=prom metric=up
level=INFO msg="Problems found" Bug=2 Warning=1
rules.yml:8 Bug: `prom` Prometheus server at http://127.0.0.1:7160 didn't have any series for `up` metric in the last 1w. (promql/series)
rules.yml:8 Warning: pint disable comment `promql/series(xxx)` doesn't match any selector in this query (promql/series)
8 | expr: up == 0

rules.yml:8 Warning: pint disable comment `promql/series(xxx)` doesn't match any selector in this query (promql/series)
rules.yml:8 Bug: `prom` Prometheus server at http://127.0.0.1:7160 didn't have any series for `up` metric in the last 1w. (promql/series)
8 | expr: up == 0

rules.yml:16 Bug: `prom` Prometheus server at http://127.0.0.1:7160 didn't have any series for `up` metric in the last 1w. (promql/series)
Expand Down
4 changes: 2 additions & 2 deletions cmd/pint/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
_ "net/http/pprof"
"os"
"os/signal"
"sort"
"slices"
"strings"
"sync"
"syscall"
Expand Down Expand Up @@ -406,7 +406,7 @@ func (c *problemCollector) Collect(ch chan<- prometheus.Metric) {

ch <- prometheus.MustNewConstMetric(c.problems, prometheus.GaugeValue, float64(len(done)))

sort.Strings(keys)
slices.Sort(keys)
var reported int
for _, key := range keys {
ch <- done[key]
Expand Down
3 changes: 1 addition & 2 deletions internal/checks/promql_vector_matching.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"slices"
"sort"
"strings"

"github.com/prometheus/common/model"
Expand Down Expand Up @@ -243,7 +242,7 @@ func (c VectorMatchingCheck) seriesLabels(ctx context.Context, query string, ign
ls.add(l.Name)
})
if len(ls.names) > 1 {
sort.Strings(ls.names)
slices.Sort(ls.names)
}
lsets = append(lsets, ls)
}
Expand Down
17 changes: 8 additions & 9 deletions internal/checks/rule_dependency.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package checks

import (
"cmp"
"context"
"fmt"
"sort"
"slices"
"strconv"
"strings"

Expand Down Expand Up @@ -83,14 +84,12 @@ func (c RuleDependencyCheck) Check(_ context.Context, path discovery.Path, rule
return problems
}

sort.Slice(broken, func(i, j int) bool {
if broken[i].path != broken[j].path {
return broken[i].path < broken[j].path
}
if broken[i].line != broken[j].line {
return broken[i].line < broken[j].line
}
return broken[i].name < broken[j].name
slices.SortFunc(broken, func(a, b *brokenDependency) int {
return cmp.Or(
cmp.Compare(a.path, b.path),
cmp.Compare(a.line, b.line),
cmp.Compare(a.name, b.name),
)
})

var details strings.Builder
Expand Down
4 changes: 2 additions & 2 deletions internal/output/ranges.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package output

import (
"fmt"
"sort"
"slices"
"strconv"
"strings"
)

func FormatLineRangeString(lines []int) string {
ls := make([]int, len(lines))
copy(ls, lines)
sort.Ints(ls)
slices.Sort(ls)

var ranges []string
start := -1
Expand Down
3 changes: 2 additions & 1 deletion internal/promapi/range_normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package promapi

import (
"fmt"
"slices"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -35,7 +36,7 @@ func labelsBefore(ls, o labels.Labels) bool {
o.Range(func(l labels.Label) {
lns = append(lns, l.Name)
})
sort.Strings(lns)
slices.Sort(lns)
for _, ln := range lns {
mlv, ok := labelValue(ls, ln)
if !ok {
Expand Down
33 changes: 3 additions & 30 deletions internal/reporter/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"io"
"log/slog"
"os"
"sort"
"slices"
"strings"

"github.com/fatih/color"
Expand All @@ -23,10 +23,8 @@ type ConsoleReporter struct {
}

func (cr ConsoleReporter) Submit(summary Summary) (err error) {
reports := sortReports(summary.Reports())

perFile := map[string][]string{}
for _, report := range reports {
for _, report := range summary.Reports() {
if report.Problem.Severity < cr.minSeverity {
continue
}
Expand Down Expand Up @@ -88,7 +86,7 @@ func (cr ConsoleReporter) Submit(summary Summary) (err error) {
for path := range perFile {
paths = append(paths, path)
}
sort.Strings(paths)
slices.Sort(paths)

for _, path := range paths {
msgs := perFile[path]
Expand Down Expand Up @@ -122,28 +120,3 @@ func countDigits(n int) (c int) {
}
return c
}

func sortReports(reports []Report) []Report {
sort.SliceStable(reports, func(i, j int) bool {
if reports[i].Path.Name < reports[j].Path.Name {
return true
}
if reports[i].Path.Name > reports[j].Path.Name {
return false
}
if reports[i].Problem.Lines.First < reports[j].Problem.Lines.First {
return true
}
if reports[i].Problem.Lines.First > reports[j].Problem.Lines.First {
return false
}
if reports[i].Problem.Reporter < reports[j].Problem.Reporter {
return true
}
if reports[i].Problem.Reporter > reports[j].Problem.Reporter {
return false
}
return reports[i].Problem.Text < reports[j].Problem.Text
})
return reports
}
2 changes: 1 addition & 1 deletion internal/reporter/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type JSONReport struct {
}

func (jr JSONReporter) Submit(summary Summary) (err error) {
reports := sortReports(summary.Reports())
reports := summary.Reports()
out := make([]JSONReport, 0, len(reports))

for _, report := range reports {
Expand Down
26 changes: 11 additions & 15 deletions internal/reporter/reporter.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package reporter

import (
"sort"
"cmp"
"slices"
"time"

"github.com/cloudflare/pint/internal/checks"
Expand Down Expand Up @@ -79,20 +80,15 @@ func (s Summary) hasReport(r Report) bool {
}

func (s *Summary) SortReports() {
sort.SliceStable(s.reports, func(i, j int) bool {
if s.reports[i].Path.SymlinkTarget != s.reports[j].Path.SymlinkTarget {
return s.reports[i].Path.SymlinkTarget < s.reports[j].Path.SymlinkTarget
}
if s.reports[i].Path.Name != s.reports[j].Path.Name {
return s.reports[i].Path.Name < s.reports[j].Path.Name
}
if s.reports[i].Problem.Lines.First != s.reports[j].Problem.Lines.First {
return s.reports[i].Problem.Lines.First < s.reports[j].Problem.Lines.First
}
if s.reports[i].Problem.Reporter != s.reports[j].Problem.Reporter {
return s.reports[i].Problem.Reporter < s.reports[j].Problem.Reporter
}
return s.reports[i].Problem.Text < s.reports[j].Problem.Text
slices.SortFunc(s.reports, func(a, b Report) int {
return cmp.Or(
cmp.Compare(a.Path.Name, b.Path.Name),
cmp.Compare(a.Problem.Lines.First, b.Problem.Lines.First),
cmp.Compare(a.Problem.Lines.Last, b.Problem.Lines.Last),
cmp.Compare(a.Problem.Severity, b.Problem.Severity),
cmp.Compare(a.Problem.Reporter, b.Problem.Reporter),
cmp.Compare(a.Problem.Text, b.Problem.Text),
)
})
}

Expand Down

0 comments on commit b704144

Please sign in to comment.