From 3980d66b49fb71cc3792d8676820cd909a7c8e9e Mon Sep 17 00:00:00 2001 From: CN-JS-HuiBai Date: Mon, 6 Apr 2026 15:32:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96CPU=E7=BB=98=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/js/app.js | 1 - server/prometheus-service.js | 50 +++++++----------------------------- 2 files changed, 9 insertions(+), 42 deletions(-) diff --git a/public/js/app.js b/public/js/app.js index aa45588..1c2fbdc 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -1340,7 +1340,6 @@ const cpuValueHtml = `
${formatPercent(data.cpuBusy)} - (IO Wait: ${data.cpuIowait.toFixed(1)}%, Busy Others: ${data.cpuOther.toFixed(1)}%)
`; // Define metrics to show diff --git a/server/prometheus-service.js b/server/prometheus-service.js index c348788..3ff45e3 100644 --- a/server/prometheus-service.js +++ b/server/prometheus-service.js @@ -549,14 +549,6 @@ async function getServerDetails(baseUrl, instance, job) { // Queries based on the requested dashboard structure const queries = { - // Split CPU - cpuSystem: `avg(rate(node_cpu_seconds_total{mode="system", instance="${node}"}[1m])) * 100`, - cpuUser: `avg(rate(node_cpu_seconds_total{mode="user", instance="${node}"}[1m])) * 100`, - cpuIowait: `avg(rate(node_cpu_seconds_total{mode="iowait", instance="${node}"}[1m])) * 100`, - cpuIrq: `avg(rate(node_cpu_seconds_total{mode=~"irq|softirq", instance="${node}"}[1m])) * 100`, - cpuOther: `avg(rate(node_cpu_seconds_total{mode=~"nice|steal|guest|guest_nice", instance="${node}"}[1m])) * 100`, - cpuIdle: `avg(rate(node_cpu_seconds_total{mode="idle", instance="${node}"}[1m])) * 100`, - cpuBusy: `100 * (1 - avg(rate(node_cpu_seconds_total{mode="idle", instance="${node}"}[1m])))`, sysLoad: `node_load1{instance="${node}",job="${job}"} * 100 / count(count(node_cpu_seconds_total{instance="${node}",job="${job}"}) by (cpu))`, memUsedPct: `(1 - (node_memory_MemAvailable_bytes{instance="${node}", job="${job}"} / node_memory_MemTotal_bytes{instance="${node}", job="${job}"})) * 100`, @@ -636,42 +628,18 @@ async function getServerHistory(baseUrl, instance, job, metric, range = '1h', st const url = normalizeUrl(baseUrl); const node = resolveToken(instance); - // Custom multi-metric handler for CPU Busy + // CPU Busy history: 100 - idle if (metric === 'cpuBusy') { - const modes = { - system: 'system', - user: 'user', - iowait: 'iowait', - irq: 'irq|softirq', - other: 'nice|steal|guest|guest_nice', - idle: 'idle' - }; - + const expr = `100 * (1 - avg(rate(node_cpu_seconds_total{mode="idle", instance="${node}"}[1m])))`; const rangeObj = parseRange(range, start, end); - const timestamps = []; - const series = {}; - Object.keys(modes).forEach(m => series[m] = []); - - const results = await Promise.all(Object.entries(modes).map(async ([name, mode]) => { - const expr = `avg(rate(node_cpu_seconds_total{mode=~"${mode}", instance="${node}"}[1m])) * 100`; - const res = await queryRange(url, expr, rangeObj.queryStart, rangeObj.queryEnd, rangeObj.step); - return { name, values: res.length > 0 ? res[0].values : [] }; - })); - - if (results[0].values.length === 0) return { timestamps: [], series: {} }; - - // Use first result for timestamps - results[0].values.forEach(v => timestamps.push(v[0] * 1000)); + const result = await queryRange(url, expr, rangeObj.queryStart, rangeObj.queryEnd, rangeObj.step); - results.forEach(r => { - r.values.forEach(v => series[r.name].push(parseFloat(v[1]))); - }); - - // Pre-calculate busy percentage: 100 - idle - const idleValues = series.idle || []; - const busyValues = idleValues.map(idleVal => Math.max(0, 100 - idleVal)); - - return { timestamps, series, values: busyValues }; + if (!result || result.length === 0) return { timestamps: [], values: [] }; + + return { + timestamps: result[0].values.map(v => v[0] * 1000), + values: result[0].values.map(v => parseFloat(v[1])) + }; } // Map metric keys to Prometheus expressions