-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.go
75 lines (66 loc) · 1.43 KB
/
state.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
/*
* Author: Markus Stenberg <[email protected]>
*
* Copyright (c) 2024 Markus Stenberg
*
* Created: Sun Jun 2 19:57:04 2024 mstenber
* Last modified: Tue Jun 4 13:08:37 2024 mstenber
* Edit time: 26 min
*
*/
package main
import (
"fmt"
"sort"
"strings"
"github.com/a-h/templ"
"github.com/fingon/lixie/data"
"golang.org/x/exp/maps"
)
type State struct {
// How often we retry page load
RefreshIntervalMs int
// Current build version
BuildTimestamp string
DB *data.Database
}
type LogSourceSummary struct {
Source string
RuleCount int
Hits int
}
func (self *LogSourceSummary) SearchLink() templ.SafeURL {
search := self.Source
search, _ = strings.CutPrefix(search, "=~")
search, _ = strings.CutPrefix(search, "=")
return templ.URL(fmt.Sprintf("%s?%s=%s",
topLevelLogRule.Path,
globalSearchKey,
search))
}
func (self *State) RuleStats() []*LogSourceSummary {
temp := make(map[string]*LogSourceSummary)
for _, rule := range self.DB.LogRules.Rules {
ss := rule.SourceString()
entry, ok := temp[ss]
if !ok {
entry = &LogSourceSummary{Source: ss}
temp[ss] = entry
}
entry.RuleCount++
entry.Hits += self.DB.RuleCount(rule.ID)
}
stats := maps.Values(temp)
sort.Slice(stats, func(i, j int) bool {
r1 := stats[i]
r2 := stats[j]
if r1.RuleCount < r2.RuleCount {
return false
}
if r1.RuleCount == r2.RuleCount && r1.Hits < r2.Hits {
return false
}
return true
})
return stats
}