-
Notifications
You must be signed in to change notification settings - Fork 52
/
main.go
252 lines (221 loc) · 4.99 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"log"
"os"
"time"
"github.com/gdamore/tcell"
"github.com/hokaccha/go-prettyjson"
"github.com/rivo/tview"
"github.com/satyrius/gonx"
)
var (
duration time.Duration
distance int
format string
nginxConfig string
nginxFormat string
showHelp bool
keys []string
app *tview.Application
table *tview.Table
store *Store
)
const (
trendColumn int = iota
countColumn
firstDataColumn
)
func init() {
flag.DurationVar(&duration, "trend", 10*time.Second, "duration of trend")
flag.IntVar(&distance, "distance", 3, "levenshtein distance for combining similar log entities")
flag.StringVar(&format, "format", "json", "stdin format")
flag.StringVar(&nginxConfig, "nginx-config", "/etc/nginx/nginx.conf", "nginx config file")
flag.StringVar(&nginxFormat, "nginx-format", "main", "nginx log_format name")
flag.BoolVar(&showHelp, "help", false, "show help")
}
func main() {
flag.Parse()
keys = flag.Args()
if showHelp {
flag.Usage()
os.Exit(2)
}
store = NewStore(duration, distance, keys)
app = tview.NewApplication()
viewerOpen := false
viewer := tview.NewTextView().
SetDynamicColors(true).
SetScrollable(true)
viewer.SetBorder(true)
table = tview.NewTable().
SetFixed(1, 2).
SetDoneFunc(func(key tcell.Key) {
if key == tcell.KeyEscape {
table.SetSelectable(false, false)
}
if key == tcell.KeyEnter {
table.SetSelectable(true, false)
}
})
renderColumns()
flex := tview.NewFlex()
flex.AddItem(table, 0, 1, true)
app.SetRoot(flex, true)
showRowData := func() {
store.RLock()
row, _ := table.GetSelection()
if row == 0 {
row = 1
}
data := store.Get(row - 1).GetData()
store.RUnlock()
text, err := prettyjson.Marshal(data)
if err != nil {
panic(err)
}
viewer.SetText(tview.TranslateANSI(string(text)))
viewer.ScrollToBeginning()
}
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyDown || event.Key() == tcell.KeyUp {
table.SetSelectable(true, false)
if viewerOpen {
showRowData()
}
}
if event.Key() == tcell.KeyEnter && !viewerOpen {
viewerOpen = true
flex.AddItem(viewer, 0, 1, false)
showRowData()
}
if event.Key() == tcell.KeyEsc && viewerOpen {
viewerOpen = false
flex.RemoveItem(viewer)
}
return event
})
switch format {
case "json":
go read()
case "nginx":
go readNginx()
}
go draw()
go shift(duration)
err := app.Run()
if err != nil {
panic(err)
}
}
func renderColumns() {
headerCell := func(s string) *tview.TableCell {
return tview.NewTableCell(s).
SetBackgroundColor(tcell.ColorRed).
SetTextColor(tcell.ColorBlack).
SetAlign(tview.AlignCenter).
SetSelectable(false)
}
table.SetCell(0, trendColumn, headerCell("trend"))
table.SetCell(0, countColumn, headerCell("count"))
for i, key := range keys {
table.SetCell(0, firstDataColumn+i, headerCell(key))
}
}
func update(value map[string]interface{}) {
if len(keys) == 0 {
keys = mapKeys(value)
store.SetKeys(keys)
renderColumns()
}
store.Lock()
store.Push(value)
store.Unlock()
}
func read() {
dec := json.NewDecoder(os.Stdin)
for dec.More() {
var value map[string]interface{}
err := dec.Decode(&value)
if err != nil {
log.Println(err)
app.Stop()
}
update(value)
}
}
func readNginx() {
config, err := os.Open(nginxConfig)
if err != nil {
panic(err)
}
defer config.Close()
reader, err := gonx.NewNginxReader(os.Stdin, config, nginxFormat)
if err != nil {
panic(err)
}
for {
rec, err := reader.Read()
if err == io.EOF {
break
} else if err != nil {
panic(err)
}
// Process the record... e.g.
fmt.Printf("Parsed entry: %+v\n", rec)
}
}
func readCommon(format string) {
reader := gonx.NewReader(os.Stdin, format)
for {
rec, err := reader.Read()
if err == io.EOF {
break
} else if err != nil {
panic(err)
}
// Process the record... e.g.
fmt.Printf("Parsed entry: %+v\n", rec)
}
}
func shift(duration time.Duration) {
for {
store.Lock()
store.Shift()
store.Unlock()
time.Sleep(duration / trendSize)
}
}
func draw() {
for {
app.QueueUpdateDraw(func() {
store.RLock()
defer store.RUnlock()
row := 1
for ; row < table.GetRowCount(); row++ {
data := store.Get(row - 1)
table.GetCell(row, trendColumn).SetText(Spark(data.GetTrend()))
table.GetCell(row, countColumn).SetText(data.GetCount())
for j := 0; j < len(keys); j++ {
text := fmt.Sprintf("%v", data.Get(keys[j]))
table.GetCell(row, firstDataColumn+j).SetText(text)
}
}
for ; row <= store.Len(); row++ {
data := store.Get(row - 1)
table.SetCell(row, trendColumn, tview.NewTableCell(Spark(data.GetTrend())).
SetSelectable(false))
table.SetCell(row, countColumn, tview.NewTableCell(data.GetCount()).
SetSelectable(false))
for j := 0; j < len(keys); j++ {
text := fmt.Sprintf("%v", data.Get(keys[j]))
table.SetCellSimple(row, firstDataColumn+j, text)
}
}
})
time.Sleep(100 * time.Millisecond)
}
}