-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
348 lines (285 loc) · 8.39 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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package main
import (
"bufio"
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"math/rand"
"net"
"net/http"
"net/url"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
)
const clearLine = "\033[2K\r"
// flags
var (
// grep parameters
beforeLines = flag.Uint("B", 0, "How many lines of context to return before the found line")
afterLines = flag.Uint("A", 0, "How many lines of context to return after the found line")
contextLines = flag.Uint("C", 0, "How many lines of context to return both before and after the found line")
fixedString = flag.String("F", "", "Fixed string search")
regexString = flag.String("E", "", "Regex string search")
tailF = flag.Bool("tailf", false, "Print incoming logs continiously")
// log filtering parameters
reverse = flag.Bool("reverse", true, "Whether or not to return results in reverse chronological order")
before = flag.String("before", "", "Date and time before which to display results (without milliseconds)")
after = flag.String("after", "", "Date and time after which to display results (without milliseconds)")
// clickhouse parameters
fields = flag.String("fields", "review_body", "Comma-separated list of fields to return in the result in addition to timestamp (possible fields: marketplace,customer_id,review_id,product_id,product_parent,product_title,product_category,star_rating,helpful_votes,total_votes,vine,verified_purchase,review_headline,review_body,review_date)")
textField = flag.String("text-field", "review_body", "The name of the text field that is being matched")
additionalWhere = flag.String("where", "", `Additional filters in WHERE (e.g. "vine='Y' AND star_rating>4")`)
limit = flag.Uint("limit", 0, "Limit the number of results (0 means no limit)")
table = flag.String("table", "amazon", "The name of the table to scan")
chAddr = flag.String("ch-addr", "localhost:8123", "ClickHouse server address (HTTP endpoint)")
debug = flag.Bool("debug", false, "Whether or not debug mode is enabled")
)
// Progress describes ClickHouse query progress result.
type Progress struct {
ReadRows int64 `json:"read_rows,string"`
ReadBytes int64 `json:"read_bytes,string"`
WrittenRows int64 `json:"written_rows,string"`
WrittenBytes int64 `json:"written_bytes,string"`
TotalRowsToRead int64 `json:"total_rows_to_read,string"`
}
// Escape escapes string for MySQL. It should work for ClickHouse as well.
func Escape(txt string) string {
var (
esc string
buf bytes.Buffer
)
last := 0
for ii, bb := range txt {
switch bb {
case 0:
esc = `\0`
case '\n':
esc = `\n`
case '\r':
esc = `\r`
case '\\':
esc = `\\`
case '\'':
esc = `\'`
case '"':
esc = `\"`
case '\032':
esc = `\Z`
default:
continue
}
io.WriteString(&buf, txt[last:ii])
io.WriteString(&buf, esc)
last = ii + 1
}
io.WriteString(&buf, txt[last:])
return buf.String()
}
func makeFilterConds() []string {
var conds []string
conds = append(conds, "1=1")
if *additionalWhere != "" {
conds = append(conds, "("+(*additionalWhere)+")")
}
if *fixedString != "" {
conds = append(conds, `position(`+(*textField)+`, '`+Escape(*fixedString)+`') <> 0`)
}
if *regexString != "" {
conds = append(conds, `match(`+(*textField)+`, '`+Escape(*regexString)+`') = 1`)
}
if *before != "" {
conds = append(conds, `time < toDateTime('`+Escape(*before)+`')`)
}
if *after != "" {
conds = append(conds, `time > toDateTime('`+Escape(*after)+`')`)
}
return conds
}
func printContextResults(date string, millis int, isBefore bool, numLines uint) error {
start := time.Now()
comparison := "<"
desc := " DESC"
if *reverse && isBefore || !isBefore && !*reverse {
comparison = ">"
desc = ""
}
query := fmt.Sprintf(`SELECT time,millis,%s FROM %s
WHERE (time = '%s' AND millis %s %d) OR (time %s '%s')
ORDER BY time%s, millis%s
LIMIT %d
SETTINGS max_threads=1
FORMAT TabSeparatedRaw`,
*fields, *table,
date, comparison, millis, comparison, date,
desc, desc,
numLines)
if *debug {
fmt.Printf("Context query: %s\n", query)
}
u := url.Values{}
u.Set("query", query)
resp, err := http.Get("http://" + (*chAddr) + "/?" + u.Encode())
if err != nil {
return err
}
defer resp.Body.Close()
allLines, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
res := strings.Split(strings.TrimSpace(string(allLines)), "\n")
// reverse the result if context sorting does not match the desired
// sorting
if desc == "" && *reverse || desc != "" && !*reverse {
for i := 0; i < len(res)/2; i++ {
res[i], res[len(res)-i-1] = res[len(res)-i-1], res[i]
}
}
if *debug {
fmt.Printf("(context calculated for %s) ", time.Since(start))
}
_, _, err = printResults(bufio.NewReader(strings.NewReader(strings.Join(res, "\n")+"\n")), false)
return err
}
func runMain() (lastDate string, lastMillis int, err error) {
rand.Seed(time.Now().UnixNano())
desc := " DESC"
if !*reverse {
desc = ""
}
limitPart := ""
if *limit != 0 {
limitPart = fmt.Sprintf("LIMIT %d", *limit)
}
query := `SELECT time,millis,` + (*fields) + `
FROM ` + (*table) + `
WHERE ` + strings.Join(makeFilterConds(), " AND ") + `
ORDER BY time` + desc + `, millis` + desc + `
` + limitPart + `
FORMAT TabSeparatedRaw`
if *debug {
fmt.Printf("Executed query: %s\n", query)
}
u := url.Values{}
u.Set("cancel_http_readonly_queries_on_client_close", "1")
u.Set("send_progress_in_http_headers", "1")
u.Set("query", query)
conn, err := net.Dial("tcp", *chAddr)
if err != nil {
return "", 0, err
}
defer conn.Close()
rd := bufio.NewReader(conn)
wr := bufio.NewWriter(conn)
if _, err := fmt.Fprintf(wr, "GET /?%s HTTP/1.0\n\n", u.Encode()); err != nil {
return "", 0, err
}
if err := wr.Flush(); err != nil {
return "", 0, err
}
start := time.Now()
for {
ln, err := rd.ReadString('\n')
if err != nil {
return "", 0, fmt.Errorf("unexpected error while reading headers: %v", err)
}
ln = strings.TrimSpace(ln)
if ln == "" {
break
}
if strings.HasPrefix(ln, "X-ClickHouse-Progress: ") {
var p Progress
data := strings.TrimPrefix(ln, "X-ClickHouse-Progress: ")
if err := json.Unmarshal([]byte(data), &p); err != nil {
return "", 0, fmt.Errorf("unmarshalling %q: %v", data, err)
}
read := float64(p.ReadBytes) / (1 << 30)
readPerSec := float64(p.ReadBytes) / (float64(time.Since(start)) / float64(time.Second)) / (1 << 30)
fmt.Fprintf(os.Stderr, clearLine+"Progress: %.0f%% (read %.2f GiB so far, %.2f GiB/sec)", float64(p.ReadRows)/float64(p.TotalRowsToRead)*100, read, readPerSec)
}
}
fmt.Fprintf(os.Stderr, clearLine)
return printResults(rd, true)
}
func printResults(rd *bufio.Reader, printContext bool) (lastDate string, lastMillis int, err error) {
for {
ln, err := rd.ReadString('\n')
if err == io.EOF {
return lastDate, lastMillis, nil
} else if err != nil {
return "", 0, err
}
parts := strings.SplitN(ln, "\t", 3)
if len(parts) < 3 {
if _, err := os.Stdout.WriteString(ln); err != nil {
return "", 0, err
}
continue
}
date, millisStr, rest := parts[0], parts[1], parts[2]
millis, _ := strconv.Atoi(millisStr)
lastDate = date
lastMillis = millis
if printContext && (*beforeLines > 0) {
if err := printContextResults(date, millis, true, *beforeLines); err != nil {
return "", 0, err
}
}
fmt.Printf("%s.%03d\t%s\n", date, millis, strings.TrimSpace(strings.ReplaceAll(rest, "\t", " ")))
if printContext && (*afterLines > 0) {
if err := printContextResults(date, millis, false, *afterLines); err != nil {
return "", 0, err
}
}
if printContext && (*beforeLines > 0 || *afterLines > 0) {
fmt.Printf("---\n")
}
}
return lastDate, lastMillis, nil
}
func main() {
flag.Parse()
if *contextLines != 0 {
*beforeLines = *contextLines
*afterLines = *contextLines
}
ch := make(chan os.Signal)
signal.Notify(ch, syscall.SIGPIPE)
go func() {
<-ch
os.Exit(0)
}()
if *tailF {
*reverse = false
if *after == "" {
*after = fmt.Sprint(time.Now().Add(-time.Minute).Unix())
}
for {
lastDate, _, err := runMain()
if err != nil {
if strings.Contains(err.Error(), "broken pipe") {
return
}
log.Fatalf("FATAL error: %v", err)
}
if lastDate != "" {
*after = lastDate
}
time.Sleep(time.Second)
}
}
if _, _, err := runMain(); err != nil {
if strings.Contains(err.Error(), "broken pipe") {
return
}
log.Fatalf("FATAL error: %v", err)
}
}