-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
334 lines (302 loc) · 7.69 KB
/
model.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package main
import (
"encoding/json"
"fmt"
"log/slog"
"os"
"strings"
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/tidwall/gjson"
"github.com/torarvid/gloglog/config"
"github.com/torarvid/gloglog/schema"
"github.com/torarvid/gloglog/search"
"github.com/torarvid/gloglog/table"
)
var baseStyle = lipgloss.NewStyle().
BorderStyle(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("240"))
const (
stateTable = iota
stateZoomRow
stateSchema
stateSearch
)
type model struct {
table table.Model[string]
schema schema.Model
rows []string
filteredRows []string
view config.LogView
filters []RowFilter
search search.Model
state int
termWidth int
termHeight int
}
func newModel(logView config.LogView) *model {
modelInitTime := time.Now()
rows := logView.GetRows()
scanTime := time.Since(modelInitTime)
fmt.Fprintf(os.Stderr, " done in %d ms. %d rows found.\n", scanTime.Milliseconds(), len(rows))
t := table.New(
table.WithFocused[string](true),
table.WithHeight[string](27),
)
s := table.DefaultStyles()
s.Header = s.Header.
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("240")).
BorderBottom(true).
Bold(false)
s.Selected = s.Selected.
Foreground(lipgloss.Color("229")).
Background(lipgloss.Color("27")).
Bold(false)
t.SetStyles(s)
m := &model{
table: t,
state: stateTable,
rows: rows,
view: logView,
filters: make([]RowFilter, 0),
schema: schema.FromLogView(logView, 1, 1),
search: search.FromLogView(logView, 40, 15),
}
m.updateColumns(logView.Attrs)
m.table.SetRows(m.rows)
return m
}
type RowFilter func(string) bool
func (rf RowFilter) Filter(s string) bool {
return rf(s)
}
func (m model) Init() tea.Cmd { return nil }
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cmds := make([]tea.Cmd, 0)
var cmd tea.Cmd
switch m.state {
case stateTable:
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case " ":
m.state = stateZoomRow
case "s":
m.state = stateSchema
case "/":
m.state = stateSearch
case "q", "ctrl+c":
return m, tea.Quit
}
}
m.table, cmd = m.table.Update(msg)
cmds = append(cmds, cmd)
case stateZoomRow:
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case " ", "esc":
m.state = stateTable
}
}
case stateSchema:
switch msg := msg.(type) {
case schema.Close:
m.state = stateTable
return m, nil
case schema.UpdatedSchemaMsg:
m.updateColumns(msg.Attributes)
}
m.schema, cmd = m.schema.Update(msg)
cmds = append(cmds, cmd)
case stateSearch:
switch msg := msg.(type) {
case search.Close:
m.state = stateTable
return m, nil
case search.UpdatedFiltersMsg:
m.SetFilters(msg.Filters)
}
m.search, cmd = m.search.Update(msg)
cmds = append(cmds, cmd)
}
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c":
return m, tea.Quit
}
case tea.WindowSizeMsg:
m.table, cmd = m.table.Update(msg)
cmds = append(cmds, cmd)
m.schema, cmd = m.schema.Update(msg)
cmds = append(cmds, cmd)
m.termWidth, m.termHeight = msg.Width, msg.Height
}
return m, tea.Batch(cmds...)
}
func (m *model) updateColumns(attrs []config.Attribute) {
columns := make([]table.ColumnSpec[string], len(attrs))
for i, c := range attrs {
columns[i] = ColumnFromConfig(c)
}
m.table.SetColumns(columns)
view := config.TheConfig.GetActiveView()
view.Attrs = attrs
config.TheConfig.Save()
}
func (m model) View() string {
if !firstDraw {
timeToFirstDraw := time.Since(appStartTime)
slog.Info("First draw happened", "delay", timeToFirstDraw)
firstDraw = true
}
switch m.state {
case stateTable:
return baseStyle.Render(m.table.View())
case stateZoomRow:
rawRow := m.table.SelectedRow()
var row map[string]interface{}
err := json.Unmarshal([]byte(rawRow), &row)
if err != nil {
return "Invalid"
}
rendered, err := json.MarshalIndent(row, "", " ")
if err != nil {
return "Invalid"
}
return baseStyle.Width(m.termWidth - 2).Render(string(rendered))
case stateSchema:
return baseStyle.Render(m.schema.View())
case stateSearch:
return baseStyle.Render(m.search.View())
default:
panic("Unknown state")
}
}
func (m *model) SetFilters(filters []config.Filter) {
rowFilters := make([]RowFilter, len(filters))
for i, filter := range filters {
filter := filter
rowFilter := func(row string) bool {
return strings.Contains(row, filter.Term)
}
rowFilters[i] = rowFilter
}
m.filters = rowFilters
m.updateFilteredRows()
m.table.SetRows(m.filteredRows)
}
func (m *model) updateFilteredRows() {
m.filteredRows = make([]string, 0, len(m.rows)/10)
for _, row := range m.rows {
include := true
for _, filter := range m.filters {
if !filter(row) {
include = false
break
}
}
if include {
m.filteredRows = append(m.filteredRows, row)
}
}
}
type Column struct {
title string
width int
valueGetter func(string) string
}
func ColumnFromConfig(c config.Attribute) *Column {
return &Column{
title: c.Name,
width: c.Width,
valueGetter: valueGetterFromSelectors(c.Selectors, c.Type, c.Format),
}
}
func (c *Column) Title() string {
return c.title
}
func (c *Column) Width() int {
return c.width
}
func (c *Column) SetWidth(width int) {
c.width = width
}
func (c *Column) GetValue(s string) string {
return c.valueGetter(s)
}
func identity(s string) string {
return s
}
// valueGetterFromSelectors returns a function that can be used to get column values.
//
// For now it is useful for getting values from JSON data (including nested JSON data).
//
// Example:
//
// valueGetterFromSelectors([]string{"json(data.barcode)"}, "", nil)
//
// The call above will return a function that can be called for each log file entry to retrieve
// barcode information. It uses gjson to extract the value from JSON looking like
// {"data": {"barcode": "1234", "whatever": 111}, "other_data": []}.
//
// If the extracted data is of a formattable type (e.g. time), the typ+format parameters can be
// used to format the value. For now, only time is supported.
//
// In cases where you have nested data (say {"data": "{\"barcode\": \"1234\"}"}), you can use the
// | character to pipe the result of one parsed json into another. For example:
//
// valueGetterFromSelectors([]string{"json(data)|json(barcode)"}, "", nil)
//
// In cases where your log source has imperfectly structured data, you can use the fact that the
// 'selectors' parameter is a slice. This means that you can provide multiple selectors and the
// first one to yield a non-empty result is returned.
func valueGetterFromSelectors(selectors []string, typ string, format *string) func(string) string {
getters := make([]func(string) string, len(selectors))
for i, selector := range selectors {
if selector == "." {
getters[i] = identity
continue
}
partialSelectors := strings.Split(selector, "|")
getters[i] = func(s string) string {
for _, sel := range partialSelectors {
sel = strings.TrimSpace(sel)
if strings.HasPrefix(sel, "json(") {
// set jsonPath to whatever is inside the parentheses of
// 'json(...)'
jsonPath := sel[5 : len(sel)-1]
if jsonPath == "." {
continue
}
s = gjson.Get(s, jsonPath).String()
}
}
switch typ {
case "time":
t, err := time.Parse(time.RFC3339, s)
if err != nil {
return "Invalid"
}
if format == nil {
return t.Format(time.StampMilli)
}
return t.Format(*format)
default:
return s
}
}
}
return func(input string) (out string) {
for _, getter := range getters {
out = getter(input)
if out != "" {
break
}
}
return out
}
}