forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult.go
78 lines (70 loc) · 1.62 KB
/
result.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
package models
import (
"encoding/json"
"errors"
"time"
)
type Result struct {
Series Rows `json:"series"`
Err error `json:"error,omitempty"`
}
func (r Result) String() string {
b, _ := json.Marshal(r)
return string(b)
}
func (r *Result) UnmarshalJSON(data []byte) error {
var o struct {
Series Rows `json:"series"`
Err string `json:"error"`
}
if err := json.Unmarshal(data, &o); err != nil {
return err
}
r.Series = o.Series
if o.Err != "" {
r.Err = errors.New(o.Err)
}
return nil
}
// Rows represents a collection of rows. Rows implements sort.Interface.
type Rows []*Row
// Row represents a single row returned from the execution of a statement.
type Row struct {
Name string `json:"name,omitempty"`
Tags map[string]string `json:"tags,omitempty"`
Columns []string `json:"columns,omitempty"`
Values [][]interface{} `json:"values,omitempty"`
}
func (r *Row) UnmarshalJSON(data []byte) error {
var o struct {
Name string `json:"name,omitempty"`
Tags map[string]string `json:"tags,omitempty"`
Columns []string `json:"columns,omitempty"`
Values [][]interface{} `json:"values,omitempty"`
}
if err := json.Unmarshal(data, &o); err != nil {
return err
}
r.Name = o.Name
r.Tags = o.Tags
r.Columns = o.Columns
r.Values = o.Values
// Parse all time columns
for i, v := range r.Values {
for j, c := range r.Columns {
if c == "time" {
tStr, ok := v[j].(string)
if !ok {
continue
}
t, err := time.Parse(time.RFC3339, tStr)
if err != nil {
continue
}
r.Values[i][j] = t
break
}
}
}
return nil
}