11let refreshInterval = 0 ; // Initialize with a default value
22let refreshTimer ; // Variable to hold the setInterval timer
3+ let refreshTimeout ; // Variable to hold the setTimeout timer
34
45function fetchRefreshInterval ( ) {
56 fetch ( '/refresh-interval' )
@@ -16,6 +17,38 @@ function fetchRefreshInterval() {
1617 . catch ( error => console . error ( 'Error:' , error ) ) ;
1718}
1819
20+ // Event listener for select input change
21+ document . getElementById ( 'refresh-interval' ) . addEventListener ( 'change' , function ( ) {
22+ // Clear the existing timeout
23+ clearTimeout ( refreshTimeout ) ;
24+
25+ // Update the interval based on the selected value
26+ refreshInterval = parseInt ( this . value ) * 1000 ; // Convert to milliseconds
27+
28+ // refresh the page once
29+ refreshTimeout = setTimeout ( ( ) => {
30+ window . location . reload ( ) ;
31+ } , refreshInterval ) ;
32+
33+ // Send the updated refresh interval to the server
34+ fetch ( '/refresh-interval' , {
35+ method : 'POST' ,
36+ headers : {
37+ 'Content-Type' : 'application/json'
38+ } ,
39+ body : JSON . stringify ( { refresh_interval : parseInt ( this . value ) } ) // Send in seconds, not milliseconds
40+ } )
41+ . then ( response => response . json ( ) )
42+ . then ( data => {
43+ if ( data . success ) {
44+ console . log ( 'Refresh interval updated successfully:' , data . refresh_interval ) ;
45+ } else {
46+ console . error ( 'Failed to update refresh interval:' , data . error ) ;
47+ }
48+ } )
49+ . catch ( error => console . error ( 'Error:' , error ) ) ;
50+ } ) ;
51+
1952// Function to fetch system data from the API once
2053function fetchSystemData ( apiEndpoint ) {
2154 if ( ! apiEndpoint ) {
@@ -53,6 +86,11 @@ function updateCard(cardSelector, dataKey, data, unit = '') {
5386 cardElement . querySelector ( '.card-text' ) . textContent = 'Data not available' ;
5487 return ;
5588 }
89+ else if ( dataValue === null ) {
90+ console . warn ( `Data key ${ dataKey } is null in the response.` ) ;
91+ cardElement . querySelector ( '.card-text' ) . textContent = 'Data not available' ;
92+ return ;
93+ }
5694
5795 cardElement . querySelector ( '.card-text' ) . textContent = `${ dataValue } ${ unit } ` ;
5896}
@@ -68,7 +106,7 @@ function refreshData() {
68106 updateCard ( '.bg-dashboard-memory' , 'dashboard_memory_usage' , data ) ; // Memory Usage
69107 updateCard ( '.bg-memory' , 'memory_percent' , data , '%' ) ; // Memory Usage
70108 updateCard ( '.cpu-frequency' , 'cpu_frequency' , data , " MHz" ) ; // CPU Frequency
71- updateCard ( '.cpu-usage' , 'cpu_percent' , data , '%' ) ; // CPU Usage
109+ updateCard ( '.cpu-card- usage' , 'cpu_percent' , data , '%' ) ; // CPU Usage
72110 updateCard ( '.network-received' , 'network_received' , data , "MB" ) ; // Network Received
73111 updateCard ( '.network-sent' , 'network_sent' , data , "MB" ) ; // Network Sent
74112 } ) ;
0 commit comments