forked from astaxie/bat
/
influx.go
65 lines (55 loc) · 1.32 KB
/
influx.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
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
)
func createTableWriter() table.Writer {
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
style := table.StyleDefault
style.Format.Header = text.FormatDefault
t.SetStyle(style)
return t
}
type Series struct {
Name string `json:"name"`
Columns []string `json:"columns"`
Values [][]interface{} `json:"values"`
}
type InfluxQueryResult struct {
Results []struct {
Series []Series `json:"series"`
StatementID int `json:"statement_id"`
} `json:"results"`
}
func influxTablePrint(ugly bool, influxDB bool, dat []byte) bool {
if ugly || !influxDB {
return false
}
var qr InfluxQueryResult
_ = json.Unmarshal(dat, &qr)
if len(qr.Results) > 0 && len(qr.Results[0].Series) > 0 {
for _, series := range qr.Results[0].Series {
influxSeriesPrint(series)
}
return true
}
return false
}
func influxSeriesPrint(series Series) {
fmt.Printf("%s:\n", series.Name)
header := make([]any, 1+len(series.Columns))
header[0] = "#"
for i, h := range series.Columns {
header[i+1] = h
}
tw := createTableWriter()
tw.AppendHeader(header)
for i, cells := range series.Values {
tw.AppendRow(append([]any{fmt.Sprintf("%d", i+1)}, cells...))
}
tw.Render()
}