mirror of
https://github.com/AvengeMedia/DankMaterialShell.git
synced 2025-12-07 05:55:37 -05:00
Implement some data smoothing
This commit is contained in:
@@ -71,7 +71,7 @@ Column {
|
|||||||
anchors.verticalCenter: parent.verticalCenter
|
anchors.verticalCenter: parent.verticalCenter
|
||||||
|
|
||||||
StyledText {
|
StyledText {
|
||||||
text: DankgopService.cpuUsage.toFixed(1) + "%"
|
text: DankgopService.smoothedCpuUsage.toFixed(1) + "%"
|
||||||
font.pixelSize: Theme.fontSizeSmall
|
font.pixelSize: Theme.fontSizeSmall
|
||||||
font.weight: Font.Bold
|
font.weight: Font.Bold
|
||||||
color: Theme.primary
|
color: Theme.primary
|
||||||
|
|||||||
@@ -62,10 +62,10 @@ Rectangle {
|
|||||||
name: DankgopService.getProcessIcon(process ? process.command : "")
|
name: DankgopService.getProcessIcon(process ? process.command : "")
|
||||||
size: Theme.iconSize - 4
|
size: Theme.iconSize - 4
|
||||||
color: {
|
color: {
|
||||||
if (process && process.cpu > 80)
|
if (process && process.smoothedCpu > 80)
|
||||||
return Theme.error
|
return Theme.error
|
||||||
|
|
||||||
if (process && process.cpu > 50)
|
if (process && process.smoothedCpu > 50)
|
||||||
return Theme.warning
|
return Theme.warning
|
||||||
|
|
||||||
return Theme.surfaceText
|
return Theme.surfaceText
|
||||||
@@ -95,10 +95,10 @@ Rectangle {
|
|||||||
height: 20
|
height: 20
|
||||||
radius: Theme.cornerRadius
|
radius: Theme.cornerRadius
|
||||||
color: {
|
color: {
|
||||||
if (process && process.cpu > 80)
|
if (process && process.smoothedCpu > 80)
|
||||||
return Qt.rgba(Theme.error.r, Theme.error.g, Theme.error.b, 0.12)
|
return Qt.rgba(Theme.error.r, Theme.error.g, Theme.error.b, 0.12)
|
||||||
|
|
||||||
if (process && process.cpu > 50)
|
if (process && process.smoothedCpu > 50)
|
||||||
return Qt.rgba(Theme.warning.r, Theme.warning.g,
|
return Qt.rgba(Theme.warning.r, Theme.warning.g,
|
||||||
Theme.warning.b, 0.12)
|
Theme.warning.b, 0.12)
|
||||||
|
|
||||||
@@ -110,15 +110,15 @@ Rectangle {
|
|||||||
anchors.verticalCenter: parent.verticalCenter
|
anchors.verticalCenter: parent.verticalCenter
|
||||||
|
|
||||||
StyledText {
|
StyledText {
|
||||||
text: DankgopService.formatCpuUsage(process ? process.cpu : 0)
|
text: DankgopService.formatCpuUsage(process ? process.smoothedCpu : 0)
|
||||||
font.pixelSize: Theme.fontSizeSmall
|
font.pixelSize: Theme.fontSizeSmall
|
||||||
font.family: SettingsData.monoFontFamily
|
font.family: SettingsData.monoFontFamily
|
||||||
font.weight: Font.Bold
|
font.weight: Font.Bold
|
||||||
color: {
|
color: {
|
||||||
if (process && process.cpu > 80)
|
if (process && process.smoothedCpu > 80)
|
||||||
return Theme.error
|
return Theme.error
|
||||||
|
|
||||||
if (process && process.cpu > 50)
|
if (process && process.smoothedCpu > 50)
|
||||||
return Theme.warning
|
return Theme.warning
|
||||||
|
|
||||||
return Theme.surfaceText
|
return Theme.surfaceText
|
||||||
|
|||||||
@@ -57,9 +57,9 @@ Row {
|
|||||||
|
|
||||||
StyledText {
|
StyledText {
|
||||||
text: {
|
text: {
|
||||||
if (DankgopService.cpuUsage === undefined || DankgopService.cpuUsage === null)
|
if (DankgopService.smoothedCpuUsage === undefined || DankgopService.smoothedCpuUsage === null)
|
||||||
return "--%"
|
return "--%"
|
||||||
return DankgopService.cpuUsage.toFixed(1) + "%"
|
return DankgopService.smoothedCpuUsage.toFixed(1) + "%"
|
||||||
}
|
}
|
||||||
font.pixelSize: Theme.fontSizeLarge
|
font.pixelSize: Theme.fontSizeLarge
|
||||||
font.family: SettingsData.monoFontFamily
|
font.family: SettingsData.monoFontFamily
|
||||||
|
|||||||
@@ -59,10 +59,10 @@ Rectangle {
|
|||||||
name: "memory"
|
name: "memory"
|
||||||
size: Theme.iconSize - 8
|
size: Theme.iconSize - 8
|
||||||
color: {
|
color: {
|
||||||
if (DankgopService.cpuUsage > 80)
|
if (DankgopService.smoothedCpuUsage > 80)
|
||||||
return Theme.tempDanger
|
return Theme.tempDanger
|
||||||
|
|
||||||
if (DankgopService.cpuUsage > 60)
|
if (DankgopService.smoothedCpuUsage > 60)
|
||||||
return Theme.tempWarning
|
return Theme.tempWarning
|
||||||
|
|
||||||
return Theme.surfaceText
|
return Theme.surfaceText
|
||||||
@@ -72,12 +72,12 @@ Rectangle {
|
|||||||
|
|
||||||
StyledText {
|
StyledText {
|
||||||
text: {
|
text: {
|
||||||
if (DankgopService.cpuUsage === undefined
|
if (DankgopService.smoothedCpuUsage === undefined
|
||||||
|| DankgopService.cpuUsage === null
|
|| DankgopService.smoothedCpuUsage === null
|
||||||
|| DankgopService.cpuUsage === 0) {
|
|| DankgopService.smoothedCpuUsage === 0) {
|
||||||
return "--%"
|
return "--%"
|
||||||
}
|
}
|
||||||
return DankgopService.cpuUsage.toFixed(0) + "%"
|
return DankgopService.smoothedCpuUsage.toFixed(0) + "%"
|
||||||
}
|
}
|
||||||
font.pixelSize: Theme.fontSizeSmall
|
font.pixelSize: Theme.fontSizeSmall
|
||||||
font.weight: Font.Medium
|
font.weight: Font.Medium
|
||||||
|
|||||||
@@ -72,6 +72,8 @@ Singleton {
|
|||||||
property var memoryHistory: []
|
property var memoryHistory: []
|
||||||
property var networkHistory: ({ "rx": [], "tx": [] })
|
property var networkHistory: ({ "rx": [], "tx": [] })
|
||||||
property var diskHistory: ({ "read": [], "write": [] })
|
property var diskHistory: ({ "read": [], "write": [] })
|
||||||
|
property var processCpuHistory: ({})
|
||||||
|
property real smoothedCpuUsage: 0
|
||||||
|
|
||||||
function addRef(modules = null) {
|
function addRef(modules = null) {
|
||||||
refCount++
|
refCount++
|
||||||
@@ -124,6 +126,13 @@ Singleton {
|
|||||||
enabledModules.splice(index, 1)
|
enabledModules.splice(index, 1)
|
||||||
modulesChanged = true
|
modulesChanged = true
|
||||||
console.log("Disabling module:", module, "(no more refs)")
|
console.log("Disabling module:", module, "(no more refs)")
|
||||||
|
|
||||||
|
// Clear sample data when module is disabled
|
||||||
|
if (module === "cpu") {
|
||||||
|
cpuSampleData = null
|
||||||
|
} else if (module === "processes") {
|
||||||
|
procSampleData = null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -274,6 +283,7 @@ Singleton {
|
|||||||
cpuModel = cpu.model || ""
|
cpuModel = cpu.model || ""
|
||||||
perCoreCpuUsage = cpu.coreUsage || []
|
perCoreCpuUsage = cpu.coreUsage || []
|
||||||
addToHistory(cpuHistory, cpuUsage)
|
addToHistory(cpuHistory, cpuUsage)
|
||||||
|
updateSmoothedCpuUsage()
|
||||||
|
|
||||||
// Update CPU sample data for the next run
|
// Update CPU sample data for the next run
|
||||||
if (cpu.total && cpu.cores) {
|
if (cpu.total && cpu.cores) {
|
||||||
@@ -360,11 +370,34 @@ Singleton {
|
|||||||
if (data.processes && Array.isArray(data.processes)) {
|
if (data.processes && Array.isArray(data.processes)) {
|
||||||
const newProcesses = []
|
const newProcesses = []
|
||||||
const newProcSample = []
|
const newProcSample = []
|
||||||
|
const currentPids = {}
|
||||||
|
|
||||||
for (const proc of data.processes) {
|
for (const proc of data.processes) {
|
||||||
|
const pid = proc.pid || 0
|
||||||
|
if (pid === 0) continue
|
||||||
|
|
||||||
|
currentPids[pid] = true
|
||||||
|
const cpu = proc.cpu || 0
|
||||||
|
|
||||||
|
let history = processCpuHistory[pid]
|
||||||
|
if (!history) {
|
||||||
|
history = []
|
||||||
|
}
|
||||||
|
|
||||||
|
history.push(cpu)
|
||||||
|
if (history.length > 3) {
|
||||||
|
history.shift()
|
||||||
|
}
|
||||||
|
processCpuHistory[pid] = history
|
||||||
|
|
||||||
|
const sum = history.reduce((a, b) => a + b, 0)
|
||||||
|
const smoothedCpu = sum / history.length
|
||||||
|
|
||||||
newProcesses.push({
|
newProcesses.push({
|
||||||
"pid": proc.pid || 0,
|
"pid": pid,
|
||||||
"ppid": proc.ppid || 0,
|
"ppid": proc.ppid || 0,
|
||||||
"cpu": proc.cpu || 0,
|
"cpu": cpu,
|
||||||
|
"smoothedCpu": smoothedCpu,
|
||||||
"memoryPercent": proc.memoryPercent || proc.pssPercent || 0,
|
"memoryPercent": proc.memoryPercent || proc.pssPercent || 0,
|
||||||
"memoryKB": proc.memoryKB || proc.pssKB || 0,
|
"memoryKB": proc.memoryKB || proc.pssKB || 0,
|
||||||
"command": proc.command || "",
|
"command": proc.command || "",
|
||||||
@@ -385,6 +418,14 @@ Singleton {
|
|||||||
if (newProcSample.length > 0) {
|
if (newProcSample.length > 0) {
|
||||||
procSampleData = newProcSample
|
procSampleData = newProcSample
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Garbage collect old PIDs from history
|
||||||
|
for (const pid in processCpuHistory) {
|
||||||
|
if (!currentPids[pid]) {
|
||||||
|
delete processCpuHistory[pid]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
processCpuHistory = Object.assign({}, processCpuHistory)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle both gpu and gpu-temp module data
|
// Handle both gpu and gpu-temp module data
|
||||||
@@ -444,6 +485,16 @@ Singleton {
|
|||||||
isUpdating = false
|
isUpdating = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function updateSmoothedCpuUsage() {
|
||||||
|
if (cpuHistory.length === 0) {
|
||||||
|
smoothedCpuUsage = 0
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const valuesToAverage = cpuHistory.slice(-3) // Average last 3 values
|
||||||
|
const sum = valuesToAverage.reduce((a, b) => a + b, 0)
|
||||||
|
smoothedCpuUsage = sum / valuesToAverage.length
|
||||||
|
}
|
||||||
|
|
||||||
function addToHistory(array, value) {
|
function addToHistory(array, value) {
|
||||||
array.push(value)
|
array.push(value)
|
||||||
if (array.length > historySize) {
|
if (array.length > historySize) {
|
||||||
|
|||||||
Reference in New Issue
Block a user