AnnotationQueryEditor.tsx 3.1 KB

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