forked from grafana/xk6-loki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.go
86 lines (75 loc) · 1.59 KB
/
query.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
package loki
import (
"fmt"
"net/url"
"strconv"
"time"
)
type QueryType int
const (
InstantQuery QueryType = iota
RangeQuery
LabelsQuery
LabelValuesQuery
SeriesQuery
)
func (t QueryType) Endpoint() string {
switch t {
case InstantQuery:
return "/loki/api/v1/query"
case RangeQuery:
return "/loki/api/v1/query_range"
case LabelsQuery:
return "/loki/api/v1/labels"
case LabelValuesQuery:
return "/loki/api/v1/label/%s/values"
case SeriesQuery:
return "/loki/api/v1/series"
default:
return ""
}
}
// Query contains all necessary fields to execute instant and range queries and print the results.
type Query struct {
Type QueryType
QueryString string
Start time.Time
End time.Time
Limit int
PathParams []interface{}
}
func (q *Query) Endpoint() string {
return fmt.Sprintf(q.Type.Endpoint(), q.PathParams...)
}
func (q *Query) Values() url.Values {
v := url.Values{}
if q.QueryString != "" {
if q.Type == RangeQuery || q.Type == InstantQuery {
v.Set("query", q.QueryString)
}
if q.Type == SeriesQuery {
v.Set("match[]", q.QueryString)
}
}
if q.Type == InstantQuery {
if q.End.Unix() > 0 {
v.Set("time", strconv.FormatInt(q.End.UnixNano(), 10))
}
} else {
if q.Start.Unix() > 0 {
v.Set("start", strconv.FormatInt(q.Start.UnixNano(), 10))
}
if q.End.Unix() > 0 {
v.Set("end", strconv.FormatInt(q.End.UnixNano(), 10))
}
}
if q.Limit > 0 {
v.Set("limit", strconv.Itoa(q.Limit))
}
return v
}
// SetInstant makes the Query an instant type
func (q *Query) SetInstant(time time.Time) {
q.Start = time
q.End = time
}