Skip to content

Commit

Permalink
Add uptime column to the dashboard (#170)
Browse files Browse the repository at this point in the history
* Add uptime column to the dashboard
* Add color to column

Signed-off-by: Dave Thaler <[email protected]>
  • Loading branch information
dthaler authored Oct 23, 2024
1 parent 4827902 commit 4695e6f
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
4 changes: 3 additions & 1 deletion OrcanodeMonitor/Models/Orcanode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -374,14 +374,16 @@ public string Type
}
}

public static string OnlineString => "up";

public string OrcasoundOnlineStatusString {
get
{
// Snapshot the status.
OrcanodeOnlineStatus status = S3StreamStatus;

// Convert to a display string.
return (status == OrcanodeOnlineStatus.Online) ? "up" : S3StreamStatus.ToString().ToUpper();
return (status == OrcanodeOnlineStatus.Online) ? OnlineString : S3StreamStatus.ToString().ToUpper();
}
}
#endregion derived
Expand Down
4 changes: 4 additions & 0 deletions OrcanodeMonitor/Pages/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<th>SD Card Util.</th>
<th><a href="https://app.mezmo.com/313dbd82f3/logs/view" target="_blank">Mezmo</a></th>
<th><a href="https://open.quiltdata.com/b/audio-orcasound-net/tree/" target="_blank">S3 Stream</a></th>
<th>Up%</th>
<th><a href="https://live.orcasound.net/listen" target="_blank">Orcasound</a></th>
<th><a href="https://aifororcas2.azurewebsites.net/hydrophones" target="_blank">OrcaHello</a></th>
</tr>
Expand Down Expand Up @@ -70,6 +71,9 @@
</a>
</td>
}
<td style="background-color: @Model.NodeUptimePercentageBackgroundColor(item); color: @Model.NodeUptimePercentageTextColor(item)">
@Model.GetUptimePercentage(item)%
</td>
@if (item.OrcasoundStatus == Models.OrcanodeOnlineStatus.Absent)
{
<td style="background-color: @Model.NodeOrcasoundBackgroundColor(item); color: @Model.NodeOrcasoundTextColor(item)">
Expand Down
79 changes: 79 additions & 0 deletions OrcanodeMonitor/Pages/Index.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ public class IndexModel : PageModel
{
private OrcanodeMonitorContext _databaseContext;
private readonly ILogger<IndexModel> _logger;
private List<OrcanodeEvent> _events;
private List<Orcanode> _nodes;
public List<Orcanode> Nodes => _nodes;
private const int _maxEventCountToDisplay = 20;
public List<OrcanodeEvent> RecentEvents => Fetcher.GetEvents(_databaseContext, _maxEventCountToDisplay);
private TimeSpan _uptimeEvaluationPeriod = TimeSpan.FromDays(7); // 1 week.

public IndexModel(OrcanodeMonitorContext context, ILogger<IndexModel> logger)

Check warning on line 24 in OrcanodeMonitor/Pages/Index.cshtml.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field '_events' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 24 in OrcanodeMonitor/Pages/Index.cshtml.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field '_nodes' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.
{
Expand Down Expand Up @@ -92,6 +94,78 @@ private string GetTextColor(OrcanodeOnlineStatus status)

public string NodeOrcasoundTextColor(Orcanode node) => GetTextColor(node.OrcasoundStatus);

public int GetUptimePercentage(Orcanode node)
{
if (_events == null)
{
return 0;
}

TimeSpan up = TimeSpan.Zero;
TimeSpan down = TimeSpan.Zero;
DateTime start = DateTime.UtcNow - _uptimeEvaluationPeriod;
string lastValue = string.Empty;

// Get events sorted by date to ensure correct chronological processing
var nodeEvents = _events
.Where(e => e.Orcanode == node)
.OrderBy(e => e.DateTimeUtc)
.ToList();

// Compute uptime percentage by looking at OrcanodeEvents over the past week.
foreach (OrcanodeEvent e in nodeEvents)
{
if (DateTime.UtcNow - e.DateTimeUtc >= _uptimeEvaluationPeriod) {
// More than a week old.
lastValue = e.Value;
continue;
}
DateTime current = e.DateTimeUtc;
if (lastValue == Orcanode.OnlineString)
{
up += (current - start);
}
else
{
down += (current - start);
}
start = current;
lastValue = e.Value;
}
if (lastValue == Orcanode.OnlineString)
{
up += DateTime.UtcNow - start;
} else
{
down += DateTime.UtcNow - start;
}

return (int)((100.0 * up) / _uptimeEvaluationPeriod + 0.5);
}

public string NodeUptimePercentageBackgroundColor(Orcanode node)
{
int value = GetUptimePercentage(node);
if (value < 1)
{
return ColorTranslator.ToHtml(Color.Red);
}
else if (value > 99)
{
return ColorTranslator.ToHtml(Color.LightGreen);
}

return ColorTranslator.ToHtml(Color.Yellow);
}
public string NodeUptimePercentageTextColor(Orcanode node)
{
if (NodeUptimePercentageBackgroundColor(node) == ColorTranslator.ToHtml(Color.Red))
{
return ColorTranslator.ToHtml(Color.White);
}
return ColorTranslator.ToHtml(Color.FromArgb(0, 0, 238));
}

public string NodeDataplicityUpgradeColor(Orcanode node)
{
OrcanodeUpgradeStatus status = node.DataplicityUpgradeStatus;
Expand All @@ -104,6 +178,7 @@ public string NodeDataplicityUpgradeColor(Orcanode node)

public async Task OnGetAsync()
{
// Fetch nodes for display.
var nodes = await _databaseContext.Orcanodes.ToListAsync();
_nodes = nodes.Where(n => ((n.DataplicityConnectionStatus != OrcanodeOnlineStatus.Absent) ||
(n.OrcasoundStatus != OrcanodeOnlineStatus.Absent) ||
Expand All @@ -112,6 +187,10 @@ public async Task OnGetAsync()
(n.OrcasoundHost != "dev.orcasound.net"))
.OrderBy(n => n.DisplayName)
.ToList();

// Fetch events for uptime computation.
var events = await _databaseContext.OrcanodeEvents.ToListAsync();
_events = events.Where(e => e.Type == "hydrophone stream").ToList();
}
}
}

0 comments on commit 4695e6f

Please sign in to comment.