This repository has been archived by the owner on Jul 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdoc_test.go
91 lines (77 loc) · 2.26 KB
/
doc_test.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
87
88
89
90
91
package simplejson_test
import (
"context"
"fmt"
"github.com/clambin/simplejson/v6"
"net/http"
"time"
)
func Example() {
r := simplejson.New(map[string]simplejson.Handler{
"A": &handler{},
"B": &handler{table: true},
})
_ = http.ListenAndServe(":8080", r)
}
type handler struct{ table bool }
func (h *handler) Endpoints() simplejson.Endpoints {
return simplejson.Endpoints{
Query: h.Query,
Annotations: h.Annotations,
TagKeys: h.TagKeys,
TagValues: h.TagValues,
}
}
func (h *handler) Query(ctx context.Context, req simplejson.QueryRequest) (simplejson.Response, error) {
if h.table == false {
return h.timeSeriesQuery(ctx, req)
}
return h.tableQuery(ctx, req)
}
func (h *handler) timeSeriesQuery(_ context.Context, _ simplejson.QueryRequest) (simplejson.TimeSeriesResponse, error) {
dataPoints := make([]simplejson.DataPoint, 60)
timestamp := time.Now().Add(-1 * time.Hour)
for i := 0; i < 60; i++ {
dataPoints[i] = simplejson.DataPoint{
Timestamp: timestamp,
Value: float64(i),
}
timestamp = timestamp.Add(1 * time.Minute)
}
return simplejson.TimeSeriesResponse{
DataPoints: dataPoints,
}, nil
}
func (h *handler) tableQuery(_ context.Context, _ simplejson.QueryRequest) (simplejson.TableResponse, error) {
timestamps := make(simplejson.TimeColumn, 60)
seriesA := make(simplejson.NumberColumn, 60)
seriesB := make(simplejson.NumberColumn, 60)
timestamp := time.Now().Add(-1 * time.Hour)
for i := 0; i < 60; i++ {
timestamps[i] = timestamp
seriesA[i] = float64(i)
seriesB[i] = float64(-i)
timestamp = timestamp.Add(1 * time.Minute)
}
return simplejson.TableResponse{Columns: []simplejson.Column{
{Text: "timestamp", Data: timestamps},
{Text: "series A", Data: seriesA},
{Text: "series B", Data: seriesB},
}}, nil
}
func (h *handler) Annotations(_ simplejson.AnnotationRequest) ([]simplejson.Annotation, error) {
return []simplejson.Annotation{{
Time: time.Now().Add(-5 * time.Minute),
Title: "foo",
Text: "bar",
}}, nil
}
func (h *handler) TagKeys(_ context.Context) []string {
return []string{"some-key"}
}
func (h *handler) TagValues(_ context.Context, key string) ([]string, error) {
if key != "some-key" {
return nil, fmt.Errorf("invalid key: %s", key)
}
return []string{"A", "B", "C"}, nil
}