-
Notifications
You must be signed in to change notification settings - Fork 11.7k
/
DashboardQueryEditor.tsx
203 lines (180 loc) · 6.33 KB
/
DashboardQueryEditor.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
import { css } from '@emotion/css';
import { useId } from '@react-aria/utils';
import pluralize from 'pluralize';
import React, { useCallback, useMemo } from 'react';
import { useAsync } from 'react-use';
import { DataQuery, GrafanaTheme2, SelectableValue, DataTopic, QueryEditorProps } from '@grafana/data';
import { OperationsEditorRow } from '@grafana/experimental';
import { Field, Select, useStyles2, Spinner, RadioButtonGroup, Stack, InlineSwitch } from '@grafana/ui';
import config from 'app/core/config';
import { getDashboardSrv } from 'app/features/dashboard/services/DashboardSrv';
import { PanelModel } from 'app/features/dashboard/state';
import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';
import { filterPanelDataToQuery } from 'app/features/query/components/QueryEditorRow';
import { DashboardDatasource } from './datasource';
import { DashboardQuery, ResultInfo, SHARED_DASHBOARD_QUERY } from './types';
function getQueryDisplayText(query: DataQuery): string {
return JSON.stringify(query);
}
interface Props extends QueryEditorProps<DashboardDatasource, DashboardQuery> {}
const topics = [
{ label: 'All data', value: false },
{ label: 'Annotations', value: true, description: 'Include annotations as regular data' },
];
export function DashboardQueryEditor({ data, query, onChange, onRunQuery }: Props) {
const { value: defaultDatasource } = useAsync(() => getDatasourceSrv().get());
const panel = useMemo(() => {
const dashboard = getDashboardSrv().getCurrent();
return dashboard?.getPanelById(query.panelId ?? -124134);
}, [query.panelId]);
const { value: results, loading: loadingResults } = useAsync(async (): Promise<ResultInfo[]> => {
if (!panel || !data) {
return [];
}
const mainDS = await getDatasourceSrv().get(panel.datasource);
return Promise.all(
panel.targets.map(async (query) => {
const ds = query.datasource ? await getDatasourceSrv().get(query.datasource) : mainDS;
const fmt = ds.getQueryDisplayText || getQueryDisplayText;
const queryData = filterPanelDataToQuery(data, query.refId) ?? data;
return {
refId: query.refId,
query: fmt(query),
name: ds.name,
img: ds.meta.info.logos.small,
data: queryData.series,
error: queryData.error,
};
})
);
}, [data, panel]);
const onUpdateQuery = useCallback(
(query: DashboardQuery) => {
onChange(query);
onRunQuery();
},
[onChange, onRunQuery]
);
const onPanelChanged = useCallback(
(id: number) => {
onUpdateQuery({
...query,
panelId: id,
});
},
[query, onUpdateQuery]
);
const onTransformToggle = useCallback(() => {
onUpdateQuery({
...query,
withTransforms: !query.withTransforms,
});
}, [query, onUpdateQuery]);
const onTopicChanged = useCallback(
(t: boolean) => {
onUpdateQuery({
...query,
topic: t ? DataTopic.Annotations : undefined,
});
},
[query, onUpdateQuery]
);
const getPanelDescription = useCallback(
(panel: PanelModel): string => {
const datasource = panel.datasource ?? defaultDatasource;
const dsname = getDatasourceSrv().getInstanceSettings(datasource)?.name;
const queryCount = panel.targets.length;
return `${queryCount} ${pluralize('query', queryCount)} to ${dsname}`;
},
[defaultDatasource]
);
const dashboard = getDashboardSrv().getCurrent();
const showTransforms = Boolean(query.withTransforms || panel?.transformations?.length);
const panels: Array<SelectableValue<number>> = useMemo(
() =>
dashboard?.panels
.filter(
(panel) =>
config.panels[panel.type] &&
panel.targets &&
panel.id !== dashboard.panelInEdit?.id &&
panel.datasource?.uid !== SHARED_DASHBOARD_QUERY
)
.map((panel) => ({
value: panel.id,
label: panel.title ?? 'Panel ' + panel.id,
description: getPanelDescription(panel),
imgUrl: config.panels[panel.type].info.logos.small,
})) ?? [],
[dashboard, getPanelDescription]
);
const styles = useStyles2(getStyles);
const selectId = useId();
if (!dashboard) {
return null;
}
if (panels.length < 1) {
return (
<p className={styles.noQueriesText}>
This dashboard does not have any other panels. Add queries to other panels and try again.
</p>
);
}
const selected = panels.find((panel) => panel.value === query.panelId);
return (
<OperationsEditorRow>
<Stack direction="column">
<Stack gap={3}>
<Field label="Source panel" description="Use query results from another panel">
<Select
inputId={selectId}
placeholder="Choose panel"
isSearchable={true}
options={panels}
value={selected}
onChange={(item) => onPanelChanged(item.value!)}
/>
</Field>
<Field label="Data" description="Use data or annotations from the panel">
<RadioButtonGroup
options={topics}
value={query.topic === DataTopic.Annotations}
onChange={onTopicChanged}
/>
</Field>
{showTransforms && (
<Field label="Transform" description="Apply transformations from the source panel">
<InlineSwitch value={Boolean(query.withTransforms)} onChange={onTransformToggle} />
</Field>
)}
</Stack>
{loadingResults ? (
<Spinner />
) : (
<>
{results && Boolean(results.length) && (
<Field label="Queries from panel">
<Stack direction="column">
{results.map((target, i) => (
<Stack key={i} alignItems="center" gap={1}>
<div>{target.refId}</div>
<img src={target.img} alt={target.name} title={target.name} width={16} />
<div>{target.query}</div>
</Stack>
))}
</Stack>
</Field>
)}
</>
)}
</Stack>
</OperationsEditorRow>
);
}
function getStyles(theme: GrafanaTheme2) {
return {
noQueriesText: css({
padding: theme.spacing(1.25),
}),
};
}