Skip to content

Commit

Permalink
CDMS-200 adds total import notifications and movements to analytics d…
Browse files Browse the repository at this point in the history
…ashboard (#44)
  • Loading branch information
craigedmunds authored and Lim Sim committed Jan 8, 2025
1 parent 56abf11 commit 438d3b2
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 16 deletions.
17 changes: 8 additions & 9 deletions Btms.Analytics.Tests/ImportNotificationsByMaxVersionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,20 @@ public async Task WhenCalledLastMonth_ReturnExpectedAggregation()
public async Task WhenCalledLast48Hours_ReturnExpectedAggregation()
{
TestOutputHelper.WriteLine("Querying for aggregated data");
var result = (await GetImportNotificationsAggregationService()
.ByMaxVersion(DateTime.Now.NextHour().AddDays(-2), DateTime.Now.NextHour()));
var result = await ImportNotificationsAggregationService
.ByMaxVersion(DateTime.Now.NextHour().AddDays(-2), DateTime.Now.NextHour());

TestOutputHelper.WriteLine($"{result.Values.Count} aggregated items found");

result.Values.Count.Should().Be(1);

}

[Fact]
public async Task WhenCalledWithTimePeriodYieldingNoResults_ReturnEmptyAggregation()
{
TestOutputHelper.WriteLine("Querying for aggregated data");
var result = (await GetImportNotificationsAggregationService()
.ByMaxVersion(DateTime.MaxValue.AddDays(-1), DateTime.MaxValue));
var result = await ImportNotificationsAggregationService
.ByMaxVersion(DateTime.MaxValue.AddDays(-1), DateTime.MaxValue);

TestOutputHelper.WriteLine($"{result.Values.Count} aggregated items found");

Expand All @@ -55,8 +54,8 @@ public async Task WhenCalledWithTimePeriodYieldingNoResults_ReturnEmptyAggregati
public async Task WhenCalledWithChedType_ReturnsResults()
{
TestOutputHelper.WriteLine("Querying for aggregated data");
var result = (await GetImportNotificationsAggregationService()
.ByMaxVersion(DateTime.Now.NextHour().AddDays(-2), DateTime.Now.NextHour(), chedTypes: [ImportNotificationTypeEnum.Cveda]));
var result = await ImportNotificationsAggregationService
.ByMaxVersion(DateTime.Now.NextHour().AddDays(-2), DateTime.Now.NextHour(), chedTypes: [ImportNotificationTypeEnum.Cveda]);

TestOutputHelper.WriteLine($"{result.Values.Count} aggregated items found");

Expand All @@ -67,8 +66,8 @@ public async Task WhenCalledWithChedType_ReturnsResults()
public async Task WhenCalledWithCountry_ReturnsResults()
{
TestOutputHelper.WriteLine("Querying for aggregated data");
var result = (await GetImportNotificationsAggregationService()
.ByMaxVersion(DateTime.Now.NextHour().AddDays(-2), DateTime.Now.NextHour(), country: "ES"));
var result = await ImportNotificationsAggregationService
.ByMaxVersion(DateTime.Now.NextHour().AddDays(-2), DateTime.Now.NextHour(), country: "ES");

TestOutputHelper.WriteLine($"{result.Values.Count} aggregated items found");

Expand Down
2 changes: 1 addition & 1 deletion Btms.Analytics/IImportNotificationsAggregationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public interface IImportNotificationsAggregationService
{
public Task<MultiSeriesDatetimeDataset> ByCreated(DateTime from, DateTime to, AggregationPeriod aggregateBy = AggregationPeriod.Day);
public Task<MultiSeriesDatetimeDataset> ByArrival(DateTime from, DateTime to, AggregationPeriod aggregateBy = AggregationPeriod.Day);
public Task<SingleSeriesDataset> ByStatus(DateTime from, DateTime to);
public Task<SingleSeriesDataset> ByStatus(DateTime? from = null, DateTime? to = null);
public Task<MultiSeriesDataset> ByCommodityCount(DateTime from, DateTime to);
public Task<SingleSeriesDataset> ByMaxVersion(DateTime from, DateTime to, ImportNotificationTypeEnum[]? chedTypes = null, string? country = null);
}
2 changes: 1 addition & 1 deletion Btms.Analytics/IMovementsAggregationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Btms.Analytics;
public interface IMovementsAggregationService
{
public Task<MultiSeriesDatetimeDataset> ByCreated(DateTime from, DateTime to, AggregationPeriod aggregateBy = AggregationPeriod.Day);
public Task<SingleSeriesDataset> ByStatus(DateTime from, DateTime to);
public Task<SingleSeriesDataset> ByStatus(DateTime? from = null, DateTime? to = null);
public Task<MultiSeriesDataset> ByItemCount(DateTime from, DateTime to);
public Task<SummarisedDataset<SingleSeriesDataset, StringBucketDimensionResult>> ByDecision(DateTime from, DateTime to, ImportNotificationTypeEnum[]? chedTypes = null, string? country = null);
// public Task<TabularDataset<ByNameDimensionResult>> ByDecisionAndLinkStatus(DateTime from, DateTime to);
Expand Down
4 changes: 2 additions & 2 deletions Btms.Analytics/ImportNotificationsAggregationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ string CreateDatasetName(BsonDocument b) =>
return AggregateByLinkedAndNotificationType(dateRange, CreateDatasetName, matchFilter, "$partOne.arrivesAt", aggregateBy);
}

public Task<SingleSeriesDataset> ByStatus(DateTime from, DateTime to)
public Task<SingleSeriesDataset> ByStatus(DateTime? from = null, DateTime? to = null)
{
var data = context
.Notifications
.Where(n => n.CreatedSource >= from && n.CreatedSource < to)
.Where(n => (from == null || n.CreatedSource >= from) && (to == null || 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()),
Expand Down
4 changes: 2 additions & 2 deletions Btms.Analytics/MovementsAggregationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ public Task<MultiSeriesDatetimeDataset> ByCreated(DateTime from, DateTime to, Ag
return Aggregate(dateRange, CreateDatasetName, matchFilter, "$createdSource", aggregateBy);
}

public Task<SingleSeriesDataset> ByStatus(DateTime from, DateTime to)
public Task<SingleSeriesDataset> ByStatus(DateTime? from = null, DateTime? to = null)
{
var data = context
.Movements
.SelectLinkStatus()
.Where(n => n.CreatedSource >= from && n.CreatedSource < to)
.Where(n => (from == null || n.CreatedSource >= from) && (to == null || n.CreatedSource < to))
.GroupBy(m => m.Description)
.Select(g => new { g.Key, Count = g.Count() })
.ToDictionary(g => g.Key, g => g.Count);
Expand Down
8 changes: 8 additions & 0 deletions Btms.Backend/Config/AnalyticsDashboards.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public static async Task<IDictionary<string, IDataset>> GetCharts(
"lastMonthImportNotificationsByTypeAndStatus",
() => importService.ByStatus(DateTime.Today.MonthAgo(), DateTime.Now).AsIDataset()
},
{
"allImportNotificationsByTypeAndStatus",
() => importService.ByStatus().AsIDataset()
},
{
"last24HoursMovementsLinkingByCreated",
() => movementsService.ByCreated(DateTime.Now.NextHour().Yesterday(), DateTime.Now.NextHour(), AggregationPeriod.Hour).AsIDataset()
Expand All @@ -56,6 +60,10 @@ public static async Task<IDictionary<string, IDataset>> GetCharts(
"movementsLinkingByCreated",
() => movementsService.ByCreated(DateTime.Today.MonthAgo(), DateTime.Today).AsIDataset()
},
{
"allMovementsByStatus",
() => movementsService.ByStatus().AsIDataset()
},
{
"lastMonthMovementsByStatus",
() => movementsService.ByStatus(DateTime.Today.MonthAgo(), DateTime.Now).AsIDataset()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ public abstract class ScenarioDatasetBaseTest

private static Dictionary<string, List<GeneratedResult>> AllDatasets
= new Dictionary<string, List<GeneratedResult>>();


protected readonly IImportNotificationsAggregationService ImportNotificationsAggregationService;
protected readonly IMovementsAggregationService MovementsAggregationService;

/// <summary>
///
/// </summary>
Expand Down Expand Up @@ -49,6 +52,12 @@ protected ScenarioDatasetBaseTest(

Client = BackendFixture.BtmsClient;
MongoDbContext = BackendFixture.MongoDbContext;

ImportNotificationsAggregationService = new ImportNotificationsAggregationService(MongoDbContext,
TestOutputHelper.GetLogger<ImportNotificationsAggregationService>());

MovementsAggregationService = new MovementsAggregationService(MongoDbContext,
TestOutputHelper.GetLogger<MovementsAggregationService>());

if (reloadData)
{
Expand Down Expand Up @@ -95,6 +104,7 @@ protected List<GeneratedResult> GetLoadedData()
/// </summary>
/// <param name="testOutputHelper"></param>
/// <returns></returns>
// [Obsolete("Use the ImportNotificationsAggregationService property instead")]
protected IImportNotificationsAggregationService GetImportNotificationsAggregationService()
{
var logger = TestOutputHelper.GetLogger<ImportNotificationsAggregationService>();
Expand All @@ -106,6 +116,7 @@ protected IImportNotificationsAggregationService GetImportNotificationsAggregati
/// </summary>
/// <param name="testOutputHelper"></param>
/// <returns></returns>
// [Obsolete("Use the MovementsAggregationService property instead")]
protected IMovementsAggregationService GetMovementsAggregationService()
{
var logger = TestOutputHelper.GetLogger<MovementsAggregationService>();
Expand Down

0 comments on commit 438d3b2

Please sign in to comment.