Skip to content

Commit

Permalink
Merge pull request #14 from DEFRA/task/cdms-118-coding-standards
Browse files Browse the repository at this point in the history
Task/cdms 118 coding standards
  • Loading branch information
ishimmings authored Dec 12, 2024
2 parents 2c10d51 + e193f1d commit 89ea62d
Show file tree
Hide file tree
Showing 192 changed files with 2,093 additions and 2,344 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Btms.Common.Extensions;
using FluentAssertions;
using FluentAssertions.Collections;

Expand Down
2 changes: 0 additions & 2 deletions Btms.Analytics.Tests/Helpers/SingleSeriesDatasetAssertions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Btms.Common.Extensions;
using FluentAssertions;
using FluentAssertions.Collections;

namespace Btms.Analytics.Tests.Helpers;

Expand Down
2 changes: 1 addition & 1 deletion Btms.Analytics.Tests/Helpers/TestContextHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Btms.Analytics.Tests.Helpers;

public static class TestContextHelper
{
public static IHostBuilder CreateBuilder<T>(ITestOutputHelper testOutputHelper = null!)
public static IHostBuilder CreateBuilder<T>(ITestOutputHelper? testOutputHelper = null)
{
var builder = Host.CreateDefaultBuilder();

Expand Down
20 changes: 9 additions & 11 deletions Btms.Analytics.Tests/Helpers/TestDataGeneratorHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections;
using Btms.Consumers;
using Btms.Model.Extensions;
using Btms.Types.Alvs;
using Btms.Types.Ipaffs;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -15,14 +13,14 @@ namespace Btms.Analytics.Tests.Helpers;

public static class TestDataGeneratorHelpers
{
private static int scenarioIndex = 0;
private static int _scenarioIndex;

public static async Task<IHost> PushToConsumers(this IHost app, ScenarioConfig scenario)
{
var generatorResults = app.Generate(scenario);
scenarioIndex++;
_scenarioIndex++;

var logger = app.Services.GetRequiredService<ILogger<ScenarioGenerator>>();
app.Services.GetRequiredService<ILogger<ScenarioGenerator>>();

foreach (var generatorResult in generatorResults)
{
Expand All @@ -31,9 +29,9 @@ public static async Task<IHost> PushToConsumers(this IHost app, ScenarioConfig s
var scope = app.Services.CreateScope();
var consumer = (AlvsClearanceRequestConsumer)scope.ServiceProvider.GetRequiredService<IConsumer<AlvsClearanceRequest>>();

consumer.Context = new ConsumerContext()
consumer.Context = new ConsumerContext
{
Headers = new Dictionary<string, object>() { { "messageId", cr!.Header!.EntryReference! } }
Headers = new Dictionary<string, object> { { "messageId", cr.Header!.EntryReference! } }
};

await consumer.OnHandle(cr);
Expand All @@ -44,9 +42,9 @@ public static async Task<IHost> PushToConsumers(this IHost app, ScenarioConfig s
var scope = app.Services.CreateScope();
var consumer = (NotificationConsumer)scope.ServiceProvider.GetRequiredService<IConsumer<ImportNotification>>();

consumer.Context = new ConsumerContext()
consumer.Context = new ConsumerContext
{
Headers = new Dictionary<string, object>() { { "messageId", n!.ReferenceNumber! } }
Headers = new Dictionary<string, object> { { "messageId", n.ReferenceNumber! } }
};

await consumer.OnHandle(n);
Expand All @@ -63,7 +61,7 @@ private static ScenarioGenerator.GeneratorResult[] Generate(this IHost app, Scen
var count = scenario.Count;
var generator = scenario.Generator;

logger.LogInformation("Generating {Count}x{Days} {Generator}.", count, days, generator);
logger.LogInformation("Generating {Count}x{Days} {@Generator}", count, days, generator);
var results = new List<ScenarioGenerator.GeneratorResult>();

for (var d = -days + 1; d <= 0; d++)
Expand All @@ -75,7 +73,7 @@ private static ScenarioGenerator.GeneratorResult[] Generate(this IHost app, Scen
{
logger.LogInformation("Generating item {I}", i);

results.Add(generator.Generate(scenarioIndex, i, entryDate, scenario));
results.Add(generator.Generate(_scenarioIndex, i, entryDate, scenario));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public async Task WhenCalledLastWeek_ReturnExpectedAggregation()
testOutputHelper.WriteLine("Querying for aggregated data");
var result = (await multiItemDataTestFixture.ImportNotificationsAggregationService
.ByCommodityCount(DateTime.Today.WeekAgo(), DateTime.Today.Tomorrow()))
.ToList();;
.ToList();

testOutputHelper.WriteLine("{0} aggregated items found", result.Count);

Expand Down
4 changes: 2 additions & 2 deletions Btms.Analytics.Tests/ImportNotificationsByCreatedDateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public async Task WhenCalledLastMonth_ReturnExpectedAggregation()
[Fact]
public async Task WhenCalledWithTimePeriodYieldingNoResults_ReturnEmptyAggregation()
{
DateTime from = DateTime.MaxValue.AddDays(-1);
DateTime to = DateTime.MaxValue;
var from = DateTime.MaxValue.AddDays(-1);
var to = DateTime.MaxValue;

var result = (await basicSampleDataTestFixture.ImportNotificationsAggregationService
.ByCreated(from, to, AggregationPeriod.Hour))
Expand Down
8 changes: 4 additions & 4 deletions Btms.Analytics.Tests/MovementsByCreatedDateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ namespace Btms.Analytics.Tests;
[Collection(nameof(BasicSampleDataTestCollection))]
public class MovementsByCreatedDateTests(
BasicSampleDataTestFixture basicSampleDataTestFixture,
ITestOutputHelper testOutputHelper) {
ITestOutputHelper testOutputHelper)
{
[Fact]
public async Task WhenCalledLast48Hours_ReturnExpectedAggregation()
{
Expand All @@ -33,8 +33,8 @@ public async Task WhenCalledLast48Hours_ReturnExpectedAggregation()
[Fact]
public async Task WhenCalledWithTimePeriodYieldingNoResults_ReturnEmptyAggregation()
{
DateTime from = DateTime.MaxValue.AddDays(-1);
DateTime to = DateTime.MaxValue;
var from = DateTime.MaxValue.AddDays(-1);
var to = DateTime.MaxValue;

var result = (await basicSampleDataTestFixture.MovementsAggregationService
.ByCreated(from, to, AggregationPeriod.Hour))
Expand Down
2 changes: 1 addition & 1 deletion Btms.Analytics.Tests/MovementsByItemsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public async Task WhenCalledLastWeek_ReturnExpectedAggregation()
testOutputHelper.WriteLine("Querying for aggregated data");
var result = (await multiItemDataTestFixture.MovementsAggregationService
.ByItemCount(DateTime.Today.WeekAgo(), DateTime.Today.Tomorrow()))
.ToList();;
.ToList();

testOutputHelper.WriteLine("{0} aggregated items found", result.Count);

Expand Down
1 change: 0 additions & 1 deletion Btms.Analytics.Tests/MovementsByStatusTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public class MovementsByStatusTests(
BasicSampleDataTestFixture basicSampleDataTestFixture,
ITestOutputHelper testOutputHelper)
{

[Fact]
public async Task WhenCalledLastWeek_ReturnExpectedAggregation()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public async Task WhenCalledLastWeek_ReturnExpectedAggregation()
testOutputHelper.WriteLine("Querying for aggregated data");
var result = (await multiItemDataTestFixture.MovementsAggregationService
.ByUniqueDocumentReferenceCount(DateTime.Today.WeekAgo(), DateTime.Today.Tomorrow()))
.ToList();;
.ToList();

testOutputHelper.WriteLine("{0} aggregated items found", result.Count);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Btms.Common.Extensions;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;

Expand Down
21 changes: 7 additions & 14 deletions Btms.Analytics/Extensions/AnalyticsExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
using System.Collections;
using System.Linq.Expressions;
using Btms.Backend.Data;
using Btms.Model.Data;
using Btms.Model.Extensions;
using Btms.Model.Ipaffs;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
Expand All @@ -17,15 +12,15 @@ namespace Btms.Analytics.Extensions;

public static class AnalyticsExtensions
{
private static readonly bool enableMetrics = true;
private static readonly bool EnableMetrics = true;
public static IServiceCollection AddAnalyticsServices(this IServiceCollection services,
IConfiguration configuration)
{
services.AddScoped<IImportNotificationsAggregationService, ImportNotificationsAggregationService>();
services.AddScoped<IMovementsAggregationService, MovementsAggregationService>();

// To revisit in future
if (enableMetrics)
if (EnableMetrics)
{
services.TryAddScoped<ImportNotificationMetrics>();
}
Expand Down Expand Up @@ -76,11 +71,11 @@ public static MultiSeriesDatetimeDataset AsDataset(this Dictionary<string, BsonD
{
Periods = dateRange
.Select(resultDate =>
new ByDateTimeResult()
new ByDateTimeResult
{
Period = resultDate, Value = dates.GetValueOrDefault(resultDate, 0)
})
.Order(AnalyticsHelpers.byDateTimeResultComparer)
.Order(AnalyticsHelpers.ByDateTimeResultComparer)
.ToList()
};
}
Expand All @@ -102,7 +97,7 @@ internal static IEnumerable<IGrouping<TKey, TSource>> Execute<TSource, TKey>(thi
}
catch(Exception ex)
{
logger.LogError(ex, "Error querying Mongo : {message}", ex.Message);
logger.LogError(ex, "Error querying Mongo : {Message}", ex.Message);
throw new AnalyticsException("Error querying Mongo", ex);
}
finally
Expand All @@ -122,7 +117,7 @@ internal static IEnumerable<TSource> Execute<TSource>(
}
catch(Exception ex)
{
logger.LogError(ex, "Error querying Mongo : {message}", ex.Message);
logger.LogError(ex, "Error querying Mongo : {Message}", ex.Message);
throw new AnalyticsException("Error querying Mongo", ex);
}
finally
Expand All @@ -141,8 +136,6 @@ private static void LogExecutedMongoString(this ILogger logger, IQueryable sourc
{
var stages = ((IMongoQueryProvider)source.Provider).LoggedStages;

var query = "[" + String.Join(",", stages.Select(s => s.ToString()).ToArray()) +"]";

logger.LogInformation(query);
logger.LogInformation("[{Query}]", string.Join(",", stages.Select(s => s.ToString()).ToArray()));
}
}
9 changes: 3 additions & 6 deletions Btms.Analytics/Extensions/AnalyticsHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using Btms.Common.Extensions;
using Btms.Model.Extensions;
using System.Collections.Generic;
using Btms.Model;
using MongoDB.Bson;
namespace Btms.Analytics.Extensions;
Expand Down Expand Up @@ -40,17 +37,17 @@ internal static DateTime[] CreateDateRange(DateTime from, DateTime to, Aggregati
.Select(offset => from.Increment(offset, aggregateBy)) // from.AddDays(offset))
.ToArray();

internal static readonly Comparer<ByDateTimeResult>? byDateTimeResultComparer = Comparer<ByDateTimeResult>.Create((d1, d2) => d1.Period.CompareTo(d2.Period));
internal static readonly Comparer<ByDateTimeResult>? ByDateTimeResultComparer = Comparer<ByDateTimeResult>.Create((d1, d2) => d1.Period.CompareTo(d2.Period));

public static string[] GetImportNotificationSegments()
{
return ModelHelpers.GetChedTypes()
.SelectMany(chedType => new string[] { $"{chedType} Linked", $"{chedType} Not Linked" })
.SelectMany(chedType => new[] { $"{chedType} Linked", $"{chedType} Not Linked" })
.ToArray();
}

public static string[] GetMovementSegments()
{
return new string[] { "Linked", "Not Linked" };
return ["Linked", "Not Linked"];
}
}
15 changes: 3 additions & 12 deletions Btms.Analytics/ImportNotificationMetrics.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@


using System.Diagnostics;
using System.Diagnostics.Metrics;
using System.Runtime.InteropServices.JavaScript;
using System.Text;
using Btms.Common;
using Btms.Metrics;
using Btms.Analytics.Extensions;
using Btms.Common.Extensions;
using Btms.Model;
using Btms.Model.Extensions;
using Btms.Model.Ipaffs;
using Microsoft.Extensions.Logging;

namespace Btms.Analytics;
Expand All @@ -22,7 +13,7 @@ namespace Btms.Analytics;
public class ImportNotificationMetrics
{
private readonly IImportNotificationsAggregationService _importService;
private readonly Dictionary<string, Instrument> _metrics = new Dictionary<string, Instrument>();
private readonly Dictionary<string, Instrument> _metrics = new();
private readonly ILogger<ImportNotificationMetrics> _logger;

private void Add(Instrument i)
Expand Down Expand Up @@ -59,12 +50,12 @@ public async Task RecordCurrentState()
}
else
{
_logger.LogWarning("Unexpected type of instrument {type} for metric {name}", instrument.GetType(), key);
_logger.LogWarning("Unexpected type of instrument {Type} for metric {Name}", instrument.GetType(), key);
}
}
else
{
_logger.LogWarning("No instrument present for metric {name}", key);
_logger.LogWarning("No instrument present for metric {Name}", key);
}
}
}
Expand Down
19 changes: 7 additions & 12 deletions Btms.Analytics/ImportNotificationsAggregationService.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
using System.Collections;
using Microsoft.Extensions.Logging;
using System.Linq.Expressions;
using Btms.Backend.Data;
using Btms.Common.Extensions;
using Btms.Model.Extensions;
using Btms.Model.Ipaffs;
using Btms.Model;
using Microsoft.AspNetCore.Http;
using MongoDB.Bson;
using MongoDB.Driver;

using Btms.Analytics.Extensions;
using MongoDB.Driver.Linq;

namespace Btms.Analytics;

Expand All @@ -25,7 +20,7 @@ public Task<MultiSeriesDatetimeDataset[]> ByCreated(DateTime from, DateTime to,
n.CreatedSource >= from && n.CreatedSource < to;

string CreateDatasetName(BsonDocument b) =>
AnalyticsHelpers.GetLinkedName(b["_id"]["linked"].ToBoolean(), b["_id"]["importNotificationType"].ToString()!.FromImportNotificationTypeEnumString()!);
AnalyticsHelpers.GetLinkedName(b["_id"]["linked"].ToBoolean(), b["_id"]["importNotificationType"].ToString()!.FromImportNotificationTypeEnumString());

return Aggregate(dateRange, CreateDatasetName, matchFilter, "$createdSource", aggregateBy);
}
Expand All @@ -38,7 +33,7 @@ public Task<MultiSeriesDatetimeDataset[]> ByArrival(DateTime from, DateTime to,
n.PartOne!.ArrivesAt >= from && n.PartOne!.ArrivesAt < to;

string CreateDatasetName(BsonDocument b) =>
AnalyticsHelpers.GetLinkedName(b["_id"]["linked"].ToBoolean(), b["_id"]["importNotificationType"].ToString()!.FromImportNotificationTypeEnumString()!);
AnalyticsHelpers.GetLinkedName(b["_id"]["linked"].ToBoolean(), b["_id"]["importNotificationType"].ToString()!.FromImportNotificationTypeEnumString());

return Aggregate(dateRange, CreateDatasetName, matchFilter, "$partOne.arrivesAt", aggregateBy);
}
Expand All @@ -50,10 +45,10 @@ public Task<SingeSeriesDataset> ByStatus(DateTime from, DateTime to)
.Where(n => n.CreatedSource >= from && n.CreatedSource < to)
.GroupBy(n => new { n.ImportNotificationType, Linked = n.Relationships.Movements.Data.Count > 0 })
.Select(g => new { g.Key.Linked, g.Key.ImportNotificationType, Count = g.Count() })
.ToDictionary(g => AnalyticsHelpers.GetLinkedName(g.Linked, g.ImportNotificationType.AsString()!),
.ToDictionary(g => AnalyticsHelpers.GetLinkedName(g.Linked, g.ImportNotificationType.AsString()),
g => g.Count);

return Task.FromResult(new SingeSeriesDataset()
return Task.FromResult(new SingeSeriesDataset
{
Values = AnalyticsHelpers.GetImportNotificationSegments().ToDictionary(title => title, title => data.GetValueOrDefault(title, 0))
});
Expand Down Expand Up @@ -86,7 +81,7 @@ public Task<MultiSeriesDataset[]> ByCommodityCount(DateTime from, DateTime to)
.SelectMany(g =>
g.Select(r =>
new {
Title = AnalyticsHelpers.GetLinkedName(g.Key.Linked, g.Key.ImportNotificationType.AsString()!),
Title = AnalyticsHelpers.GetLinkedName(g.Key.Linked, g.Key.ImportNotificationType.AsString()),
r.Key.CommodityCount,
NotificationCount = r.Count
})
Expand All @@ -105,7 +100,7 @@ public Task<MultiSeriesDataset[]> ByCommodityCount(DateTime from, DateTime to)
{
// Results = asDictionary.AsResultList(title, maxCommodities)
Results = Enumerable.Range(0, maxCommodities)
.Select(i => new ByNumericDimensionResult()
.Select(i => new ByNumericDimensionResult
{
Dimension = i,
Value = asDictionary.GetValueOrDefault(new { Title=title, CommodityCount = i })
Expand Down Expand Up @@ -136,7 +131,7 @@ private Task<MultiSeriesDatetimeDataset[]> Aggregate(DateTime[] dateRange, Func<
.Select(title => mongoResult.AsDataset(dateRange, title))
.AsOrderedArray(d => d.Name);

logger.LogDebug("Aggregated Data {result}", output.ToList().ToJsonString());
logger.LogDebug("Aggregated Data {Result}", output.ToList().ToJsonString());

return Task.FromResult(output);
}
Expand Down
Loading

0 comments on commit 89ea62d

Please sign in to comment.