Skip to content

Commit

Permalink
Address more coderabbit feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Thaler <[email protected]>
  • Loading branch information
dthaler committed Oct 25, 2024
1 parent 467e3ce commit a06d79f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
29 changes: 24 additions & 5 deletions OrcanodeMonitor/Models/Orcanode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,24 @@ public string OrcasoundOnlineStatusString {

public OrcanodeIftttDTO ToIftttDTO() => new OrcanodeIftttDTO(ID, DisplayName);

/// <summary>
/// Calculates the uptime percentage for a node based on its events since a specified date.
/// </summary>
/// <param name="orcanodeId">The ID of the node to calculate uptime for</param>
/// <param name="events">List of node events</param>
/// <param name="since">The start date for uptime calculation</param>
/// <returns>Uptime percentage as an integer between 0 and 100</returns>
/// <exception cref="ArgumentException">Thrown when orcanodeId is null or empty</exception>
public static int GetUptimePercentage(string orcanodeId, List<OrcanodeEvent> events, DateTime since)
{
if (string.IsNullOrEmpty(orcanodeId))
{
throw new ArgumentException("Node ID cannot be null or empty", nameof(orcanodeId));
}
if (since > DateTime.UtcNow)
{
throw new ArgumentException("Start date cannot be in the future", nameof(since));
}
if (events == null)
{
return 0;
Expand All @@ -404,7 +420,7 @@ public static int GetUptimePercentage(string orcanodeId, List<OrcanodeEvent> eve
DateTime start = since;
string lastValue = string.Empty;

// Get events sorted by date to ensure correct chronological processing
// Get events sorted by date to ensure correct chronological processing.
var nodeEvents = events
.Where(e => e.OrcanodeId == orcanodeId)
.OrderBy(e => e.DateTimeUtc)
Expand All @@ -420,7 +436,7 @@ public static int GetUptimePercentage(string orcanodeId, List<OrcanodeEvent> eve
continue;
}
DateTime current = e.DateTimeUtc;
if (lastValue == Orcanode.OnlineString)
if (lastValue == OnlineString)
{
up += (current - start);
}
Expand All @@ -431,13 +447,16 @@ public static int GetUptimePercentage(string orcanodeId, List<OrcanodeEvent> eve
start = current;
lastValue = e.Value;
}
if (lastValue == Orcanode.OnlineString)

// Account for the reminder of the time until now.
DateTime now = DateTime.UtcNow;
if (lastValue == OnlineString)
{
up += DateTime.UtcNow - start;
up += now - start;
}
else
{
down += DateTime.UtcNow - start;
down += now - start;
}

TimeSpan totalTime = up + down;
Expand Down
3 changes: 2 additions & 1 deletion OrcanodeMonitor/Pages/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@
</td>
}
<td style="background-color: @Model.NodeUptimePercentageBackgroundColor(item)">
<a asp-page="/NodeEvents" asp-route-id="@item.ID" style="color: @Model.NodeUptimePercentageTextColor(item)" target="_blank">
<a asp-page="/NodeEvents" asp-route-id="@item.ID" style="color: @Model.NodeUptimePercentageTextColor(item)" target="_blank"
aria-label="View events for @item.DisplayName (opens in new tab)">
@Model.GetUptimePercentage(item)%
</a>
</td>
Expand Down
2 changes: 1 addition & 1 deletion OrcanodeMonitor/Pages/Index.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private string GetTextColor(OrcanodeOnlineStatus status)

public string NodeUptimePercentageBackgroundColor(Orcanode node)
{
int value = Orcanode.GetUptimePercentage(node.ID, _events, SinceTime);
int value = GetUptimePercentage(node);
if (value < 1)
{
return ColorTranslator.ToHtml(Color.Red);
Expand Down
1 change: 0 additions & 1 deletion OrcanodeMonitor/Pages/NodeEvents.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using OrcanodeMonitor.Core;
using OrcanodeMonitor.Data;
using OrcanodeMonitor.Models;
using System.Xml.Linq;

namespace OrcanodeMonitor.Pages
{
Expand Down

0 comments on commit a06d79f

Please sign in to comment.