From a95aeceed4a4f8bcc89f3474baccca962bef5c79 Mon Sep 17 00:00:00 2001 From: Konstantina Chremmou Date: Sat, 31 Jul 2021 23:54:03 +0100 Subject: [PATCH] Removed id from the DataSet constructor parameters. It can be constructed from the XenObject and the datasource name. Signed-off-by: Konstantina Chremmou --- .../CustomDataGraph/ArchiveMaintainer.cs | 28 ++++++++++-- .../Controls/CustomDataGraph/DataArchive.cs | 2 +- XenAdmin/Controls/CustomDataGraph/DataSet.cs | 43 +++++-------------- 3 files changed, 36 insertions(+), 37 deletions(-) diff --git a/XenAdmin/Controls/CustomDataGraph/ArchiveMaintainer.cs b/XenAdmin/Controls/CustomDataGraph/ArchiveMaintainer.cs index 1e38b20a3..eecf4ed72 100644 --- a/XenAdmin/Controls/CustomDataGraph/ArchiveMaintainer.cs +++ b/XenAdmin/Controls/CustomDataGraph/ArchiveMaintainer.cs @@ -33,6 +33,7 @@ using System; using System.Collections.Generic; using System.Globalization; using System.IO; +using System.Linq; using System.Net; using System.Threading; using System.Xml; @@ -443,8 +444,7 @@ namespace XenAdmin.Controls.CustomDataGraph if (LastNode == "name") { string str = reader.ReadContentAsString(); - string id = string.Format("{0}:{1}:{2}", xmo is Host ? "host" : "vm", Helpers.GetUuid(xmo), str); - SetsAdded.Add(DataSet.Create(id, xmo, false, str)); + SetsAdded.Add(DataSet.Create(xmo, false, str)); } else if (LastNode == "step") { @@ -509,7 +509,29 @@ namespace XenAdmin.Controls.CustomDataGraph if (LastNode == "entry") { string str = reader.ReadContentAsString(); - SetsAdded.Add(DataSet.Create(str, xo)); + DataSet set = null; + + if (DataSet.ParseId(str, out string objType, out string objUuid, out string dataSourceName)) + { + if (objType == "host") + { + Host host = xo.Connection.Cache.Hosts.FirstOrDefault(h => h.uuid == objUuid); + if (host != null) + set = DataSet.Create(host, (xo as Host)?.uuid != objUuid, dataSourceName); + } + + if (objType == "vm") + { + VM vm = xo.Connection.Cache.VMs.FirstOrDefault(v => v.uuid == objUuid); + if (vm != null) + set = DataSet.Create(vm, (xo as VM)?.uuid != objUuid, dataSourceName); + } + } + + if (set == null) + set = DataSet.Create(null, true, str); + + SetsAdded.Add(set); } else if (LastNode == "t") { diff --git a/XenAdmin/Controls/CustomDataGraph/DataArchive.cs b/XenAdmin/Controls/CustomDataGraph/DataArchive.cs index b43bd1418..fc7773306 100644 --- a/XenAdmin/Controls/CustomDataGraph/DataArchive.cs +++ b/XenAdmin/Controls/CustomDataGraph/DataArchive.cs @@ -110,7 +110,7 @@ namespace XenAdmin.Controls.CustomDataGraph foreach (DataSet set in SetsAdded) { Palette.LoadSetColor(set); - DataSet copy = DataSet.Create(set.Id, set.XenObject, set.Hide, set.DataSourceName); + DataSet copy = DataSet.Create(set.XenObject, set.Hide, set.DataSourceName); foreach (DataPoint p in set.Points) copy.AddPoint(new DataPoint(p.X,p.Y)); diff --git a/XenAdmin/Controls/CustomDataGraph/DataSet.cs b/XenAdmin/Controls/CustomDataGraph/DataSet.cs index f815fee59..71e3be1a8 100644 --- a/XenAdmin/Controls/CustomDataGraph/DataSet.cs +++ b/XenAdmin/Controls/CustomDataGraph/DataSet.cs @@ -53,7 +53,7 @@ namespace XenAdmin.Controls.CustomDataGraph public bool Selected; public List CurrentlyDisplayed = new List(); public IXenObject XenObject; - public readonly string Id; + public readonly string Id = ""; public string DataSourceName; public string FriendlyName { get; } private int MultiplyingFactor = 1; @@ -61,13 +61,17 @@ namespace XenAdmin.Controls.CustomDataGraph public bool Hide { get; } - private DataSet(string id, IXenObject xo, bool hide, string datasourceName) + private DataSet(IXenObject xo, bool hide, string datasourceName) { XenObject = xo; Hide = hide; - Id = id; DataSourceName = datasourceName; + if (xo is Host host) + Id = $"host:{host.uuid}:{datasourceName}"; + else if (xo is VM vm) + Id = $"vm:{vm.uuid}:{datasourceName}"; + if (datasourceName == "memory_free_kib") FriendlyName = Helpers.GetFriendlyDataSourceName("memory_used_kib", xo); else if (datasourceName == "memory_internal_free") @@ -76,9 +80,8 @@ namespace XenAdmin.Controls.CustomDataGraph FriendlyName = Helpers.GetFriendlyDataSourceName(datasourceName, xo); } - #region Static methods - public static DataSet Create(string id, IXenObject xo, bool hide, string settype) + public static DataSet Create(IXenObject xo, bool hide, string settype) { if(settype == "xapi_open_fds" || settype == "pool_task_count" || @@ -89,7 +92,7 @@ namespace XenAdmin.Controls.CustomDataGraph hide = true; //overrides passed in value } - var dataSet = new DataSet(id, xo, hide, settype); + var dataSet = new DataSet(xo, hide, settype); if (settype.StartsWith("latency") || settype.EndsWith("latency")) { @@ -244,30 +247,6 @@ namespace XenAdmin.Controls.CustomDataGraph return dataSet; } - public static DataSet Create(string id, IXenObject xenObject) - { - if (ParseId(id, out string objType, out string objUuid, out string dataSourceName)) - { - string theId = $"{objType}:{objUuid}:{dataSourceName}"; - - if (objType == "host") - { - Host host = xenObject.Connection.Cache.Find_By_Uuid(objUuid); - if (host != null) - return Create(theId, host, (xenObject as Host)?.uuid != objUuid, dataSourceName); - } - - if (objType == "vm") - { - VM vm = xenObject.Connection.Cache.Find_By_Uuid(objUuid); - if (vm != null) - return Create(theId, vm, (xenObject as VM)?.uuid != objUuid, dataSourceName); - } - } - - return Create(id, null, true, id); - } - public static bool ParseId(string id, out string objType, out string objUuid, out string dataSourceName) { var bits = id.Split(':').ToList(); @@ -289,8 +268,6 @@ namespace XenAdmin.Controls.CustomDataGraph return false; } - #endregion - public List GetRange(DataTimeRange xrange, long intervalneed, long intervalat) { List fine = BinaryChop(Points, xrange); @@ -466,7 +443,7 @@ namespace XenAdmin.Controls.CustomDataGraph DataSet other = setsAdded.FirstOrDefault(s => s.DataSourceName == "avg_cpu"); if (other == null) { - other = Create(Palette.GetUuid("avg_cpu", XenObject), XenObject, false, "avg_cpu"); + other = Create(XenObject, false, "avg_cpu"); setsAdded.Add(other); }