Skip to content

Commit 56a7167

Browse files
author
Stan van Rooy
authored
feat: add support for dynamic variables from parseable (#31)
This makes it possible to query Parseable for Grafana dashboard variables. This is useful for dynamically retrieving application names, response codes, etc. ![image](https://github.com/parseablehq/parseable-datasource/assets/49564025/fedbb600-7f4f-4fd4-ba8b-2925a891d040)
1 parent e6584ed commit 56a7167

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import React, { useState } from 'react';
2+
3+
interface VariableQueryProps {
4+
query: string;
5+
onChange: (query: string, definition: string) => void;
6+
}
7+
8+
export const VariableQueryEditor = ({ onChange, query }: VariableQueryProps) => {
9+
const [state, setState] = useState(query);
10+
11+
const saveQuery = () => {
12+
onChange(state, state);
13+
};
14+
15+
const handleChange = (event: React.FormEvent<HTMLInputElement>) =>
16+
setState(event.currentTarget.value);
17+
18+
return (
19+
<>
20+
<div className="gf-form">
21+
<span className="gf-form-label width-10">Query</span>
22+
<input
23+
name="rawQuery"
24+
className="gf-form-input"
25+
onBlur={saveQuery}
26+
onChange={handleChange}
27+
value={state}
28+
/>
29+
</div>
30+
</>
31+
);
32+
};
33+

src/datasource.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
DataFrame,
99
FieldType,
1010
guessFieldTypeFromValue,
11+
MetricFindValue
1112
} from '@grafana/data';
1213
import { lastValueFrom, of } from 'rxjs';
1314
import { catchError, map } from 'rxjs/operators';
@@ -89,6 +90,30 @@ export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
8990
};
9091
}
9192

93+
async metricFindQuery(query: string, options?: any): Promise<MetricFindValue[]> {
94+
const to = new Date();
95+
const from = new Date();
96+
from.setFullYear(to.getFullYear() - 1);
97+
98+
options = options || {};
99+
options.range = options.range || {
100+
from: from,
101+
to: to
102+
}
103+
104+
options.targets = [];
105+
options.targets.push({ queryText: query, scopedVars: {} });
106+
107+
const response = await this.query(options);
108+
109+
return response.data.map((dataFrame) => {
110+
return dataFrame.fields[0].values.toArray();
111+
}
112+
).flat().map((value) => {
113+
return { text: value };
114+
});
115+
}
116+
92117
arrayToDataFrame(array: any[]): DataFrame {
93118
let dataFrame: MutableDataFrame = new MutableDataFrame();
94119

src/module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import { DataSourcePlugin } from '@grafana/data';
22
import { DataSource } from './datasource';
33
import { ConfigEditor } from './components/ConfigEditor';
44
import { QueryEditor } from './components/QueryEditor';
5+
import { VariableQueryEditor } from './components/VariableQueryEditor';
56
import { MyQuery, MyDataSourceOptions } from './types';
67

78
export const plugin = new DataSourcePlugin<DataSource, MyQuery, MyDataSourceOptions>(DataSource)
89
.setConfigEditor(ConfigEditor)
9-
.setQueryEditor(QueryEditor);
10+
.setQueryEditor(QueryEditor)
11+
.setVariableQueryEditor(VariableQueryEditor);

0 commit comments

Comments
 (0)