11/* eslint-disable react-hooks/exhaustive-deps */
22import { useMemo , useState , useEffect , useCallback } from 'react' ;
33import { useSafeFetch } from '../console/utils/safe-fetch-hook' ;
4- import { createAlertsQuery , fetchDataForIncidentsAndAlerts , fetchInstantData } from './api' ;
4+ import { createAlertsQuery , fetchDataForIncidentsAndAlerts } from './api' ;
55import { useTranslation } from 'react-i18next' ;
66import {
77 Bullseye ,
@@ -37,9 +37,9 @@ import {
3737 onDeleteIncidentFilterChip ,
3838 onIncidentFiltersSelect ,
3939 parseUrlParams ,
40- PROMETHEUS_QUERY_INTERVAL_SECONDS ,
4140 roundTimestampToFiveMinutes ,
4241 updateBrowserUrl ,
42+ DAY_MS ,
4343} from './utils' ;
4444import { groupAlertsForTable , convertToAlerts } from './processAlerts' ;
4545import { CompressArrowsAltIcon , CompressIcon , FilterIcon } from '@patternfly/react-icons' ;
@@ -243,12 +243,11 @@ const IncidentsPage = () => {
243243 }
244244
245245 const currentTime = incidentsLastRefreshTime ;
246- const ONE_DAY = 24 * 60 * 60 * 1000 ;
247246
248247 // Fetch timestamps and alerts in parallel, but wait for both before processing
249248 const timestampPromise = fetchDataForIncidentsAndAlerts (
250249 safeFetch ,
251- { endTime : currentTime , duration : 15 * ONE_DAY } ,
250+ { endTime : currentTime , duration : 15 * DAY_MS } ,
252251 'timestamp(ALERTS{alertstate="firing"})' ,
253252 ) . then ( ( res ) => res . data . result ) ;
254253
@@ -265,35 +264,18 @@ const IncidentsPage = () => {
265264
266265 Promise . all ( [ timestampPromise , alertsPromise ] )
267266 . then ( ( [ timestampsResults , alertsResults ] ) => {
268- // Gaps detection here such that if the same timestamp has
269- // gaps greater than 5 minutes, this will be added more than one time.
270- // For example, if there is a metric for AlertH_Gapped
271- // with values:[ "1770699000", "1770699300", "1770708300", "1770708600", "1770708900"]
272- // there will be two gaps detected. With the following min values: 1770699300 and 1770699300
273- // the interval will be [1770699300 - 1770699300] and [1770708300 - 1770699300]
274-
275- const timestampsValues = timestampsResults ?. map ( ( result : any ) => ( {
276- ...result ,
277- value : detectMinForEachGap ( result . values , PROMETHEUS_QUERY_INTERVAL_SECONDS ) ,
278- } ) ) ;
279-
280267 // Round timestamp values before storing
281268 const roundedTimestamps =
282- timestampsValues ?. map ( ( result : any ) => ( {
269+ timestampsResults ?. map ( ( result : any ) => ( {
283270 ...result ,
284- value : result . value . map ( ( value : any ) => [
285- value [ 0 ] ,
286- roundTimestampToFiveMinutes ( parseInt ( value [ 1 ] ) ) . toString ( ) ,
271+ values : result . values . map ( ( value : any ) => [
272+ roundTimestampToFiveMinutes ( parseInt ( value [ 1 ] ) ) ,
287273 ] ) ,
288274 } ) ) || [ ] ;
289275
290- const fetchedAlertsTimestamps = {
291- minOverTime : roundedTimestamps ,
292- lastOverTime : [ ] ,
293- } ;
294276 dispatch (
295277 setAlertsTimestamps ( {
296- alertsTimestamps : fetchedAlertsTimestamps ,
278+ alertsTimestamps : roundedTimestamps ,
297279 } ) ,
298280 ) ;
299281
@@ -302,7 +284,7 @@ const IncidentsPage = () => {
302284 prometheusResults ,
303285 incidentForAlertProcessing ,
304286 currentTime ,
305- fetchedAlertsTimestamps ,
287+ roundedTimestamps ,
306288 ) ;
307289 dispatch (
308290 setAlertsData ( {
@@ -350,9 +332,10 @@ const IncidentsPage = () => {
350332 : 'cluster_health_components_map' ;
351333
352334 // Fetch timestamps and incidents in parallel, but wait for both before processing
353- const timestampPromise = fetchInstantData (
335+ const timestampPromise = fetchDataForIncidentsAndAlerts (
354336 safeFetch ,
355- 'min_over_time(timestamp(cluster_health_components_map)[15d:5m])' ,
337+ { endTime : currentTime , duration : 15 * DAY_MS } ,
338+ 'timestamp(cluster_health_components_map)' ,
356339 ) . then ( ( res ) => res . data . result ) ;
357340
358341 const incidentsPromise = Promise . all (
@@ -368,32 +351,29 @@ const IncidentsPage = () => {
368351 const roundedTimestamps =
369352 timestampsResults ?. map ( ( result : any ) => ( {
370353 ...result ,
371- value : [
372- result . value [ 0 ] ,
373- roundTimestampToFiveMinutes ( parseInt ( result . value [ 1 ] ) ) . toString ( ) ,
374- ] ,
354+ values : result . values . map ( ( value : any ) => [
355+ roundTimestampToFiveMinutes ( parseInt ( value [ 1 ] ) ) ,
356+ ] ) ,
375357 } ) ) || [ ] ;
376358
377- const fetchedTimestamps = {
378- minOverTime : roundedTimestamps ,
379- lastOverTime : [ ] ,
380- } ;
381359 dispatch (
382360 setIncidentsTimestamps ( {
383- incidentsTimestamps : fetchedTimestamps ,
361+ incidentsTimestamps : roundedTimestamps ,
384362 } ) ,
385363 ) ;
386364
387365 const prometheusResults = incidentsResults . flat ( ) ;
388- const incidents = convertToIncidents ( prometheusResults , currentTime ) ;
366+ const incidents = convertToIncidents ( prometheusResults , currentTime , roundedTimestamps ) ;
389367
390368 // Update the raw, unfiltered incidents state
391369 dispatch ( setIncidents ( { incidents } ) ) ;
392370
371+ const filteredData = filterIncident ( incidentsActiveFilters , incidents ) ;
372+
393373 // Filter the incidents and dispatch
394374 dispatch (
395375 setFilteredIncidentsData ( {
396- filteredIncidentsData : filterIncident ( incidentsActiveFilters , incidents ) ,
376+ filteredIncidentsData : filteredData ,
397377 } ) ,
398378 ) ;
399379
@@ -402,7 +382,7 @@ const IncidentsPage = () => {
402382 if ( isGroupSelected ) {
403383 // Use fetchedTimestamps directly instead of stale closure value
404384 setIncidentForAlertProcessing (
405- processIncidentsForAlerts ( prometheusResults , fetchedTimestamps ) ,
385+ processIncidentsForAlerts ( prometheusResults , roundedTimestamps ) ,
406386 ) ;
407387 dispatch ( setAlertsAreLoading ( { alertsAreLoading : true } ) ) ;
408388 } else {
@@ -759,30 +739,3 @@ export const McpCmoAlertingPage = () => {
759739 </ MonitoringProvider >
760740 ) ;
761741} ;
762-
763- /**
764- * @param {Array<Array> } dataValues - The matrix from out.json (data.result[0].values)
765- * @param {number } gapThreshold - e.g., 300
766- */
767- const detectMinForEachGap = ( dataValues , gapThreshold ) => {
768- if ( ! dataValues || dataValues . length === 0 ) return [ ] ;
769-
770- const mins = [ ] ;
771- let currentMin = dataValues [ 0 ] ;
772-
773- // Start from the second element to compare with the previous one
774- for ( let i = 1 ; i < dataValues . length ; i ++ ) {
775- const delta = dataValues [ i ] [ 1 ] - dataValues [ i - 1 ] [ 1 ] ;
776-
777- if ( delta > gapThreshold ) {
778- // Gap detected: save the min of the interval that just ended
779- mins . push ( currentMin ) ;
780- // The current timestamp is the min of the NEW interval
781- currentMin = dataValues [ i ] ;
782- }
783- }
784-
785- // Always push the min of the last interval
786- mins . push ( currentMin ) ;
787- return mins ;
788- } ;
0 commit comments