mirror of
https://github.com/grafana/grafana.git
synced 2025-02-20 11:48:34 -06:00
* moving to data WIP * more refactoring * add missing test * mock full path * remove sinon from grafana-ui
67 lines
1.8 KiB
TypeScript
67 lines
1.8 KiB
TypeScript
// Libraries
|
|
import React, { PureComponent } from 'react';
|
|
import _ from 'lodash';
|
|
|
|
// Services & Utils
|
|
import { getBackendSrv } from '@grafana/runtime';
|
|
|
|
// Components
|
|
import { FormLabel, Select, SelectOptionItem } from '@grafana/ui';
|
|
|
|
// Types
|
|
import { QueryEditorProps } from '@grafana/ui';
|
|
import { TestDataDatasource } from './datasource';
|
|
import { TestDataQuery, Scenario } from './types';
|
|
|
|
interface State {
|
|
scenarioList: Scenario[];
|
|
current: Scenario | null;
|
|
}
|
|
|
|
type Props = QueryEditorProps<TestDataDatasource, TestDataQuery>;
|
|
|
|
export class QueryEditor extends PureComponent<Props> {
|
|
backendSrv = getBackendSrv();
|
|
|
|
state: State = {
|
|
scenarioList: [],
|
|
current: null,
|
|
};
|
|
|
|
async componentDidMount() {
|
|
const { query, datasource } = this.props;
|
|
|
|
query.scenarioId = query.scenarioId || 'random_walk';
|
|
|
|
// const scenarioList = await this.backendSrv.get('/api/tsdb/testdata/scenarios');
|
|
const scenarioList = await datasource.getScenarios();
|
|
const current: any = _.find(scenarioList, { id: query.scenarioId });
|
|
|
|
this.setState({ scenarioList: scenarioList, current: current });
|
|
}
|
|
|
|
onScenarioChange = (item: SelectOptionItem<string>) => {
|
|
this.props.onChange({
|
|
...this.props.query,
|
|
scenarioId: item.value,
|
|
});
|
|
};
|
|
|
|
render() {
|
|
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);
|
|
|
|
return (
|
|
<div className="gf-form-inline">
|
|
<div className="gf-form">
|
|
<FormLabel className="query-keyword" width={7}>
|
|
Scenario
|
|
</FormLabel>
|
|
<Select options={options} value={current} onChange={this.onScenarioChange} />
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
}
|