-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathuseGetChartData.ts
More file actions
81 lines (76 loc) · 1.95 KB
/
useGetChartData.ts
File metadata and controls
81 lines (76 loc) · 1.95 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
import { GroupByList, Methods } from './../constants';
import { Post } from 'shared/utils/request';
type UseGetChartDataType = {
from: number;
to: number;
spaceId?: string;
roomId?: string;
nodes?: string[];
dimensions?: string[];
contextId?: string;
groupBy?: string;
method?: string;
group?: string;
filterBy?: string;
filterValue?: string;
baseUrl: string;
};
export const useGetChartData = async ({
baseUrl,
roomId,
nodes = [],
spaceId,
contextId,
filterBy,
filterValue,
groupBy = GroupByList[0].value,
method = Methods[0].value,
group = 'average',
dimensions = [],
from,
to,
}: UseGetChartDataType) => {
let metrics = [];
switch (groupBy) {
case 'node':
metrics = [{ aggregation: method, group_by: ['node'], group_by_label: [] }];
break;
case 'dimension':
metrics = [{ group_by: ['dimension'], group_by_label: [], aggregation: method }];
break;
case 'instance':
metrics = [{ aggregation: method, group_by: ['instance'], group_by_label: [] }];
break;
default:
metrics = [{ aggregation: method, group_by: ['label'], group_by_label: [groupBy] }];
break;
}
const defaultSelectorValue = ['*'];
const labels = filterBy && filterValue ? [`${filterBy}:${filterValue}`] : [];
return await Post({
path: `/v3/spaces/${spaceId}/rooms/${roomId}/data`,
baseUrl,
data: {
format: 'json2',
options: ['jsonwrap', 'flip', 'ms'],
scope: {
contexts: [contextId],
nodes,
dimensions,
labels,
},
selectors: {
contexts: ['*'],
nodes: ['*'],
instances: ['*'],
dimensions: dimensions.length ? dimensions : defaultSelectorValue,
labels: labels.length ? labels : defaultSelectorValue,
},
aggregations: {
metrics,
time: { time_group: group, time_resampling: 0 },
},
window: { after: from, before: to, points: 269 },
},
});
};