-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot.go
81 lines (70 loc) · 2.12 KB
/
plot.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
package lrq
import (
"context"
"encoding/json"
"errors"
"time"
)
type PlotRequestAttribs struct {
StartTime *time.Time
EndTime *time.Time
Filter *string
Slices *uint
SliceWidth *string
BreakdownFacet *string
}
type plotRequest struct {
QueryType string `json:"queryType"`
StartTime *int64 `json:"startTime,omitempty"`
EndTime *int64 `json:"endTime,omitempty"`
Plot plotRequestOpts `json:"plot"`
}
type plotRequestOpts struct {
Filter *string `json:"filter,omitempty"`
Slices *uint `json:"slices,omitempty"`
SliceWidth *string `json:"sliceWidth,omitempty"`
Expression *string `json:"expression"`
BreakdownFacet *string `json:"breakdownFacet,omitempty"`
Frequency *string `json:"frequency"`
}
type PlotResponseData struct {
XAxis []int64 `json:"xAxis"`
Plots []struct {
Label *string `json:"label"`
Values []float64 `json:"samples"`
} `json:"plots"`
}
func (c *Client) DoPlotRequest(ctx context.Context, expression string, attribs PlotRequestAttribs) (*PlotResponseData, error) {
// At least and at most one of Slices or SliceWidth must be defined
if attribs.Slices == nil && attribs.SliceWidth == nil {
return nil, errors.New("either Slices or SliceWidth must be defined")
} else if attribs.Slices != nil && attribs.SliceWidth != nil {
return nil, errors.New("both Slices and SliceWidth cannot be defined")
}
reqBody := plotRequest{
QueryType: "PLOT",
StartTime: timeToInt64(attribs.StartTime),
EndTime: timeToInt64(attribs.EndTime),
Plot: plotRequestOpts{
Filter: attribs.Filter,
Slices: attribs.Slices,
SliceWidth: attribs.SliceWidth,
Expression: &expression,
BreakdownFacet: attribs.BreakdownFacet,
Frequency: func() *string { s := "LOW"; return &s }(),
},
}
reqBytes, err := json.Marshal(reqBody)
if err != nil {
return nil, err
}
respBytes, err := c.doRequest(ctx, reqBytes)
if err != nil {
return nil, err
}
var respBody PlotResponseData
if err := json.Unmarshal(respBytes, &respBody); err != nil {
return nil, err
}
return &respBody, nil
}