-
Notifications
You must be signed in to change notification settings - Fork 34
/
VisualQueryEditor.tsx
161 lines (142 loc) · 5.37 KB
/
VisualQueryEditor.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
import { QueryEditorProps, SelectableValue } from '@grafana/data';
import { getTemplateSrv } from '@grafana/runtime';
import { Alert, Select } from '@grafana/ui';
import { EditorField, EditorFieldGroup, EditorRow, EditorRows } from '@grafana/experimental';
import { QueryEditorExpressionType } from 'components/LegacyQueryEditor/editor/expressions';
import { AdxDataSource } from 'datasource';
import React, { useMemo } from 'react';
import { useAsync } from 'react-use';
import { AdxSchemaResolver } from 'schema/AdxSchemaResolver';
import { QueryEditorPropertyDefinition, QueryEditorPropertyType } from 'schema/types';
import { AdxDataSourceOptions, AdxSchema, defaultQuery, KustoQuery } from 'types';
import FilterSection from './VisualQueryEditor/FilterSection';
import AggregateSection from './VisualQueryEditor/AggregateSection';
import GroupBySection from './VisualQueryEditor/GroupBySection';
import KQLPreview from './VisualQueryEditor/KQLPreview';
import Timeshift from './VisualQueryEditor/Timeshift';
type Props = QueryEditorProps<AdxDataSource, KustoQuery, AdxDataSourceOptions>;
interface VisualQueryEditorProps extends Props {
schema?: AdxSchema;
database: string;
templateVariableOptions: SelectableValue<string>;
}
export const VisualQueryEditor: React.FC<VisualQueryEditorProps> = (props) => {
const templateSrv = getTemplateSrv();
const { schema, database, datasource, query, onChange, templateVariableOptions } = props;
const { id: datasourceId, parseExpression, getSchemaMapper } = datasource;
const databaseName = templateSrv.replace(database);
const tables = useTableOptions(schema, databaseName, datasource);
const table = useSelectedTable(tables, query, datasource);
const tableOptions = (tables as Array<SelectableValue<string>>).concat(templateVariableOptions);
const tableName = getTemplateSrv().replace(table?.value ?? '');
const tableMapping = getSchemaMapper().getMappingByValue(table?.value);
const tableSchema = useAsync(async () => {
if (!table || !table.value) {
return [];
}
const name = tableMapping?.value ?? tableName;
const schema = await getTableSchema(datasource, databaseName, name);
const expression = query.expression ?? defaultQuery.expression;
onChange({
...query,
query: parseExpression(
{
...expression,
from: {
type: QueryEditorExpressionType.Property,
property: { type: QueryEditorPropertyType.String, name: table.value },
},
},
schema
),
});
return schema;
}, [datasourceId, databaseName, tableName, tableMapping?.value]);
if (!schema) {
return null;
}
return (
<EditorRows>
{tableSchema.error && (
<Alert severity="error" title="Could not load table schema">
{tableSchema.error?.message}
</Alert>
)}
{!tableSchema.loading && tableSchema.value?.length === 0 && (
<Alert severity="warning" title="Table schema loaded successfully but without any columns" />
)}
<EditorRow>
<EditorFieldGroup>
<EditorField label="Table" width={16}>
<Select
aria-label="Table"
isLoading={tableSchema.loading}
value={table}
options={tableOptions}
allowCustomValue
onChange={({ value }) => {
onChange({
...query,
expression: {
...defaultQuery.expression,
from: {
type: QueryEditorExpressionType.Property,
property: { type: QueryEditorPropertyType.String, name: value },
},
},
});
}}
/>
</EditorField>
</EditorFieldGroup>
</EditorRow>
<FilterSection {...props} tableSchema={tableSchema} />
<AggregateSection {...props} tableSchema={tableSchema} />
<GroupBySection {...props} tableSchema={tableSchema} />
<Timeshift {...props} tableSchema={tableSchema} />
<KQLPreview query={props.query.query} />
</EditorRows>
);
};
const useTableOptions = (
schema: AdxSchema | undefined,
database: string,
datasource: AdxDataSource
): QueryEditorPropertyDefinition[] => {
const mapper = datasource.getSchemaMapper();
return useMemo(() => {
if (!schema || !schema.Databases) {
return [];
}
return mapper.getTableOptions(schema, database);
}, [database, schema, mapper]);
};
const useSelectedTable = (
options: QueryEditorPropertyDefinition[],
query: KustoQuery,
datasource: AdxDataSource
): SelectableValue<string> | undefined => {
const variables = datasource.getVariables();
const table = query.expression?.from?.property.name;
return useMemo(() => {
const selected = options.find((option) => option.value === table);
if (selected) {
return selected;
}
const variable = variables.find((variable) => variable === table);
if (variable) {
return {
label: variable,
value: variable,
};
}
if (options.length > 0) {
return options[0];
}
return;
}, [options, table, variables]);
};
async function getTableSchema(datasource: AdxDataSource, databaseName: string, tableName: string) {
const schemaResolver = new AdxSchemaResolver(datasource);
return await schemaResolver.getColumnsForTable(databaseName, tableName);
}