@@ -2,19 +2,19 @@ let refreshInterval = 0;
22let refreshTimer , refreshTimeout ;
33
44// Fetch the refresh interval from the server
5- function fetchRefreshInterval ( ) {
6- return fetch ( '/api/v1/refresh-interval' )
7- . then ( response => response . json ( ) )
8- . then ( data => {
9- if ( data . success ) {
10- refreshInterval = data . refresh_interval * 1000 ; // Convert to ms
11- console . log ( 'Refresh interval:' , data . refresh_interval ) ;
12- return refreshInterval ;
13- } else {
14- console . error ( 'Error fetching interval:' , data . error ) ;
15- }
16- } )
17- . catch ( error => console . error ( 'Fetch interval error:' , error ) ) ;
5+ async function fetchRefreshInterval ( ) {
6+ try {
7+ const response = await fetch ( '/api/v1/refresh-interval' ) ;
8+ const data = await response . json ( ) ;
9+ if ( data . success ) {
10+ refreshInterval = data . refresh_interval * 1000 ; // Convert to ms
11+ return refreshInterval ;
12+ } else {
13+ console . error ( 'Error fetching interval:' , data . error ) ;
14+ }
15+ } catch ( error ) {
16+ console . error ( 'Fetch interval error:' , error ) ;
17+ }
1818}
1919
2020// Update the refresh interval when the user changes the value
@@ -23,122 +23,107 @@ function updateRefreshInterval() {
2323 refreshInput . addEventListener ( 'change' , function ( ) {
2424 clearTimeout ( refreshTimeout ) ;
2525 refreshInterval = parseInt ( this . value ) * 1000 ;
26-
2726 refreshTimeout = setTimeout ( ( ) => window . location . reload ( ) , refreshInterval ) ;
28-
2927 postRefreshInterval ( refreshInterval / 1000 ) ; // Send in seconds
30- updateColorBars ( ) ; // Update color bars based on data attributes
31- } ) ;
28+ } ) ;
3229}
3330
3431// Post the updated refresh interval to the server
35- function postRefreshInterval ( newInterval ) {
36- fetch ( '/api/v1/refresh-interval' , {
37- method : 'POST' ,
38- headers : { 'Content-Type' : 'application/json' } ,
39- body : JSON . stringify ( { refresh_interval : newInterval } )
40- } )
41- . then ( response => response . json ( ) )
42- . then ( data => {
43- if ( data . success ) {
44- console . log ( 'Updated interval:' , data . refresh_interval ) ;
45- } else {
46- console . error ( 'Failed to update interval:' , data . error ) ;
47- }
48- } )
49- . catch ( error => console . error ( 'Error:' , error ) ) ;
32+ async function postRefreshInterval ( newInterval ) {
33+ try {
34+ const response = await fetch ( '/api/v1/refresh-interval' , {
35+ method : 'POST' ,
36+ headers : { 'Content-Type' : 'application/json' } ,
37+ body : JSON . stringify ( { refresh_interval : newInterval } )
38+ } ) ;
39+ const data = await response . json ( ) ;
40+ if ( data . success ) {
41+ console . log ( 'Updated interval:' , data . refresh_interval ) ;
42+ } else {
43+ console . error ( 'Failed to update interval:' , data . error ) ;
44+ }
45+ } catch ( error ) {
46+ console . error ( 'Error updating interval:' , error ) ;
47+ }
5048}
5149
5250// Fetch system data from a given API endpoint
53- function fetchSystemData ( apiEndpoint ) {
54- return fetch ( apiEndpoint )
55- . then ( response => response . json ( ) )
56- . catch ( error => {
57- console . error ( `Error fetching ${ apiEndpoint } :` , error ) ;
58- return null ;
59- } ) ;
51+ async function fetchSystemData ( apiEndpoint ) {
52+ try {
53+ const response = await fetch ( apiEndpoint ) ;
54+ return await response . json ( ) ;
55+ } catch ( error ) {
56+ console . error ( `Error fetching ${ apiEndpoint } :` , error ) ;
57+ return null ;
58+ }
6059}
6160
62- // Update card content based on the fetched data
63- function updateCard ( cardSelector , dataKey , data , unit = '' , barSelector ) {
61+ // Update card content and bar width based on fetched data
62+ function updateCard ( cardSelector , dataKey , data , unit = '' , barSelector = null ) {
6463 const cardElement = document . querySelector ( cardSelector ) ;
6564 if ( ! cardElement ) return ;
66-
65+
6766 const dataValue = data ?. [ dataKey ] ;
68- cardElement . querySelector ( '.card-text' ) . textContent = dataValue
69- ? `${ dataValue } ${ unit } `
70- : 'Data not available' ;
67+ cardElement . querySelector ( '.card-text' ) . textContent = dataValue ? `${ dataValue } ${ unit } ` : 'Data not available' ;
7168
72- // update the bar element if a selector is provided
69+ // Update the bar width if a bar selector is provided
7370 if ( barSelector ) {
7471 const barElement = cardElement . querySelector ( barSelector ) ;
7572 if ( ! barElement ) return ;
7673
77- // update the bar element based on the data value
7874 const percentage = parseFloat ( dataValue ) ;
79- if ( isNaN ( percentage ) ) return ;
80-
81- barElement . style . width = ` ${ percentage } %` ;
75+ if ( ! isNaN ( percentage ) ) {
76+ barElement . style . width = ` ${ percentage } %` ;
77+ }
8278 }
8379}
8480
85-
8681// Refresh all card data
87- function refreshData ( ) {
88- fetchSystemData ( '/api/system-info' ) . then ( data => {
89- if ( ! data ) return ;
90- updateCard ( '.bg-disk' , 'disk_percent' , data , '%' , '.disk-bar' ) ;
91- updateCard ( '.cpu-temp-card ' , 'current_temp ' , data , ' °C ' , '.temp -bar' ) ;
92- updateCard ( '.bg-memory ' , 'memory_percent ' , data , '% ' , '.memory-usage -bar' ) ;
93- updateCard ( '.cpu-frequency ' , 'cpu_frequency ' , data , ' MHz ' , '.frequency -bar' ) ;
94- updateCard ( '.cpu-usage-card ' , 'cpu_percent ' , data , '% ' , '.cpu-usage -bar' ) ;
95- updateCard ( '.network-received ' , 'network_received ' , data , 'MB ' ) ;
96- updateCard ( '.network-sent ' , 'network_sent ' , data , 'MB' ) ;
97- updateCard ( '.battery-card ' , 'battery_percent ' , data , '%' , '.battery-bar ') ;
98- updateColorBars ( ) ; // Update color bars based on data attributes
99- } ) ;
82+ async function refreshData ( ) {
83+ const data = await fetchSystemData ( '/api/system-info' ) ;
84+ if ( ! data ) return ;
85+
86+ updateCard ( '.bg-disk ' , 'disk_percent ' , data , '% ' , '.disk -bar' ) ;
87+ updateCard ( '.cpu-temp-card ' , 'current_temp ' , data , ' °C ' , '.temp -bar' ) ;
88+ updateCard ( '.bg-memory ' , 'memory_percent ' , data , '% ' , '.memory-usage -bar' ) ;
89+ updateCard ( '.cpu-frequency ' , 'cpu_frequency ' , data , ' MHz ' , '.frequency -bar' ) ;
90+ updateCard ( '.cpu-usage-card ' , 'cpu_percent ' , data , '%' , '.cpu-usage-bar ') ;
91+ updateCard ( '.network-received ' , 'network_received ' , data , 'MB' ) ;
92+ updateCard ( '.network-sent ' , 'network_sent ' , data , 'MB ' ) ;
93+ updateCard ( '.battery-card' , 'battery_percent' , data , '%' , '.battery-bar' ) ;
94+ updateColorBars ( ) ; // Update color bars based on the fetched data
10095}
10196
102- // Update color bars based on data attributes
97+ // Update the color of bars based on their percentage
10398function updateColorBars ( ) {
104- const bars = [
99+ const barConfigs = [
105100 { selector : '.battery-bar' , dataAttr : 'data-battery' , limits : [ 25 , 75 ] } ,
106101 { selector : '.disk-bar' , dataAttr : 'data-disk-usage' , limits : [ 60 , 80 ] } ,
107102 { selector : '.cpu-usage-bar' , dataAttr : 'data-cpu-usage' , limits : [ 50 , 80 ] } ,
108- { selector : '.memory-usage-bar' , dataAttr : 'data-memory-usage' , limits : [ 0.5 , 0.8 ] , maxAttr : 'data-memory-total' } ,
109- { selector : '.frequency-bar' , dataAttr : 'data-cpu-frequency' , limits : [ 0.5 , 0.8 ] , maxAttr : 'data-cpu-max-frequency' } ,
110- { selector : '.temp-bar' , dataAttr : 'data-cpu-temp' , limits : [ 0.65 , 0.9 ] , maxAttr : 'data-cpu-max-temp' }
103+ { selector : '.memory-usage-bar' , dataAttr : 'data-memory-usage' , limits : [ .50 , .80 ] , maxAttr : 'data-memory-total' } ,
104+ { selector : '.frequency-bar' , dataAttr : 'data-cpu-frequency' , limits : [ .50 , .80 ] , maxAttr : 'data-cpu-max-frequency' } ,
105+ { selector : '.temp-bar' , dataAttr : 'data-cpu-temp' , limits : [ .70 , .90 ] , maxAttr : 'data-cpu-max-temp' }
111106 ] ;
112107
113- bars . forEach ( ( { selector, dataAttr, limits, maxAttr } ) => {
108+ barConfigs . forEach ( ( { selector, dataAttr, limits, maxAttr } ) => {
114109 const bar = document . querySelector ( selector ) ;
115110 const card = document . querySelector ( `[${ dataAttr } ]` ) ;
116111 const maxElement = maxAttr ? document . querySelector ( `[${ maxAttr } ]` ) : null ;
117112
118- if ( ! bar || ! card ) {
119- // console.warn(`Element not found for selector: ${selector} or data attribute: ${dataAttr}`);
120- return ;
121- }
122-
123- let percentage = parseFloat ( card . getAttribute ( dataAttr ) ) ;
124-
125- if ( isNaN ( percentage ) ) {
126- console . warn ( `Invalid percentage value for ${ dataAttr } ` ) ;
127- return ;
128- }
113+ if ( ! bar || ! card ) return ;
114+ let percentage = parseFloat ( bar . style . width ) ;
115+ if ( isNaN ( percentage ) ) return ;
129116
130- // If maxAttr is defined, use it to scale the limits
131117 if ( maxElement ) {
132118 const maxValue = parseFloat ( maxElement . getAttribute ( maxAttr ) ) ;
133- if ( isNaN ( maxValue ) ) {
134- // console.warn(`Invalid max value for ${maxAttr}`);
135- return ;
119+ if ( ! isNaN ( maxValue ) ) {
120+ limits = [ maxValue * limits [ 0 ] , maxValue * limits [ 1 ] ] ;
136121 }
137-
138- limits = [ maxValue * limits [ 0 ] , maxValue * limits [ 1 ] ] ;
139122 }
140123
141- // Apply class based on percentage within the defined limits
124+ percentage = Math . min ( percentage , 100 ) ; // Ensure percentage is not greater than 100
125+ bar . classList . remove ( 'low' , 'medium' , 'high' ) ;
126+ // Apply the appropriate class based on the limits
142127 if ( percentage <= limits [ 0 ] ) {
143128 bar . classList . add ( 'low' ) ;
144129 } else if ( percentage > limits [ 0 ] && percentage <= limits [ 1 ] ) {
@@ -149,7 +134,6 @@ function updateColorBars() {
149134 } ) ;
150135}
151136
152-
153137// Start the automatic refresh process
154138function startRefresh ( ) {
155139 clearInterval ( refreshTimer ) ;
@@ -159,10 +143,11 @@ function startRefresh() {
159143}
160144
161145// Initialize the system
162- function init ( ) {
163- fetchRefreshInterval ( ) . then ( startRefresh ) ; // Fetch interval and start refreshing
164- updateRefreshInterval ( ) ; // Listen for changes to the refresh interval input
165- updateColorBars ( ) ; // Update color bars based on data attributes
146+ async function init ( ) {
147+ await fetchRefreshInterval ( ) ;
148+ startRefresh ( ) ; // Start refreshing data at the set interval
149+ updateRefreshInterval ( ) ; // Listen for changes in the refresh interval input
150+ updateColorBars ( ) ; // Update the color bars based on the initial data
166151}
167152
168153document . addEventListener ( 'DOMContentLoaded' , init ) ;
0 commit comments