-
Notifications
You must be signed in to change notification settings - Fork 11.8k
/
panel.ts
363 lines (303 loc) · 9.43 KB
/
panel.ts
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
import { defaultsDeep } from 'lodash';
import { EventBus } from '../events';
import { StandardEditorProps } from '../field';
import { Registry } from '../utils';
import { OptionsEditorItem } from './OptionsUIRegistryBuilder';
import { ScopedVars } from './ScopedVars';
import { AlertStateInfo } from './alerts';
import { PanelModel } from './dashboard';
import { LoadingState, PreferredVisualisationType } from './data';
import { DataFrame, FieldType } from './dataFrame';
import { DataQueryError, DataQueryRequest, DataQueryTimings } from './datasource';
import { FieldConfigSource } from './fieldOverrides';
import { IconName } from './icon';
import { OptionEditorConfig } from './options';
import { PluginMeta } from './plugin';
import { AbsoluteTimeRange, TimeRange, TimeZone } from './time';
import { DataTransformerConfig } from './transformations';
export type InterpolateFunction = (value: string, scopedVars?: ScopedVars, format?: string | Function) => string;
export interface PanelPluginMeta extends PluginMeta {
/** Indicates that panel does not issue queries */
skipDataQuery?: boolean;
/** Indicates that panel should not be available in visualisation picker */
hideFromList?: boolean;
/** Sort order */
sort: number;
}
export interface PanelData {
/** State of the data (loading, done, error, streaming) */
state: LoadingState;
/** Contains data frames with field overrides applied */
series: DataFrame[];
/**
* This is a key that will change when the DataFrame[] structure changes.
* The revision is a useful way to know if only data has changed or data+structure
*/
structureRev?: number;
/** A list of annotation items */
annotations?: DataFrame[];
/**
* @internal
*/
alertState?: AlertStateInfo;
/** Request contains the queries and properties sent to the datasource */
request?: DataQueryRequest;
/** Timing measurements */
timings?: DataQueryTimings;
/** Any query errors */
errors?: DataQueryError[];
/**
* Single error for legacy reasons
* @deprecated use errors instead -- will be removed in Grafana 10+
*/
error?: DataQueryError;
/** Contains the range from the request or a shifted time range if a request uses relative time */
timeRange: TimeRange;
/** traceIds collected during the processing of the requests */
traceIds?: string[];
}
export interface PanelProps<T = any> {
/** ID of the panel within the current dashboard */
id: number;
/** Result set of panel queries */
data: PanelData;
/** Time range of the current dashboard */
timeRange: TimeRange;
/** Time zone of the current dashboard */
timeZone: TimeZone;
/** Panel options */
options: T;
/** Indicates whether or not panel should be rendered transparent */
transparent: boolean;
/** Current width of the panel */
width: number;
/** Current height of the panel */
height: number;
/** Field options configuration */
fieldConfig: FieldConfigSource;
/** @internal */
renderCounter: number;
/** Panel title */
title: string;
/** EventBus */
eventBus: EventBus;
/** Panel options change handler */
onOptionsChange: (options: T) => void;
/** Field config change handler */
onFieldConfigChange: (config: FieldConfigSource) => void;
/** Template variables interpolation function */
replaceVariables: InterpolateFunction;
/** Time range change handler */
onChangeTimeRange: (timeRange: AbsoluteTimeRange) => void;
}
export interface PanelEditorProps<T = any> {
/** Panel options */
options: T;
/** Panel options change handler */
onOptionsChange: (
options: T,
// callback can be used to run something right after update.
callback?: () => void
) => void;
/** Result set of panel queries */
data?: PanelData;
}
/**
* Called when a panel is first loaded with current panel model
*/
export type PanelMigrationHandler<TOptions = any> = (panel: PanelModel<TOptions>) => Partial<TOptions>;
/**
* Called before a panel is initialized. Allows panel inspection for any updates before changing the panel type.
*/
export type PanelTypeChangedHandler<TOptions = any> = (
panel: PanelModel<TOptions>,
prevPluginId: string,
prevOptions: Record<string, any>,
prevFieldConfig: FieldConfigSource
) => Partial<TOptions>;
export type PanelOptionEditorsRegistry = Registry<PanelOptionsEditorItem>;
export interface PanelOptionsEditorProps<TValue> extends StandardEditorProps<TValue> {}
export interface PanelOptionsEditorItem<TOptions = any, TValue = any, TSettings = any>
extends OptionsEditorItem<TOptions, TSettings, PanelOptionsEditorProps<TValue>, TValue> {}
export interface PanelOptionsEditorConfig<TOptions, TSettings = any, TValue = any>
extends OptionEditorConfig<TOptions, TSettings, TValue> {}
/**
* @internal
*/
export interface PanelMenuItem {
type?: 'submenu' | 'divider' | 'group';
text: string;
iconClassName?: IconName;
onClick?: (event: React.MouseEvent<any>) => void;
shortcut?: string;
href?: string;
subMenu?: PanelMenuItem[];
}
/**
* @internal
*/
export interface AngularPanelMenuItem {
click: Function;
icon: string;
href: string;
divider: boolean;
text: string;
shortcut: string;
submenu: any[];
}
export enum VizOrientation {
Auto = 'auto',
Vertical = 'vertical',
Horizontal = 'horizontal',
}
export interface PanelPluginDataSupport {
annotations: boolean;
alertStates: boolean;
}
/**
* @alpha
*/
export interface VisualizationSuggestion<TOptions = any, TFieldConfig = any> {
/** Name of suggestion */
name: string;
/** Description */
description?: string;
/** Panel plugin id */
pluginId: string;
/** Panel plugin options */
options?: Partial<TOptions>;
/** Panel plugin field options */
fieldConfig?: FieldConfigSource<Partial<TFieldConfig>>;
/** Data transformations */
transformations?: DataTransformerConfig[];
/** Options for how to render suggestion card */
cardOptions?: {
/** Tweak for small preview */
previewModifier?: (suggestion: VisualizationSuggestion) => void;
icon?: string;
imgSrc?: string;
};
/** A value between 0-100 how suitable suggestion is */
score?: VisualizationSuggestionScore;
}
/**
* @alpha
*/
export enum VisualizationSuggestionScore {
/** We are pretty sure this is the best possible option */
Best = 100,
/** Should be a really good option */
Good = 70,
/** Can be visualized but there are likely better options. If no score is set this score is assumed */
OK = 50,
}
/**
* @alpha
*/
export interface PanelDataSummary {
hasData?: boolean;
rowCountTotal: number;
rowCountMax: number;
frameCount: number;
fieldCount: number;
numberFieldCount: number;
timeFieldCount: number;
stringFieldCount: number;
hasNumberField?: boolean;
hasTimeField?: boolean;
hasStringField?: boolean;
/** The first frame that set's this value */
preferredVisualisationType?: PreferredVisualisationType;
}
/**
* @alpha
*/
export class VisualizationSuggestionsBuilder {
/** Current data */
data?: PanelData;
/** Current panel & options */
panel?: PanelModel;
/** Summary stats for current data */
dataSummary: PanelDataSummary;
private list: VisualizationSuggestion[] = [];
constructor(data?: PanelData, panel?: PanelModel) {
this.data = data;
this.panel = panel;
this.dataSummary = this.computeDataSummary();
}
getListAppender<TOptions, TFieldConfig>(defaults: VisualizationSuggestion<TOptions, TFieldConfig>) {
return new VisualizationSuggestionsListAppender<TOptions, TFieldConfig>(this.list, defaults);
}
private computeDataSummary() {
const frames = this.data?.series || [];
let numberFieldCount = 0;
let timeFieldCount = 0;
let stringFieldCount = 0;
let rowCountTotal = 0;
let rowCountMax = 0;
let fieldCount = 0;
let preferredVisualisationType: PreferredVisualisationType | undefined;
for (const frame of frames) {
rowCountTotal += frame.length;
if (frame.meta?.preferredVisualisationType) {
preferredVisualisationType = frame.meta.preferredVisualisationType;
}
for (const field of frame.fields) {
fieldCount++;
switch (field.type) {
case FieldType.number:
numberFieldCount += 1;
break;
case FieldType.time:
timeFieldCount += 1;
break;
case FieldType.string:
stringFieldCount += 1;
break;
}
}
if (frame.length > rowCountMax) {
rowCountMax = frame.length;
}
}
return {
numberFieldCount,
timeFieldCount,
stringFieldCount,
rowCountTotal,
rowCountMax,
fieldCount,
preferredVisualisationType,
frameCount: frames.length,
hasData: rowCountTotal > 0,
hasTimeField: timeFieldCount > 0,
hasNumberField: numberFieldCount > 0,
hasStringField: stringFieldCount > 0,
};
}
getList() {
return this.list;
}
}
/**
* @alpha
*/
export type VisualizationSuggestionsSupplier = {
/**
* Adds good suitable suggestions for the current data
*/
getSuggestionsForData: (builder: VisualizationSuggestionsBuilder) => void;
};
/**
* Helps with typings and defaults
* @alpha
*/
export class VisualizationSuggestionsListAppender<TOptions, TFieldConfig> {
constructor(
private list: VisualizationSuggestion[],
private defaults: VisualizationSuggestion<TOptions, TFieldConfig>
) {}
append(overrides: Partial<VisualizationSuggestion<TOptions, TFieldConfig>>) {
this.list.push(defaultsDeep(overrides, this.defaults));
}
}