mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Annotations: Fix types?
This commit is contained in:
parent
a82b5a0871
commit
6df0cae0c9
@ -1,7 +1,7 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { render, screen } from '@testing-library/react';
|
import { render, screen } from '@testing-library/react';
|
||||||
|
|
||||||
import { GrafanaAnnotationQuery, GrafanaAnnotationType } from '../types';
|
import { GrafanaAnnotationQuery, GrafanaAnnotationType, GrafanaQueryType } from '../types';
|
||||||
import AnnotationQueryEditor from './AnnotationQueryEditor';
|
import AnnotationQueryEditor from './AnnotationQueryEditor';
|
||||||
|
|
||||||
describe('AnnotationQueryEditor', () => {
|
describe('AnnotationQueryEditor', () => {
|
||||||
@ -11,11 +11,9 @@ describe('AnnotationQueryEditor', () => {
|
|||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
mockQuery = {
|
mockQuery = {
|
||||||
refId: 'Anno',
|
refId: 'Anno',
|
||||||
|
queryType: GrafanaQueryType.Annotations,
|
||||||
type: GrafanaAnnotationType.Tags,
|
type: GrafanaAnnotationType.Tags,
|
||||||
limit: 100,
|
limit: 100,
|
||||||
enable: true,
|
|
||||||
name: 'a name',
|
|
||||||
iconColor: 'a color',
|
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -31,7 +29,11 @@ describe('AnnotationQueryEditor', () => {
|
|||||||
expect(maxLimit).toBeInTheDocument();
|
expect(maxLimit).toBeInTheDocument();
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('when the query type is "Tags"', () => {
|
describe('when the query type is "Tags" and the tags array exists', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
mockQuery.tags = [];
|
||||||
|
});
|
||||||
|
|
||||||
it('has a "Match any" toggle', () => {
|
it('has a "Match any" toggle', () => {
|
||||||
render(<AnnotationQueryEditor query={mockQuery} onChange={mockOnChange} />);
|
render(<AnnotationQueryEditor query={mockQuery} onChange={mockOnChange} />);
|
||||||
const matchAny = screen.getByLabelText('Match any');
|
const matchAny = screen.getByLabelText('Match any');
|
||||||
|
@ -97,7 +97,7 @@ export default function AnnotationQueryEditor({ query, onChange }: Props) {
|
|||||||
/>
|
/>
|
||||||
</InlineField>
|
</InlineField>
|
||||||
</InlineFieldRow>
|
</InlineFieldRow>
|
||||||
{type === GrafanaAnnotationType.Tags && (
|
{type === GrafanaAnnotationType.Tags && tags && (
|
||||||
<InlineFieldRow>
|
<InlineFieldRow>
|
||||||
<InlineField label="Match any" labelWidth={18} tooltip={matchTooltipContent}>
|
<InlineField label="Match any" labelWidth={18} tooltip={matchTooltipContent}>
|
||||||
<InlineSwitch id="grafana-annotations__match-any" value={matchAny} onChange={onMatchAnyChange} />
|
<InlineSwitch id="grafana-annotations__match-any" value={matchAny} onChange={onMatchAnyChange} />
|
||||||
@ -105,7 +105,6 @@ export default function AnnotationQueryEditor({ query, onChange }: Props) {
|
|||||||
<InlineField label="Tags" labelWidth="auto" tooltip={tagsTooltipContent}>
|
<InlineField label="Tags" labelWidth="auto" tooltip={tagsTooltipContent}>
|
||||||
<TagFilter
|
<TagFilter
|
||||||
inputId="grafana-annotations__tags"
|
inputId="grafana-annotations__tags"
|
||||||
allowCustomValue
|
|
||||||
onChange={onTagsChange}
|
onChange={onTagsChange}
|
||||||
tagOptions={getAnnotationTags}
|
tagOptions={getAnnotationTags}
|
||||||
tags={tags}
|
tags={tags}
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import { AnnotationQueryRequest, DataSourceInstanceSettings, dateTime } from '@grafana/data';
|
import { DataSourceInstanceSettings, dateTime, RawTimeRange, TimeRange } from '@grafana/data';
|
||||||
|
|
||||||
import { backendSrv } from 'app/core/services/backend_srv'; // will use the version in __mocks__
|
import { backendSrv } from 'app/core/services/backend_srv'; // will use the version in __mocks__
|
||||||
import { GrafanaDatasource } from './datasource';
|
import { GrafanaDatasource } from './datasource';
|
||||||
import { GrafanaAnnotationQuery, GrafanaAnnotationType, GrafanaQuery } from './types';
|
import { GrafanaAnnotationQuery, GrafanaAnnotationType } from './types';
|
||||||
|
|
||||||
jest.mock('@grafana/runtime', () => ({
|
jest.mock('@grafana/runtime', () => ({
|
||||||
...((jest.requireActual('@grafana/runtime') as unknown) as object),
|
...((jest.requireActual('@grafana/runtime') as unknown) as object),
|
||||||
@ -79,7 +79,7 @@ describe('grafana data source', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
function setupAnnotationQueryOptions(annotation: Partial<GrafanaAnnotationQuery>, dashboard?: { id: number }) {
|
function setupAnnotationQueryOptions(annotation: Partial<GrafanaAnnotationQuery>, dashboard?: { id: number }) {
|
||||||
return ({
|
return {
|
||||||
annotation,
|
annotation,
|
||||||
dashboard,
|
dashboard,
|
||||||
range: {
|
range: {
|
||||||
@ -87,5 +87,10 @@ function setupAnnotationQueryOptions(annotation: Partial<GrafanaAnnotationQuery>
|
|||||||
to: dateTime(1432288401),
|
to: dateTime(1432288401),
|
||||||
},
|
},
|
||||||
rangeRaw: { from: 'now-24h', to: 'now' },
|
rangeRaw: { from: 'now-24h', to: 'now' },
|
||||||
} as unknown) as AnnotationQueryRequest<GrafanaQuery>;
|
} as {
|
||||||
|
range: TimeRange;
|
||||||
|
rangeRaw: RawTimeRange;
|
||||||
|
annotation: GrafanaAnnotationQuery;
|
||||||
|
dashboard: any;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
@ -2,29 +2,31 @@ import { from, merge, Observable, of } from 'rxjs';
|
|||||||
import { catchError, map } from 'rxjs/operators';
|
import { catchError, map } from 'rxjs/operators';
|
||||||
import { getBackendSrv, getGrafanaLiveSrv, getTemplateSrv, toDataQueryResponse } from '@grafana/runtime';
|
import { getBackendSrv, getGrafanaLiveSrv, getTemplateSrv, toDataQueryResponse } from '@grafana/runtime';
|
||||||
import {
|
import {
|
||||||
AnnotationQueryRequest,
|
AnnotationQuery,
|
||||||
DataQueryRequest,
|
DataQueryRequest,
|
||||||
DataQueryResponse,
|
DataQueryResponse,
|
||||||
DataSourceApi,
|
DataSourceApi,
|
||||||
DataSourceInstanceSettings,
|
DataSourceInstanceSettings,
|
||||||
isValidLiveChannelAddress,
|
isValidLiveChannelAddress,
|
||||||
parseLiveChannelAddress,
|
parseLiveChannelAddress,
|
||||||
|
RawTimeRange,
|
||||||
StreamingFrameOptions,
|
StreamingFrameOptions,
|
||||||
|
TimeRange,
|
||||||
toDataFrame,
|
toDataFrame,
|
||||||
} from '@grafana/data';
|
} from '@grafana/data';
|
||||||
|
|
||||||
import { GrafanaAnnotationQuery, GrafanaAnnotationType, GrafanaQuery, GrafanaQueryType } from './types';
|
import { GrafanaAnnotationQuery, GrafanaAnnotationType, GrafanaQueryType } from './types';
|
||||||
import AnnotationQueryEditor from './components/AnnotationQueryEditor';
|
import AnnotationQueryEditor from './components/AnnotationQueryEditor';
|
||||||
import { getDashboardSrv } from '../../../features/dashboard/services/DashboardSrv';
|
import { getDashboardSrv } from '../../../features/dashboard/services/DashboardSrv';
|
||||||
|
|
||||||
let counter = 100;
|
let counter = 100;
|
||||||
|
|
||||||
export class GrafanaDatasource extends DataSourceApi<GrafanaQuery> {
|
export class GrafanaDatasource extends DataSourceApi<GrafanaAnnotationQuery> {
|
||||||
constructor(instanceSettings: DataSourceInstanceSettings) {
|
constructor(instanceSettings: DataSourceInstanceSettings) {
|
||||||
super(instanceSettings);
|
super(instanceSettings);
|
||||||
this.annotations = {
|
this.annotations = {
|
||||||
QueryEditor: AnnotationQueryEditor,
|
QueryEditor: AnnotationQueryEditor,
|
||||||
prepareAnnotation(json: any): GrafanaAnnotationQuery {
|
prepareAnnotation(json: any): AnnotationQuery<GrafanaAnnotationQuery> {
|
||||||
// Previously, these properties lived outside of target
|
// Previously, these properties lived outside of target
|
||||||
// This should handle migrating them
|
// This should handle migrating them
|
||||||
json.target = json.target ?? {
|
json.target = json.target ?? {
|
||||||
@ -35,13 +37,13 @@ export class GrafanaDatasource extends DataSourceApi<GrafanaQuery> {
|
|||||||
}; // using spread syntax caused an infinite loop in StandardAnnotationQueryEditor
|
}; // using spread syntax caused an infinite loop in StandardAnnotationQueryEditor
|
||||||
return json;
|
return json;
|
||||||
},
|
},
|
||||||
prepareQuery(anno: GrafanaAnnotationQuery): GrafanaQuery {
|
prepareQuery(anno: AnnotationQuery<GrafanaAnnotationQuery>): GrafanaAnnotationQuery {
|
||||||
return { ...anno, refId: anno.name, queryType: GrafanaQueryType.Annotations };
|
return { ...anno.target!, refId: anno.name, queryType: GrafanaQueryType.Annotations };
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
query(request: DataQueryRequest<GrafanaQuery>): Observable<DataQueryResponse> {
|
query(request: DataQueryRequest<GrafanaAnnotationQuery>): Observable<DataQueryResponse> {
|
||||||
const queries: Array<Observable<DataQueryResponse>> = [];
|
const queries: Array<Observable<DataQueryResponse>> = [];
|
||||||
const templateSrv = getTemplateSrv();
|
const templateSrv = getTemplateSrv();
|
||||||
for (const target of request.targets) {
|
for (const target of request.targets) {
|
||||||
@ -53,7 +55,7 @@ export class GrafanaDatasource extends DataSourceApi<GrafanaQuery> {
|
|||||||
this.getAnnotations({
|
this.getAnnotations({
|
||||||
range: request.range,
|
range: request.range,
|
||||||
rangeRaw: request.range.raw,
|
rangeRaw: request.range.raw,
|
||||||
annotation: target.target as any,
|
annotation: target,
|
||||||
dashboard: getDashboardSrv().getCurrent(),
|
dashboard: getDashboardSrv().getCurrent(),
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
@ -109,7 +111,12 @@ export class GrafanaDatasource extends DataSourceApi<GrafanaQuery> {
|
|||||||
return Promise.resolve([]);
|
return Promise.resolve([]);
|
||||||
}
|
}
|
||||||
|
|
||||||
async getAnnotations(options: AnnotationQueryRequest<GrafanaQuery>): Promise<DataQueryResponse> {
|
async getAnnotations(options: {
|
||||||
|
range: TimeRange;
|
||||||
|
rangeRaw: RawTimeRange;
|
||||||
|
annotation: GrafanaAnnotationQuery;
|
||||||
|
dashboard: any;
|
||||||
|
}): Promise<DataQueryResponse> {
|
||||||
const templateSrv = getTemplateSrv();
|
const templateSrv = getTemplateSrv();
|
||||||
const annotation = (options.annotation as unknown) as GrafanaAnnotationQuery;
|
const annotation = (options.annotation as unknown) as GrafanaAnnotationQuery;
|
||||||
const params: any = {
|
const params: any = {
|
||||||
@ -154,7 +161,7 @@ export class GrafanaDatasource extends DataSourceApi<GrafanaQuery> {
|
|||||||
const annotations = await getBackendSrv().get(
|
const annotations = await getBackendSrv().get(
|
||||||
'/api/annotations',
|
'/api/annotations',
|
||||||
params,
|
params,
|
||||||
`grafana-data-source-annotations-${annotation.name}-${options.dashboard?.id}`
|
`grafana-data-source-annotations-${annotation.refId}-${options.dashboard?.id}`
|
||||||
);
|
);
|
||||||
return { data: [toDataFrame(annotations)] };
|
return { data: [toDataFrame(annotations)] };
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { AnnotationQuery, DataQuery } from '@grafana/data';
|
import { DataQuery } from '@grafana/data';
|
||||||
import { LiveDataFilter } from '@grafana/runtime';
|
import { LiveDataFilter } from '@grafana/runtime';
|
||||||
|
|
||||||
//----------------------------------------------
|
//----------------------------------------------
|
||||||
@ -32,7 +32,7 @@ export enum GrafanaAnnotationType {
|
|||||||
Tags = 'tags',
|
Tags = 'tags',
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface GrafanaAnnotationQuery extends AnnotationQuery<GrafanaQuery> {
|
export interface GrafanaAnnotationQuery extends GrafanaQuery {
|
||||||
type: GrafanaAnnotationType; // tags
|
type: GrafanaAnnotationType; // tags
|
||||||
limit: number; // 100
|
limit: number; // 100
|
||||||
tags?: string[];
|
tags?: string[];
|
||||||
|
Loading…
Reference in New Issue
Block a user