From a2c04afc11e5c01a630ddadd0a1d3955dddfd2e9 Mon Sep 17 00:00:00 2001 From: Craig Edmunds Date: Tue, 10 Dec 2024 13:21:10 +0000 Subject: [PATCH] CDMS-179 optimises Movements ByItemCount analytics for bigger datasets --- Btms.Analytics/MovementsAggregationService.cs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/Btms.Analytics/MovementsAggregationService.cs b/Btms.Analytics/MovementsAggregationService.cs index 7a4a9f84..ce31078e 100644 --- a/Btms.Analytics/MovementsAggregationService.cs +++ b/Btms.Analytics/MovementsAggregationService.cs @@ -13,6 +13,7 @@ using MongoDB.Driver; using Btms.Analytics.Extensions; +using Microsoft.EntityFrameworkCore.Query.SqlExpressions; namespace Btms.Analytics; @@ -58,17 +59,11 @@ public Task ByItemCount(DateTime from, DateTime to) var mongoQuery = context .Movements .Where(n => n.CreatedSource >= from && n.CreatedSource < to) - .GroupBy(m => new { Linked = m.Relationships.Notifications.Data.Count > 0, Items = m.Items.Count }) - .GroupBy(g => g.Key.Linked); - + .GroupBy(m => new { Linked = m.Relationships.Notifications.Data.Count > 0, ItemCount = m.Items.Count }) + .Select(g => new { g.Key.Linked, g.Key.ItemCount, Count = g.Count() }); + var mongoResult = mongoQuery .Execute(logger) - .SelectMany(g => g.Select(l => new - { - Linked = g.Key, - ItemCount = l.Key.Items, - Count = l.Count() - })) .ToList(); var dictionary = mongoResult