AnnotationQueryEditor.tsx 2.9 KB

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