2019-01-17 13:02:43 -06:00
|
|
|
// Libraries
|
2019-01-17 10:37:34 -06:00
|
|
|
import React, { PureComponent } from 'react';
|
2019-01-17 13:02:43 -06:00
|
|
|
import _ from 'lodash';
|
2019-01-17 10:37:34 -06:00
|
|
|
|
2019-01-17 13:02:43 -06:00
|
|
|
// Services & Utils
|
2019-06-03 10:55:59 -05:00
|
|
|
import { getBackendSrv } from '@grafana/runtime';
|
2019-01-17 13:02:43 -06:00
|
|
|
|
|
|
|
// Components
|
2020-04-21 04:42:21 -05:00
|
|
|
import { InlineFormLabel, LegacyForms } from '@grafana/ui';
|
2020-04-02 03:57:35 -05:00
|
|
|
const { Select } = LegacyForms;
|
2019-10-31 04:48:05 -05:00
|
|
|
import { QueryEditorProps, SelectableValue } from '@grafana/data';
|
2019-01-17 13:02:43 -06:00
|
|
|
|
|
|
|
// Types
|
2019-08-06 11:17:12 -05:00
|
|
|
import { TestDataDataSource } from './datasource';
|
2019-01-18 10:19:35 -06:00
|
|
|
import { TestDataQuery, Scenario } from './types';
|
2019-01-17 13:02:43 -06:00
|
|
|
|
|
|
|
interface State {
|
|
|
|
scenarioList: Scenario[];
|
|
|
|
current: Scenario | null;
|
2019-01-17 10:37:34 -06:00
|
|
|
}
|
|
|
|
|
2019-08-06 11:17:12 -05:00
|
|
|
type Props = QueryEditorProps<TestDataDataSource, TestDataQuery>;
|
2019-01-18 10:19:35 -06:00
|
|
|
|
|
|
|
export class QueryEditor extends PureComponent<Props> {
|
2019-06-03 10:55:59 -05:00
|
|
|
backendSrv = getBackendSrv();
|
2019-01-17 13:02:43 -06:00
|
|
|
|
|
|
|
state: State = {
|
|
|
|
scenarioList: [],
|
|
|
|
current: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
async componentDidMount() {
|
2019-01-18 12:11:30 -06:00
|
|
|
const { query, datasource } = this.props;
|
2019-01-17 13:02:43 -06:00
|
|
|
|
|
|
|
query.scenarioId = query.scenarioId || 'random_walk';
|
|
|
|
|
2020-01-21 03:08:07 -06:00
|
|
|
// const scenarioList = await backendSrv.get('/api/tsdb/testdata/scenarios');
|
2019-01-18 12:11:30 -06:00
|
|
|
const scenarioList = await datasource.getScenarios();
|
2019-04-15 05:11:52 -05:00
|
|
|
const current: any = _.find(scenarioList, { id: query.scenarioId });
|
2019-01-17 13:02:43 -06:00
|
|
|
|
|
|
|
this.setState({ scenarioList: scenarioList, current: current });
|
|
|
|
}
|
|
|
|
|
2019-07-16 13:40:23 -05:00
|
|
|
onScenarioChange = (item: SelectableValue<string>) => {
|
2019-01-23 10:44:22 -06:00
|
|
|
this.props.onChange({
|
|
|
|
...this.props.query,
|
2020-04-20 08:48:38 -05:00
|
|
|
scenarioId: item.value!,
|
2019-01-17 13:02:43 -06:00
|
|
|
});
|
2019-02-13 04:14:53 -06:00
|
|
|
};
|
2019-01-17 13:02:43 -06:00
|
|
|
|
2019-01-17 10:37:34 -06:00
|
|
|
render() {
|
2019-01-17 13:02:43 -06:00
|
|
|
const { query } = this.props;
|
|
|
|
const options = this.state.scenarioList.map(item => ({ label: item.name, value: item.id }));
|
|
|
|
const current = options.find(item => item.value === query.scenarioId);
|
|
|
|
|
2019-01-17 10:37:34 -06:00
|
|
|
return (
|
2019-01-17 13:02:43 -06:00
|
|
|
<div className="gf-form-inline">
|
|
|
|
<div className="gf-form">
|
2020-04-21 04:42:21 -05:00
|
|
|
<InlineFormLabel className="query-keyword" width={7}>
|
2019-01-17 13:02:43 -06:00
|
|
|
Scenario
|
2020-04-21 04:42:21 -05:00
|
|
|
</InlineFormLabel>
|
2019-01-17 13:02:43 -06:00
|
|
|
<Select options={options} value={current} onChange={this.onScenarioChange} />
|
|
|
|
</div>
|
|
|
|
</div>
|
2019-01-17 10:37:34 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|