-
Notifications
You must be signed in to change notification settings - Fork 54
/
DataSource.ts
133 lines (121 loc) · 4.03 KB
/
DataSource.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import {
AnnotationEvent,
DataFrame,
DataFrameView,
DataQueryRequest,
DataQueryResponse,
DataSourceInstanceSettings,
LegacyMetricFindQueryOptions,
MetricFindValue,
ScopedVars,
} from '@grafana/data';
import { DataSourceWithBackend, getTemplateSrv } from '@grafana/runtime';
import { GithubDataSourceOptions, GitHubQuery, GitHubVariableQuery } from './types';
import { replaceVariables } from './variables';
import { isValid } from './validation';
import { getAnnotationsFromFrame } from 'common/annotationsFromDataFrame';
import { prepareAnnotation } from 'migrations';
import { Observable } from 'rxjs';
import { trackRequest } from 'tracking';
export class GithubDataSource extends DataSourceWithBackend<GitHubQuery, GithubDataSourceOptions> {
templateSrv = getTemplateSrv();
constructor(instanceSettings: DataSourceInstanceSettings<GithubDataSourceOptions>) {
super(instanceSettings);
this.annotations = {
prepareAnnotation,
};
}
// Required by DataSourceApi. It executes queries based on the provided DataQueryRequest.
query(request: DataQueryRequest<GitHubQuery>): Observable<DataQueryResponse> {
trackRequest(request);
return super.query(request);
}
// Implemented as a part of DataSourceApi
// Only execute queries that have a query type
filterQuery = (query: GitHubQuery) => {
return isValid(query) && !query.hide;
};
// Implemented as a part of DataSourceApi. Interpolates variables and adds ad hoc filters to a list of Github queries.
applyTemplateVariables(query: GitHubQuery, scoped: ScopedVars): GitHubQuery {
return replaceVariables(this.templateSrv, query, scoped);
}
/**
* Implemented as part of the DataSourceAPI. It allows the datasource to serve as a source of annotations for a dashboard.
* @returns A promise that resolves to an array of AnnotationEvent objects representing the annotations for the dashboard.
* @todo This is deprecated and it is recommended to use the `AnnotationSupport` feature for annotations.
*/
async annotationQuery(request: any): Promise<AnnotationEvent[]> {
const { annotation } = request.annotation;
const query = {
targets: [
{
...annotation,
datasourceId: this.id,
refId: this.name,
},
],
range: request.range,
interval: request.interval,
} as DataQueryRequest<GitHubQuery>;
const res = await this.query(query).toPromise();
if (!res?.data?.length) {
return [];
}
return getAnnotationsFromFrame(res.data[0], {
field: {
time: annotation.timeField, // or first time field
text: annotation.field || 'name',
},
});
}
// Used in VariableQueryEditor to get the choices for variables
async getChoices(query: GitHubQuery): Promise<string[]> {
const request = {
targets: [
{
...query,
refId: 'metricFindQuery',
},
],
range: {
to: {},
from: {},
},
} as DataQueryRequest;
try {
const res = await this.query(request).toPromise();
const columns = (res?.data[0]?.fields || []).map((f: any) => f.name) || [];
return columns;
} catch (err) {
return Promise.reject(err);
}
}
// Implemented as part of DataSourceAPI and used for template variable queries
async metricFindQuery(query: GitHubVariableQuery, options: LegacyMetricFindQueryOptions): Promise<MetricFindValue[]> {
const request = {
targets: [
{
...query,
refId: 'metricFindQuery',
},
],
range: options.range,
} as DataQueryRequest;
try {
const res = await this.query(request).toPromise();
if (!res?.data?.length) {
return [];
}
const view = new DataFrameView(res.data[0] as DataFrame);
return view.map((item) => {
const value = item[query.key || ''] || item[query.field || 'name'];
return {
value,
text: item[query.field || 'name'],
};
});
} catch (ex) {
return Promise.reject(ex);
}
}
}