Skip to content

Commit

Permalink
Merge pull request #22 from brenordv/feature/adding-avg-aggregate
Browse files Browse the repository at this point in the history
Feature/adding avg aggregate
  • Loading branch information
brenordv committed Jun 26, 2024
2 parents be21864 + 8d39396 commit 66407fe
Show file tree
Hide file tree
Showing 73 changed files with 2,477 additions and 415 deletions.
36 changes: 19 additions & 17 deletions Raccoon.Ninja.AzFn.DataApi/DataLatestHbA1cFunc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using Raccoon.Ninja.AzFn.DataApi.ExtensionMethods;
using Raccoon.Ninja.AzFn.DataApi.Utils;
using Raccoon.Ninja.Domain.Core.Entities;
using Raccoon.Ninja.Domain.Core.Entities.StatisticalDataPoint;
using Raccoon.Ninja.Domain.Core.Models;
using Microsoft.Azure.Functions.Worker;

namespace Raccoon.Ninja.AzFn.DataApi;

Expand All @@ -26,25 +26,27 @@ public DataLatestHbA1CFunc(ILogger<DataLatestHbA1CFunc> logger)
public async Task<IActionResult> RunAsync(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)]
HttpRequest req, [CosmosDBInput(
databaseName: "%CosmosDatabaseName%",
containerName: "%CosmosAggregateContainerName%",
"%CosmosDatabaseName%",
"%CosmosAggregateContainerName%",
Connection = "CosmosConnectionString",
SqlQuery = "SELECT TOP 1 * FROM c WHERE c.docType = 1 and c.status = 1 ORDER BY c.createdAt DESC"
SqlQuery =
"SELECT TOP 1 * FROM c WHERE c.docType = 1 and c.full.status = 1 and c.full.hbA1c.status = 1 ORDER BY c.createdAt DESC"
)]
IEnumerable<HbA1CCalculation> latestSuccessCalculations, [CosmosDBInput(
databaseName: "%CosmosDatabaseName%",
containerName: "%CosmosAggregateContainerName%",
IEnumerable<StatisticDataPointDocument> latestSuccessCalculations, [CosmosDBInput(
"%CosmosDatabaseName%",
"%CosmosAggregateContainerName%",
Connection = "CosmosConnectionString",
SqlQuery = "SELECT TOP 1 * FROM c WHERE c.docType = 1 and c.status = 2 ORDER BY c.createdAt DESC"
SqlQuery =
"SELECT TOP 1 * FROM c WHERE c.docType = 1 and c.full.status = 1 and c.full.hbA1c.status = 2 ORDER BY c.createdAt DESC"
)]
IEnumerable<HbA1CCalculation> latestPartialSuccessCalculations)
IEnumerable<StatisticDataPointDocument> latestPartialSuccessCalculations)
{
_logger.LogInformation("Data Latest HbA1c API call received. Request by IP: {Ip}",
req.HttpContext.Connection.RemoteIpAddress);
req.HttpContext.Connection.RemoteIpAddress);

HbA1CCalculation latestSuccessful = null;
StatisticDataPointDocument latestSuccessful = null;

HbA1CCalculation latestPartialSuccessful = null;
StatisticDataPointDocument latestPartialSuccessful = null;

try
{
Expand All @@ -60,15 +62,15 @@ public async Task<IActionResult> RunAsync(

return new OkObjectResult(new DataLatestHbA1CFuncResponse
{
LatestSuccessful = latestSuccessful,
LatestPartialSuccessful = latestPartialSuccessful
LatestSuccessful = latestSuccessful?.Full.ToLegacyHbA1cCalculation(),
LatestPartialSuccessful = latestPartialSuccessful?.Full.ToLegacyHbA1cCalculation()
});
}
catch (Exception e)
{
_logger.LogError(e,
"Error while processing request from IP: {Ip} | Latest success : {LatestSuccessReading} / Latest Partial Success: {LatestPartialSuccess}",
req.HttpContext.Connection.RemoteIpAddress, latestSuccessful, latestPartialSuccessful);
"Error while processing request from IP: {Ip} | Latest success : {LatestSuccessReading} / Latest Partial Success: {LatestPartialSuccess}",
req.HttpContext.Connection.RemoteIpAddress, latestSuccessful, latestPartialSuccessful);

return new StatusCodeResult(500);
}
Expand Down
14 changes: 7 additions & 7 deletions Raccoon.Ninja.AzFn.DataApi/Raccoon.Ninja.AzFn.DataApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
<AssemblyVersion>2.0.2</AssemblyVersion>
<FileVersion>2.0.2</FileVersion>
<AssemblyVersion>2.0.3</AssemblyVersion>
<FileVersion>2.0.3</FileVersion>
<NeutralLanguage>en-US</NeutralLanguage>
<OutputType>Exe</OutputType>
<ImplicitUsings>enabled</ImplicitUsings>
Expand All @@ -24,12 +24,12 @@
<Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.CosmosDB" Version="4.6.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.CosmosDB" Version="4.9.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.2.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.2" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.21.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.22.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.2.1" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.3.2" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="xunit" Version="2.6.6" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
23 changes: 13 additions & 10 deletions Raccoon.Ninja.AzFn.ScheduledTasks/DataTransferFunc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using MongoDB.Driver;
using Newtonsoft.Json;
using Raccoon.Ninja.AzFn.ScheduledTasks.ExtensionMethods;
using Raccoon.Ninja.Domain.Core.Entities;
using Raccoon.Ninja.Domain.Core.Exceptions;
using Raccoon.Ninja.Domain.Core.ExtensionMethods;
using Raccoon.Ninja.Extensions.MongoDb.Builders;
using Raccoon.Ninja.Extensions.MongoDb.ExtensionMethods;
Expand All @@ -26,17 +26,19 @@ public DataTransferFunc(ILogger<DataTransferFunc> logger)
[Function("DataTransferFunc")]
[CosmosDBOutput(
"%CosmosDatabaseName%",
"%CosmosContainerName%",
"%CosmosContainerName%",
Connection = "CosmosConnectionString",
CreateIfNotExists = false)]
public IEnumerable<GlucoseReading> Run(
[TimerTrigger("0 */5 * * * *", RunOnStartup = true)] TimerInfo timer,
[TimerTrigger("0 */5 * * * *", RunOnStartup = true)]
TimerInfo timer,
[CosmosDBInput(
databaseName: "%CosmosDatabaseName%",
containerName: "%CosmosContainerName%",
"%CosmosDatabaseName%",
"%CosmosContainerName%",
Connection = "CosmosConnectionString",
SqlQuery = "SELECT TOP 1 * FROM c ORDER BY c.readAt DESC"
)] IEnumerable<GlucoseReading> previousReadings)
)]
IEnumerable<GlucoseReading> previousReadings)
{
_logger.LogInformation("Nightscout Data Transfer Function started");

Expand Down Expand Up @@ -65,9 +67,10 @@ public IEnumerable<GlucoseReading> Run(
}
catch (Exception e)
{
_logger.LogError(e, "Failed to transfer data from MongoDb to CosmosDb");
const string errorMessage = "Failed to transfer data from MongoDb to CosmosDb";
_logger.LogError(e, errorMessage);

throw;
throw new NightScoutException(errorMessage, e);
}
finally
{
Expand All @@ -76,8 +79,8 @@ public IEnumerable<GlucoseReading> Run(
}

/// <summary>
/// Initializes the MongoDb collection.
/// This is the source of data for Nightscout.
/// Initializes the MongoDb collection.
/// This is the source of data for Nightscout.
/// </summary>
/// <param name="log">Log instance created by Azure.</param>
/// <returns>Collection Instance</returns>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using Raccoon.Ninja.Domain.Core.Entities;
using Raccoon.Ninja.Extensions.MongoDb.ExtensionMethods;
using Raccoon.Ninja.Extensions.MongoDb.Models;
Expand All @@ -8,24 +9,33 @@ namespace Raccoon.Ninja.AzFn.ScheduledTasks.ExtensionMethods;
public static class ListExtensions
{
/// <summary>
/// Converts a list of native Nightscout MongoDB documents to a list of entities owned by
/// this repo (<see cref="GlucoseReading"/>).
/// Converts a list of native Nightscout MongoDB documents to a list of entities owned by
/// this repo (<see cref="GlucoseReading" />).
/// </summary>
/// <param name="documents">Documents that will be converted</param>
/// <param name="previousReading">Previous GlucoseReading. The value will be used to calculate the delta</param>
/// <returns>Converted list</returns>
public static IEnumerable<GlucoseReading> ToGlucoseReadings(this IList<NightScoutMongoDocument> documents, GlucoseReading previousReading)
public static IEnumerable<GlucoseReading> ToGlucoseReadings(this IList<NightScoutMongoDocument> documents,
GlucoseReading previousReading)
{
var previous = previousReading ?? new GlucoseReading();

foreach (var mongoDbDoc in documents)
{
var current = mongoDbDoc.ToGlucoseReading(previous.Value);

if (current is null) continue;

yield return current;
previous = current;
}
}

public static IList<GlucoseReading> GetLastDays(this IList<GlucoseReading> readings, int days)
{
var lastDate = readings.Max(r => r.ReadTimestampUtcAsDateTime);
var firstDate = lastDate.AddDays(-days);

return readings.Where(r => r.ReadTimestampUtcAsDateTime >= firstDate).ToList();
}
}
71 changes: 0 additions & 71 deletions Raccoon.Ninja.AzFn.ScheduledTasks/HbA1cCalcFunc.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
<AssemblyVersion>2.0.2</AssemblyVersion>
<FileVersion>2.0.2</FileVersion>
<AssemblyVersion>2.0.3</AssemblyVersion>
<FileVersion>2.0.3</FileVersion>
<NeutralLanguage>en-US</NeutralLanguage>
<OutputType>Exe</OutputType>
<ImplicitUsings>enabled</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.CosmosDB" Version="4.6.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.CosmosDB" Version="4.9.0" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" />
<PackageReference Include="MongoDB.Driver" Version="2.23.1" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.21.0" />
<PackageReference Include="MongoDB.Driver" Version="2.26.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.2.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.2" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.22.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Timer" Version="4.3.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Timer" Version="4.3.1" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
Expand Down
Loading

0 comments on commit 66407fe

Please sign in to comment.