ExpressionQueryEditor.tsx 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import React, { PureComponent } from 'react';
  2. import { DataSourceApi, QueryEditorProps, SelectableValue } from '@grafana/data';
  3. import { InlineField, Select } from '@grafana/ui';
  4. import { ClassicConditions } from './components/ClassicConditions';
  5. import { Math } from './components/Math';
  6. import { Reduce } from './components/Reduce';
  7. import { Resample } from './components/Resample';
  8. import { ExpressionQuery, ExpressionQueryType, gelTypes } from './types';
  9. import { getDefaults } from './utils/expressionTypes';
  10. type Props = QueryEditorProps<DataSourceApi<ExpressionQuery>, ExpressionQuery>;
  11. const labelWidth = 14;
  12. export class ExpressionQueryEditor extends PureComponent<Props> {
  13. onSelectExpressionType = (item: SelectableValue<ExpressionQueryType>) => {
  14. const { query, onChange } = this.props;
  15. onChange(getDefaults({ ...query, type: item.value! }));
  16. };
  17. renderExpressionType() {
  18. const { onChange, onRunQuery, query, queries } = this.props;
  19. const refIds = queries!.filter((q) => query.refId !== q.refId).map((q) => ({ value: q.refId, label: q.refId }));
  20. switch (query.type) {
  21. case ExpressionQueryType.math:
  22. return <Math onChange={onChange} query={query} labelWidth={labelWidth} onRunQuery={onRunQuery} />;
  23. case ExpressionQueryType.reduce:
  24. return <Reduce refIds={refIds} onChange={onChange} labelWidth={labelWidth} query={query} />;
  25. case ExpressionQueryType.resample:
  26. return <Resample query={query} labelWidth={labelWidth} onChange={onChange} refIds={refIds} />;
  27. case ExpressionQueryType.classic:
  28. return <ClassicConditions onChange={onChange} query={query} refIds={refIds} />;
  29. }
  30. }
  31. render() {
  32. const { query } = this.props;
  33. const selected = gelTypes.find((o) => o.value === query.type);
  34. return (
  35. <div>
  36. <InlineField label="Operation" labelWidth={labelWidth}>
  37. <Select options={gelTypes} value={selected} onChange={this.onSelectExpressionType} width={25} />
  38. </InlineField>
  39. {this.renderExpressionType()}
  40. </div>
  41. );
  42. }
  43. }