Skip to content

Commit 40e08ea

Browse files
committed
supported value and label
1 parent 96fd6ab commit 40e08ea

File tree

3 files changed

+41
-24
lines changed

3 files changed

+41
-24
lines changed
+19-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Alert, CodeEditor, Field, Label, Stack } from '@grafana/ui';
12
import React, { useState } from 'react';
23

34
import { VariableQueryProps } from '../types';
@@ -9,18 +10,27 @@ export const VariableQueryEditor = ({ onChange, query }: VariableQueryProps) =>
910
onChange(state, `${state.query}`);
1011
};
1112

12-
const handleChange = (event: React.FormEvent<HTMLInputElement>) =>
13+
const onSQLChange = (sql: string) => {
1314
setState({
14-
...state,
15-
[event.currentTarget.name]: event.currentTarget.value,
15+
query: sql,
1616
});
17+
};
1718

1819
return (
19-
<>
20-
<div className="gf-form">
21-
<span className="gf-form-label width-10">Query</span>
22-
<input name="query" className="gf-form-input" onBlur={saveQuery} onChange={handleChange} value={state.query} />
23-
</div>
24-
</>
20+
<Field
21+
label=""
22+
description="Make sure your query returns either 1 or 2 columns. If your query returns 1 column only, it will be used as the value and label. If it returns 2 columns, the first column will be used as the value while the second column will be used as the label."
23+
>
24+
<CodeEditor
25+
onChange={onSQLChange}
26+
onBlur={saveQuery}
27+
width={600}
28+
height={100}
29+
language="sql"
30+
showLineNumbers={true}
31+
showMiniMap={false}
32+
value={state.query}
33+
/>
34+
</Field>
2535
);
2636
};

src/datasource.ts

+19-12
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,47 @@
1-
import { DataSourceInstanceSettings, ScopedVars } from '@grafana/data';
1+
import { DataQueryRequest, DataSourceInstanceSettings, MetricFindValue, ScopedVars } from '@grafana/data';
22
import { DataSourceWithBackend, getTemplateSrv } from '@grafana/runtime';
3-
import { MyVariableQuery, TpDataSourceOptions, TpQuery } from './types';
3+
import { TpDataSourceOptions, TpQuery, TpVariableQuery } from './types';
44

55
export class DataSource extends DataSourceWithBackend<TpQuery, TpDataSourceOptions> {
66
constructor(instanceSettings: DataSourceInstanceSettings<TpDataSourceOptions>) {
77
super(instanceSettings);
88
}
99

1010

11-
metricFindQuery(query: MyVariableQuery, options?: any) {
11+
metricFindQuery(query: TpVariableQuery, options?: any) {
12+
let metrics: MetricFindValue[] = []
1213
if (!query || !options.variable.datasource) {
13-
return Promise.resolve([]);
14+
return Promise.resolve(metrics);
1415
}
1516

16-
const prom = new Promise((resolve) => {
17+
const prom = new Promise<MetricFindValue[]>((resolve) => {
1718
const req = {
1819
targets: [{ datasource: options.variable.datasource,
1920
sql: query.query,
2021
refId: String(Math.random()) }],
2122
range: options ? options.range : (getTemplateSrv() as any).timeRange,
22-
};
23+
} as DataQueryRequest<TpQuery> ;
2324

2425
this.query(req).subscribe((res) => {
2526
const result = res.data[0] || { fields: [] }
2627

27-
if (result.fields.length > 0) {
28-
const labels = result.fields[0].values.map((v) => {
28+
29+
if (result.fields.length === 2) {
30+
for (let i = 0; i < result.fields[0].values.length; i++) {
31+
metrics.push({
32+
text: result.fields[1].values[i],
33+
value: result.fields[0].values[i]
34+
})
35+
}
36+
} else if (result.fields.length === 1) {
37+
metrics = result.fields[0].values.map((v: string) => {
2938
return {
3039
text: v,
3140
value: v
3241
}});
33-
34-
resolve(labels);
35-
return
3642
}
37-
resolve([]);
43+
44+
resolve(metrics);
3845
});
3946
})
4047

src/types.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ export interface TpSecureJsonData {
2222
password?: string;
2323
}
2424

25-
export interface MyVariableQuery {
25+
export interface TpVariableQuery {
2626
query: string;
2727
}
2828

2929
export interface VariableQueryProps {
30-
query: MyVariableQuery;
31-
onChange: (query: MyVariableQuery, definition: string) => void;
30+
query: TpVariableQuery;
31+
onChange: (query: TpVariableQuery, definition: string) => void;
3232
}

0 commit comments

Comments
 (0)