@@ -41,7 +41,6 @@ interface PrometheusQueryResult {
4141 description ?: string ;
4242 icon ?: string ;
4343 unit : string ;
44- alertThreshold : number ;
4544 } ;
4645}
4746
@@ -57,6 +56,7 @@ interface AlertDetails {
5756 alertName : string ;
5857 severity : string ;
5958 value : number ;
59+ description : string ;
6060}
6161
6262function usePrometheusWebSocket ( ) {
@@ -65,8 +65,8 @@ function usePrometheusWebSocket() {
6565 const reconnectAttempts = useRef ( 0 ) ;
6666 const reconnectTimeout = useRef < NodeJS . Timeout | null > ( null ) ;
6767 const maxReconnectAttempts = 10 ;
68- const baseDelay = 1000 ; // 1 second
69- const maxDelay = 30000 ; // 30 seconds
68+ const baseDelay = 1000 ;
69+ const maxDelay = 30000 ;
7070
7171 const pendingRequests = useRef <
7272 Map <
@@ -86,7 +86,7 @@ function usePrometheusWebSocket() {
8686 websocket . onopen = ( ) => {
8787 setIsConnected ( true ) ;
8888 setWs ( websocket ) ;
89- reconnectAttempts . current = 0 ; // Reset reconnect attempts on successful connection
89+ reconnectAttempts . current = 0 ;
9090 } ;
9191
9292 websocket . onmessage = ( event ) => {
@@ -129,15 +129,13 @@ function usePrometheusWebSocket() {
129129 setIsConnected ( false ) ;
130130 setWs ( null ) ;
131131
132- // Reject all pending requests
133132 if ( pendingRequests . current . size > 0 ) {
134133 pendingRequests . current . forEach ( ( pending ) => {
135134 pending . reject ( new Error ( "WebSocket connection closed" ) ) ;
136135 } ) ;
137136 pendingRequests . current . clear ( ) ;
138137 }
139138
140- // Attempt to reconnect if not at max attempts
141139 if ( reconnectAttempts . current < maxReconnectAttempts && ! event . wasClean ) {
142140 const delay = Math . min (
143141 baseDelay * Math . pow ( 2 , reconnectAttempts . current ) ,
@@ -162,7 +160,7 @@ function usePrometheusWebSocket() {
162160 clearTimeout ( reconnectTimeout . current ) ;
163161 reconnectTimeout . current = null ;
164162 }
165- websocket . close ( 1000 , "Component unmounting" ) ; // Clean close
163+ websocket . close ( 1000 , "Component unmounting" ) ;
166164 } ;
167165 } , [ connect ] ) ;
168166
@@ -229,7 +227,6 @@ export function PrometheusQueryBox({
229227
230228 const { sendPrometheusQuery } = usePrometheusWebSocket ( ) ;
231229
232- // Hardcoded query definition wrapped in useMemo
233230 const hardcodedQuery = useMemo ( ( ) => {
234231 const query = `ALERTS` ;
235232 return {
@@ -241,15 +238,12 @@ export function PrometheusQueryBox({
241238 description : "Alerts for this session" ,
242239 icon : "memory" ,
243240 unit : "" ,
244- alertThreshold : 0 ,
245241 } ;
246242 } , [ sessionName ] ) ;
247243
248- // Use refs to keep stable references to current values
249244 const setPrometheusQueryBtnColorRef = useRef ( setPrometheusQueryBtnColor ) ;
250245 const sendPrometheusQueryRef = useRef ( sendPrometheusQuery ) ;
251246
252- // Update refs when values change
253247 useEffect ( ( ) => {
254248 setPrometheusQueryBtnColorRef . current = setPrometheusQueryBtnColor ;
255249 } , [ setPrometheusQueryBtnColor ] ) ;
@@ -266,7 +260,6 @@ export function PrometheusQueryBox({
266260 description ?: string ;
267261 icon ?: string ;
268262 unit : string ;
269- alertThreshold : number ;
270263 } ) => {
271264 if ( ! predefinedQuery . path ?. trim ( ) ) return ;
272265
@@ -286,30 +279,15 @@ export function PrometheusQueryBox({
286279
287280 const getAllQueryResults = useCallback ( async ( ) => {
288281 console . log ( "🔄 Executing Prometheus query for session:" , sessionName ) ;
289- let newColor = "text-dark" ;
290282
291283 const result = await executeQuery ( hardcodedQuery ) ;
292284 console . log ( "📊 Query result:" , result ) ;
293285
294286 if ( result ?. data ?. result ?. length && result . data . result . length > 0 ) {
295287 const filteredResults = [ { ...result , predefinedQuery : hardcodedQuery } ] ;
296288 const currentValue = result . data . result [ 0 ] ?. value ?. [ 1 ] ;
297- console . log (
298- `📈 Current value: ${ currentValue } , threshold: ${ hardcodedQuery . alertThreshold } `
299- ) ;
300-
301- if (
302- currentValue &&
303- parseFloat ( currentValue ) > hardcodedQuery . alertThreshold
304- ) {
305- newColor = "text-danger" ;
306- console . log ( "🚨 Alert threshold exceeded - setting danger color" ) ;
307- } else {
308- newColor = "text-warning" ;
309- console . log ( "⚠️ Value detected - setting warning color" ) ;
310- }
289+ console . log ( `📈 Current value: ${ currentValue } ` ) ;
311290
312- setPrometheusQueryBtnColorRef . current ( newColor ) ;
313291 setQueryResults ( filteredResults ) ;
314292 } else {
315293 console . log ( "❌ No results found - clearing query results" ) ;
@@ -318,22 +296,22 @@ export function PrometheusQueryBox({
318296 } , [ executeQuery , hardcodedQuery , sessionName ] ) ;
319297
320298 const getAlertDetails = useCallback ( ( ) => {
321- if ( queryResults . length === 0 ) return null ;
299+ if ( queryResults . length === 0 ) return [ ] ;
322300 const result = queryResults [ 0 ] ;
323- if ( result . data . result . length === 0 ) return null ;
301+ if ( result . data . result . length === 0 ) return [ ] ;
324302
325- const alertInfo = result . data . result [ 0 ] . metric ;
326- console . log ( "alertName: " , alertInfo . name ) ;
327- const alertName = alertInfo . name ;
328- return alertName ;
303+ const alertNames = result . data . result . map (
304+ ( alertResult ) => alertResult . metric . name
305+ ) ;
306+ console . log ( "Alert names from ALERTS query: " , alertNames ) ;
307+ return alertNames ;
329308 } , [ queryResults ] ) ;
330309
331310 const handleCloseButton = useCallback ( ( ) => {
332311 onClose ( ) ;
333312 } , [ onClose ] ) ;
334313
335314 useEffect ( ( ) => {
336- // Execute immediately on mount
337315 getAllQueryResults ( ) ;
338316
339317 const interval = setInterval ( ( ) => {
@@ -346,40 +324,50 @@ export function PrometheusQueryBox({
346324 } , [ getAllQueryResults ] ) ;
347325
348326 useEffect ( ( ) => {
349- let alertDetails = getAlertDetails ( ) ;
350- console . log ( "🚨 Alert details:" , alertDetails ) ;
351- getAllAlertDetails ( alertDetails ) ;
352- } , [ queryResults ] ) ;
327+ const alertNames = getAlertDetails ( ) ;
328+ console . log ( "🚨 Alert names:" , alertNames ) ;
329+ getAllAlertDetails ( alertNames ) ;
330+ } , [ queryResults , getAlertDetails ] ) ;
331+
332+ async function getAllAlertDetails ( alertNames : string [ ] ) {
333+ if ( ! alertNames || alertNames . length === 0 ) {
334+ setAlerts ( [ ] ) ;
335+ return ;
336+ }
353337
354- async function getAllAlertDetails ( alertName : string ) {
355338 const detailsQuery = {
356339 label : "Alerts for this session" ,
357340 path : `http://prometheus-server.monitoring.svc.cluster.local/api/v1/alerts` ,
358341 description : "Alerts for this session" ,
359342 icon : "memory" ,
360343 unit : "" ,
361- alertThreshold : 0 ,
362344 } ;
363345
364346 const result = await executeQuery ( detailsQuery ) ;
365- console . log ( "📊 Alert details query result:" , result ?. data . alerts [ 0 ] . value ) ;
347+ console . log ( "📊 Alert details query result:" , result ?. data . alerts ) ;
348+
366349 if ( result ?. data ?. alerts ?. length && result . data . alerts . length > 0 ) {
367- const alert = result . data . alerts . find ( ( a ) => a . labels . name === alertName ) ;
368- if ( alert ) {
350+ const relevantAlerts = result . data . alerts . filter ( ( alert ) =>
351+ alertNames . includes ( alert . labels . name )
352+ ) ;
353+
354+ const alertDetails : AlertDetails [ ] = relevantAlerts . map ( ( alert ) => {
369355 const severity = alert . labels . severity || "unknown" ;
370356 const value = parseFloat ( alert . value ) || 0 ;
371357 console . log (
372358 `🚨 Alert found - Name: ${ alert . labels . alertname } , Severity: ${ severity } , Value: ${ value } `
373359 ) ;
374- const newAlert : AlertDetails = {
360+ return {
375361 alertName : alert . labels . alertname ,
376362 severity,
377363 value,
364+ description : alert . annotations ?. description || "" ,
378365 } ;
379- setAlerts ( [ newAlert ] ) ;
380- } else {
381- console . log ( `ℹ️ No alert found with name: ${ alertName } ` ) ;
382- }
366+ } ) ;
367+ setAlerts ( alertDetails ) ;
368+ } else {
369+ console . log ( `ℹ️ No alerts found` ) ;
370+ setAlerts ( [ ] ) ;
383371 }
384372 }
385373
@@ -408,15 +396,16 @@ export function PrometheusQueryBox({
408396
409397 { alerts . map ( ( alert , idx ) => (
410398 < div key = { idx } className = "mb-2" >
411- < div className = "fw-bold" > { alert . alertName } </ div >
412- < div className = "mb-1" >
413- < div
414- className = {
415- "text-warning"
416- }
417- >
418- Severity: { alert . severity } , Value: { alert . value }
419- </ div >
399+ < div className = "fw-normal text-dark medium" >
400+ { alert . description || alert . alertName }
401+ </ div >
402+ < div
403+ className = { cx (
404+ "medium" ,
405+ alert . severity === "warning" ? "text-warning" : "text-danger"
406+ ) }
407+ >
408+ { alert . value }
420409 </ div >
421410 </ div >
422411 ) ) }
0 commit comments