Skip to content

Commit

Permalink
CDMS-200 analytics adjustments for novembers prod data
Browse files Browse the repository at this point in the history
  • Loading branch information
craigedmunds committed Dec 19, 2024
1 parent 5862145 commit 36ff163
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 10 deletions.
6 changes: 3 additions & 3 deletions Btms.Analytics/Extensions/AnalyticsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ internal static IEnumerable<TSource> Execute<TSource>(
private static void LogExecutedMongoString(this ILogger logger, IQueryable source)
{
var stages = ((IMongoQueryProvider)source.Provider).LoggedStages;

logger.LogInformation("[{Query}]", string.Join(",", stages.Select(s => s.ToString()).ToArray()));
var query = string.Join(",", stages.Select(s => s.ToString()).ToArray());
logger.LogInformation("[{Query}]", query);
}

public static async Task<IDataset> AsIDataset(this Task<MultiSeriesDatetimeDataset> ms)
{
await ms;
Expand Down
1 change: 1 addition & 0 deletions Btms.Analytics/IImportNotificationsAggregationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ public interface IImportNotificationsAggregationService
public Task<MultiSeriesDatetimeDataset> ByArrival(DateTime from, DateTime to, AggregationPeriod aggregateBy = AggregationPeriod.Day);
public Task<SingleSeriesDataset> ByStatus(DateTime from, DateTime to);
public Task<MultiSeriesDataset> ByCommodityCount(DateTime from, DateTime to);
public Task<MultiSeriesDatetimeDataset> ByVersionCount(DateTime from, DateTime to, AggregationPeriod aggregateBy = AggregationPeriod.Day);
}
27 changes: 23 additions & 4 deletions Btms.Analytics/ImportNotificationsAggregationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public Task<MultiSeriesDatetimeDataset> ByCreated(DateTime from, DateTime to, Ag
string CreateDatasetName(BsonDocument b) =>
AnalyticsHelpers.GetLinkedName(b["_id"]["linked"].ToBoolean(), b["_id"]["importNotificationType"].ToString()!.FromImportNotificationTypeEnumString());

return Aggregate(dateRange, CreateDatasetName, matchFilter, "$createdSource", aggregateBy);
return AggregateByLinkedAndNotificationType(dateRange, CreateDatasetName, matchFilter, "$createdSource", aggregateBy);
}

public Task<MultiSeriesDatetimeDataset> ByArrival(DateTime from, DateTime to, AggregationPeriod aggregateBy = AggregationPeriod.Day)
Expand All @@ -35,7 +35,7 @@ public Task<MultiSeriesDatetimeDataset> ByArrival(DateTime from, DateTime to, Ag
string CreateDatasetName(BsonDocument b) =>
AnalyticsHelpers.GetLinkedName(b["_id"]["linked"].ToBoolean(), b["_id"]["importNotificationType"].ToString()!.FromImportNotificationTypeEnumString());

return Aggregate(dateRange, CreateDatasetName, matchFilter, "$partOne.arrivesAt", aggregateBy);
return AggregateByLinkedAndNotificationType(dateRange, CreateDatasetName, matchFilter, "$partOne.arrivesAt", aggregateBy);
}

public Task<SingleSeriesDataset> ByStatus(DateTime from, DateTime to)
Expand All @@ -59,11 +59,17 @@ public Task<MultiSeriesDataset> ByCommodityCount(DateTime from, DateTime to)
var query = context
.Notifications
.Where(n => n.CreatedSource >= from && n.CreatedSource < to)
// .Select(n => new
// {
// ImportNotificationType = n.ImportNotificationType,
// Relationships = n.Relationships,
// Commodities = n.Commodities
// })
.GroupBy(n => new
{
ImportNotificationType = n.ImportNotificationType!.Value,
Linked = n.Relationships.Movements.Data.Count > 0,
CommodityCount = n.Commodities.Count()
CommodityCount = n.Commodities == null ? 0 : n.Commodities.Count()
})
.Select(g => new { g.Key, Count = g.Count() });

Expand Down Expand Up @@ -108,7 +114,20 @@ public Task<MultiSeriesDataset> ByCommodityCount(DateTime from, DateTime to)
});
}

private Task<MultiSeriesDatetimeDataset> Aggregate(DateTime[] dateRange, Func<BsonDocument, string> createDatasetName, Expression<Func<ImportNotification, bool>> filter, string dateField, AggregationPeriod aggregateBy)
public Task<MultiSeriesDatetimeDataset> ByVersionCount(DateTime from, DateTime to, AggregationPeriod aggregateBy = AggregationPeriod.Day)
{
var dateRange = AnalyticsHelpers.CreateDateRange(from, to, aggregateBy);

Expression<Func<ImportNotification, bool>> matchFilter = n =>
n.CreatedSource >= from && n.CreatedSource < to;

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

return AggregateByLinkedAndNotificationType(dateRange, CreateDatasetName, matchFilter, "$createdSource", aggregateBy);
}

private Task<MultiSeriesDatetimeDataset> AggregateByLinkedAndNotificationType(DateTime[] dateRange, Func<BsonDocument, string> createDatasetName, Expression<Func<ImportNotification, bool>> filter, string dateField, AggregationPeriod aggregateBy)
{
var truncateBy = aggregateBy == AggregationPeriod.Hour ? "hour" : "day";

Expand Down
4 changes: 4 additions & 0 deletions Btms.Backend/Config/AnalyticsDashboards.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public static async Task<IDictionary<string, IDataset>> GetCharts(
{
"lastMonthDecisionsByStatus",
() => movementsService.ByDecision(DateTime.Today.MonthAgo(), DateTime.Now).AsIDataset()
},
{
"importNotificationVersionsByVersionCount",
() => importService.ByVersionCount(DateTime.Today.AddMonths(-3), DateTime.Today).AsIDataset()
}
};

Expand Down
6 changes: 5 additions & 1 deletion Btms.Model/Movement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,14 @@ public bool MergeDecision(string path, AlvsClearanceRequest clearanceRequest)
}

var decisionAuditContext = new Dictionary<string, Dictionary<string, string>>();
decisionAuditContext.Add("movements", new Dictionary<string, string>()
decisionAuditContext.Add("clearanceRequests", new Dictionary<string, string>()
{
{ clearanceRequest.Header!.EntryReference!, clearanceRequest.Header!.EntryVersionNumber!.ToString()! }
});
decisionAuditContext.Add("decisions", new Dictionary<string, string>()
{
{ clearanceRequest.Header!.EntryReference!, clearanceRequest.Header!.DecisionNumber!.ToString()! }
});
decisionAuditContext.Add("importNotifications", new Dictionary<string, string>()
{
{ "todo", "todo" }
Expand Down
4 changes: 2 additions & 2 deletions Btms.Model/Relationships/RelationshipDataItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static RelationshipDataItem CreateFromNotification(ImportNotification not
SourceItem = movement.Items
.Find(x => x.Documents!.ToList().Exists(d => d.DocumentReference!.Contains(matchReference)))
?.ItemNumber,
DestinationItem = notification.Commodities.FirstOrDefault()?.ComplementId,
DestinationItem = notification.Commodities!.FirstOrDefault()?.ComplementId,
Links = new ResourceLink { Self = LinksBuilder.Notification.BuildSelfNotificationLink(notification.Id!) },
MatchingLevel = 1
};
Expand All @@ -74,7 +74,7 @@ public static RelationshipDataItem CreateFromMovement(ImportNotification notific
Matched = matched,
Type = "movements",
Id = movement.Id!,
SourceItem = notification.Commodities.FirstOrDefault()?.ComplementId,
SourceItem = notification.Commodities!.FirstOrDefault()?.ComplementId,
DestinationItem = movement.Items
.Find(x => x.Documents!.ToList().Exists(d => d.DocumentReference!.Contains(matchReference)))
?.ItemNumber,
Expand Down

0 comments on commit 36ff163

Please sign in to comment.