Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/components/VariableQueryEditor.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React, { useState } from 'react';

interface VariableQueryProps {
query: string;
onChange: (query: string, definition: string) => void;
}

export const VariableQueryEditor = ({ onChange, query }: VariableQueryProps) => {
const [state, setState] = useState(query);

const saveQuery = () => {
onChange(state, state);
};

const handleChange = (event: React.FormEvent<HTMLInputElement>) =>
setState(event.currentTarget.value);

return (
<>
<div className="gf-form">
<span className="gf-form-label width-10">Query</span>
<input
name="rawQuery"
className="gf-form-input"
onBlur={saveQuery}
onChange={handleChange}
value={state}
/>
</div>
</>
);
};

25 changes: 25 additions & 0 deletions src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
DataFrame,
FieldType,
guessFieldTypeFromValue,
MetricFindValue
} from '@grafana/data';
import { lastValueFrom, of } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
Expand Down Expand Up @@ -89,6 +90,30 @@ export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
};
}

async metricFindQuery(query: string, options?: any): Promise<MetricFindValue[]> {
const to = new Date();
const from = new Date();
from.setFullYear(to.getFullYear() - 1);

options = options || {};
options.range = options.range || {
from: from,
to: to
}

options.targets = [];
options.targets.push({ queryText: query, scopedVars: {} });

const response = await this.query(options);

return response.data.map((dataFrame) => {
return dataFrame.fields[0].values.toArray();
}
).flat().map((value) => {
return { text: value };
});
}

arrayToDataFrame(array: any[]): DataFrame {
let dataFrame: MutableDataFrame = new MutableDataFrame();

Expand Down
4 changes: 3 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { DataSourcePlugin } from '@grafana/data';
import { DataSource } from './datasource';
import { ConfigEditor } from './components/ConfigEditor';
import { QueryEditor } from './components/QueryEditor';
import { VariableQueryEditor } from './components/VariableQueryEditor';
import { MyQuery, MyDataSourceOptions } from './types';

export const plugin = new DataSourcePlugin<DataSource, MyQuery, MyDataSourceOptions>(DataSource)
.setConfigEditor(ConfigEditor)
.setQueryEditor(QueryEditor);
.setQueryEditor(QueryEditor)
.setVariableQueryEditor(VariableQueryEditor);