-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_list.go
204 lines (169 loc) · 4.18 KB
/
log_list.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
/*
* Author: Markus Stenberg <[email protected]>
*
* Copyright (c) 2024 Markus Stenberg
*
*/
package main
import (
"net/http"
"strconv"
"github.com/fingon/lixie/cm"
"github.com/fingon/lixie/data"
)
// This struct represents external configuration - what we can get as query/form parameters
type LogListConfig struct {
// Global configuration (cookie)
Global GlobalConfig `json:"-"`
// Local state (cookie)
AutoRefresh bool `json:"ar" cm:"auto-refresh"`
Filter int `json:"f" cm:"filter"`
// These are only handled via links
BeforeHash uint64
Expand uint64
}
// These must match ^ cm tags
const (
llAutoRefreshKey = "auto-refresh"
llFilterKey = "filter"
)
// These are handled only in templates
const (
expandKey = "expand"
beforeKey = "before"
)
func (self *LogListConfig) Init(s cm.CookieSource, wr *cm.URLWrapper, w http.ResponseWriter) error {
// Global config
err := cm.RunWrapper(s, wr, w, &self.Global)
if err != nil {
return err
}
// Local config
err = cm.RunWrapper(s, wr, w, self)
if err != nil {
return err
}
// Link-based state
_, err = cm.Uint64FromForm(wr, expandKey, &self.Expand)
if err != nil {
return err
}
_, err = cm.Uint64FromForm(wr, beforeKey, &self.BeforeHash)
if err != nil {
return err
}
// before is omitted intentionally
return nil
}
func (self LogListConfig) WithBeforeHash(v uint64) LogListConfig {
self.BeforeHash = v
return self
}
func (self LogListConfig) WithExpand(v uint64) LogListConfig {
self.Expand = v
return self
}
func (self LogListConfig) Query() *QueryWrapper {
base := topLevelLog.Path + "/"
q := QueryWrapper{Base: base}
// Cookie-based stuff is handled in Init
// Link-based things start here
if self.BeforeHash != 0 {
q.Add(beforeKey, strconv.FormatUint(self.BeforeHash, 10))
}
if self.Expand != 0 {
q.Add(expandKey, strconv.FormatUint(self.Expand, 10))
}
return &q
}
type LogListModel struct {
Config LogListConfig
DB *data.Database
// This is subset of the database actually shown to the user
Logs []*data.Log
// This can be used if specifying custom set of rules, it defaults to DB.LogRules
LogRules []*data.LogRule
// Filtering criteria
ExcludeVerdict int
// Actions
DisableActions bool
DisablePagination bool
// Is this post request (.. sigh ..)
Post bool
// Convenience results from filter()
EnableAccurateCounting bool
FilteredCount int
TotalCount int
// For filtering
Limit int
}
func (self *LogListModel) LogToRule(log *data.Log) *data.LogRule {
if self.LogRules != nil {
return data.LogToRule(log, self.LogRules)
}
return log.ToRule(&self.DB.LogRules)
}
func (self *LogListModel) LogVerdict(log *data.Log) int {
rule := self.LogToRule(log)
return data.LogRuleToVerdict(rule)
}
func (self *LogListModel) Filter() error {
// Some spare capacity but who really cares
logs := make([]*data.Log, 0, self.Limit)
active := self.Config.BeforeHash == 0
count := 0
allLogs, err := self.DB.Logs()
if err != nil {
return err
}
allLogs = filterFTS(allLogs, self.Config.Global.Search, len(allLogs))
self.TotalCount = len(allLogs)
for _, log := range allLogs {
if !active {
if log.Hash() == self.Config.BeforeHash {
active = true
}
if self.EnableAccurateCounting && self.LogVerdict(log) != self.Config.Filter {
count++
}
continue
}
if self.LogVerdict(log) == self.Config.Filter {
continue
}
count++
if len(logs) < self.Limit {
logs = append(logs, log)
} else if !self.EnableAccurateCounting {
break
}
}
self.Logs = logs
self.FilteredCount = count
return nil
}
func logListHandler(st State) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
config := LogListConfig{Filter: data.LogVerdictSpam}
wr, err := cm.GetWrapper(r)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
err = config.Init(r, wr, w)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
model := LogListModel{Config: config, DB: st.DB, Limit: 20, Post: r.Method == "POST"}
err = model.Filter()
if err != nil {
http.Error(w, err.Error(), 400)
return
}
err = LogList(st, model).Render(r.Context(), w)
if err != nil {
http.Error(w, err.Error(), 400)
}
})
}