-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_rule.go
216 lines (194 loc) · 4.94 KB
/
log_rule.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* Author: Markus Stenberg <[email protected]>
*
* Copyright (c) 2024 Markus Stenberg
*
*/
package main
import (
"fmt"
"net/http"
"slices"
"strconv"
"github.com/a-h/templ"
"github.com/fingon/lixie/cm"
"github.com/fingon/lixie/data"
"github.com/sourcegraph/conc/iter"
)
// top-level form fields
const idKey = "rid"
const (
commentKey = "com"
disabledKey = "d"
hamKey = "h"
versionKey = "ver"
)
// per-matcher fields
const (
deleteField = "del"
fieldField = "f"
opField = "o"
valueField = "v"
)
// actions
const (
actionAdd = "a"
actionSave = "s"
)
func NewLogRuleFromForm(r cm.FormValued) (result *data.LogRule, err error) {
rule := data.LogRule{}
if _, err = cm.IntFromForm(r, idKey, &rule.ID); err != nil {
return
}
if _, err = cm.IntFromForm(r, versionKey, &rule.Version); err != nil {
return
}
cm.BoolFromForm(r, disabledKey, &rule.Disabled)
cm.BoolFromForm(r, hamKey, &rule.Ham)
rule.Comment = r.FormValue(commentKey)
// Read the matcher fields
i := 0
del := -1
for {
// Keep track of what to delete here too
if r.FormValue(fieldID(i, deleteField)) != "" {
del = i
}
field := r.FormValue(fieldID(i, fieldField))
op := r.FormValue(fieldID(i, opField))
value := r.FormValue(fieldID(i, valueField))
if field == "" && op == "" && value == "" {
break
}
matcher := data.LogFieldMatcher{Field: field, Op: op, Value: value}
rule.Matchers = append(rule.Matchers, matcher)
i++
}
// Handle the mutation actions
if del >= 0 {
rule.Matchers = slices.Delete(rule.Matchers, del, del+1)
}
if r.FormValue(actionAdd) != "" {
rule.Matchers = append(rule.Matchers, data.LogFieldMatcher{Op: "="})
}
// Save is dealt with externally
result = &rule
return
}
func findMatchingLogs(db *data.Database, rule *data.LogRule) *LogListModel {
m := LogListModel{
Config: LogListConfig{Filter: data.LogVerdictUnknown},
DB: db,
LogRules: []*data.LogRule{rule},
DisableActions: true,
DisablePagination: true,
EnableAccurateCounting: true,
Limit: 5,
}
_ = m.Filter()
return &m
}
func findMatchingOtherRules(db *data.Database, logs []*data.Log, skipRule *data.LogRule) *LogRuleListModel {
// We could also check for strict supersets (but regexp+regexp
// matching is tricky). So we just show rules that out of the
// box seem to overlap as they match the same rules.
lrules := db.LogRules
rules := iter.Map(lrules.Rules, func(rulep **data.LogRule) *data.LogRule {
rule := *rulep
if rule == skipRule {
return nil
}
for _, log := range logs {
if data.LogMatchesRule(log, rule) {
return rule
}
}
return nil
})
rules = FilterSparse(rules, IsNotNil)
if len(rules) > 0 {
return &LogRuleListModel{LogRules: rules}
}
return nil
}
func logRuleEditHandler(st State) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
db := st.DB
rule, err := NewLogRuleFromForm(r)
if err != nil {
// TODO log error?
return
}
if r.FormValue(actionSave) != "" {
err := db.AddOrUpdate(*rule)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
http.Redirect(w, r, topLevelLogRule.Path, http.StatusSeeOther)
return
}
logs := findMatchingLogs(db, rule)
rules := findMatchingOtherRules(db, logs.Logs, rule)
err = LogRuleEdit(st, *rule, rules, logs).Render(r.Context(), w)
if err != nil {
http.Error(w, err.Error(), 500)
}
})
}
func logRuleDeleteSpecificHandler(st State) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ridString := r.PathValue("id")
rid, err := strconv.Atoi(ridString)
if err != nil {
// TODO handle error
return
}
if err = st.DB.Delete(rid); err != nil {
http.NotFound(w, r)
return
}
http.Redirect(w, r, topLevelLogRule.Path, http.StatusSeeOther)
})
}
func logRuleEditSpecificHandler(st State) http.Handler {
db := st.DB
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ridString := r.PathValue("id")
rid, err := strconv.Atoi(ridString)
if err != nil {
// TODO handle error
return
}
for _, rule := range db.LogRules.Rules {
if rule.ID == rid {
logs := findMatchingLogs(db, rule)
rules := findMatchingOtherRules(db, logs.Logs, rule)
err = LogRuleEdit(st, *rule, rules, logs).Render(r.Context(), w)
if err != nil {
http.Error(w, err.Error(), 500)
}
return
}
}
http.NotFound(w, r)
})
}
func fieldID(id int, suffix string) string {
return fmt.Sprintf("row-%d-%s", id, suffix)
}
func ruleTitle(rule data.LogRule) string {
if rule.ID > 0 {
return fmt.Sprintf("Log rule editor - editing #%d", rule.ID)
}
return "Log rule creator"
}
func ruleIDString(rule data.LogRule) string {
return strconv.Itoa(rule.ID)
}
func ruleLinkString(id int, op string) string {
return fmt.Sprintf("%s/%d/%s", topLevelLogRule.Path, id, op)
}
func ruleLink(id int, op string) templ.SafeURL {
return templ.URL(ruleLinkString(id, op))
}