@@ -25,6 +25,32 @@ let categoryHealthData = {
2525 ]
2626} ;
2727
28+ // Device Status Management
29+ let updateInterval = 30000 ; // 30 seconds
30+ let isUpdating = false ;
31+ let updateIndicator = document . getElementById ( 'updateIndicator' ) ;
32+ let refreshButton = document . getElementById ( 'refreshButton' ) ;
33+
34+ function showUpdateIndicator ( ) {
35+ if ( updateIndicator ) {
36+ updateIndicator . style . display = 'inline-flex' ;
37+ if ( refreshButton ) {
38+ refreshButton . classList . add ( 'updating' ) ;
39+ refreshButton . disabled = true ;
40+ }
41+ }
42+ }
43+
44+ function hideUpdateIndicator ( ) {
45+ if ( updateIndicator ) {
46+ updateIndicator . style . display = 'none' ;
47+ if ( refreshButton ) {
48+ refreshButton . classList . remove ( 'updating' ) ;
49+ refreshButton . disabled = false ;
50+ }
51+ }
52+ }
53+
2854async function fetchDevices ( ) {
2955 if ( isFetching ) return ;
3056 isFetching = true ;
@@ -581,48 +607,114 @@ function resetPassword() {
581607 }
582608}
583609
584- async function updateDeviceStatuses ( ) {
585- try {
586- const response = await fetch ( '/devices' ) ;
587- if ( ! response . ok ) throw new Error ( 'Failed to fetch devices' ) ;
588-
589- const devices = await response . json ( ) ;
590- let statusChanged = false ;
610+ async function fetchDeviceStatuses ( ) {
611+ if ( isUpdating ) return ;
612+ isUpdating = true ;
613+ showUpdateIndicator ( ) ;
614+
615+ try {
616+ const response = await fetch ( '/api/devices/status' ) ;
617+ if ( ! response . ok ) throw new Error ( 'Failed to fetch device statuses' ) ;
618+ const newStatuses = await response . json ( ) ;
619+
620+ // Only update the UI if there are significant changes
621+ let hasChanges = false ;
622+ for ( const [ deviceName , status ] of Object . entries ( newStatuses ) ) {
623+ const lastStatus = deviceStatuses . get ( deviceName ) ;
624+ const hasSignificantChange = ! lastStatus ||
625+ lastStatus . ping_status !== status . ping_status ||
626+ lastStatus . http_status !== status . http_status ||
627+ lastStatus . down !== status . down ;
628+
629+ if ( hasSignificantChange ) {
630+ hasChanges = true ;
631+ deviceStatuses . set ( deviceName , status ) ;
632+ updateDeviceRow ( deviceName , status ) ;
633+ }
634+ }
591635
592- devices . forEach ( device => {
593- const currentStatus = deviceStatuses . get ( device . ip ) ;
636+ // Only update charts if there are significant changes
637+ if ( hasChanges ) {
638+ updateBandwidthChart ( devicesData ) ;
639+ updateCategoryHealthChart ( devicesData ) ;
640+ }
641+ } catch ( error ) {
642+ console . error ( 'Error fetching device statuses:' , error ) ;
643+ } finally {
644+ isUpdating = false ;
645+ hideUpdateIndicator ( ) ;
646+ }
647+ }
594648
595- if ( ! currentStatus ||
596- currentStatus . ping_status !== device . ping_status ||
597- currentStatus . http_status !== device . http_status ||
598- currentStatus . bandwidth_usage !== device . bandwidth_usage ) {
649+ function updateDeviceRow ( deviceName , status ) {
650+ const row = document . querySelector ( `tr[data-device="${ deviceName } "]` ) ;
651+ if ( ! row ) return ;
652+
653+ // Update status indicators
654+ const pingStatus = row . querySelector ( '.ping-status' ) ;
655+ const httpStatus = row . querySelector ( '.http-status' ) ;
656+ const bandwidthStatus = row . querySelector ( '.bandwidth-status' ) ;
657+
658+ // Only update if there's a significant change
659+ if ( pingStatus ) {
660+ const currentPingStatus = pingStatus . textContent . trim ( ) ;
661+ if ( currentPingStatus !== status . ping_status ) {
662+ pingStatus . textContent = status . ping_status ;
663+ pingStatus . className = `ping-status status-indicator ${ status . ping_status . toLowerCase ( ) === 'fail' ? 'status-fail' : 'status-success' } ` ;
664+ }
665+ }
599666
600- deviceStatuses . set ( device . ip , {
601- ping_status : device . ping_status ,
602- http_status : device . http_status ,
603- bandwidth_usage : device . bandwidth_usage ,
604- lastChange : Date . now ( )
605- } ) ;
606- statusChanged = true ;
607- }
608- } ) ;
667+ if ( httpStatus ) {
668+ const currentHttpStatus = httpStatus . textContent . trim ( ) ;
669+ if ( currentHttpStatus !== status . http_status ) {
670+ httpStatus . textContent = status . http_status ;
671+ httpStatus . className = `http-status status-indicator ${ status . http_status . toLowerCase ( ) === 'fail' ? 'status-fail' : 'status-success' } ` ;
672+ }
673+ }
609674
610- // Only update UI if status actually changed
611- if ( statusChanged ) {
612- renderData ( devices ) ;
675+ if ( bandwidthStatus ) {
676+ const currentBandwidth = bandwidthStatus . textContent . trim ( ) ;
677+ if ( currentBandwidth !== status . bandwidth_usage ) {
678+ bandwidthStatus . textContent = status . bandwidth_usage ;
679+ }
613680 }
614- } catch ( error ) {
615- console . error ( 'Error updating device statuses:' , error ) ;
616- }
681+
682+ // Update row status
683+ row . classList . toggle ( 'device-down' , status . down ) ;
617684}
618685
619- // Update status check interval
620- const STATUS_CHECK_INTERVAL = 5000 ; // 5 seconds
621- setInterval ( updateDeviceStatuses , STATUS_CHECK_INTERVAL ) ;
686+ // Initialize device statuses
687+ async function initializeDeviceStatuses ( ) {
688+ try {
689+ const response = await fetch ( '/api/devices/status' ) ;
690+ if ( ! response . ok ) throw new Error ( 'Failed to fetch initial device statuses' ) ;
691+ const statuses = await response . json ( ) ;
692+
693+ for ( const [ deviceName , status ] of Object . entries ( statuses ) ) {
694+ deviceStatuses . set ( deviceName , status ) ;
695+ updateDeviceRow ( deviceName , status ) ;
696+ }
697+ } catch ( error ) {
698+ console . error ( 'Error initializing device statuses:' , error ) ;
699+ }
700+ }
622701
623- // Initial load
702+ // Event Listeners
624703document . addEventListener ( 'DOMContentLoaded' , ( ) => {
625- updateDeviceStatuses ( ) ;
704+ // Initialize device statuses
705+ initializeDeviceStatuses ( ) ;
706+
707+ // Set up automatic updates
708+ setInterval ( fetchDeviceStatuses , updateInterval ) ;
709+
710+ // Add click handler for refresh button
711+ if ( refreshButton ) {
712+ refreshButton . addEventListener ( 'click' , ( ) => {
713+ if ( ! isUpdating ) {
714+ fetchDeviceStatuses ( ) ;
715+ }
716+ } ) ;
717+ }
626718} ) ;
627719
628720function initCategoryHealthChart ( ) {
0 commit comments