-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatasource.ts
51 lines (45 loc) · 1.62 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
import { DataSourceInstanceSettings, CoreApp, ScopedVars } from "@grafana/data";
import { DataSourceWithBackend, getTemplateSrv } from "@grafana/runtime";
import { CriblQuery, CriblDataSourceOptions, DEFAULT_QUERY } from "types";
export class CriblDataSource extends DataSourceWithBackend<CriblQuery, CriblDataSourceOptions> {
constructor(instanceSettings: DataSourceInstanceSettings<CriblDataSourceOptions>) {
super(instanceSettings);
}
getDefaultQuery(_: CoreApp): Partial<CriblQuery> {
return DEFAULT_QUERY;
}
applyTemplateVariables(criblQuery: CriblQuery, scopedVars: ScopedVars) {
switch (criblQuery.type) {
case 'adhoc':
return {
...criblQuery,
// You can use dashboard variables in your query
query: getTemplateSrv().replace(criblQuery.query, scopedVars),
};
case 'saved':
return {
...criblQuery,
// The savedSearchId can also be composed using dashboard variable(s)
savedSearchId: getTemplateSrv().replace(criblQuery.savedSearchId, scopedVars),
};
default: // exhaustive check
throw new Error(`Unexpected query type`);
}
}
filterQuery(query: CriblQuery): boolean {
return !query.hide && this.canRunQuery(query);
}
async loadSavedSearchIds() {
return await this.getResource('savedSearchIds');
}
private canRunQuery(criblQuery: CriblQuery): boolean {
switch (criblQuery.type) {
case 'adhoc':
return (criblQuery.query?.trim() ?? '').length > 0;
case 'saved':
return criblQuery.savedSearchId != null;
default:
return false;
}
}
}