From 444de221174e5eade16b9cdc44c42cf085660b9e Mon Sep 17 00:00:00 2001 From: Alex <77341673+defectiveAi@users.noreply.github.com> Date: Thu, 30 Mar 2023 13:26:31 +0200 Subject: [PATCH] refac: Improve performance of getting actors. (#1596) * refac: Improve performance of getting actors. * Update tests. --- .../DataHub.WebApi.Tests.csproj | 4 +-- .../MarketParticipantControllerTests.cs | 4 +-- .../MarketParticipantController.cs | 27 +++++++++---------- .../DataHub.WebApi/DataHub.WebApi.csproj | 6 ++--- 4 files changed, 19 insertions(+), 22 deletions(-) diff --git a/apps/dh/api-dh/source/DataHub.WebApi.Tests/DataHub.WebApi.Tests.csproj b/apps/dh/api-dh/source/DataHub.WebApi.Tests/DataHub.WebApi.Tests.csproj index dfbeedb8d3..b1c1eac7e7 100644 --- a/apps/dh/api-dh/source/DataHub.WebApi.Tests/DataHub.WebApi.Tests.csproj +++ b/apps/dh/api-dh/source/DataHub.WebApi.Tests/DataHub.WebApi.Tests.csproj @@ -31,8 +31,8 @@ limitations under the License. - - + + diff --git a/apps/dh/api-dh/source/DataHub.WebApi.Tests/Integration/Controllers/MarketParticipantControllerTests.cs b/apps/dh/api-dh/source/DataHub.WebApi.Tests/Integration/Controllers/MarketParticipantControllerTests.cs index 92b2a764fa..6d6032a69b 100644 --- a/apps/dh/api-dh/source/DataHub.WebApi.Tests/Integration/Controllers/MarketParticipantControllerTests.cs +++ b/apps/dh/api-dh/source/DataHub.WebApi.Tests/Integration/Controllers/MarketParticipantControllerTests.cs @@ -72,7 +72,7 @@ public async Task GetFilteredActors_NotFas_ReturnsSingleActor(OrganizationDto or .ReturnsAsync(organizations); MarketParticipantClientMock - .Setup(client => client.GetActorsAsync(organization.OrganizationId)) + .Setup(client => client.GetActorsAsync()) .ReturnsAsync(actors); MarketParticipantClientMock @@ -118,7 +118,7 @@ public async Task GetFilteredActors_IsFas_ReturnsAllActors(OrganizationDto organ .ReturnsAsync(organizations); MarketParticipantClientMock - .Setup(client => client.GetActorsAsync(organization.OrganizationId)) + .Setup(client => client.GetActorsAsync()) .ReturnsAsync(actors); MarketParticipantClientMock diff --git a/apps/dh/api-dh/source/DataHub.WebApi/Controllers/MarketParticipantController.cs b/apps/dh/api-dh/source/DataHub.WebApi/Controllers/MarketParticipantController.cs index cfec1a9aae..09e3da594c 100644 --- a/apps/dh/api-dh/source/DataHub.WebApi/Controllers/MarketParticipantController.cs +++ b/apps/dh/api-dh/source/DataHub.WebApi/Controllers/MarketParticipantController.cs @@ -58,15 +58,19 @@ public Task>> GetAllOrganiza .GetOrganizationsAsync() .ConfigureAwait(false); - var result = new List(); + var allActors = await _client + .GetActorsAsync() + .ConfigureAwait(false); + + var groupByOrganization = allActors.ToLookup(a => a.OrganizationId); - foreach (var organization in organizations) + var result = organizations.Select(organization => { - var actors = await _client.GetActorsAsync(organization.OrganizationId); - result.Add(new OrganizationWithActorsDto(organization, actors)); - } + var actors = groupByOrganization[organization.OrganizationId]; + return new OrganizationWithActorsDto(organization, actors); + }); - return (IEnumerable)result; + return result; }); } @@ -121,17 +125,10 @@ public Task>> GetFilteredActorsAsync( var gridAreas = await _client.GetGridAreasAsync().ConfigureAwait(false); var gridAreaLookup = gridAreas.ToDictionary(x => x.Id); - var organizations = await _client - .GetOrganizationsAsync() + var actors = await _client + .GetActorsAsync() .ConfigureAwait(false); - var actors = new List(); - - foreach (var organizationDto in organizations) - { - actors.AddRange(await _client.GetActorsAsync(organizationDto.OrganizationId).ConfigureAwait(false)); - } - var accessibleActors = actors .Select(x => new FilteredActorDto( diff --git a/apps/dh/api-dh/source/DataHub.WebApi/DataHub.WebApi.csproj b/apps/dh/api-dh/source/DataHub.WebApi/DataHub.WebApi.csproj index 2299bf47d1..1fb9c724cd 100644 --- a/apps/dh/api-dh/source/DataHub.WebApi/DataHub.WebApi.csproj +++ b/apps/dh/api-dh/source/DataHub.WebApi/DataHub.WebApi.csproj @@ -55,9 +55,9 @@ limitations under the License. - - - + + +