Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add uptime column to the dashboard #170

Merged
merged 5 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
@Model.GetUptimePercentage(item)%
</td>
@if (item.OrcasoundStatus == Models.OrcanodeOnlineStatus.Absent)
{
<td style="background-color: @Model.NodeOrcasoundBackgroundColor(item); color: @Model.NodeOrcasoundTextColor(item)">
Expand Down
50 changes: 50 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)
{
Expand Down Expand Up @@ -92,6 +94,49 @@ 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;

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

return (int)((100.0 * up) / _uptimeEvaluationPeriod + 0.5);
}
dthaler marked this conversation as resolved.
Show resolved Hide resolved

public string NodeDataplicityUpgradeColor(Orcanode node)
{
OrcanodeUpgradeStatus status = node.DataplicityUpgradeStatus;
Expand All @@ -104,6 +149,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 +158,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();
}
}
}