@@ -59,20 +59,33 @@ async function fetchSystemData(apiEndpoint) {
5959}
6060
6161// Update card content and bar width based on fetched data
62- function updateCard ( cardSelector , dataKey , data , unit = '' , barSelector = null ) {
62+ function updateCard ( cardSelector , dataKey , data , unit = '' , barSelector = null , maxDataKey = null ) {
6363 const cardElement = document . querySelector ( cardSelector ) ;
6464 if ( ! cardElement ) return ;
6565
66- const dataValue = data ?. [ dataKey ] ;
66+ let dataValue = data ?. [ dataKey ] ;
6767 cardElement . querySelector ( '.card-text' ) . textContent = dataValue ? `${ dataValue } ${ unit } ` : 'Data not available' ;
6868
6969 // Update the bar width if a bar selector is provided
7070 if ( barSelector ) {
7171 const barElement = cardElement . querySelector ( barSelector ) ;
7272 if ( ! barElement ) return ;
7373
74- const percentage = parseFloat ( dataValue ) ;
74+ let percentage = parseFloat ( dataValue ) ;
75+
7576 if ( ! isNaN ( percentage ) ) {
77+ // If a maxDataKey is provided, calculate the percentage based on the max value
78+ if ( maxDataKey ) {
79+ const maxDataValue = parseFloat ( data ?. [ maxDataKey ] ) ;
80+ if ( ! isNaN ( maxDataValue ) && maxDataValue > 0 ) {
81+ percentage = ( percentage / maxDataValue ) * 100 ;
82+ }
83+ }
84+
85+ // Ensure the percentage doesn't exceed 100%
86+ percentage = Math . min ( percentage , 100 ) ;
87+
88+ // Set the bar width based on the percentage
7689 barElement . style . width = `${ percentage } %` ;
7790 }
7891 }
@@ -86,7 +99,7 @@ async function refreshData() {
8699 updateCard ( '.bg-disk' , 'disk_percent' , data , '%' , '.disk-bar' ) ;
87100 updateCard ( '.cpu-temp-card' , 'current_temp' , data , ' °C' , '.temp-bar' ) ;
88101 updateCard ( '.bg-memory' , 'memory_percent' , data , '%' , '.memory-usage-bar' ) ;
89- updateCard ( '.cpu-frequency-card' , 'cpu_frequency' , data , ' MHz' , '.frequency-bar' ) ;
102+ updateCard ( '.cpu-frequency-card' , 'cpu_frequency' , data , ' MHz' , '.frequency-bar' , 'cpu_max_frequency' ) ;
90103 updateCard ( '.cpu-usage-card' , 'cpu_percent' , data , '%' , '.cpu-usage-bar' ) ;
91104 updateCard ( '.network-received' , 'network_received' , data , 'MB' ) ;
92105 updateCard ( '.network-sent' , 'network_sent' , data , 'MB' ) ;
@@ -120,7 +133,7 @@ function updateColorBars() {
120133 limits = [ maxValue * limits [ 0 ] / 100 , maxValue * limits [ 1 ] / 100 ] ;
121134 }
122135 }
123-
136+ // console.log("card_value", card_value, limits)
124137 bar . classList . remove ( 'low' , 'medium' , 'high' ) ;
125138 // Apply the appropriate class based on the limits
126139 if ( card_value <= limits [ 0 ] ) {
@@ -130,6 +143,7 @@ function updateColorBars() {
130143 } else {
131144 bar . classList . add ( 'high' ) ;
132145 }
146+
133147 } ) ;
134148}
135149
0 commit comments