/
QueryEditorRow.tsx
629 lines (545 loc) · 18.7 KB
/
QueryEditorRow.tsx
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
// Libraries
import classNames from 'classnames';
import { cloneDeep, filter, has, uniqBy, uniqueId } from 'lodash';
import pluralize from 'pluralize';
import React, { PureComponent, ReactNode } from 'react';
// Utils & Services
import {
CoreApp,
DataQuery,
DataSourceApi,
DataSourceInstanceSettings,
DataSourcePluginContextProvider,
EventBusExtended,
EventBusSrv,
HistoryItem,
LoadingState,
PanelData,
PanelEvents,
QueryResultMetaNotice,
TimeRange,
toLegacyResponseData,
} from '@grafana/data';
import { selectors } from '@grafana/e2e-selectors';
import { AngularComponent, getAngularLoader, getDataSourceSrv } from '@grafana/runtime';
import { Badge, ErrorBoundaryAlert } from '@grafana/ui';
import { OperationRowHelp } from 'app/core/components/QueryOperationRow/OperationRowHelp';
import {
QueryOperationAction,
QueryOperationToggleAction,
} from 'app/core/components/QueryOperationRow/QueryOperationAction';
import {
QueryOperationRow,
QueryOperationRowRenderProps,
} from 'app/core/components/QueryOperationRow/QueryOperationRow';
import { t, Trans } from 'app/core/internationalization';
import { getTimeSrv } from 'app/features/dashboard/services/TimeSrv';
import { DashboardModel } from 'app/features/dashboard/state/DashboardModel';
import { PanelModel } from 'app/features/dashboard/state/PanelModel';
import { RowActionComponents } from './QueryActionComponent';
import { QueryEditorRowHeader } from './QueryEditorRowHeader';
import { QueryErrorAlert } from './QueryErrorAlert';
export interface Props<TQuery extends DataQuery> {
data: PanelData;
query: TQuery;
queries: TQuery[];
id: string;
index: number;
dataSource: DataSourceInstanceSettings;
onChangeDataSource?: (dsSettings: DataSourceInstanceSettings) => void;
onDataSourceLoaded?: (instance: DataSourceApi) => void;
renderHeaderExtras?: () => ReactNode;
onAddQuery: (query: TQuery) => void;
onRemoveQuery: (query: TQuery) => void;
onChange: (query: TQuery) => void;
onRunQuery: () => void;
visualization?: ReactNode;
hideDisableQuery?: boolean;
app?: CoreApp;
history?: Array<HistoryItem<TQuery>>;
eventBus?: EventBusExtended;
alerting?: boolean;
onQueryCopied?: () => void;
onQueryRemoved?: () => void;
onQueryToggled?: (queryStatus?: boolean | undefined) => void;
collapsable?: boolean;
}
interface State<TQuery extends DataQuery> {
/** DatasourceUid or ds variable expression used to resolve current datasource */
queriedDataSourceIdentifier?: string | null;
datasource: DataSourceApi<TQuery> | null;
datasourceUid?: string | null;
hasTextEditMode: boolean;
data?: PanelData;
isOpen?: boolean;
showingHelp: boolean;
}
export class QueryEditorRow<TQuery extends DataQuery> extends PureComponent<Props<TQuery>, State<TQuery>> {
element: HTMLElement | null = null;
angularScope: AngularQueryComponentScope<TQuery> | null = null;
angularQueryEditor: AngularComponent | null = null;
dataSourceSrv = getDataSourceSrv();
id = '';
state: State<TQuery> = {
datasource: null,
hasTextEditMode: false,
data: undefined,
isOpen: true,
showingHelp: false,
};
componentDidMount() {
const { data, query, id } = this.props;
const dataFilteredByRefId = filterPanelDataToQuery(data, query.refId);
this.id = uniqueId(id + '_');
this.setState({ data: dataFilteredByRefId });
this.loadDatasource();
}
componentWillUnmount() {
if (this.angularQueryEditor) {
this.angularQueryEditor.destroy();
}
}
getAngularQueryComponentScope(): AngularQueryComponentScope<TQuery> {
const { query, queries } = this.props;
const { datasource } = this.state;
const panel = new PanelModel({ targets: queries });
const dashboard = {} as DashboardModel;
const me = this;
return {
datasource: datasource,
target: query,
panel: panel,
dashboard: dashboard,
refresh: () => {
// Old angular editors modify the query model and just call refresh
// Important that this use this.props here so that as this function is only created on mount and it's
// important not to capture old prop functions in this closure
// the "hide" attribute of the queries can be changed from the "outside",
// it will be applied to "this.props.query.hide", but not to "query.hide".
// so we have to apply it.
if (query.hide !== me.props.query.hide) {
query.hide = me.props.query.hide;
}
this.props.onChange(query);
this.props.onRunQuery();
},
render: () => () => console.log('legacy render function called, it does nothing'),
events: this.props.eventBus || new EventBusSrv(),
range: getTimeSrv().timeRange(),
};
}
/**
* When datasource variables are used the query.datasource.uid property is a string variable expression
* DataSourceSettings.uid can also be this variable expression.
* This function always returns the current interpolated datasource uid.
*/
getInterpolatedDataSourceUID(): string | undefined {
if (this.props.query.datasource) {
const instanceSettings = this.dataSourceSrv.getInstanceSettings(this.props.query.datasource);
return instanceSettings?.rawRef?.uid ?? instanceSettings?.uid;
}
return this.props.dataSource.rawRef?.uid ?? this.props.dataSource.uid;
}
async loadDatasource() {
let datasource: DataSourceApi;
const interpolatedUID = this.getInterpolatedDataSourceUID();
try {
datasource = await this.dataSourceSrv.get(interpolatedUID);
} catch (error) {
// If the DS doesn't exist, it fails. Getting with no args returns the default DS.
datasource = await this.dataSourceSrv.get();
}
if (typeof this.props.onDataSourceLoaded === 'function') {
this.props.onDataSourceLoaded(datasource);
}
this.setState({
datasource: datasource as unknown as DataSourceApi<TQuery>,
queriedDataSourceIdentifier: interpolatedUID,
hasTextEditMode: has(datasource, 'components.QueryCtrl.prototype.toggleEditorMode'),
});
}
componentDidUpdate(prevProps: Props<TQuery>) {
const { datasource, queriedDataSourceIdentifier } = this.state;
const { data, query } = this.props;
if (prevProps.id !== this.props.id) {
this.id = uniqueId(this.props.id + '_');
}
if (data !== prevProps.data) {
const dataFilteredByRefId = filterPanelDataToQuery(data, query.refId);
this.setState({ data: dataFilteredByRefId });
if (this.angularScope) {
this.angularScope.range = getTimeSrv().timeRange();
}
if (this.angularQueryEditor && dataFilteredByRefId) {
notifyAngularQueryEditorsOfData(this.angularScope!, dataFilteredByRefId, this.angularQueryEditor);
}
}
// check if we need to load another datasource
if (datasource && queriedDataSourceIdentifier !== this.getInterpolatedDataSourceUID()) {
if (this.angularQueryEditor) {
this.angularQueryEditor.destroy();
this.angularQueryEditor = null;
}
this.loadDatasource();
return;
}
if (!this.element || this.angularQueryEditor) {
return;
}
this.renderAngularQueryEditor();
}
renderAngularQueryEditor = () => {
if (!this.element) {
return;
}
if (this.angularQueryEditor) {
this.angularQueryEditor.destroy();
this.angularQueryEditor = null;
}
const loader = getAngularLoader();
const template = '<plugin-component type="query-ctrl" />';
const scopeProps = { ctrl: this.getAngularQueryComponentScope() };
this.angularQueryEditor = loader.load(this.element, scopeProps, template);
this.angularScope = scopeProps.ctrl;
};
onOpen = () => {
this.renderAngularQueryEditor();
};
getReactQueryEditor(ds: DataSourceApi<TQuery>) {
if (!ds) {
return;
}
switch (this.props.app) {
case CoreApp.Explore:
return (
ds.components?.ExploreMetricsQueryField ||
ds.components?.ExploreLogsQueryField ||
ds.components?.ExploreQueryField ||
ds.components?.QueryEditor
);
case CoreApp.PanelEditor:
case CoreApp.Dashboard:
default:
return ds.components?.QueryEditor;
}
}
isWaitingForDatasourceToLoad(): boolean {
// if we not yet have loaded the datasource in state the
// ds in props and the ds in state will have different values.
return this.getInterpolatedDataSourceUID() !== this.state.queriedDataSourceIdentifier;
}
renderPluginEditor = () => {
const { query, onChange, queries, onRunQuery, onAddQuery, app = CoreApp.PanelEditor, history } = this.props;
const { datasource, data } = this.state;
if (this.isWaitingForDatasourceToLoad()) {
return null;
}
if (datasource?.components?.QueryCtrl) {
return <div ref={(element) => (this.element = element)} />;
}
if (datasource) {
let QueryEditor = this.getReactQueryEditor(datasource);
if (QueryEditor) {
return (
<DataSourcePluginContextProvider instanceSettings={this.props.dataSource}>
<QueryEditor
key={datasource?.name}
query={query}
datasource={datasource}
onChange={onChange}
onRunQuery={onRunQuery}
onAddQuery={onAddQuery}
data={data}
range={getTimeSrv().timeRange()}
queries={queries}
app={app}
history={history}
/>
</DataSourcePluginContextProvider>
);
}
}
return (
<div>
<Trans i18nKey="query-operation.query-editor-not-exported">
Data source plugin does not export any Query Editor component
</Trans>
</div>
);
};
onToggleEditMode = (e: React.MouseEvent, props: QueryOperationRowRenderProps) => {
e.stopPropagation();
if (this.angularScope && this.angularScope.toggleEditorMode) {
this.angularScope.toggleEditorMode();
this.angularQueryEditor?.digest();
if (!props.isOpen) {
props.onOpen();
}
}
};
onRemoveQuery = () => {
const { onRemoveQuery, query, onQueryRemoved } = this.props;
onRemoveQuery(query);
if (onQueryRemoved) {
onQueryRemoved();
}
};
onCopyQuery = () => {
const { query, onAddQuery, onQueryCopied } = this.props;
const copy = cloneDeep(query);
onAddQuery(copy);
if (onQueryCopied) {
onQueryCopied();
}
};
onDisableQuery = () => {
const { query, onChange, onRunQuery, onQueryToggled } = this.props;
onChange({ ...query, hide: !query.hide });
onRunQuery();
if (onQueryToggled) {
onQueryToggled(query.hide);
}
};
onToggleHelp = () => {
this.setState((state) => ({
showingHelp: !state.showingHelp,
}));
};
onClickExample = (query: TQuery) => {
if (query.datasource === undefined) {
query.datasource = { type: this.props.dataSource.type, uid: this.props.dataSource.uid };
}
this.props.onChange({
...query,
refId: this.props.query.refId,
});
this.onToggleHelp();
};
renderCollapsedText(): string | null {
const { datasource } = this.state;
if (datasource?.getQueryDisplayText) {
return datasource.getQueryDisplayText(this.props.query);
}
if (this.angularScope && this.angularScope.getCollapsedText) {
return this.angularScope.getCollapsedText();
}
return null;
}
renderWarnings = (): JSX.Element | null => {
const { data, query } = this.props;
const dataFilteredByRefId = filterPanelDataToQuery(data, query.refId)?.series ?? [];
const allWarnings = dataFilteredByRefId.reduce((acc: QueryResultMetaNotice[], serie) => {
if (!serie.meta?.notices) {
return acc;
}
const warnings = filter(serie.meta.notices, { severity: 'warning' }) ?? [];
return acc.concat(warnings);
}, []);
const uniqueWarnings = uniqBy(allWarnings, 'text');
const hasWarnings = uniqueWarnings.length > 0;
if (!hasWarnings) {
return null;
}
const serializedWarnings = uniqueWarnings.map((warning) => warning.text).join('\n');
return (
<Badge
color="orange"
icon="exclamation-triangle"
text={
<>
{uniqueWarnings.length} {pluralize('warning', uniqueWarnings.length)}
</>
}
tooltip={serializedWarnings}
/>
);
};
renderExtraActions = () => {
const { query, queries, data, onAddQuery, dataSource } = this.props;
const extraActions = RowActionComponents.getAllExtraRenderAction()
.map((action, index) =>
action({
query,
queries,
timeRange: data.timeRange,
onAddQuery: onAddQuery as (query: DataQuery) => void,
dataSource,
key: index,
})
)
.filter(Boolean);
extraActions.push(this.renderWarnings());
return extraActions;
};
renderActions = (props: QueryOperationRowRenderProps) => {
const { query, hideDisableQuery = false } = this.props;
const { hasTextEditMode, datasource, showingHelp } = this.state;
const isDisabled = !!query.hide;
const hasEditorHelp = datasource?.components?.QueryEditorHelp;
return (
<>
{hasEditorHelp && (
<QueryOperationToggleAction
title={t('query-operation.header.datasource-help', 'Show data source help')}
icon="question-circle"
onClick={this.onToggleHelp}
active={showingHelp}
/>
)}
{hasTextEditMode && (
<QueryOperationAction
title={t('query-operation.header.toggle-edit-mode', 'Toggle text edit mode')}
icon="pen"
onClick={(e) => {
this.onToggleEditMode(e, props);
}}
/>
)}
{this.renderExtraActions()}
<QueryOperationAction
title={t('query-operation.header.duplicate-query', 'Duplicate query')}
icon="copy"
onClick={this.onCopyQuery}
/>
{!hideDisableQuery ? (
<QueryOperationToggleAction
title={t('query-operation.header.disable-query', 'Disable query')}
icon={isDisabled ? 'eye-slash' : 'eye'}
active={isDisabled}
onClick={this.onDisableQuery}
/>
) : null}
<QueryOperationAction
title={t('query-operation.header.remove-query', 'Remove query')}
icon="trash-alt"
onClick={this.onRemoveQuery}
/>
</>
);
};
renderHeader = (props: QueryOperationRowRenderProps) => {
const { alerting, query, dataSource, onChangeDataSource, onChange, queries, renderHeaderExtras } = this.props;
return (
<QueryEditorRowHeader
query={query}
queries={queries}
onChangeDataSource={onChangeDataSource}
dataSource={dataSource}
disabled={query.hide}
onClick={(e) => this.onToggleEditMode(e, props)}
onChange={onChange}
collapsedText={!props.isOpen ? this.renderCollapsedText() : null}
renderExtras={renderHeaderExtras}
alerting={alerting}
/>
);
};
render() {
const { query, index, visualization, collapsable } = this.props;
const { datasource, showingHelp, data } = this.state;
const isDisabled = query.hide;
const error =
data?.error && data.error.refId === query.refId ? data.error : data?.errors?.find((e) => e.refId === query.refId);
const rowClasses = classNames('query-editor-row', {
'query-editor-row--disabled': isDisabled,
'gf-form-disabled': isDisabled,
});
if (!datasource) {
return null;
}
const editor = this.renderPluginEditor();
const DatasourceCheatsheet = datasource.components?.QueryEditorHelp;
return (
<div data-testid="query-editor-row" aria-label={selectors.components.QueryEditorRows.rows}>
<QueryOperationRow
id={this.id}
draggable={true}
collapsable={collapsable}
index={index}
headerElement={this.renderHeader}
actions={this.renderActions}
onOpen={this.onOpen}
>
<div className={rowClasses} id={this.id}>
<ErrorBoundaryAlert>
{showingHelp && DatasourceCheatsheet && (
<OperationRowHelp>
<DatasourceCheatsheet
onClickExample={(query) => this.onClickExample(query)}
query={this.props.query}
datasource={datasource}
/>
</OperationRowHelp>
)}
{editor}
</ErrorBoundaryAlert>
{error && <QueryErrorAlert error={error} />}
{visualization}
</div>
</QueryOperationRow>
</div>
);
}
}
function notifyAngularQueryEditorsOfData<TQuery extends DataQuery>(
scope: AngularQueryComponentScope<TQuery>,
data: PanelData,
editor: AngularComponent
) {
if (data.state === LoadingState.Done) {
const legacy = data.series.map((v) => toLegacyResponseData(v));
scope.events.emit(PanelEvents.dataReceived, legacy);
} else if (data.state === LoadingState.Error) {
scope.events.emit(PanelEvents.dataError, data.error);
}
// Some query controllers listen to data error events and need a digest
// for some reason this needs to be done in next tick
setTimeout(editor.digest);
}
export interface AngularQueryComponentScope<TQuery extends DataQuery> {
target: TQuery;
panel: PanelModel;
dashboard: DashboardModel;
events: EventBusExtended;
refresh: () => void;
render: () => void;
datasource: DataSourceApi<TQuery> | null;
toggleEditorMode?: () => void;
getCollapsedText?: () => string;
range: TimeRange;
}
/**
* Get a version of the PanelData limited to the query we are looking at
*/
export function filterPanelDataToQuery(data: PanelData, refId: string): PanelData | undefined {
const series = data.series.filter((series) => series.refId === refId);
// If there was an error with no data and the panel is not in a loading state, pass it to the QueryEditors
if (data.state !== LoadingState.Loading && (data.error || data.errors?.length) && !data.series.length) {
return {
...data,
state: LoadingState.Error,
};
}
// Only say this is an error if the error links to the query
let state = data.state;
let error = data.errors?.find((e) => e.refId === refId);
if (!error && data.error) {
error = data.error.refId === refId ? data.error : undefined;
}
if (state !== LoadingState.Loading) {
if (error) {
state = LoadingState.Error;
} else if (data.state === LoadingState.Error) {
state = LoadingState.Done;
}
}
const timeRange = data.timeRange;
return {
...data,
state,
series,
error,
errors: error ? [error] : undefined,
timeRange,
};
}