Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Thaler <dthaler1968@gmail.com>
  • Loading branch information
dthaler committed Jan 15, 2025
1 parent 70942a7 commit 293cdc0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
3 changes: 3 additions & 0 deletions OrcanodeMonitor/Pages/NodeEvents.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
backgroundColor: 'rgba(255, 99, 132, 0.2)',
borderColor: 'rgba(255, 99, 132, 1)',
borderWidth: 1,
spanGaps: true,
stepped: 'before'
},
{
Expand All @@ -55,6 +56,7 @@
backgroundColor: 'rgba(75, 192, 192, 0.2)',
borderColor: 'rgba(0, 255, 0, 1)',
borderWidth: 1,
spanGaps: true,
stepped: 'before'
},
{
Expand All @@ -63,6 +65,7 @@
backgroundColor: 'rgba(54, 162, 235, 0.2)',
borderColor: 'rgba(54, 162, 235, 1)',
borderWidth: 1,
spanGaps: true,
stepped: 'before'
}
]
Expand Down
52 changes: 29 additions & 23 deletions OrcanodeMonitor/Pages/NodeEvents.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Copyright (c) Orcanode Monitor contributors
// SPDX-License-Identifier: MIT
using MathNet.Numerics.Statistics;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using OrcanodeMonitor.Core;
using OrcanodeMonitor.Data;
Expand Down Expand Up @@ -89,7 +87,7 @@ private static int StatusStringToInt(string value)
public string JsonMezmoData { get; set; }
public string JsonHydrophoneStreamData { get; set; }

private void AddCurrentEvent(List<OrcanodeEvent> events, DateTime origin, DateTime now)
private void AddCurrentEvent(List<OrcanodeEvent> events, DateTime now)
{
if (!events.Any())
{
Expand All @@ -102,41 +100,49 @@ private void AddCurrentEvent(List<OrcanodeEvent> events, DateTime origin, DateTi
return;
}

if (last.DateTimeUtc == now)
{
return;
}

var current = new OrcanodeEvent(last.Orcanode, last.Type, last.Value, now, null);
events.Add(current);
}

private string CreateJsonDataset(string type, List<DateTime> allTimestamps, DateTime now)
{
List<OrcanodeEvent> dataset = _events.Where(e => e.Type == type).OrderBy(e => e.DateTimeUtc).ToList();
AddCurrentEvent(dataset, now);

OrcanodeEvent first = events.First();
if (first.DateTimeUtc == origin)
var alignedDataset = allTimestamps.Select(timestamp =>
{
return;
}
var original = new OrcanodeEvent(last.Orcanode, last.Type, Orcanode.GetStatusString(OrcanodeOnlineStatus.Absent), origin, null);
events.Insert(0, original);
var matchingEvent = dataset.FirstOrDefault(e => e.DateTimeUtc == timestamp);
return new
{
Timestamp = timestamp,
StateValue = matchingEvent != null ? StatusStringToInt(matchingEvent.Value) : (int?)null
};
}).ToList();

string json = JsonSerializer.Serialize(alignedDataset);
return json;
}

private void FetchEvents(ILogger logger)
{
_events = Fetcher.GetRecentEventsForNode(_databaseContext, Id, DateTime.MinValue, logger)
.ToList() ?? new List<OrcanodeEvent>();

DateTime now = DateTime.UtcNow;
if (!_events.Any())
{
return; // Nothing to do.
}
OrcanodeEvent firstEvent = _events.OrderBy(e => e.DateTimeUtc).First();
DateTime firstTimeUtc = firstEvent.DateTimeUtc;

List<OrcanodeEvent> dataplicityEvents = _events.Where(e => e.Type == OrcanodeEventTypes.DataplicityConnection).OrderBy(e => e.DateTimeUtc).ToList();
AddCurrentEvent(dataplicityEvents, firstTimeUtc, now);
List<OrcanodeEvent> hydrophoneStreamEvents = _events.Where(e => e.Type == OrcanodeEventTypes.HydrophoneStream).OrderBy(e => e.DateTimeUtc).ToList();
AddCurrentEvent(hydrophoneStreamEvents, firstTimeUtc, now);
List<OrcanodeEvent> mezmoEvents = _events.Where(e => e.Type == OrcanodeEventTypes.MezmoLogging).OrderBy(e => e.DateTimeUtc).ToList();
AddCurrentEvent(mezmoEvents, firstTimeUtc, now);

JsonDataplicityData = JsonSerializer.Serialize(dataplicityEvents.Select(e => new { Timestamp = e.DateTimeUtc, StateValue = StatusStringToInt(e.Value) }));
JsonMezmoData = JsonSerializer.Serialize(mezmoEvents.Select(e => new { Timestamp = e.DateTimeUtc, StateValue = StatusStringToInt(e.Value) }));
JsonHydrophoneStreamData = JsonSerializer.Serialize(hydrophoneStreamEvents.Select(e => new { Timestamp = e.DateTimeUtc, StateValue = StatusStringToInt(e.Value) }));

DateTime now = DateTime.UtcNow;
List<DateTime> allTimestamps = _events.Select(e => e.DateTimeUtc).Union(new List<DateTime> { now }).OrderBy(t => t).ToList();
JsonDataplicityData = CreateJsonDataset(OrcanodeEventTypes.DataplicityConnection, allTimestamps, now);
JsonMezmoData = CreateJsonDataset(OrcanodeEventTypes.MezmoLogging, allTimestamps, now);
JsonHydrophoneStreamData = CreateJsonDataset(OrcanodeEventTypes.HydrophoneStream, allTimestamps, now);
}

public void OnGet(string id)
Expand Down

0 comments on commit 293cdc0

Please sign in to comment.