forked from oplehto/platform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.go
470 lines (416 loc) · 14.5 KB
/
view.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
package platform
import (
"context"
"encoding/json"
"fmt"
)
// ErrViewNotFound is the error for a missing View.
const ErrViewNotFound = ChronografError("view not found")
// ViewService represents a service for managing View data.
type ViewService interface {
// FindViewByID returns a single View by ID.
FindViewByID(ctx context.Context, id ID) (*View, error)
// FindViews returns a list of Views that match filter and the total count of matching Views.
// Additional options provide pagination & sorting.
FindViews(ctx context.Context, filter ViewFilter) ([]*View, int, error)
// CreateView creates a new View and sets b.ID with the new identifier.
CreateView(ctx context.Context, b *View) error
// UpdateView updates a single View with changeset.
// Returns the new View state after update.
UpdateView(ctx context.Context, id ID, upd ViewUpdate) (*View, error)
// DeleteView removes a View by ID.
DeleteView(ctx context.Context, id ID) error
}
// ViewUpdate is a struct for updating Views.
type ViewUpdate struct {
ViewContentsUpdate
Properties ViewProperties
}
// Valid validates the update struct. It expects minimal values to be set.
func (u ViewUpdate) Valid() error {
_, ok := u.Properties.(EmptyViewProperties)
if u.Name == nil && ok {
return fmt.Errorf("expected at least one attribute to be updated")
}
return nil
}
// ViewContentsUpdate is a struct for updating the non properties content of a View.
type ViewContentsUpdate struct {
Name *string `json:"name"`
}
// ViewFilter represents a set of filter that restrict the returned results.
type ViewFilter struct {
ID *ID
Types []string
}
// View holds positional and visual information for a View.
type View struct {
ViewContents
Properties ViewProperties
}
// ViewContents is the id and name of a specific view.
type ViewContents struct {
ID ID `json:"id,omitempty"`
Name string `json:"name"`
}
// ViewProperties is used to mark other structures as conforming to a View.
type ViewProperties interface {
viewProperties()
GetType() string
}
// EmptyViewProperties is visualization that has no values
type EmptyViewProperties struct{}
func (v EmptyViewProperties) viewProperties() {}
func (v EmptyViewProperties) GetType() string { return "" }
// UnmarshalViewPropertiesJSON unmarshals JSON bytes into a ViewProperties.
func UnmarshalViewPropertiesJSON(b []byte) (ViewProperties, error) {
var v struct {
B json.RawMessage `json:"properties"`
}
if err := json.Unmarshal(b, &v); err != nil {
return nil, err
}
if len(v.B) == 0 {
// Then there wasn't any visualization field, so there's no need unmarshal it
return EmptyViewProperties{}, nil
}
var t struct {
Shape string `json:"shape"`
Type string `json:"type"`
}
if err := json.Unmarshal(v.B, &t); err != nil {
return nil, err
}
var vis ViewProperties
switch t.Shape {
case "chronograf-v2":
switch t.Type {
case "xy":
var xyv XYViewProperties
if err := json.Unmarshal(v.B, &xyv); err != nil {
return nil, err
}
vis = xyv
case "single-stat":
var ssv SingleStatViewProperties
if err := json.Unmarshal(v.B, &ssv); err != nil {
return nil, err
}
vis = ssv
case "gauge":
var gv GaugeViewProperties
if err := json.Unmarshal(v.B, &gv); err != nil {
return nil, err
}
vis = gv
case "table":
var tv TableViewProperties
if err := json.Unmarshal(v.B, &tv); err != nil {
return nil, err
}
vis = tv
case "markdown":
var mv MarkdownViewProperties
if err := json.Unmarshal(v.B, &mv); err != nil {
return nil, err
}
vis = mv
case "log-viewer": // happens in log viewer stays in log viewer.
var lv LogViewProperties
if err := json.Unmarshal(v.B, &lv); err != nil {
return nil, err
}
vis = lv
case "line-plus-single-stat":
var lv LinePlusSingleStatProperties
if err := json.Unmarshal(v.B, &lv); err != nil {
return nil, err
}
vis = lv
}
case "empty":
var ev EmptyViewProperties
if err := json.Unmarshal(v.B, &ev); err != nil {
return nil, err
}
vis = ev
default:
return nil, fmt.Errorf("unknown type %v", t.Shape)
}
return vis, nil
}
// MarshalViewPropertiesJSON encodes a view into JSON bytes.
func MarshalViewPropertiesJSON(v ViewProperties) ([]byte, error) {
var s interface{}
switch vis := v.(type) {
case SingleStatViewProperties:
s = struct {
Shape string `json:"shape"`
SingleStatViewProperties
}{
Shape: "chronograf-v2",
SingleStatViewProperties: vis,
}
case TableViewProperties:
s = struct {
Shape string `json:"shape"`
TableViewProperties
}{
Shape: "chronograf-v2",
TableViewProperties: vis,
}
case GaugeViewProperties:
s = struct {
Shape string `json:"shape"`
GaugeViewProperties
}{
Shape: "chronograf-v2",
GaugeViewProperties: vis,
}
case XYViewProperties:
s = struct {
Shape string `json:"shape"`
XYViewProperties
}{
Shape: "chronograf-v2",
XYViewProperties: vis,
}
case LinePlusSingleStatProperties:
s = struct {
Shape string `json:"shape"`
LinePlusSingleStatProperties
}{
Shape: "chronograf-v2",
LinePlusSingleStatProperties: vis,
}
case MarkdownViewProperties:
s = struct {
Shape string `json:"shape"`
MarkdownViewProperties
}{
Shape: "chronograf-v2",
MarkdownViewProperties: vis,
}
case LogViewProperties:
s = struct {
Shape string `json:"shape"`
LogViewProperties
}{
Shape: "chronograf-v2",
LogViewProperties: vis,
}
default:
s = struct {
Shape string `json:"shape"`
EmptyViewProperties
}{
Shape: "empty",
EmptyViewProperties: EmptyViewProperties{},
}
}
return json.Marshal(s)
}
// MarshalJSON encodes a view to JSON bytes.
func (c View) MarshalJSON() ([]byte, error) {
vis, err := MarshalViewPropertiesJSON(c.Properties)
if err != nil {
return nil, err
}
return json.Marshal(struct {
ViewContents
ViewProperties json.RawMessage `json:"properties"`
}{
ViewContents: c.ViewContents,
ViewProperties: vis,
})
}
// UnmarshalJSON decodes JSON bytes into the corresponding view type (those that implement ViewProperties).
func (c *View) UnmarshalJSON(b []byte) error {
if err := json.Unmarshal(b, &c.ViewContents); err != nil {
return err
}
v, err := UnmarshalViewPropertiesJSON(b)
if err != nil {
return err
}
c.Properties = v
return nil
}
// UnmarshalJSON decodes JSON bytes into the corresponding view update type (those that implement ViewProperties).
func (u *ViewUpdate) UnmarshalJSON(b []byte) error {
if err := json.Unmarshal(b, &u.ViewContentsUpdate); err != nil {
return err
}
v, err := UnmarshalViewPropertiesJSON(b)
if err != nil {
return err
}
u.Properties = v
return nil
}
// MarshalJSON encodes a view to JSON bytes.
func (u ViewUpdate) MarshalJSON() ([]byte, error) {
vis, err := MarshalViewPropertiesJSON(u.Properties)
if err != nil {
return nil, err
}
return json.Marshal(struct {
ViewContentsUpdate
ViewProperties json.RawMessage `json:"properties,omitempty"`
}{
ViewContentsUpdate: u.ViewContentsUpdate,
ViewProperties: vis,
})
}
// LinePlusSingleStatProperties represents options for line plus single stat view in Chronograf
type LinePlusSingleStatProperties struct {
Queries []DashboardQuery `json:"queries"`
Axes map[string]Axis `json:"axes"`
Type string `json:"type"`
Legend Legend `json:"legend"`
ViewColors []ViewColor `json:"colors"`
Prefix string `json:"prefix"`
Suffix string `json:"suffix"`
DecimalPlaces DecimalPlaces `json:"decimalPlaces"`
Note string `json:"note"`
ShowNoteWhenEmpty bool `json:"showNoteWhenEmpty"`
}
// XYViewProperties represents options for line, bar, step, or stacked view in Chronograf
type XYViewProperties struct {
Queries []DashboardQuery `json:"queries"`
Axes map[string]Axis `json:"axes"`
Type string `json:"type"`
Legend Legend `json:"legend"`
Geom string `json:"geom"` // Either "line", "step", "stacked", or "bar"
ViewColors []ViewColor `json:"colors"`
Note string `json:"note"`
ShowNoteWhenEmpty bool `json:"showNoteWhenEmpty"`
}
// SingleStatViewProperties represents options for single stat view in Chronograf
type SingleStatViewProperties struct {
Type string `json:"type"`
Queries []DashboardQuery `json:"queries"`
Prefix string `json:"prefix"`
Suffix string `json:"suffix"`
ViewColors []ViewColor `json:"colors"`
DecimalPlaces DecimalPlaces `json:"decimalPlaces"`
Note string `json:"note"`
ShowNoteWhenEmpty bool `json:"showNoteWhenEmpty"`
}
// GaugeViewProperties represents options for gauge view in Chronograf
type GaugeViewProperties struct {
Type string `json:"type"`
Queries []DashboardQuery `json:"queries"`
Prefix string `json:"prefix"`
Suffix string `json:"suffix"`
ViewColors []ViewColor `json:"colors"`
DecimalPlaces DecimalPlaces `json:"decimalPlaces"`
Note string `json:"note"`
ShowNoteWhenEmpty bool `json:"showNoteWhenEmpty"`
}
// TableViewProperties represents options for table view in Chronograf
type TableViewProperties struct {
Type string `json:"type"`
Queries []DashboardQuery `json:"queries"`
ViewColors []ViewColor `json:"colors"`
TableOptions TableOptions `json:"tableOptions"`
FieldOptions []RenamableField `json:"fieldOptions"`
TimeFormat string `json:"timeFormat"`
DecimalPlaces DecimalPlaces `json:"decimalPlaces"`
Note string `json:"note"`
ShowNoteWhenEmpty bool `json:"showNoteWhenEmpty"`
}
type MarkdownViewProperties struct {
Type string `json:"type"`
Note string `json:"note"`
}
// LogViewProperties represents options for log viewer in Chronograf.
type LogViewProperties struct {
Type string `json:"type"`
Columns []LogViewerColumn `json:"columns"`
}
// LogViewerColumn represents a specific column in a Log Viewer.
type LogViewerColumn struct {
Name string `json:"name"`
Position int32 `json:"position"`
Settings []LogColumnSetting `json:"settings"`
}
// LogColumnSetting represent the settings for a specific column of a Log Viewer.
type LogColumnSetting struct {
Type string `json:"type"`
Value string `json:"value"`
Name string `json:"name,omitempty"`
}
func (XYViewProperties) viewProperties() {}
func (LinePlusSingleStatProperties) viewProperties() {}
func (SingleStatViewProperties) viewProperties() {}
func (GaugeViewProperties) viewProperties() {}
func (TableViewProperties) viewProperties() {}
func (MarkdownViewProperties) viewProperties() {}
func (LogViewProperties) viewProperties() {}
func (v XYViewProperties) GetType() string { return v.Type }
func (v LinePlusSingleStatProperties) GetType() string { return v.Type }
func (v SingleStatViewProperties) GetType() string { return v.Type }
func (v GaugeViewProperties) GetType() string { return v.Type }
func (v TableViewProperties) GetType() string { return v.Type }
func (v MarkdownViewProperties) GetType() string { return v.Type }
func (v LogViewProperties) GetType() string { return v.Type }
/////////////////////////////
// Old Chronograf Types
/////////////////////////////
// DashboardQuery represents a query used in a dashboard cell
type DashboardQuery struct {
Text string `json:"text"`
Type string `json:"type"`
SourceID string `json:"sourceID"`
EditMode string `json:"editMode"` // Either "builder" or "advanced"
Name string `json:"name"` // Term or phrase that refers to the query
BuilderConfig BuilderConfig `json:"builderConfig"`
}
type BuilderConfig struct {
Buckets []string `json:"buckets"`
Measurements []string `json:"measurements"`
Fields []string `json:"fields"`
Functions []string `json:"functions"`
}
// Axis represents the visible extents of a visualization
type Axis struct {
Bounds []string `json:"bounds"` // bounds are an arbitrary list of client-defined strings that specify the viewport for a View
LegacyBounds [2]int64 `json:"-"` // legacy bounds are for testing a migration from an earlier version of axis
Label string `json:"label"` // label is a description of this Axis
Prefix string `json:"prefix"` // Prefix represents a label prefix for formatting axis values
Suffix string `json:"suffix"` // Suffix represents a label suffix for formatting axis values
Base string `json:"base"` // Base represents the radix for formatting axis values
Scale string `json:"scale"` // Scale is the axis formatting scale. Supported: "log", "linear"
}
// ViewColor represents the encoding of data into visualizations
type ViewColor struct {
ID string `json:"id"` // ID is the unique id of the View color
Type string `json:"type"` // Type is how the color is used. Accepted (min,max,threshold)
Hex string `json:"hex"` // Hex is the hex number of the color
Name string `json:"name"` // Name is the user-facing name of the hex color
Value float64 `json:"value"` // Value is the data value mapped to this color
}
// Legend represents the encoding of data into a legend
type Legend struct {
Type string `json:"type,omitempty"`
Orientation string `json:"orientation,omitempty"`
}
// TableOptions is a type of options for a DashboardView with type Table
type TableOptions struct {
VerticalTimeAxis bool `json:"verticalTimeAxis"`
SortBy RenamableField `json:"sortBy"`
Wrapping string `json:"wrapping"`
FixFirstColumn bool `json:"fixFirstColumn"`
}
// RenamableField is a column/row field in a DashboardView of type Table
type RenamableField struct {
InternalName string `json:"internalName"`
DisplayName string `json:"displayName"`
Visible bool `json:"visible"`
}
// DecimalPlaces indicates whether decimal places should be enforced, and how many digits it should show.
type DecimalPlaces struct {
IsEnforced bool `json:"isEnforced"`
Digits int32 `json:"digits"`
}