Skip to content

Commit

Permalink
Store orcasound site (dev vs prod) in cosmos db
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Thaler <[email protected]>
  • Loading branch information
dthaler committed Oct 9, 2024
1 parent 2445a89 commit c44cc40
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
24 changes: 15 additions & 9 deletions OrcanodeMonitor/Core/Fetcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ public class Fetcher
{
private static TimeZoneInfo _pacificTimeZone = TimeZoneInfo.FindSystemTimeZoneById("America/Los_Angeles");
private static HttpClient _httpClient = new HttpClient();
private static string _orcasoundProdFeedsUrl = "https://live.orcasound.net/api/json/feeds";
private static string _orcasoundDevFeedsUrl = "https://dev.orcasound.net/api/json/feeds";
private static string _orcasoundProdSite = "live.orcasound.net";
private static string _orcasoundDevSite = "dev.orcasound.net";
private static string _orcasoundFeedsUrlPath = "/api/json/feeds";
private static string _dataplicityDevicesUrl = "https://apps.dataplicity.com/devices/";
private static string _orcaHelloHydrophonesUrl = "https://aifororcasdetections2.azurewebsites.net/api/hydrophones";
private static DateTime _unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
Expand Down Expand Up @@ -448,10 +449,11 @@ public async static Task UpdateDataplicityDataAsync(OrcanodeMonitorContext conte
}
}

private async static Task<JsonElement?> GetOrcasoundDataAsync(OrcanodeMonitorContext context, string url)
private async static Task<JsonElement?> GetOrcasoundDataAsync(OrcanodeMonitorContext context, string site)
{
try
{
string url = "https://" + site + _orcasoundFeedsUrlPath;
string json = await _httpClient.GetStringAsync(url);
if (json.IsNullOrEmpty())
{
Expand All @@ -476,7 +478,7 @@ public async static Task UpdateDataplicityDataAsync(OrcanodeMonitorContext conte
}
}

private static void UpdateOrcasoundNode(JsonElement feed, List<Orcanode> foundList, List<Orcanode> unfoundList, OrcanodeMonitorContext context)
private static void UpdateOrcasoundNode(JsonElement feed, List<Orcanode> foundList, List<Orcanode> unfoundList, OrcanodeMonitorContext context, string site)
{
if (!feed.TryGetProperty("id", out var feedId))
{
Expand Down Expand Up @@ -574,6 +576,10 @@ private static void UpdateOrcasoundNode(JsonElement feed, List<Orcanode> foundLi
{
node.OrcasoundFeedId = feedId.ToString();
}
if (!site.IsNullOrEmpty())
{
node.OrcasoundHost = site;
}
if (orcasoundName != node.OrcasoundName)
{
// We just detected a name change.
Expand All @@ -597,14 +603,14 @@ private static void UpdateOrcasoundNode(JsonElement feed, List<Orcanode> foundLi
}
}

private async static Task UpdateOrcasoundSiteDataAsync(OrcanodeMonitorContext context, string url, List<Orcanode> foundList, List<Orcanode> unfoundList)
private async static Task UpdateOrcasoundSiteDataAsync(OrcanodeMonitorContext context, string site, List<Orcanode> foundList, List<Orcanode> unfoundList)
{
JsonElement? dataArray = await GetOrcasoundDataAsync(context, url);
JsonElement? dataArray = await GetOrcasoundDataAsync(context, site);
if (dataArray.HasValue)
{
foreach (JsonElement feed in dataArray.Value.EnumerateArray())
{
UpdateOrcasoundNode(feed, foundList, unfoundList, context);
UpdateOrcasoundNode(feed, foundList, unfoundList, context, site);
}
}
}
Expand All @@ -623,8 +629,8 @@ public async static Task UpdateOrcasoundDataAsync(OrcanodeMonitorContext context

try
{
await UpdateOrcasoundSiteDataAsync(context, _orcasoundProdFeedsUrl, foundList, unfoundList);
await UpdateOrcasoundSiteDataAsync(context, _orcasoundDevFeedsUrl, foundList, unfoundList);
await UpdateOrcasoundSiteDataAsync(context, _orcasoundProdSite, foundList, unfoundList);
await UpdateOrcasoundSiteDataAsync(context, _orcasoundDevSite, foundList, unfoundList);

// Mark any remaining unfound nodes as absent.
foreach (var unfoundNode in unfoundList)
Expand Down
8 changes: 6 additions & 2 deletions OrcanodeMonitor/Models/Orcanode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public Orcanode()
OrcasoundName = string.Empty;
OrcasoundSlug = string.Empty;
OrcasoundFeedId = string.Empty;
OrcasoundHost = string.Empty;
S3Bucket = string.Empty;
S3NodeName = string.Empty;
AgentVersion = string.Empty;
Expand Down Expand Up @@ -187,6 +188,11 @@ public Orcanode()
/// Audio stream status of most recent sample (defaults to absent).
/// </summary>
public OrcanodeOnlineStatus? AudioStreamStatus { get; set; }

/// <summary>
/// Orcasound site host (defaults to empty).
/// </summary>
public string OrcasoundHost { get; set; }

#endregion persisted

Expand Down Expand Up @@ -316,8 +322,6 @@ private bool IsDev
}
}

public string OrcasoundHost => IsDev ? "dev.orcasound.net" : "live.orcasound.net";

public string Type => IsDev ? "Dev" : "Prod";

public string OrcasoundOnlineStatusString {
Expand Down

0 comments on commit c44cc40

Please sign in to comment.