QueryEditor.tsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 } = this.props;
  25. query.scenarioId = query.scenarioId || 'random_walk';
  26. const scenarioList = await this.backendSrv.get('/api/tsdb/testdata/scenarios');
  27. const current = _.find(scenarioList, { id: query.scenarioId });
  28. this.setState({ scenarioList: scenarioList, current: current });
  29. }
  30. onScenarioChange = (item: SelectOptionItem) => {
  31. this.props.onQueryChange({
  32. scenarioId: item.value,
  33. ...this.props.query
  34. });
  35. }
  36. render() {
  37. const { query } = this.props;
  38. const options = this.state.scenarioList.map(item => ({ label: item.name, value: item.id }));
  39. const current = options.find(item => item.value === query.scenarioId);
  40. return (
  41. <div className="gf-form-inline">
  42. <div className="gf-form">
  43. <FormLabel className="query-keyword" width={7}>
  44. Scenario
  45. </FormLabel>
  46. <Select options={options} value={current} onChange={this.onScenarioChange} />
  47. </div>
  48. </div>
  49. );
  50. }
  51. }