Skip to content

Commit ae850b2

Browse files
authored
Expand value field as variable result (#28)
1 parent 67358d5 commit ae850b2

File tree

2 files changed

+19
-21
lines changed

2 files changed

+19
-21
lines changed

src/components/VariableQueryEditor.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useState } from "react";
2-
import { DEFAULT_QUERY, VariableQuery } from "../types";
3-
import { CodeEditor, Field, InlineField, Input } from "@grafana/ui";
2+
import { VariableQuery } from "../types";
3+
import { CodeEditor, Field, InlineField, Input, Button, Alert } from "@grafana/ui";
44

55
interface VariableQueryProps {
66
query: VariableQuery;
@@ -32,17 +32,21 @@ export const VariableQueryEditor = ({ onChange, query }: VariableQueryProps) =>
3232
error="Please enter the collection" invalid={!query.collection}>
3333
<Input
3434
name="collection"
35-
onBlur={saveQuery}
3635
onChange={handleCollectionChange}
3736
value={state.collection}>
3837
</Input>
3938
</InlineField>
4039
<Field label="Query Text" description="MongoDB aggregate (JSON)">
4140
<CodeEditor width="100%" height={300} language="json" onBlur={saveQuery}
42-
value={query.queryText || DEFAULT_QUERY.queryText!} showMiniMap={false} showLineNumbers={true}
41+
value={query.queryText || ""} showMiniMap={false} showLineNumbers={true}
4342
onChange={handleQueryTextChange}
43+
monacoOptions={{ fontSize: 14 }}
4444
/>
4545
</Field>
46+
<Alert title="Query info" severity="info">
47+
The query result is expected to contain <code>value</code> field which has elements of type <code>string</code> or <code>number</code>
48+
</Alert>
49+
<Button onClick={saveQuery} variant="primary">Query</Button>
4650
</>
4751
);
4852
};

src/utils.ts

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -102,27 +102,21 @@ export function randomId(length: number) {
102102

103103
export function getMetricValues(response: DataQueryResponse): MetricFindValue[] {
104104
const dataframe = response.data[0] as DataFrameSchema;
105-
const fields = dataframe.
106-
fields.filter(f => f.type === FieldType.string || f.type === FieldType.number)
107-
// @ts-ignore
108-
.filter(f => f.values && f.values.length > 0);
105+
const field = dataframe.fields.find(f => f.name === "value");
109106

110-
// @ts-ignore
111-
return fields.map(function (field) {
112-
// @ts-ignore
113-
const values: Array<string | number> = field.values;
114-
let text: string;
107+
if (!field) {
108+
throw new Error("Field \"value\" not found");
109+
}
115110

116-
if (values.length === 1) {
117-
text = `${field.name}:${values[0]}`;
118-
} else {
119-
text = `${field.name}:[${values[0]}, ...]`;
120-
}
111+
if (field.type !== FieldType.string && field.type !== FieldType.number) {
112+
throw new Error("Each element should be string or number");
113+
}
121114

115+
// @ts-ignore
116+
return field.values.map((value: string | number) => {
122117
return {
123-
text: text,
124-
// @ts-ignore
125-
value: values.length === 1 ? values[0] : values,
118+
text: value.toString(),
119+
value: value,
126120
expandable: true
127121
};
128122
});

0 commit comments

Comments
 (0)