QueryEditor.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Libraries
  2. import React, { PureComponent } from 'react';
  3. import _ from 'lodash';
  4. // Services & Utils
  5. import { getBackendSrv, BackendSrv } from 'app/core/services/backend_srv';
  6. // Components
  7. import { FormLabel, Select, SelectOptionItem } from '@grafana/ui';
  8. // Types
  9. import { QueryEditorProps } from '@grafana/ui/src/types';
  10. import { TestDataDatasource } from './datasource';
  11. import { TestDataQuery, Scenario } from './types';
  12. interface State {
  13. scenarioList: Scenario[];
  14. current: Scenario | null;
  15. }
  16. type Props = QueryEditorProps<TestDataDatasource, TestDataQuery>;
  17. export class QueryEditor extends PureComponent<Props> {
  18. backendSrv: BackendSrv = getBackendSrv();
  19. state: State = {
  20. scenarioList: [],
  21. current: null,
  22. };
  23. async componentDidMount() {
  24. const { query, datasource } = this.props;
  25. query.scenarioId = query.scenarioId || 'random_walk';
  26. // const scenarioList = await this.backendSrv.get('/api/tsdb/testdata/scenarios');
  27. const scenarioList = await datasource.getScenarios();
  28. const current: any = _.find(scenarioList, { id: query.scenarioId });
  29. this.setState({ scenarioList: scenarioList, current: current });
  30. }
  31. onScenarioChange = (item: SelectOptionItem<string>) => {
  32. this.props.onChange({
  33. ...this.props.query,
  34. scenarioId: item.value,
  35. });
  36. };
  37. render() {
  38. const { query } = this.props;
  39. const options = this.state.scenarioList.map(item => ({ label: item.name, value: item.id }));
  40. const current = options.find(item => item.value === query.scenarioId);
  41. return (
  42. <div className="gf-form-inline">
  43. <div className="gf-form">
  44. <FormLabel className="query-keyword" width={7}>
  45. Scenario
  46. </FormLabel>
  47. <Select options={options} value={current} onChange={this.onScenarioChange} />
  48. </div>
  49. </div>
  50. );
  51. }
  52. }