-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchart.go
59 lines (48 loc) · 1.16 KB
/
chart.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
package coreapi
import (
"encoding/base64"
"encoding/json"
"fmt"
)
// DataItem represents data item
type DataItem struct {
Timestamp float64
Value float64
}
// DataSeries represents data series
type DataSeries struct {
Color string
LineColor string
PointColor string
Data []DataItem
}
type ModuleChart struct {
rf requestFunc
}
func (c ModuleChart) Render(title string, series []DataSeries) ([]byte, error) {
req := struct {
Title string `json:"title"`
Series []DataSeries `json:"series"`
}{
Title: title,
Series: series,
}
payload, errMarshal := json.Marshal(req)
if errMarshal != nil {
return nil, fmt.Errorf("request marshal error, %w", errMarshal)
}
resp, err := c.rf("chart/render", "application/json", payload)
if err != nil {
return nil, fmt.Errorf("failed to call chart/render: %w", err)
}
var s string
errUnmarshal := json.Unmarshal(resp, &s)
if errUnmarshal != nil {
return nil, fmt.Errorf("unmarshal response error, %w", errUnmarshal)
}
img, errDecode := base64.StdEncoding.DecodeString(s)
if errDecode != nil {
return nil, fmt.Errorf("decode response error, %w", errDecode)
}
return img, nil
}