-
Notifications
You must be signed in to change notification settings - Fork 57
/
frame_meta.go
219 lines (176 loc) · 7.03 KB
/
frame_meta.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
package data
import (
"encoding/json"
"fmt"
)
// FrameMeta matches:
// https://github.com/grafana/grafana/blob/master/packages/grafana-data/src/types/data.ts#L11
// NOTE -- in javascript this can accept any `[key: string]: any;` however
// this interface only exposes the values we want to be exposed
//
//swagger:model
type FrameMeta struct {
// Type asserts that the frame matches a known type structure
Type FrameType `json:"type,omitempty"`
// Path is a browsable path on the datasource.
Path string `json:"path,omitempty"`
// PathSeparator defines the separator pattern to decode a hierarchy. The default separator is '/'.
PathSeparator string `json:"pathSeparator,omitempty"`
// Custom datasource specific values.
Custom interface{} `json:"custom,omitempty"`
// Stats is an array of query result statistics.
Stats []QueryStat `json:"stats,omitempty"`
// Notices provide additional information about the data in the Frame that
// Grafana can display to the user in the user interface.
Notices []Notice `json:"notices,omitempty"`
// Channel is the path to a stream in grafana live that has real-time updates for this data.
Channel string `json:"channel,omitempty"`
// PreferredVisualization is currently used to show results in Explore only in preferred visualisation option.
PreferredVisualization VisType `json:"preferredVisualisationType,omitempty"`
// ExecutedQueryString is the raw query sent to the underlying system. All macros and templating
// have been applied. When metadata contains this value, it will be shown in the query inspector.
ExecutedQueryString string `json:"executedQueryString,omitempty"`
// Optionally identify which topic the frame should be assigned to.
// A value specified in the response will override what the request asked for.
DataTopic DataTopic `json:"dataTopic,omitempty"`
}
// Should be kept in sync with grafana/packages/grafana-data/src/types/data.ts#PreferredVisualisationType
const (
// VisTypeGraph indicates the response should be visualized using a graph.
VisTypeGraph VisType = "graph"
// VisTypeTable indicates the response should be visualized using a table.
VisTypeTable = "table"
// VisTypeLogs indicates the response should be visualized using a logs visualization.
VisTypeLogs = "logs"
// VisTypeTrace indicates the response should be visualized using a trace view visualization.
VisTypeTrace = "trace"
// VisTypeNodeGraph indicates the response should be visualized using a node graph visualization.
VisTypeNodeGraph = "nodeGraph"
// VisTypeFlameGraph indicates the response should be visualized using a flame graph visualization.
VisTypeFlameGraph = "flamegraph"
)
// VisType is used to indicate how the data should be visualized in explore.
type VisType string
const (
// DataTopicAnnotations is used to specify that the frame should be used as annotation of the actual data frame response.
// Example: When DataTopic is set to DataTopicAnnotations, the frame will be used as exemplar data in timeseries panel
DataTopicAnnotations DataTopic = "annotations"
)
// DataTopic is used to identify which topic the frame should be assigned to.
type DataTopic string
// FrameMetaFromJSON creates a QueryResultMeta from a json string
func FrameMetaFromJSON(jsonStr string) (*FrameMeta, error) {
var m FrameMeta
err := json.Unmarshal([]byte(jsonStr), &m)
if err != nil {
return nil, err
}
return &m, nil
}
// AppendNotices adds notices to Frame f's metadata (Frame.Meta.Notices).
// If f has no metadata, this method will initialize it before adding notices.
func (f *Frame) AppendNotices(notices ...Notice) {
if f.Meta == nil {
f.Meta = &FrameMeta{}
}
f.Meta.Notices = append(f.Meta.Notices, notices...)
}
// QueryStat is used for storing arbitrary statistics metadata related to a query and its result, e.g. total request time, data processing time.
// The embedded FieldConfig's display name must be set.
// It corresponds to the QueryResultMetaStat on the frontend (https://github.com/grafana/grafana/blob/master/packages/grafana-data/src/types/data.ts#L53).
type QueryStat struct {
FieldConfig
Value float64 `json:"value"`
}
// Notice provides a structure for presenting notifications in Grafana's user interface.
type Notice struct {
// Severity is the severity level of the notice: info, warning, or error.
Severity NoticeSeverity `json:"severity,omitempty"`
// Text is freeform descriptive text for the notice.
Text string `json:"text"`
// Link is an optional link for display in the user interface and can be an
// absolute URL or a path relative to Grafana's root url.
Link string `json:"link,omitempty"`
// Inspect is an optional suggestion for which tab to display in the panel inspector
// in Grafana's User interface. Can be meta, error, data, or stats.
Inspect InspectType `json:"inspect,omitempty"`
}
const (
noticeSeverityInfoString = "info"
noticeSeverityWarningString = "warning"
noticeSeverityErrorString = "error"
)
// NoticeSeverity is a type for the Severity property of a Notice.
type NoticeSeverity int
const (
// NoticeSeverityInfo is informational severity.
NoticeSeverityInfo NoticeSeverity = iota
// NoticeSeverityWarning is warning severity.
NoticeSeverityWarning
// NoticeSeverityError is error severity.
NoticeSeverityError
)
func (n NoticeSeverity) String() string {
switch n {
case NoticeSeverityInfo:
return noticeSeverityInfoString
case NoticeSeverityWarning:
return noticeSeverityWarningString
case NoticeSeverityError:
return noticeSeverityErrorString
default:
return ""
}
}
// MarshalJSON implements the json.Marshaler interface.
func (n NoticeSeverity) MarshalJSON() ([]byte, error) {
return json.Marshal(n.String())
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (n *NoticeSeverity) UnmarshalJSON(b []byte) error {
var s string
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
switch s {
case noticeSeverityInfoString:
*n = NoticeSeverityInfo
case noticeSeverityWarningString:
*n = NoticeSeverityWarning
case noticeSeverityErrorString:
*n = NoticeSeverityError
default:
return fmt.Errorf("unrecognized notice severity %v", s)
}
return nil
}
// InspectType is a type for the Inspect property of a Notice.
type InspectType int
const (
// InspectTypeNone is no suggestion for a tab of the panel editor in Grafana's user interface.
InspectTypeNone InspectType = iota
// InspectTypeMeta suggests the "meta" tab of the panel editor in Grafana's user interface.
InspectTypeMeta
// InspectTypeError suggests the "error" tab of the panel editor in Grafana's user interface.
InspectTypeError
// InspectTypeData suggests the "data" tab of the panel editor in Grafana's user interface.
InspectTypeData
// InspectTypeStats suggests the "stats" tab of the panel editor in Grafana's user interface.
InspectTypeStats
)
func (n InspectType) String() string {
switch n {
case InspectTypeNone:
return "" // default, omitempty when encoded to json.
case InspectTypeMeta:
return "meta"
case InspectTypeError:
return "error"
case InspectTypeData:
return "data"
case InspectTypeStats:
return "stats"
}
return ""
}