forked from influxdata/chronograf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayout.go
119 lines (98 loc) · 2.68 KB
/
layout.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package server
import (
"fmt"
"net/http"
"github.com/bouk/httprouter"
"github.com/influxdata/chronograf"
)
type link struct {
Href string `json:"href"`
Rel string `json:"rel"`
}
type layoutResponse struct {
chronograf.Layout
Link link `json:"link"`
}
func newLayoutResponse(layout chronograf.Layout) layoutResponse {
httpAPILayouts := "/chronograf/v1/layouts"
href := fmt.Sprintf("%s/%s", httpAPILayouts, layout.ID)
rel := "self"
for idx, cell := range layout.Cells {
axes := []string{"x", "y", "y2"}
if cell.Axes == nil {
layout.Cells[idx].Axes = make(map[string]chronograf.Axis, len(axes))
}
if cell.CellColors == nil {
layout.Cells[idx].CellColors = []chronograf.CellColor{}
}
for _, axis := range axes {
if _, found := cell.Axes[axis]; !found {
layout.Cells[idx].Axes[axis] = chronograf.Axis{
Bounds: []string{},
}
}
}
}
return layoutResponse{
Layout: layout,
Link: link{
Href: href,
Rel: rel,
},
}
}
type getLayoutsResponse struct {
Layouts []layoutResponse `json:"layouts"`
}
// Layouts retrieves all layouts from store
func (s *Service) Layouts(w http.ResponseWriter, r *http.Request) {
// Construct a filter sieve for both applications and measurements
filtered := map[string]bool{}
for _, a := range r.URL.Query()["app"] {
filtered[a] = true
}
for _, m := range r.URL.Query()["measurement"] {
filtered[m] = true
}
ctx := r.Context()
layouts, err := s.Store.Layouts(ctx).All(ctx)
if err != nil {
Error(w, http.StatusInternalServerError, "Error loading layouts", s.Logger)
return
}
filter := func(layout *chronograf.Layout) bool {
// If the length of the filter is zero then all values are acceptable.
if len(filtered) == 0 {
return true
}
// If filter contains either measurement or application
return filtered[layout.Measurement] || filtered[layout.Application]
}
res := getLayoutsResponse{
Layouts: []layoutResponse{},
}
seen := make(map[string]bool)
for _, layout := range layouts {
// remove duplicates
if seen[layout.Measurement+layout.ID] {
continue
}
// filter for data that belongs to provided application or measurement
if filter(&layout) {
res.Layouts = append(res.Layouts, newLayoutResponse(layout))
}
}
encodeJSON(w, http.StatusOK, res, s.Logger)
}
// LayoutsID retrieves layout with ID from store
func (s *Service) LayoutsID(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
id := httprouter.GetParamFromContext(ctx, "id")
layout, err := s.Store.Layouts(ctx).Get(ctx, id)
if err != nil {
Error(w, http.StatusNotFound, fmt.Sprintf("ID %s not found", id), s.Logger)
return
}
res := newLayoutResponse(layout)
encodeJSON(w, http.StatusOK, res, s.Logger)
}