-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Home.tsx
158 lines (145 loc) · 4.89 KB
/
Home.tsx
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import React, { SetStateAction, useEffect, useState } from 'react';
import { useAppSelector, useAppDispatch } from '../../Store/hooks';
import { storeClusterDbData } from '../../Store/actions';
import { clusterMetric, nodeMetric } from "../../Queries";
import { storeClusterQueryData } from "../../Store/actions";
import NavBar from './NavBar';
import Kube from '../Cards/Kube';
import { Get } from '../../Services';
import { apiRoute } from '../../utils';
import { IReducers } from '../../Interfaces/IReducers';
import { IClusterMetrics } from "../../Interfaces/IAction";
import './styles.css';
import { ClusterTypes } from '../../Interfaces/ICluster';
const Home = () => {
const dispatch = useAppDispatch();
const clusterReducer = useAppSelector((state: IReducers) => state.clusterReducer);
const apiReducer = useAppSelector((state: IReducers) => state.apiReducer);
const [noClusterError, setNoClusterError] = useState('');
const [clustersArray, setClustersArray] = useState([]);
useEffect(() => {
const getClusterDbData = async () => {
const res = await Get(
apiRoute.getRoute('cluster'),
{
authorization: localStorage.getItem('token')
}
);
const user = await Get(
apiRoute.getRoute(`user:${localStorage.getItem('username')}`),
{
authorization: localStorage.getItem('token')
}
);
if (res.message) {
setNoClusterError('Please add cluster information in administrator portal');
} else {
dispatch(storeClusterDbData(res));
console.log(res);
setClustersArray(res);
}
setTimeout(() => getClusterDbData(), user.refreshRate);
};
if (apiReducer.initialLoad || (apiReducer.lastFetch + 3000 < new Date().getTime())) {
console.log(apiReducer.initialLoad);
getClusterDbData();
}
}, [clusterReducer.render]);
useEffect(() => {
const getClusterDbData = async () => {
const res = await Get(
apiRoute.getRoute('cluster'),
{
authorization: localStorage.getItem('token')
}
);
if (res.message) {
setNoClusterError('Please add cluster information in administrator portal');
} else {
dispatch(storeClusterDbData(res));
setClustersArray(res);
}
};
getClusterDbData();
}, [clusterReducer.favRender]);
useEffect(() => {
clustersArray.forEach(async (element: ClusterTypes) => {
const metrics: IClusterMetrics = {
allNodes: '',
cpuLoad: 0,
memoryLoad: 0,
totalDeployments: '',
totalPods: '',
allNamespaces: '',
allServices: '',
allNameList: ''
};
const fetchNodes = async () => {
const res = await clusterMetric.allNodes(element._id, "k8");
if (res) metrics.allNodes = res;
};
await fetchNodes();
const fetchCpuUsage = async () => {
const res = await nodeMetric.cpuLoad(element._id, "k8");
if (res) metrics.cpuLoad = res;
};
await fetchCpuUsage();
const fetchMemoryUsage = async () => {
const res = await clusterMetric.memoryLoad(element._id, "k8");
if (res) metrics.memoryLoad = Number((res / 1000000).toFixed(1));
};
await fetchMemoryUsage();
const fetchTotalDeployments = async () => {
const res = await clusterMetric.totalDeployments(element._id, "k8");
if (res) metrics.totalDeployments = res;
};
await fetchTotalDeployments();
const fetchTotalPods = async () => {
const res = await clusterMetric.totalPods(element._id, "k8");
if (res) metrics.totalPods = res;
};
await fetchTotalPods();
const fetchNamespaces = async () => {
const res = await clusterMetric.allNamespaces(element._id, "k8");
if (res) metrics.allNamespaces = res;
};
await fetchNamespaces();
const fetchServices = async () => {
const res = await clusterMetric.allServices(element._id, "k8");
if (res) metrics.allServices = res;
};
await fetchServices();
dispatch(storeClusterQueryData(element._id, metrics));
});
}, [clustersArray]);
const favClusters: JSX.Element[] = [];
const nonFavClusters: JSX.Element[] = [];
apiReducer.clusterDbData.forEach((element, idx) => {
if (element.favorite?.includes(localStorage.getItem('userId') as string)) {
favClusters.push(<Kube
key={'fav' + idx}
_id={element._id}
favorite={element.favorite}
favoriteStatus={true}
/>);
} else {
nonFavClusters.push(<Kube
key={'nonFav' + idx}
_id={element._id}
favorite={element.favorite}
favoriteStatus={false}
/>);
}
});
return (
<div className="Kube-port">
<div className="Kube-container">
{favClusters}
{nonFavClusters}
</div>
{noClusterError}
<NavBar />
</div>
);
};
export default Home;