AnnotationQueryEditor.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import React from 'react';
  2. import _ from 'lodash';
  3. import { Metrics } from './Metrics';
  4. import { Filter } from './Filter';
  5. import { AnnotationTarget } from '../types';
  6. import { AnnotationsHelp } from './AnnotationsHelp';
  7. export interface Props {
  8. onQueryChange: (target: AnnotationTarget) => void;
  9. target: AnnotationTarget;
  10. datasource: any;
  11. }
  12. interface State extends AnnotationTarget {
  13. [key: string]: any;
  14. }
  15. const DefaultTarget: State = {
  16. defaultProject: 'loading project...',
  17. metricType: '',
  18. filters: [],
  19. metricKind: '',
  20. valueType: '',
  21. refId: 'annotationQuery',
  22. title: '',
  23. text: '',
  24. };
  25. export class AnnotationQueryEditor extends React.Component<Props, State> {
  26. state: State = DefaultTarget;
  27. componentDidMount() {
  28. this.setState({
  29. ...this.props.target,
  30. });
  31. }
  32. onMetricTypeChange({ valueType, metricKind, type, unit }) {
  33. const { onQueryChange } = this.props;
  34. this.setState(
  35. {
  36. metricType: type,
  37. unit,
  38. valueType,
  39. metricKind,
  40. },
  41. () => {
  42. onQueryChange(this.state);
  43. }
  44. );
  45. }
  46. onChange(prop, value) {
  47. this.setState({ [prop]: value }, () => {
  48. this.props.onQueryChange(this.state);
  49. });
  50. }
  51. render() {
  52. const { defaultProject, metricType, filters, refId, title, text } = this.state;
  53. const { datasource } = this.props;
  54. return (
  55. <>
  56. <Metrics
  57. defaultProject={defaultProject}
  58. metricType={metricType}
  59. templateSrv={datasource.templateSrv}
  60. datasource={datasource}
  61. onChange={value => this.onMetricTypeChange(value)}
  62. >
  63. {metric => (
  64. <>
  65. <Filter
  66. filtersChanged={value => this.onChange('filters', value)}
  67. filters={filters}
  68. refId={refId}
  69. hideGroupBys={true}
  70. templateSrv={datasource.templateSrv}
  71. datasource={datasource}
  72. metricType={metric ? metric.type : ''}
  73. />
  74. </>
  75. )}
  76. </Metrics>
  77. <div className="gf-form gf-form-inline">
  78. <div className="gf-form">
  79. <span className="gf-form-label query-keyword width-9">Title</span>
  80. <input
  81. type="text"
  82. className="gf-form-input width-20"
  83. value={title}
  84. onChange={e => this.onChange('title', e.target.value)}
  85. />
  86. </div>
  87. <div className="gf-form">
  88. <span className="gf-form-label query-keyword width-9">Text</span>
  89. <input
  90. type="text"
  91. className="gf-form-input width-20"
  92. value={text}
  93. onChange={e => this.onChange('text', e.target.value)}
  94. />
  95. </div>
  96. <div className="gf-form gf-form--grow">
  97. <div className="gf-form-label gf-form-label--grow" />
  98. </div>
  99. </div>
  100. <AnnotationsHelp />
  101. </>
  102. );
  103. }
  104. }