From 53d1a643b95b067438bbc48934069d761785ec91 Mon Sep 17 00:00:00 2001 From: Rory& Date: Sat, 20 Jan 2024 16:12:11 +0100 Subject: Add graph, add start/stop/kill/restart --- LibSystemdCli.Models/SystemdServiceData.cs | 33 ++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) (limited to 'LibSystemdCli.Models') diff --git a/LibSystemdCli.Models/SystemdServiceData.cs b/LibSystemdCli.Models/SystemdServiceData.cs index 1efcfd7..8d35c26 100644 --- a/LibSystemdCli.Models/SystemdServiceData.cs +++ b/LibSystemdCli.Models/SystemdServiceData.cs @@ -9,14 +9,30 @@ public class SystemdServiceData { [JsonPropertyName("multiData")] public Dictionary> MultiData { get; set; } = new(); - public bool IsRunning => Status is "active" or "reloading"; + public bool IsRunning => Status is "active" or "reloading" or "deactating" or "activating"; public string Status => GetSingleData("ActiveState") ?? "unknown"; + + public long MemoryCurrent { + get { + if (long.TryParse(GetSingleData("MemoryCurrent") ?? "0", out long mem)) return mem; + return 0; + } + } + + public long MemoryPeak { + get { + if (long.TryParse(GetSingleData("MemoryPeak") ?? "0", out long mem)) return mem; + return 0; + } + } + + public void AddData(string key, string value) { if (MultiData.ContainsKey(key)) { MultiData[key].Add(value); } else if (SingleData.ContainsKey(key)) { - MultiData[key] = new List { SingleData[key], value }; + MultiData[key] = [SingleData[key], value]; SingleData.Remove(key); } else { @@ -43,4 +59,17 @@ public class SystemdServiceData { return null; } + + public void SetOldData(SystemdServiceData oldData) { + var now = DateTime.Now; + + var cpuDiff = CpuUsageNs - oldData.CpuUsageNs; + var timeDiff = now - oldData.Timestamp; + CpuUsagePercent = cpuDiff / timeDiff.TotalMilliseconds / 1_000_000d; + } + + public DateTime Timestamp { get; set; } = DateTime.Now; + public long CpuUsageNs => long.Parse(GetSingleData("CPUUsageNSec") ?? "0"); + + public double CpuUsagePercent { get; set; } } \ No newline at end of file -- cgit 1.4.1