-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathuseGetChartData.ts
More file actions
116 lines (111 loc) · 2.44 KB
/
useGetChartData.ts
File metadata and controls
116 lines (111 loc) · 2.44 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
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 = [
{
group_by: ['chart', 'node'],
group_by_label: [],
aggregation: 'sum',
},
{
group_by: ['node'],
group_by_label: [],
aggregation: method,
},
];
break;
case 'dimension':
metrics = [
{
group_by: ['dimension'],
group_by_label: [],
aggregation: method,
},
];
break;
case 'instance':
metrics = [
{
group_by: ['chart', 'node'],
group_by_label: [],
aggregation: 'sum',
},
];
break;
default:
metrics = [
{
group_by: ['chart'],
group_by_label: groupBy,
aggregation: 'sum',
},
{
group_by: [],
group_by_label: groupBy,
aggregation: 'avg',
},
];
break;
}
const defaultScopeValue = [];
const defaultSelectorValue = ['*'];
const labels = filterBy && filterValue ? [`${filterBy}:${filterValue}`] : null;
return await Post({
path: `/v3/spaces/${spaceId}/rooms/${roomId}/data`,
baseUrl,
data: {
format: 'json2',
options: ['jsonwrap', 'flip', 'ms'],
scope: {
contexts: [contextId],
nodes,
dimensions,
labels: labels || defaultScopeValue,
},
selectors: {
contexts: ['*'],
nodes: ['*'],
instances: ['*'],
dimensions: dimensions.length ? dimensions : defaultSelectorValue,
labels: labels || defaultSelectorValue,
},
aggregations: {
metrics,
time: { time_group: group, time_resampling: 0 },
},
window: { after: from, before: to, points: 269 },
},
});
};