-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
140 lines (125 loc) · 4.76 KB
/
index.js
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
134
135
136
137
138
139
140
import React, { useEffect, useState } from 'react';
import dayjs from 'dayjs';
import { useSelector } from 'react-redux';
import { Tabs } from 'antd';
import GraphExpanded from './charts/GraphExpanded.jsx';
import { authHeader, handleError } from '../../../common/AuthHeader.js';
import StorageUsageHistoryCharts from './charts/StorageUsageHistoryCharts.jsx';
import Filters from './charts/Filters.jsx';
import CurrentClusterUsageCharts from './charts/CurrentClusterUsageCharts.jsx';
import ExportMenu from '../ExportMenu/ExportMenu.jsx';
import { getQueryParamsFromUrl, addQueriesToUrl } from '../../../common/AddQueryToUrl.js';
function ClusterUsage() {
const [clusterUsageHistory, setClusterUsageHistory] = useState({});
const [selectedCluster, setSelectedCluster] = useState(null);
const [historyDateRange, setHistoryDateRange] = useState([]);
const [activeTab, setActiveTab] = useState(null);
const [viewExpandedGraph, setViewExpandedGraph] = useState(false);
const [expandedGraphData, setExpandedGraphData] = useState([]);
const [clusterOptions, setClusterOptions] = useState([]);
const clusters = useSelector((state) => state.applicationReducer.clusters);
// Sets default cluster and cluster options when clusters from redux store are loaded
useEffect(() => {
const urlQueries = getQueryParamsFromUrl();
// If cluster id in url
if (urlQueries.clusterId) {
setSelectedCluster(urlQueries.clusterId);
}
// If no clusterId in url
if (clusters?.length && !urlQueries.clusterId) {
setSelectedCluster(clusters[0].id);
addQueriesToUrl({ queryName: 'clusterId', queryValue: clusters[0].id });
}
// If url has active tab, use that as active tab, else set "1" as active tab
if (urlQueries.activeTab) {
setActiveTab(urlQueries.activeTab);
} else {
setActiveTab('1');
addQueriesToUrl({ queryName: 'activeTab', queryValue: '1' });
}
// If date range in URL
if (urlQueries.historyDateRange) {
const { historyDateRange } = urlQueries;
const range = historyDateRange.split(',');
const startDate = range[0];
const endDate = range[1];
setHistoryDateRange([dayjs(new Date(startDate)), dayjs(new Date(endDate))]);
} else {
setHistoryDateRange([dayjs().subtract(30, 'days'), dayjs()]);
}
}, []);
useEffect(() => {
if (clusters) {
//Options to display in drop down
const options = clusters.map((cluster) => ({
label: cluster.name,
value: cluster.id,
}));
setClusterOptions(options);
}
}, [clusters]);
//Handle tab switching - TODO pass this as props
const handleTabSwitching = (tab) => {
addQueriesToUrl({ queryName: 'activeTab', queryValue: tab });
setActiveTab(tab);
};
// When the component loads || cluster selection changes || date range changes
useEffect(() => {
if (selectedCluster && historyDateRange) getClusterUsageHistory(selectedCluster, historyDateRange);
}, [selectedCluster, historyDateRange]);
//Get usage history func
const getClusterUsageHistory = async (clusterId) => {
try {
const payload = {
method: 'GET',
header: authHeader(),
};
//Query data
const queryData = JSON.stringify({ clusterId, historyDateRange });
const response = await fetch(`/api/cluster/clusterStorageHistory/${queryData}`, payload);
if (!response.ok) handleError(response);
const data = await response.json();
setClusterUsageHistory(data);
} catch (err) {
setClusterUsageHistory([]);
handleError(err);
}
};
return (
<Tabs
tabBarExtraContent={<ExportMenu selectedCluster={selectedCluster} />}
activeKey={activeTab}
type="card"
onChange={handleTabSwitching}>
<Tabs.TabPane tab="Usage history" key="1">
<Filters
setSelectedCluster={setSelectedCluster}
selectedCluster={selectedCluster}
setHistoryDateRange={setHistoryDateRange}
historyDateRange={historyDateRange}
clusterOptions={clusterOptions}
/>
<StorageUsageHistoryCharts
clusterUsageHistory={clusterUsageHistory}
setViewExpandedGraph={setViewExpandedGraph}
setExpandedGraphData={setExpandedGraphData}
/>
{viewExpandedGraph ? (
<GraphExpanded
setViewExpandedGraph={setViewExpandedGraph}
clusterUsageHistory={expandedGraphData}
selectedCluster={selectedCluster}
/>
) : null}
</Tabs.TabPane>
<Tabs.TabPane tab="Current usage" key="2">
<CurrentClusterUsageCharts
setActiveTab={setActiveTab}
selectedCluster={selectedCluster}
setSelectedCluster={setSelectedCluster}
/>
</Tabs.TabPane>
</Tabs>
);
}
export default ClusterUsage;