From 30f120fa63cb9ae53db5538c5ff956df8005e5c8 Mon Sep 17 00:00:00 2001 From: Ashley Harrison Date: Tue, 29 Jun 2021 10:26:55 +0100 Subject: [PATCH] Annotations: create React component, naive attempt at hooking together --- packages/grafana-data/src/types/datasource.ts | 1 + .../StandardAnnotationQueryEditor.tsx | 1 + .../grafana/components/AnnotationQuery.tsx | 102 ++++++++++++++++++ .../plugins/datasource/grafana/datasource.ts | 4 + 4 files changed, 108 insertions(+) create mode 100644 public/app/plugins/datasource/grafana/components/AnnotationQuery.tsx diff --git a/packages/grafana-data/src/types/datasource.ts b/packages/grafana-data/src/types/datasource.ts index 63159188674..1551dcc4d2f 100644 --- a/packages/grafana-data/src/types/datasource.ts +++ b/packages/grafana-data/src/types/datasource.ts @@ -354,6 +354,7 @@ export interface QueryEditorProps< TOptions extends DataSourceJsonData = DataSourceJsonData, TVQuery extends DataQuery = TQuery > { + annotation?: AnnotationQuery; datasource: DSType; query: TVQuery; onRunQuery: () => void; diff --git a/public/app/features/annotations/components/StandardAnnotationQueryEditor.tsx b/public/app/features/annotations/components/StandardAnnotationQueryEditor.tsx index 3a223a3185a..4bd8ef950c3 100644 --- a/public/app/features/annotations/components/StandardAnnotationQueryEditor.tsx +++ b/public/app/features/annotations/components/StandardAnnotationQueryEditor.tsx @@ -167,6 +167,7 @@ export default class StandardAnnotationQueryEditor extends PureComponent +
  • + Dashboard: This will fetch annotation and alert state changes for whole dashboard and show them only on the event + {"'"}s originating panel. +
  • +
  • Tags: This will fetch any annotation events that match the tags filter.
  • + +); + +const matchTooltipContent = + 'By default Grafana only shows annotations that match all tags in the query. Enabling this returns annotations that match any of the tags in the query.'; + +const tagsTooltipContent = ( +
    + A tag entered here as {"'"}foo{"'"} will match +
      +
    • + annotation tags {"'"}foo{"'"} +
    • +
    • + annotation key-value tags formatted as {"'"}foo:bar{"'"} +
    • +
    +
    +); + +const annotationTypes = [ + { label: 'Dashboard', value: GrafanaAnnotationType.Dashboard }, + { label: 'Tags', value: GrafanaAnnotationType.Tags }, +]; + +const limitOptions = [10, 50, 100, 200, 300, 500, 1000, 2000].map((limit) => ({ + label: String(limit), + value: limit, +})); + +interface Props { + annotation: GrafanaAnnotationQuery; + onChange: (newValue: GrafanaAnnotationQuery) => void; +} + +export default function AnnotationQuery({ annotation, onChange }: Props) { + const { limit = 100, matchAny = false, tags = [], type = GrafanaAnnotationType.Tags } = annotation; + + const onFilterByChange = (newValue: SelectableValue) => + onChange({ + ...annotation, + type: newValue.value!, + }); + + const onMaxLimitChange = (newValue: SelectableValue) => + onChange({ + ...annotation, + limit: newValue.value!, + }); + + const onMatchAnyChange = (newValue: React.ChangeEvent) => + onChange({ + ...annotation, + matchAny: newValue.target.checked, + }); + + const onTagsChange = (tags: string[]) => + onChange({ + ...annotation, + tags, + }); + + return ( +
    +
    + + + +
    +
    + {annotation.type === 'tags' && ( + <> + +
    + +
    +
    + + Promise.resolve([])} tags={tags} /> + + + )} +
    +
    + ); +} diff --git a/public/app/plugins/datasource/grafana/datasource.ts b/public/app/plugins/datasource/grafana/datasource.ts index 5ff3617edc6..4a2763275e5 100644 --- a/public/app/plugins/datasource/grafana/datasource.ts +++ b/public/app/plugins/datasource/grafana/datasource.ts @@ -14,12 +14,16 @@ import { GrafanaQuery, GrafanaAnnotationQuery, GrafanaAnnotationType, GrafanaQue import { getBackendSrv, getGrafanaLiveSrv, getTemplateSrv, toDataQueryResponse } from '@grafana/runtime'; import { Observable, of, merge } from 'rxjs'; import { map, catchError } from 'rxjs/operators'; +import AnnotationQuery from './components/AnnotationQuery'; let counter = 100; export class GrafanaDatasource extends DataSourceApi { constructor(instanceSettings: DataSourceInstanceSettings) { super(instanceSettings); + this.annotations = { + QueryEditor: AnnotationQuery, + }; } query(request: DataQueryRequest): Observable {