From fc09da564906cd025d6d5e441cc6c0bd8e5e34be Mon Sep 17 00:00:00 2001 From: Dave Thaler Date: Thu, 5 Sep 2024 09:41:11 -0700 Subject: [PATCH] Add Dataplicity Absent status (#117) * Set Dataplicity status to Absent for a node that is not returned by dataplicity (addresses part of #114) * Set Orcasound status to Absent for a node that is not returned by Orcasite (addresses part of #114) * Update legend (fixes #113) * Fix bug when a node is on dataplicity but not orcasite Signed-off-by: Dave Thaler --- OrcanodeMonitor/Core/Fetcher.cs | 42 ++++++++++++++++++++++++++++++ OrcanodeMonitor/Models/Orcanode.cs | 12 ++++++++- OrcanodeMonitor/Pages/Index.cshtml | 6 +++++ 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/OrcanodeMonitor/Core/Fetcher.cs b/OrcanodeMonitor/Core/Fetcher.cs index 99089f0..ba52d12 100644 --- a/OrcanodeMonitor/Core/Fetcher.cs +++ b/OrcanodeMonitor/Core/Fetcher.cs @@ -289,6 +289,9 @@ public async static Task UpdateDataplicityDataAsync(OrcanodeMonitorContext conte jsonArray = await response.Content.ReadAsStringAsync(); } + // Create a list to track what nodes are no longer returned. + var unfoundList = context.Orcanodes.ToList(); + dynamic deviceArray = JsonSerializer.Deserialize(jsonArray); if (deviceArray.ValueKind != JsonValueKind.Array) { @@ -301,6 +304,13 @@ public async static Task UpdateDataplicityDataAsync(OrcanodeMonitorContext conte continue; } + // Remove the found node from the unfound list. + Orcanode? oldListNode = unfoundList.Find(a => a.DataplicitySerial == serial.ToString()); + if (oldListNode != null) + { + unfoundList.Remove(oldListNode); + } + Orcanode node = FindOrCreateOrcanodeByDataplicitySerial(context.Orcanodes, serial.ToString(), out OrcanodeOnlineStatus oldStatus); OrcanodeUpgradeStatus oldAgentUpgradeStatus = node.DataplicityUpgradeStatus; long oldDiskCapacityInGigs = node.DiskCapacityInGigs; @@ -375,6 +385,16 @@ public async static Task UpdateDataplicityDataAsync(OrcanodeMonitorContext conte } } + // Mark any remaining unfound nodes as absent. + foreach (var unfoundNode in unfoundList) + { + var oldNode = FindOrcanodeByDataplicitySerial(context.Orcanodes, unfoundNode.DataplicitySerial, out OrcanodeOnlineStatus unfoundNodeStatus); + if (oldNode != null) + { + oldNode.DataplicityOnline = null; + } + } + MonitorState.GetFrom(context).LastUpdatedTimestampUtc = DateTime.UtcNow; await context.SaveChangesAsync(); } @@ -408,6 +428,10 @@ public async static Task UpdateOrcasoundDataAsync(OrcanodeMonitorContext context { return; } + + // Create a list to track what nodes are no longer returned. + var unfoundList = context.Orcanodes.ToList(); + foreach (JsonElement feed in dataArray.EnumerateArray()) { if (!feed.TryGetProperty("id", out var feedId)) @@ -434,6 +458,13 @@ public async static Task UpdateOrcasoundDataAsync(OrcanodeMonitorContext context } string dataplicitySerial = dataplicity_id.ToString(); + // Remove the found node from the unfound list. + Orcanode? oldListNode = unfoundList.Find(a => a.OrcasoundFeedId == feedId.ToString()); + if (oldListNode != null) + { + unfoundList.Remove(oldListNode); + } + Orcanode? node = null; node = FindOrcanodeByOrcasoundFeedId(context.Orcanodes, feedId.ToString()); if (node == null) @@ -512,6 +543,17 @@ public async static Task UpdateOrcasoundDataAsync(OrcanodeMonitorContext context node.OrcasoundVisible = visible.GetBoolean(); } } + + // Mark any remaining unfound nodes as absent. + foreach (var unfoundNode in unfoundList) + { + var oldNode = FindOrcanodeByOrcasoundFeedId(context.Orcanodes, unfoundNode.OrcasoundFeedId); + if (oldNode != null) + { + oldNode.OrcasoundFeedId = String.Empty; + } + } + await context.SaveChangesAsync(); } catch (Exception ex) diff --git a/OrcanodeMonitor/Models/Orcanode.cs b/OrcanodeMonitor/Models/Orcanode.cs index b4df61c..408de43 100644 --- a/OrcanodeMonitor/Models/Orcanode.cs +++ b/OrcanodeMonitor/Models/Orcanode.cs @@ -50,6 +50,7 @@ public Orcanode() // Initialize reference types. OrcasoundName = string.Empty; OrcasoundSlug = string.Empty; + OrcasoundFeedId = string.Empty; S3Bucket = string.Empty; S3NodeName = string.Empty; AgentVersion = string.Empty; @@ -231,7 +232,16 @@ private static double MinIntelligibleStreamDeviation public long DiskCapacityInGigs => DiskCapacity / 1000000000; public OrcanodeUpgradeStatus DataplicityUpgradeStatus => DataplicityUpgradeAvailable ?? false ? OrcanodeUpgradeStatus.UpgradeAvailable : OrcanodeUpgradeStatus.UpToDate; - public OrcanodeOnlineStatus DataplicityConnectionStatus => DataplicityOnline ?? false ? OrcanodeOnlineStatus.Online : OrcanodeOnlineStatus.Offline; + public OrcanodeOnlineStatus DataplicityConnectionStatus { + get + { + if (!DataplicityOnline.HasValue) + { + return OrcanodeOnlineStatus.Absent; + } + return (DataplicityOnline.Value) ? OrcanodeOnlineStatus.Online : OrcanodeOnlineStatus.Offline; + } + } #if ORCAHELLO public string OrcaHelloName diff --git a/OrcanodeMonitor/Pages/Index.cshtml b/OrcanodeMonitor/Pages/Index.cshtml index e5a48a4..80547aa 100644 --- a/OrcanodeMonitor/Pages/Index.cshtml +++ b/OrcanodeMonitor/Pages/Index.cshtml @@ -96,6 +96,9 @@

Legend

    +
  • + Dataplicity Absent: Dataplicity does not know about the node. +
  • Dataplicity Offline: Dataplicity connection is down.
  • @@ -105,6 +108,9 @@
  • Orcasound Absent: live.orcasound.net does not know about the node.
  • +
  • + Orcasound Hidden: live.orcasound.net knows about but does not display the node. +
  • Orcasound Online: live.orcasound.net knows about the node.