Skip to content
This repository was archived by the owner on Mar 1, 2024. It is now read-only.

Commit 7665c50

Browse files
rawagnermareklibra
authored andcommitted
Streamline fetching data for dashboards
1 parent 7028705 commit 7665c50

File tree

1 file changed

+33
-120
lines changed

1 file changed

+33
-120
lines changed

frontend/public/kubevirt/components/cluster/cluster-overview.jsx

Lines changed: 33 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -86,36 +86,15 @@ const getAlertManagerBaseURL = () => window.SERVER_FLAGS.alertManagerBaseURL;
8686
export class ClusterOverview extends React.Component {
8787
constructor(props){
8888
super(props);
89-
this.messages = {};
90-
9189
this.state = {
92-
openshiftClusterVersions: null,
9390
healthData: {
9491
data: {},
9592
loaded: false,
9693
},
97-
consumersData: {},
98-
capacityData: {},
99-
utilizationData: {},
100-
diskStats: {},
10194
};
10295

103-
this.setConsumersData = this._setConsumersData.bind(this);
10496
this.setHealthData = this._setHealthData.bind(this);
105-
this.setDetailsOpenshiftResponse = this._setDetailsOpenshiftResponse.bind(this);
106-
this.setCapacityData = this._setCapacityData.bind(this);
107-
this.setUtilizationData = this._setUtilizationData.bind(this);
10897
this.getStorageMetrics = this._getStorageMetrics.bind(this);
109-
this.setCephDiskStats = this._setCephDiskStats.bind(this);
110-
}
111-
112-
_setConsumersData(key, response) {
113-
this.setState(state => ({
114-
consumersData: {
115-
...state.consumersData,
116-
[key]: response,
117-
},
118-
}));
11998
}
12099

121100
_setHealthData(healthy, message) {
@@ -130,40 +109,10 @@ export class ClusterOverview extends React.Component {
130109
});
131110
}
132111

133-
_setDetailsOpenshiftResponse(response) {
134-
let openshiftClusterVersions = _.get(response, 'data.result', []);
135-
if (!Array.isArray(openshiftClusterVersions)){ // only one node
136-
openshiftClusterVersions = [openshiftClusterVersions];
137-
}
138-
139-
this.setState({
140-
openshiftClusterVersions,
141-
});
142-
}
143-
144-
_setCapacityData(key, response) {
145-
this.setState(state => ({
146-
capacityData: {
147-
...state.capacityData,
148-
[key]: response,
149-
},
150-
}));
151-
}
152-
153-
_setUtilizationData(key, response) {
154-
this.setState(state => ({
155-
utilizationData: {
156-
...state.utilizationData,
157-
[key]: response,
158-
},
159-
}));
160-
}
161-
162112
async _getStorageMetrics() {
113+
let queryTotal = CAPACITY_STORAGE_TOTAL_DEFAULT_QUERY;
114+
let queryUsed = UTILIZATION_STORAGE_USED_DEFAULT_QUERY;
163115
try {
164-
let queryTotal = CAPACITY_STORAGE_TOTAL_DEFAULT_QUERY;
165-
let queryUsed = UTILIZATION_STORAGE_USED_DEFAULT_QUERY;
166-
167116
const metrics = await this.getPrometheusMetrics();
168117
if (_.get(metrics, 'data', []).find(metric => metric === CAPACITY_STORAGE_TOTAL_BASE_CEPH_METRIC)) {
169118
const cephData = await this.getPrometheusQuery(CAPACITY_STORAGE_TOTAL_QUERY);
@@ -172,26 +121,12 @@ export class ClusterOverview extends React.Component {
172121
queryUsed = UTILIZATION_STORAGE_USED_QUERY;
173122
}
174123
}
175-
176-
this.fetchPrometheusQuery(queryTotal, response => this.setCapacityData('storageTotal', response));
177-
this.fetchPrometheusQuery(queryUsed, response => this.setUtilizationData('storageUsed', response));
178-
} catch (error) {
179-
if (this._isMounted) {
180-
this.setCapacityData('storageTotal', error);
181-
this.setUtilizationData('storageUsed', error);
182-
}
124+
} finally {
125+
this.fetchPrometheusQuery(queryTotal, 'storageTotal');
126+
this.fetchPrometheusQuery(queryUsed, 'storageUsed');
183127
}
184128
}
185129

186-
_setCephDiskStats(key, response) {
187-
this.setState(state => ({
188-
diskStats: {
189-
...state.diskStats,
190-
[key]: response,
191-
},
192-
}));
193-
}
194-
195130
async getPrometheusMetrics() {
196131
const url = `${getPrometheusBaseURL()}/api/v1/label/__name__/values`;
197132
return coFetchJSON(url);
@@ -202,20 +137,9 @@ export class ClusterOverview extends React.Component {
202137
return coFetchJSON(url);
203138
}
204139

205-
fetchPrometheusQuery(query, callback) {
206-
this.getPrometheusQuery(query).then(result => {
207-
if (this._isMounted) {
208-
callback(result);
209-
}
210-
}).catch(error => {
211-
if (this._isMounted) {
212-
callback(error);
213-
}
214-
}).then(() => {
215-
if (this._isMounted) {
216-
setTimeout(() => this.fetchPrometheusQuery(query, callback), REFRESH_TIMEOUT);
217-
}
218-
});
140+
fetchPrometheusQuery(query, key) {
141+
const url = `${getPrometheusBaseURL()}/api/v1/query?query=${encodeURIComponent(query)}`;
142+
this.fetchAndStore(url, key);
219143
}
220144

221145
fetchHealth(callback) {
@@ -234,45 +158,51 @@ export class ClusterOverview extends React.Component {
234158
});
235159
}
236160

237-
async fetchAlerts() {
161+
fetchAlerts() {
238162
const url = `${getAlertManagerBaseURL()}/api/v2/alerts`;
239-
let alertsResponse;
163+
this.fetchAndStore(url, 'alertsResponse');
164+
}
165+
166+
167+
async fetchAndStore(url, key) {
168+
let response;
240169
try {
241-
alertsResponse = await coFetchJSON(url);
170+
response = await coFetchJSON(url);
242171
} catch (error) {
243-
alertsResponse = error;
172+
response = error;
244173
} finally {
245174
if (this._isMounted) {
246175
this.setState({
247-
alertsResponse,
176+
[key]: response,
248177
});
249-
setTimeout(() => this.fetchAlerts(), REFRESH_TIMEOUT);
178+
setTimeout(() => this.fetchAndStore(url, key), REFRESH_TIMEOUT);
250179
}
251180
}
252181
}
253182

254183
componentDidMount() {
255184
this._isMounted = true;
256185

257-
this.fetchPrometheusQuery(CONSUMERS_CPU_QUERY, response => this.setConsumersData('workloadCpuResults', response));
258-
this.fetchPrometheusQuery(CONSUMERS_MEMORY_QUERY, response => this.setConsumersData('workloadMemoryResults', response));
259-
this.fetchPrometheusQuery(NODE_CONSUMERS_MEMORY_QUERY, response => this.setConsumersData('infraMemoryResults', response));
260-
this.fetchPrometheusQuery(NODE_CONSUMERS_CPU_QUERY, response => this.setConsumersData('infraCpuResults', response));
186+
this.fetchPrometheusQuery(CONSUMERS_CPU_QUERY, 'workloadCpuResults');
187+
this.fetchPrometheusQuery(CONSUMERS_MEMORY_QUERY, 'workloadMemoryResults');
188+
this.fetchPrometheusQuery(NODE_CONSUMERS_MEMORY_QUERY, 'infraMemoryResults');
189+
this.fetchPrometheusQuery(NODE_CONSUMERS_CPU_QUERY, 'infraCpuResults');
261190

262191
this.fetchHealth(this.setHealthData);
263-
this.fetchPrometheusQuery(OPENSHIFT_VERSION_QUERY, response => this.setDetailsOpenshiftResponse(response));
264192

265-
this.fetchPrometheusQuery(CAPACITY_MEMORY_TOTAL_QUERY, response => this.setCapacityData('memoryTotal', response));
266-
this.fetchPrometheusQuery(CAPACITY_NETWORK_TOTAL_QUERY, response => this.setCapacityData('networkTotal', response));
267-
this.fetchPrometheusQuery(CAPACITY_NETWORK_USED_QUERY, response => this.setCapacityData('networkUsed', response));
193+
this.fetchPrometheusQuery(OPENSHIFT_VERSION_QUERY, 'openshiftClusterVersionResponse');
194+
195+
this.fetchPrometheusQuery(CAPACITY_MEMORY_TOTAL_QUERY, 'memoryTotal');
196+
this.fetchPrometheusQuery(CAPACITY_NETWORK_TOTAL_QUERY, 'networkTotal');
197+
this.fetchPrometheusQuery(CAPACITY_NETWORK_USED_QUERY, 'networkUsed');
268198

269-
this.fetchPrometheusQuery(CEPH_OSD_UP_QUERY, response => this.setCephDiskStats('cephOsdUp', response));
270-
this.fetchPrometheusQuery(CEPH_OSD_DOWN_QUERY, response => this.setCephDiskStats('cephOsdDown', response));
199+
this.fetchPrometheusQuery(CEPH_OSD_UP_QUERY, 'cephOsdUp');
200+
this.fetchPrometheusQuery(CEPH_OSD_DOWN_QUERY, 'cephOsdDown');
271201

272202
this.getStorageMetrics();
273203

274-
this.fetchPrometheusQuery(UTILIZATION_CPU_USED_QUERY, response => this.setUtilizationData('cpuUtilization', response));
275-
this.fetchPrometheusQuery(UTILIZATION_MEMORY_USED_QUERY, response => this.setUtilizationData('memoryUtilization', response));
204+
this.fetchPrometheusQuery(UTILIZATION_CPU_USED_QUERY, 'cpuUtilization');
205+
this.fetchPrometheusQuery(UTILIZATION_MEMORY_USED_QUERY, 'memoryUtilization');
276206
this.fetchAlerts();
277207
}
278208

@@ -281,35 +211,18 @@ export class ClusterOverview extends React.Component {
281211
}
282212

283213
render() {
284-
const {
285-
openshiftClusterVersions,
286-
healthData,
287-
consumersData,
288-
capacityData,
289-
utilizationData,
290-
alertsResponse,
291-
diskStats,
292-
} = this.state;
293-
294214
const inventoryResourceMapToProps = resources => {
295215
return {
296216
value: {
297217
LoadingComponent: LoadingInline,
298218
...resources,
299-
openshiftClusterVersions,
300-
...capacityData,
301-
...consumersData,
219+
...this.state,
302220

303221
eventsData: {
304222
Component: OverviewEventStream,
305223
loaded: true,
306224
},
307-
healthData,
308-
309-
...utilizationData,
310225
complianceData, // TODO: mock, replace by real data and remove from web-ui-components
311-
alertsResponse,
312-
diskStats,
313226
},
314227
};
315228
};

0 commit comments

Comments
 (0)