|
1 | 1 | document.addEventListener('DOMContentLoaded', function () { |
2 | 2 | const bars = [ |
3 | 3 | { selector: '.battery-bar', dataAttr: 'data-battery', limits: [25, 75] }, |
4 | | - { selector: '.disk-bar', dataAttr: 'data-disk-usage', limits: [50, 80] }, |
| 4 | + { selector: '.disk-bar', dataAttr: 'data-disk-usage', limits: [60, 80] }, |
5 | 5 | { selector: '.cpu-usage-bar', dataAttr: 'data-cpu-usage', limits: [50, 80] }, |
6 | | - { selector: '.memory-usage-bar', dataAttr: 'data-memory-usage', limits: [.5, .8] , max_attr: 'data-memory-total' }, |
7 | | - { selector: '.frequency-bar', dataAttr: 'data-cpu-frequency', limits: [.5, .8], max_attr: 'data-cpu-max-frequency' }, |
8 | | - { selector: '.temp-bar', dataAttr: 'data-cpu-temp', limits: [.7, .9], max_attr: 'data-cpu-max-temp' }, |
9 | | - // todo: find max frequency and max temperature for the CPU |
| 6 | + { selector: '.memory-usage-bar', dataAttr: 'data-memory-usage', limits: [0.5, 0.8], maxAttr: 'data-memory-total' }, |
| 7 | + { selector: '.frequency-bar', dataAttr: 'data-cpu-frequency', limits: [0.5, 0.8], maxAttr: 'data-cpu-max-frequency' }, |
| 8 | + { selector: '.temp-bar', dataAttr: 'data-cpu-temp', limits: [0.75, 0.9], maxAttr: 'data-cpu-max-temp' } |
10 | 9 | ]; |
11 | 10 |
|
12 | | - bars.forEach(({ selector, dataAttr, limits, max_attr}) => { |
| 11 | + bars.forEach(({ selector, dataAttr, limits, maxAttr }) => { |
13 | 12 | const bar = document.querySelector(selector); |
14 | 13 | const card = document.querySelector(`[${dataAttr}]`); |
15 | | - const max_value = document.querySelector(`[${max_attr}]`); |
16 | | - console.log(max_value); |
17 | | - |
| 14 | + const maxElement = maxAttr ? document.querySelector(`[${maxAttr}]`) : null; |
18 | 15 |
|
19 | 16 | if (!bar || !card) { |
20 | | - // console.warn(`Element not found for selector: ${selector} or data attribute: ${dataAttr}`); |
| 17 | + console.warn(`Element not found for selector: ${selector} or data attribute: ${dataAttr}`); |
21 | 18 | return; |
22 | 19 | } |
23 | 20 |
|
24 | | - // if max_attr is defined, use that to define the limits |
25 | | - if (max_value) { |
26 | | - const max = parseInt(card.getAttribute(max_attr), 10); |
27 | | - console.log("dataAttr: ", dataAttr, "max: ", max); |
28 | | - if (isNaN(max)) { |
29 | | - // console.warn(`Invalid max value for ${max_value}`); |
30 | | - return; |
31 | | - } |
| 21 | + let percentage = parseFloat(card.getAttribute(dataAttr)); |
32 | 22 |
|
33 | | - limits = [max * limits[0], max * limits[1]]; |
34 | | - console.log(`Limits for ${dataAttr}: ${limits}`); |
| 23 | + if (isNaN(percentage)) { |
| 24 | + console.warn(`Invalid percentage value for ${dataAttr}`); |
| 25 | + return; |
35 | 26 | } |
36 | 27 |
|
37 | | - const percentage = parseInt(card.getAttribute(dataAttr), 10); |
| 28 | + // If maxAttr is defined, use it to scale the limits |
| 29 | + if (maxElement) { |
| 30 | + const maxValue = parseFloat(maxElement.getAttribute(maxAttr)); |
| 31 | + if (isNaN(maxValue)) { |
| 32 | + console.warn(`Invalid max value for ${maxAttr}`); |
| 33 | + return; |
| 34 | + } |
38 | 35 |
|
39 | | - if (isNaN(percentage)) { |
40 | | - // console.warn(`Invalid percentage value for ${dataAttr}`); |
41 | | - return; |
| 36 | + limits = [maxValue * limits[0], maxValue * limits[1]]; |
42 | 37 | } |
43 | 38 |
|
| 39 | + // Apply class based on percentage within the defined limits |
44 | 40 | if (percentage <= limits[0]) { |
45 | 41 | bar.classList.add('low'); |
46 | 42 | } else if (percentage > limits[0] && percentage <= limits[1]) { |
|
0 commit comments