Updated the logic for Stat/Metric tracking in the dashboard more robust. (#365)

* Updated the logic for Stat/Metric tracking in the dashboard more robust.

* Show used/total for stats.

* Added the ability to configure the update interval of metrics in the dashboard.
This commit is contained in:
Jas Singh
2024-10-24 02:26:39 -07:00
committed by GitHub
parent 694711e0d4
commit fcdba86fec
10 changed files with 223 additions and 95 deletions

41
services/Cpu.ts Normal file
View File

@@ -0,0 +1,41 @@
// TODO: Convert to a real service
// @ts-expect-error: This import is a special directive that tells the compiler to use the GTop library
import GTop from 'gi://GTop';
import { pollVariable } from 'customModules/PollVar';
class Cpu {
private updateFrequency = Variable(2000);
public cpu = Variable(0);
private previousCpuData = new GTop.glibtop_cpu();
constructor() {
GTop.glibtop_get_cpu(this.previousCpuData);
this.calculateUsage = this.calculateUsage.bind(this);
pollVariable(this.cpu, [], this.updateFrequency.bind('value'), this.calculateUsage);
}
public calculateUsage(): number {
const currentCpuData = new GTop.glibtop_cpu();
GTop.glibtop_get_cpu(currentCpuData);
// Calculate the differences from the previous to current data
const totalDiff = currentCpuData.total - this.previousCpuData.total;
const idleDiff = currentCpuData.idle - this.previousCpuData.idle;
const cpuUsagePercentage = totalDiff > 0 ? ((totalDiff - idleDiff) / totalDiff) * 100 : 0;
this.previousCpuData = currentCpuData;
return cpuUsagePercentage;
}
public updateTimer(timerInMs: number): void {
this.updateFrequency.value = timerInMs;
}
}
export default Cpu;

73
services/Ram.ts Normal file
View File

@@ -0,0 +1,73 @@
// TODO: Convert to a real service
const GLib = imports.gi.GLib;
import { pollVariable } from 'customModules/PollVar';
import { GenericResourceData } from 'lib/types/customModules/generic';
class Ram {
private updateFrequency = Variable(2000);
private shouldRound = false;
public ram = Variable<GenericResourceData>({ total: 0, used: 0, percentage: 0, free: 0 });
constructor() {
this.calculateUsage = this.calculateUsage.bind(this);
pollVariable(this.ram, [], this.updateFrequency.bind('value'), this.calculateUsage);
}
public calculateUsage(): GenericResourceData {
try {
const [success, meminfoBytes] = GLib.file_get_contents('/proc/meminfo');
if (!success || !meminfoBytes) {
throw new Error('Failed to read /proc/meminfo or file content is null.');
}
const meminfo = new TextDecoder('utf-8').decode(meminfoBytes);
const totalMatch = meminfo.match(/MemTotal:\s+(\d+)/);
const availableMatch = meminfo.match(/MemAvailable:\s+(\d+)/);
if (!totalMatch || !availableMatch) {
throw new Error('Failed to parse /proc/meminfo for memory values.');
}
const totalRamInBytes = parseInt(totalMatch[1], 10) * 1024;
const availableRamInBytes = parseInt(availableMatch[1], 10) * 1024;
let usedRam = totalRamInBytes - availableRamInBytes;
usedRam = isNaN(usedRam) || usedRam < 0 ? 0 : usedRam;
return {
percentage: this.divide([totalRamInBytes, usedRam]),
total: totalRamInBytes,
used: usedRam,
free: availableRamInBytes,
};
} catch (error) {
console.error('Error calculating RAM usage:', error);
return { total: 0, used: 0, percentage: 0, free: 0 };
}
}
public setShouldRound(round: boolean): void {
this.shouldRound = round;
}
private divide([total, used]: number[]): number {
const percentageTotal = (used / total) * 100;
if (this.shouldRound) {
return total > 0 ? Math.round(percentageTotal) : 0;
}
return total > 0 ? parseFloat(percentageTotal.toFixed(2)) : 0;
}
updateTimer(timerInMs: number): void {
this.updateFrequency.value = timerInMs;
}
}
export default Ram;

61
services/Storage.ts Normal file
View File

@@ -0,0 +1,61 @@
// TODO: Convert to a real service
// @ts-expect-error: This import is a special directive that tells the compiler to use the GTop library
import GTop from 'gi://GTop';
import { pollVariable } from 'customModules/PollVar';
import { GenericResourceData } from 'lib/types/customModules/generic';
class Storage {
private updateFrequency = Variable(2000);
private shouldRound = false;
public storage = Variable<GenericResourceData>({ total: 0, used: 0, percentage: 0, free: 0 });
constructor() {
this.calculateUsage = this.calculateUsage.bind(this);
pollVariable(this.storage, [], this.updateFrequency.bind('value'), this.calculateUsage);
}
public calculateUsage(): GenericResourceData {
try {
const currentFsUsage = new GTop.glibtop_fsusage();
GTop.glibtop_get_fsusage(currentFsUsage, '/');
const total = currentFsUsage.blocks * currentFsUsage.block_size;
const available = currentFsUsage.bavail * currentFsUsage.block_size;
const used = total - available;
return {
total,
used,
free: available,
percentage: this.divide([total, used]),
};
} catch (error) {
console.error('Error calculating Storage usage:', error);
return { total: 0, used: 0, percentage: 0, free: 0 };
}
}
public setShouldRound(round: boolean): void {
this.shouldRound = round;
}
private divide([total, used]: number[]): number {
const percentageTotal = (used / total) * 100;
if (this.shouldRound) {
return total > 0 ? Math.round(percentageTotal) : 0;
}
return total > 0 ? parseFloat(percentageTotal.toFixed(2)) : 0;
}
public updateTimer(timerInMs: number): void {
this.updateFrequency.value = timerInMs;
}
}
export default Storage;