Skip to content

Commit 3e2cd84

Browse files
author
wtao
committed
better query editor
1 parent 0eb35a1 commit 3e2cd84

File tree

5 files changed

+39
-24
lines changed

5 files changed

+39
-24
lines changed

README.md

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,15 @@ there are two types of query the plugin supports, `table` or `timeserie`.
2626
<img src="./imgs/query-type.png" alt="frontend" style="width: 40%;" />
2727

2828
### query schema
29+
30+
after the frontend collects a query, it sends data of the following schema to its backend
2931
```javascript
3032
{
3133
collection: "collection name",
3234
aggregations: [
3335
{
34-
string: "string",
35-
date: new Date(),
36-
id: new ObjectId("573a1393f29313caabcdc50e"),
37-
bool: true,
38-
number: 12345.4,
39-
expandable1: $from,
40-
expandable2: $to,
41-
expandable3: $intervalMs
42-
},
36+
pipeline stage (s)
37+
}
4338
],
4439
}
4540
```
@@ -48,11 +43,22 @@ there are two types of query the plugin supports, `table` or `timeserie`.
4843

4944
a query is a `javascript` object.
5045

51-
the `collection` field specifies the name of the collection to perform an aggregation.
46+
the `Collection Name` field specifies the name of the collection to perform an aggregation.
47+
48+
the `Aggregation Pipeline` field can have multiple aggregation steps. if you're familiar with `MongoDB Compass`, you can contruct your aggregations in the tool, then copy/paste the aggregration's `Node.js` export to the field - it's just simple as that. the supported data types are listed below.
5249

53-
the `aggregations` field can have multiple aggregation steps. the supported data types are listed above.
50+
```
51+
string: "string",
52+
date: new Date(),
53+
id: new ObjectId("573a1393f29313caabcdc50e"),
54+
bool: true,
55+
number: 12345.4,
56+
expandable1: $from,
57+
expandable2: $to,
58+
expandable3: $intervalMs
59+
```
5460

55-
`timeserie` in detail
61+
### `timeserie` in detail
5662

5763
- `$from`, `$to`, `$intervalMs` are grafana expandable variables for using as a time boundary and an interval
5864
- `__name`, `__value`, `__timestamp` are the plugin's internal variables to construct a named grafana timeserie data frame

frontend/src/components/QueryEditor.tsx

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ import { lastValueFrom } from 'rxjs';
1616

1717
type Props = QueryEditorProps<DataSource, MyQuery, MyDataSourceOptions>;
1818

19-
const queryHint = `{
20-
collection: "collection name",
21-
aggregations: [
19+
const queryHint = `[
2220
{
2321
string: "string",
2422
date: new Date(),
@@ -28,9 +26,11 @@ const queryHint = `{
2826
expandable1: $from,
2927
expandable2: $to,
3028
expandable3: $intervalMs
31-
},
32-
],
33-
}`;
29+
}
30+
{
31+
...more stages
32+
}
33+
]`;
3434

3535
export function QueryEditor(props: Props) {
3636
const {
@@ -56,19 +56,24 @@ export function QueryEditor(props: Props) {
5656
},
5757
data: JSON.stringify(db),
5858
});
59-
const collections = await lastValueFrom(response);
60-
setCollections(collections.data);
59+
const values = await lastValueFrom(response);
60+
const collections = values.data;
61+
if (collections.length > 0) {
62+
onChange({ ...query, collection: collections[0] });
63+
}
64+
setCollections(collections);
6165
setSpinner(false);
6266
} catch (err: any) {
6367
setErrorMsg(err?.message ?? err);
6468
}
6569
})();
6670
}
71+
// eslint-disable-next-line
6772
}, [db, baseUrl]);
6873

6974
const queryUI = (
7075
<Stack gap={0} direction="column">
71-
<InlineField label="Type" labelWidth={16} tooltip="Not used yet">
76+
<InlineField label="Type" labelWidth={20}>
7277
<Select
7378
width={20}
7479
options={[
@@ -79,7 +84,7 @@ export function QueryEditor(props: Props) {
7984
onChange={(sv) => onChange({ ...query, queryType: sv.value!! })}
8085
/>
8186
</InlineField>
82-
<InlineField label="Type" labelWidth={16} tooltip="Not used yet">
87+
<InlineField label="Collection Name" labelWidth={20}>
8388
<Select
8489
width={50}
8590
options={collections.map((collection) => ({
@@ -90,7 +95,7 @@ export function QueryEditor(props: Props) {
9095
onChange={(sv) => onChange({ ...query, collection: sv.value!! })}
9196
/>
9297
</InlineField>
93-
<InlineField label="Query Text" labelWidth={16} tooltip="Not used yet">
98+
<InlineField label="Aggregation Pipeline" labelWidth={20}>
9499
<TextArea
95100
style={{ width: 500, minHeight: 200 }}
96101
value={queryText ?? ''}

frontend/src/datasource.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,14 @@ export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
5050

5151
async query(options: DataQueryRequest<MyQuery>): Promise<DataQueryResponse> {
5252
try {
53+
const { targets } = options;
5354
const payload = {
5455
...options,
56+
targets: targets.map((target) => ({
57+
...target,
58+
queryText: `{collection: "${target.collection}", aggregations: ${target.queryText}}`,
59+
})),
5560
db: { ...this.db },
56-
targets: options.targets.filter((target) => !!target.queryType),
5761
};
5862
const response = (await this.request('/query', payload)) as FetchResponse<
5963
TimeSeriesResponse[] | TableResponse[]

imgs/query-type.png

-2.47 KB
Loading

imgs/query.png

-9.51 KB
Loading

0 commit comments

Comments
 (0)