Skip to content

Commit eb98ed3

Browse files
authored
replaced the deprecated variables editor (#453)
This PR replace the deprecated legacy variable editor to the StandardVariableSupport method.
1 parent a287845 commit eb98ed3

File tree

8 files changed

+107
-329
lines changed

8 files changed

+107
-329
lines changed

.changeset/four-drinks-argue.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'grafana-github-datasource': patch
3+
---
4+
5+
Removed unused annotations method (replaced with new annotations support in [#196](https://github.com/grafana/github-datasource/pull/196))

.changeset/tricky-parrots-marry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'grafana-github-datasource': patch
3+
---
4+
5+
Replaced the deprecated `setVariableQueryEditor` with `CustomVariableSupport`

src/DataSource.test.ts

Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,77 @@
1-
import { DataQueryResponse, DataSourceInstanceSettings, toDataFrame } from '@grafana/data';
2-
import { of } from 'rxjs';
1+
import { DataQueryRequest, DataQueryResponse, DataSourceInstanceSettings, toDataFrame } from '@grafana/data';
2+
import { lastValueFrom, of } from 'rxjs';
3+
import { GithubVariableSupport } from 'variables';
34
import { GitHubDataSource } from 'DataSource';
45
import type { GitHubVariableQuery } from 'types/query';
56

67
describe('DataSource', () => {
7-
describe('metricFindQuery', () => {
8+
describe('GithubVariableSupport', () => {
9+
const SAMPLE_RESPONSE_WITH_MULTIPLE_FIELDS = [
10+
toDataFrame({
11+
fields: [
12+
{ name: 'test', values: ['value1', 'value2'] },
13+
{ name: 'foo', values: ['foo1', 'foo2'] },
14+
{ name: 'bar', values: ['bar1', 'bar2'] },
15+
],
16+
}),
17+
];
818
it('should return empty array if data in response is empty array', async () => {
919
const ds = new GitHubDataSource({} as DataSourceInstanceSettings);
20+
const vs = new GithubVariableSupport(ds);
1021
const query = {} as GitHubVariableQuery;
11-
1222
jest.spyOn(ds, 'query').mockReturnValue(of({ data: [] }));
13-
const res = await ds.metricFindQuery(query, {});
14-
expect(res).toEqual([]);
23+
const res = await lastValueFrom(vs.query({ targets: [query] } as DataQueryRequest));
24+
expect(res?.data.map((d) => d.value)).toEqual([]);
25+
expect(res?.data.map((d) => d.text)).toEqual([]);
1526
});
16-
1727
it('should return empty array if no data in response', async () => {
1828
const ds = new GitHubDataSource({} as DataSourceInstanceSettings);
29+
const vs = new GithubVariableSupport(ds);
1930
const query = {} as GitHubVariableQuery;
20-
2131
jest.spyOn(ds, 'query').mockReturnValue(of({} as DataQueryResponse));
22-
const res = await ds.metricFindQuery(query, {});
23-
expect(res).toEqual([]);
32+
const res = await lastValueFrom(vs.query({ targets: [query] } as DataQueryRequest));
33+
expect(res?.data.map((d) => d.value)).toEqual([]);
34+
expect(res?.data.map((d) => d.text)).toEqual([]);
2435
});
25-
2636
it('should return array with values if response has data', async () => {
2737
const ds = new GitHubDataSource({} as DataSourceInstanceSettings);
38+
const vs = new GithubVariableSupport(ds);
2839
const query = { key: 'test', field: 'test' } as GitHubVariableQuery;
29-
30-
jest.spyOn(ds, 'query').mockReturnValue(
31-
of({
32-
data: [
33-
toDataFrame({
34-
fields: [{ name: 'test', values: ['value1', 'value2'] }],
35-
}),
36-
],
37-
} as DataQueryResponse)
38-
);
39-
const res = await ds.metricFindQuery(query, {});
40-
expect(res).toEqual([
41-
{ value: 'value1', text: 'value1' },
42-
{ value: 'value2', text: 'value2' },
43-
]);
40+
const data = [toDataFrame({ fields: [{ name: 'test', values: ['value1', 'value2'] }] })];
41+
jest.spyOn(ds, 'query').mockReturnValue(of({ data }));
42+
const res = await lastValueFrom(vs.query({ targets: [query] } as DataQueryRequest));
43+
expect(res?.data.map((d) => d.value)).toEqual(['value1', 'value2']);
44+
expect(res?.data.map((d) => d.text)).toEqual(['value1', 'value2']);
45+
});
46+
it('mapping of key', async () => {
47+
const ds = new GitHubDataSource({} as DataSourceInstanceSettings);
48+
const vs = new GithubVariableSupport(ds);
49+
const query = { key: 'foo' } as GitHubVariableQuery;
50+
const data = SAMPLE_RESPONSE_WITH_MULTIPLE_FIELDS;
51+
jest.spyOn(ds, 'query').mockReturnValue(of({ data }));
52+
const res = await lastValueFrom(vs.query({ targets: [query] } as DataQueryRequest));
53+
expect(res?.data.map((d) => d.value)).toEqual(['foo1', 'foo2']);
54+
expect(res?.data.map((d) => d.text)).toEqual(['foo1', 'foo2']);
55+
});
56+
it('mapping of key and field', async () => {
57+
const ds = new GitHubDataSource({} as DataSourceInstanceSettings);
58+
const vs = new GithubVariableSupport(ds);
59+
const query = { key: 'bar', field: 'foo' } as GitHubVariableQuery;
60+
const data = SAMPLE_RESPONSE_WITH_MULTIPLE_FIELDS;
61+
jest.spyOn(ds, 'query').mockReturnValue(of({ data }));
62+
const res = await lastValueFrom(vs.query({ targets: [query] } as DataQueryRequest));
63+
expect(res?.data.map((d) => d.value)).toEqual(['bar1', 'bar2']);
64+
expect(res?.data.map((d) => d.text)).toEqual(['foo1', 'foo2']);
65+
});
66+
it('mapping of field', async () => {
67+
const ds = new GitHubDataSource({} as DataSourceInstanceSettings);
68+
const vs = new GithubVariableSupport(ds);
69+
const query = { field: 'foo' } as GitHubVariableQuery;
70+
const data = SAMPLE_RESPONSE_WITH_MULTIPLE_FIELDS;
71+
jest.spyOn(ds, 'query').mockReturnValue(of({ data }));
72+
const res = await lastValueFrom(vs.query({ targets: [query] } as DataQueryRequest));
73+
expect(res?.data.map((d) => d.value)).toEqual(['foo1', 'foo2']);
74+
expect(res?.data.map((d) => d.text)).toEqual(['foo1', 'foo2']);
4475
});
4576
});
4677
});

src/DataSource.ts

Lines changed: 6 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,11 @@
1-
import {
2-
AnnotationEvent,
3-
DataFrame,
4-
DataFrameView,
5-
DataQueryRequest,
6-
DataQueryResponse,
7-
DataSourceInstanceSettings,
8-
LegacyMetricFindQueryOptions,
9-
MetricFindValue,
10-
ScopedVars,
11-
} from '@grafana/data';
1+
import { DataQueryRequest, DataQueryResponse, DataSourceInstanceSettings, ScopedVars } from '@grafana/data';
122
import { DataSourceWithBackend, getTemplateSrv } from '@grafana/runtime';
13-
import { replaceVariables } from './variables';
3+
import { replaceVariables, GithubVariableSupport } from './variables';
144
import { isValid } from './validation';
15-
import { getAnnotationsFromFrame } from 'common/annotationsFromDataFrame';
165
import { prepareAnnotation } from 'migrations';
17-
import { Observable } from 'rxjs';
6+
import { Observable, lastValueFrom } from 'rxjs';
187
import { trackRequest } from 'tracking';
19-
import type { GitHubQuery, GitHubVariableQuery } from './types/query';
8+
import type { GitHubQuery } from './types/query';
209
import type { GitHubDataSourceOptions } from './types/config';
2110

2211
export class GitHubDataSource extends DataSourceWithBackend<GitHubQuery, GitHubDataSourceOptions> {
@@ -27,6 +16,7 @@ export class GitHubDataSource extends DataSourceWithBackend<GitHubQuery, GitHubD
2716
this.annotations = {
2817
prepareAnnotation,
2918
};
19+
this.variables = new GithubVariableSupport(this);
3020
}
3121

3222
// Required by DataSourceApi. It executes queries based on the provided DataQueryRequest.
@@ -46,39 +36,6 @@ export class GitHubDataSource extends DataSourceWithBackend<GitHubQuery, GitHubD
4636
return replaceVariables(this.templateSrv, query, scoped);
4737
}
4838

49-
/**
50-
* Implemented as part of the DataSourceAPI. It allows the datasource to serve as a source of annotations for a dashboard.
51-
* @returns A promise that resolves to an array of AnnotationEvent objects representing the annotations for the dashboard.
52-
* @todo This is deprecated and it is recommended to use the `AnnotationSupport` feature for annotations.
53-
*/
54-
async annotationQuery(request: any): Promise<AnnotationEvent[]> {
55-
const { annotation } = request.annotation;
56-
57-
const query = {
58-
targets: [
59-
{
60-
...annotation,
61-
datasourceId: this.id,
62-
refId: this.name,
63-
},
64-
],
65-
range: request.range,
66-
interval: request.interval,
67-
} as DataQueryRequest<GitHubQuery>;
68-
69-
const res = await this.query(query).toPromise();
70-
71-
if (!res?.data?.length) {
72-
return [];
73-
}
74-
return getAnnotationsFromFrame(res.data[0], {
75-
field: {
76-
time: annotation.timeField, // or first time field
77-
text: annotation.field || 'name',
78-
},
79-
});
80-
}
81-
8239
// Used in VariableQueryEditor to get the choices for variables
8340
async getChoices(query: GitHubQuery): Promise<string[]> {
8441
const request = {
@@ -95,40 +52,11 @@ export class GitHubDataSource extends DataSourceWithBackend<GitHubQuery, GitHubD
9552
} as DataQueryRequest;
9653

9754
try {
98-
const res = await this.query(request).toPromise();
55+
const res = await lastValueFrom(this.query(request));
9956
const columns = (res?.data[0]?.fields || []).map((f: any) => f.name) || [];
10057
return columns;
10158
} catch (err) {
10259
return Promise.reject(err);
10360
}
10461
}
105-
106-
// Implemented as part of DataSourceAPI and used for template variable queries
107-
async metricFindQuery(query: GitHubVariableQuery, options: LegacyMetricFindQueryOptions): Promise<MetricFindValue[]> {
108-
const request = {
109-
targets: [
110-
{
111-
...query,
112-
refId: 'metricFindQuery',
113-
},
114-
],
115-
range: options.range,
116-
} as DataQueryRequest;
117-
try {
118-
const res = await this.query(request).toPromise();
119-
if (!res?.data?.length) {
120-
return [];
121-
}
122-
const view = new DataFrameView(res.data[0] as DataFrame);
123-
return view.map((item) => {
124-
const value = item[query.key || ''] || item[query.field || 'name'];
125-
return {
126-
value,
127-
text: item[query.field || 'name'],
128-
};
129-
});
130-
} catch (ex) {
131-
return Promise.reject(ex);
132-
}
133-
}
13462
}

src/common/annotationsFromDataFrame.test.ts

Lines changed: 0 additions & 85 deletions
This file was deleted.

0 commit comments

Comments
 (0)