-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_rule_summary.go
104 lines (90 loc) · 1.92 KB
/
log_rule_summary.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* Author: Markus Stenberg <[email protected]>
*
* Copyright (c) 2024 Markus Stenberg
*
* Created: Fri Jun 7 16:43:58 2024 mstenber
* Last modified: Fri Jun 7 17:36:17 2024 mstenber
* Edit time: 27 min
*
*/
package main
import (
"fmt"
"sort"
"github.com/a-h/templ"
)
type rsSortHeader struct {
Title string
CompareLess func(s1, s2 *LogSourceSummary) bool
id int
}
func (self *rsSortHeader) ID() int {
if self.id == 0 {
// Initialize our link dynamically. Not super efficient but it does not really matter.
for i, e := range rsSortRules {
if e == self {
self.id = i + 1
return self.id
}
}
}
return self.id
}
func (self *rsSortHeader) ActionLink(base string, st int) templ.SafeURL {
id := self.ID()
if st == id {
id = -id
}
return templ.URL(fmt.Sprintf("%s?rss=%d", base, id))
}
var rsSource = &rsSortHeader{
Title: "Source",
CompareLess: func(s1, s2 *LogSourceSummary) bool {
return s1.Source < s2.Source
},
}
var rsRules = &rsSortHeader{
Title: "Rules",
CompareLess: func(s1, s2 *LogSourceSummary) bool {
return s1.RuleCount < s2.RuleCount
},
}
var rsHits = &rsSortHeader{
Title: "Hits",
CompareLess: func(s1, s2 *LogSourceSummary) bool {
return s1.Hits < s2.Hits
},
}
var rsHitsPerRule = &rsSortHeader{
Title: "Hits per rule",
CompareLess: func(s1, s2 *LogSourceSummary) bool {
hpr1 := s1.Hits / s1.RuleCount
hpr2 := s2.Hits / s2.RuleCount
return hpr1 < hpr2
},
}
var rsSortRules = []*rsSortHeader{rsSource, rsRules, rsHits, rsHitsPerRule}
func rsSortedRules(sli []*LogSourceSummary, order int) []*LogSourceSummary {
var sh *rsSortHeader
var asc bool
for _, rule := range rsSortRules {
rid := rule.ID()
if order == rid {
sh = rule
asc = false
break
}
if order == -rid {
sh = rule
asc = true
break
}
}
if sh != nil {
sort.Slice(sli, func(i, j int) bool {
return sh.CompareLess(sli[i], sli[j]) == asc
})
}
return sli
}