-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdatasource.ts
More file actions
133 lines (119 loc) · 3.66 KB
/
datasource.ts
File metadata and controls
133 lines (119 loc) · 3.66 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
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 {
DataQueryRequest,
DataQueryResponse,
DataSourceApi,
DataSourceInstanceSettings,
MutableDataFrame,
FieldType,
} from '@grafana/data';
import { useGetChartData } from 'shared/hooks/useGetChartData';
import { Get } from 'shared/utils/request';
import { MyQuery, MyDataSourceOptions } from './shared/types';
import PubSub from 'pubsub-js';
export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
baseUrl: string;
constructor(instanceSettings: DataSourceInstanceSettings<MyDataSourceOptions>) {
super(instanceSettings);
this.baseUrl = instanceSettings.url!;
}
async query(options: DataQueryRequest<MyQuery>): Promise<DataQueryResponse> {
const { range } = options;
const from = range!.from.valueOf();
const to = range!.to.valueOf();
const promises = options.targets.map(
({
spaceId,
roomId,
contextId,
nodes,
groupBy,
method,
refId,
dimensions,
filterBy,
filterValue,
group,
hide,
}) => {
if (hide) {
return null;
}
if (!spaceId || !roomId || !contextId) {
const frame = new MutableDataFrame({
refId: refId,
fields: [
{ name: 'Time', type: FieldType.time },
{ name: 'Value', type: FieldType.number },
],
});
return Promise.resolve(frame);
}
return useGetChartData({
baseUrl: this.baseUrl,
spaceId,
roomId,
nodes,
contextId,
groupBy,
group,
filterBy,
filterValue,
method,
dimensions,
from: Math.floor(from / 1000), // this value in seconds
to: Math.floor(to / 1000), // this value in seconds
})
.then((response: any) => {
PubSub.publish('CHART_DATA', response);
const frame = new MutableDataFrame({
refId,
fields: response.data.result.labels.map((id: string, i: number) => {
const node = response.data.summary.nodes.find((n: any) => n.mg === id);
return {
name: node?.nm || id,
type: i === 0 ? FieldType.time : FieldType.number,
};
}),
});
const valueIndex = response.data.result.point.value;
response.data.result.data.forEach((point: any) => {
const [timestamp, ...rest] = point;
frame.appendRow([timestamp, ...rest.map((r: any[]) => r[valueIndex])]);
});
return frame;
})
.catch(() => {
return [];
});
}
);
return Promise.all(promises.filter(Boolean)).then((data) => ({ data }));
}
async testDatasource() {
const defaultErrorMessage = 'Cannot connect to API';
try {
const response = await Get({ path: '/v2/accounts/me', baseUrl: this.baseUrl });
if (response.status === 200 && response?.data?.id !== '00000000-0000-0000-0000-000000000000') {
return {
status: 'success',
message: 'Success',
};
} else {
return {
status: 'error',
message:
response.status === 401 || response?.data?.id !== '00000000-0000-0000-0000-000000000000'
? 'Invalid token. Please validate the token defined on the datasource.'
: response.statusText
? response.statusText
: defaultErrorMessage,
};
}
} catch (err) {
return {
status: 'error',
message: defaultErrorMessage,
};
}
}
}