AnnotationQueryEditor.tsx 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import React, { ChangeEvent } from 'react';
  2. import { QueryEditorProps } from '@grafana/data';
  3. import { EditorField, EditorHeader, EditorRow, InlineSelect, Space, EditorSwitch } from '@grafana/experimental';
  4. import { Alert, Input } from '@grafana/ui';
  5. import { CloudWatchDatasource } from '../datasource';
  6. import { isCloudWatchAnnotationQuery } from '../guards';
  7. import { useRegions } from '../hooks';
  8. import { CloudWatchJsonData, CloudWatchQuery, MetricStat } from '../types';
  9. import { MetricStatEditor } from './MetricStatEditor';
  10. export type Props = QueryEditorProps<CloudWatchDatasource, CloudWatchQuery, CloudWatchJsonData>;
  11. export const AnnotationQueryEditor = (props: Props) => {
  12. const { query, onChange, datasource } = props;
  13. const [regions, regionIsLoading] = useRegions(datasource);
  14. if (!isCloudWatchAnnotationQuery(query)) {
  15. return (
  16. <Alert severity="error" title="Invalid annotation query" topSpacing={2}>
  17. {JSON.stringify(query, null, 4)}
  18. </Alert>
  19. );
  20. }
  21. return (
  22. <>
  23. <EditorHeader>
  24. <InlineSelect
  25. label="Region"
  26. value={regions.find((v) => v.value === query.region)}
  27. placeholder="Select region"
  28. allowCustomValue
  29. onChange={({ value: region }) => region && onChange({ ...query, region })}
  30. options={regions}
  31. isLoading={regionIsLoading}
  32. />
  33. </EditorHeader>
  34. <Space v={0.5} />
  35. <MetricStatEditor
  36. {...props}
  37. refId={query.refId}
  38. metricStat={query}
  39. disableExpressions={true}
  40. onChange={(metricStat: MetricStat) => onChange({ ...query, ...metricStat })}
  41. onRunQuery={() => {}}
  42. ></MetricStatEditor>
  43. <Space v={0.5} />
  44. <EditorRow>
  45. <EditorField label="Period" width={26} tooltip="Minimum interval between points in seconds.">
  46. <Input
  47. value={query.period || ''}
  48. placeholder="auto"
  49. onChange={(event: ChangeEvent<HTMLInputElement>) => onChange({ ...query, period: event.target.value })}
  50. />
  51. </EditorField>
  52. <EditorField label="Enable Prefix Matching" optional={true}>
  53. <EditorSwitch
  54. value={query.prefixMatching}
  55. onChange={(e) => {
  56. onChange({
  57. ...query,
  58. prefixMatching: e.currentTarget.checked,
  59. });
  60. }}
  61. />
  62. </EditorField>
  63. <EditorField label="Action" optional={true} disabled={!query.prefixMatching}>
  64. <Input
  65. value={query.actionPrefix || ''}
  66. onChange={(event: ChangeEvent<HTMLInputElement>) =>
  67. onChange({ ...query, actionPrefix: event.target.value })
  68. }
  69. />
  70. </EditorField>
  71. <EditorField label="Alarm Name" optional={true} disabled={!query.prefixMatching}>
  72. <Input
  73. value={query.alarmNamePrefix || ''}
  74. onChange={(event: ChangeEvent<HTMLInputElement>) =>
  75. onChange({ ...query, alarmNamePrefix: event.target.value })
  76. }
  77. />
  78. </EditorField>
  79. </EditorRow>
  80. </>
  81. );
  82. };