Skip to content

Commit 4bb286c

Browse files
committed
feat(RHINENG-20863): improved fetching logic for incidents
1 parent 8f0ed86 commit 4bb286c

File tree

1 file changed

+50
-59
lines changed

1 file changed

+50
-59
lines changed

web/src/components/Incidents/IncidentsPage.tsx

Lines changed: 50 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ const IncidentsPage = () => {
8383
Array<Partial<Incident>>
8484
>([]);
8585
const [hideCharts, setHideCharts] = useState(false);
86+
const [isInitialized, setIsInitialized] = useState(false);
8687

8788
const [filtersExpanded, setFiltersExpanded] = useState<IncidentsPageFiltersExpandedState>({
8889
severity: false,
@@ -145,7 +146,6 @@ const IncidentsPage = () => {
145146
useEffect(() => {
146147
const hasUrlParams = Object.keys(urlParams).length > 0;
147148
if (hasUrlParams) {
148-
// If URL parameters exist, update incidentsActiveFilters based on them
149149
dispatch(
150150
setIncidentsActiveFilters({
151151
incidentsActiveFilters: {
@@ -157,7 +157,6 @@ const IncidentsPage = () => {
157157
}),
158158
);
159159
} else {
160-
// If no URL parameters exist, set the URL based on incidentsInitialState
161160
updateBrowserUrl(incidentsInitialState);
162161
dispatch(
163162
setIncidentsActiveFilters({
@@ -167,6 +166,7 @@ const IncidentsPage = () => {
167166
}),
168167
);
169168
}
169+
setIsInitialized(true);
170170
}, []);
171171

172172
useEffect(() => {
@@ -246,65 +246,57 @@ const IncidentsPage = () => {
246246
}, [alertsData, rules]);
247247

248248
useEffect(() => {
249-
(async () => {
250-
Promise.all(
251-
timeRanges.map(async (range) => {
252-
const response = await fetchDataForIncidentsAndAlerts(
253-
safeFetch,
254-
range,
255-
'cluster_health_components_map',
256-
);
257-
return response.data.result;
258-
}),
259-
)
260-
.then((results) => {
261-
const aggregatedData = results.flat();
262-
dispatch(
263-
setIncidents({
264-
incidents: processIncidents(aggregatedData),
265-
}),
266-
);
267-
dispatch(
268-
setFilteredIncidentsData({
269-
filteredIncidentsData: filterIncident(
270-
urlParams ? incidentsActiveFilters : incidentsInitialState,
271-
processIncidents(aggregatedData),
272-
),
273-
}),
274-
);
275-
setIncidentsAreLoading(false);
276-
})
277-
.catch((err) => {
278-
// eslint-disable-next-line no-console
279-
console.log(err);
280-
});
281-
})();
282-
}, [timeRanges]);
249+
if (!isInitialized) return;
283250

284-
useEffect(() => {
285-
if (selectedGroupId) {
286-
Promise.all(
287-
timeRanges.map(async (range) => {
288-
const response = await fetchDataForIncidentsAndAlerts(
289-
safeFetch,
290-
range,
291-
`cluster_health_components_map{group_id='${selectedGroupId}'}`,
292-
);
293-
return response.data.result;
294-
}),
295-
)
296-
.then((results) => {
297-
const aggregatedData = results.flat();
251+
setIncidentsAreLoading(true);
252+
253+
const daysDuration = parsePrometheusDuration(
254+
incidentsActiveFilters.days.length > 0
255+
? incidentsActiveFilters.days[0].split(' ')[0] + 'd'
256+
: '',
257+
);
258+
const calculatedTimeRanges = getIncidentsTimeRanges(daysDuration, now);
259+
260+
const isGroupSelected = !!selectedGroupId;
261+
const incidentsQuery = isGroupSelected
262+
? `cluster_health_components_map{group_id='${selectedGroupId}'}`
263+
: 'cluster_health_components_map';
264+
265+
Promise.all(
266+
calculatedTimeRanges.map(async (range) => {
267+
const response = await fetchDataForIncidentsAndAlerts(safeFetch, range, incidentsQuery);
268+
return response.data.result;
269+
}),
270+
)
271+
.then((results) => {
272+
const aggregatedData = results.flat();
273+
const processedIncidents = processIncidents(aggregatedData);
274+
275+
// Update the raw, unfiltered incidents state
276+
dispatch(setIncidents({ incidents: processedIncidents }));
277+
278+
// Now, dispatch the filtered data based on the full incidents list
279+
dispatch(
280+
setFilteredIncidentsData({
281+
filteredIncidentsData: filterIncident(incidentsActiveFilters, processedIncidents),
282+
}),
283+
);
284+
285+
setIncidentsAreLoading(false);
286+
287+
if (isGroupSelected) {
298288
setIncidentForAlertProcessing(processIncidentsForAlerts(aggregatedData));
299289
dispatch(setAlertsAreLoading({ alertsAreLoading: true }));
300-
setIncidentsAreLoading(false);
301-
})
302-
.catch((err) => {
303-
// eslint-disable-next-line no-console
304-
console.log(err);
305-
});
306-
}
307-
}, [selectedGroupId, timeRanges]);
290+
} else {
291+
setIncidentForAlertProcessing([]);
292+
dispatch(setAlertsAreLoading({ alertsAreLoading: false }));
293+
}
294+
})
295+
.catch((err) => {
296+
// eslint-disable-next-line no-console
297+
console.log(err);
298+
});
299+
}, [isInitialized, incidentsActiveFilters.days, selectedGroupId]);
308300

309301
const onSelect = (_event, value) => {
310302
if (value) {
@@ -316,7 +308,6 @@ const IncidentsPage = () => {
316308

317309
const incidentIdFilterOptions = incidents ? getIncidentIdOptions(incidents) : [];
318310

319-
//loading states
320311
useEffect(() => {
321312
//force a loading state for the alerts chart and table if we filtered out all of the incidents
322313
if (

0 commit comments

Comments
 (0)