-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatasource.ts
More file actions
108 lines (98 loc) · 3.56 KB
/
datasource.ts
File metadata and controls
108 lines (98 loc) · 3.56 KB
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
import {
DataSourceInstanceSettings,
ScopedVars,
DataQueryRequest,
DataFrame,
Field,
getDefaultTimeRange,
} from '@grafana/data';
import { DataSourceWithBackend, getTemplateSrv } from '@grafana/runtime';
import { HaystackQuery, OpsQuery, HaystackDataSourceOptions, QueryType } from './types';
import { firstValueFrom } from 'rxjs';
import { HaystackVariableSupport } from 'HaystackVariableSupport';
export const queryTypes: QueryType[] = [
{ label: 'Eval', value: 'eval', apiRequirements: ['eval'], description: 'Evaluate an Axon expression' },
{ label: 'HisRead', value: 'hisRead', apiRequirements: ['hisRead'], description: 'Read the history of a point' },
{
label: 'HisRead via filter',
value: 'hisReadFilter',
apiRequirements: ['read', 'hisRead'],
description: 'Read the history of points found using a filter',
},
{ label: 'Read', value: 'read', apiRequirements: ['read'], description: 'Read the records matched by a filter' },
];
export class DataSource extends DataSourceWithBackend<HaystackQuery, HaystackDataSourceOptions> {
constructor(instanceSettings: DataSourceInstanceSettings<HaystackDataSourceOptions>) {
super(instanceSettings);
this.variables = new HaystackVariableSupport(this);
}
// Queries the available ops from the datasource and returns the queryTypes that are supported.
async loadOps(refId: string): Promise<QueryType[]> {
let opsRequest = this.opsRequest(refId);
let stream = this.query(opsRequest);
let result = await firstValueFrom(stream);
if (result?.state === 'Error') {
return [];
}
let frame = result?.data?.find((frame: DataFrame) => {
return frame.refId === refId;
});
let ops: string[] = [];
let defField = frame?.fields?.find((field: Field<[string]>) => {
return field.name === 'def';
});
if (defField != null) {
ops = defField.values.map((opSymbol: string) => {
if (opSymbol.startsWith('^op:')) {
return opSymbol.substring(4);
} else {
return opSymbol;
}
});
} else {
// Include back-support for old `ops` format, which uses "name", not "defs". Used by nhaystack
let nameField = frame?.fields?.find((field: Field<[string]>) => {
return field.name === 'name';
});
if (nameField != null) {
ops = nameField.values;
}
}
let availableQueryTypes = queryTypes.filter((queryType) => {
return queryType.apiRequirements.every((apiRequirement) => {
return (
ops.find((op) => {
return op === apiRequirement;
}) !== undefined
);
});
});
return availableQueryTypes;
}
applyTemplateVariables(query: HaystackQuery, scopedVars: ScopedVars): HaystackQuery {
return {
...query,
eval: getTemplateSrv().replace(query.eval, scopedVars, 'csv'),
hisRead: getTemplateSrv().replace(query.hisRead, scopedVars, 'csv'),
hisReadFilter: getTemplateSrv().replace(query.hisReadFilter, scopedVars, 'csv'),
read: getTemplateSrv().replace(query.read, scopedVars, 'csv'),
};
}
// Returns a DataQueryRequest that gets the available ops from the datasource
// This applies a bunch of defaults because it's not a time series query
private opsRequest(refId: string): DataQueryRequest<HaystackQuery> {
return {
requestId: 'ops',
dashboardUID: '0',
interval: '0',
intervalMs: 0,
panelId: 0,
range: getDefaultTimeRange(),
scopedVars: {},
targets: [new OpsQuery(refId)],
timezone: 'UTC',
app: 'ops',
startTime: 0,
};
}
}