Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multithreaded aggregation #44

Merged
merged 2 commits into from
Dec 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 23 additions & 17 deletions CentralHub.Api/Services/LocalizationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using CentralHub.Api.Dtos;
using CentralHub.Api.Model;
using CentralHub.Api.Model.Responses.Room;
using CentralHub.Api.Threading;

namespace CentralHub.Api.Services;

Expand Down Expand Up @@ -37,34 +38,39 @@
public async Task AggregateMeasurementsAsync(CancellationToken stoppingToken)
{
var roomMeasurementGroups = await _aggregatorRepository.GetRoomMeasurementGroupsAsync(stoppingToken);
var measuredRooms = new List<RoomDto>();
var allRooms = await _roomRepository.GetRoomsAsync(stoppingToken);
using var measuredRoomsMutex = new CancellableMutex<List<RoomDto>>(new List<RoomDto>());

foreach (var (roomId, measurementGroups) in roomMeasurementGroups)
await Parallel.ForEachAsync(
roomMeasurementGroups,
stoppingToken,
async (roomMeasurementGroup, stoppingToken) =>
{
var room = await _roomRepository.GetRoomByIdAsync(
roomId,
stoppingToken);
var room = allRooms.SingleOrDefault(r => r.RoomDtoId == roomMeasurementGroup.Key);

if (room == null)
{
_logger.LogWarning("Room with id {RoomId} was not found.", roomId);
continue;
_logger.LogWarning("Room with id {RoomId} was not found.", roomMeasurementGroup.Key);
return;
}
measuredRooms.Add(room);

await measuredRoomsMutex.Lock(m => m.Add(room), stoppingToken);

await _aggregatorRepository.AddAggregatedMeasurementAsync(
CreateAggregatedMeasurement(room, measurementGroups),
CreateAggregatedMeasurement(room, roomMeasurementGroup.Value),
stoppingToken);
}

var allRooms = await _roomRepository.GetRoomsAsync(stoppingToken);
});

foreach (var room in allRooms.Where(r => !measuredRooms.Contains(r)))
await measuredRoomsMutex.Lock(async m =>
{
await _aggregatorRepository.AddAggregatedMeasurementAsync(
CreateAggregatedMeasurement(room, new List<MeasurementGroup>()),
stoppingToken);
}
foreach (var room in allRooms.Where(r => !m.Contains(r)))
{
await _aggregatorRepository.AddAggregatedMeasurementAsync(
CreateAggregatedMeasurement(room, new List<MeasurementGroup>()),
stoppingToken);
}
},
stoppingToken);
}

private static AggregatedMeasurementDto CreateAggregatedMeasurement(RoomDto room, IReadOnlyCollection<MeasurementGroup> measurementGroups)
Expand Down Expand Up @@ -133,7 +139,7 @@
/// </summary>
/// <param name="measurements">An enumerable collection of the measurements</param>
/// <returns>An enumerable collection of measurements with only unique macAddresses.</returns>
private static IEnumerable<Measurement> FilterMeasurements(IEnumerable<Measurement> measurements)

Check warning on line 142 in CentralHub.Api/Services/LocalizationService.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Change return type of method 'FilterMeasurements' from 'System.Collections.Generic.IEnumerable<CentralHub.Api.Model.Measurement>' to 'System.Collections.Generic.Dictionary<CentralHub.Api.Services.LocalizationService.FilterCriteria, CentralHub.Api.Model.Measurement>.ValueCollection' for improved performance (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1859)

Check warning on line 142 in CentralHub.Api/Services/LocalizationService.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Change return type of method 'FilterMeasurements' from 'System.Collections.Generic.IEnumerable<CentralHub.Api.Model.Measurement>' to 'System.Collections.Generic.Dictionary<CentralHub.Api.Services.LocalizationService.FilterCriteria, CentralHub.Api.Model.Measurement>.ValueCollection' for improved performance (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1859)

Check warning on line 142 in CentralHub.Api/Services/LocalizationService.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Change return type of method 'FilterMeasurements' from 'System.Collections.Generic.IEnumerable<CentralHub.Api.Model.Measurement>' to 'System.Collections.Generic.Dictionary<CentralHub.Api.Services.LocalizationService.FilterCriteria, CentralHub.Api.Model.Measurement>.ValueCollection' for improved performance (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1859)

Check warning on line 142 in CentralHub.Api/Services/LocalizationService.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Change return type of method 'FilterMeasurements' from 'System.Collections.Generic.IEnumerable<CentralHub.Api.Model.Measurement>' to 'System.Collections.Generic.Dictionary<CentralHub.Api.Services.LocalizationService.FilterCriteria, CentralHub.Api.Model.Measurement>.ValueCollection' for improved performance (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1859)

Check warning on line 142 in CentralHub.Api/Services/LocalizationService.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Change return type of method 'FilterMeasurements' from 'System.Collections.Generic.IEnumerable<CentralHub.Api.Model.Measurement>' to 'System.Collections.Generic.Dictionary<CentralHub.Api.Services.LocalizationService.FilterCriteria, CentralHub.Api.Model.Measurement>.ValueCollection' for improved performance (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1859)

Check warning on line 142 in CentralHub.Api/Services/LocalizationService.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Change return type of method 'FilterMeasurements' from 'System.Collections.Generic.IEnumerable<CentralHub.Api.Model.Measurement>' to 'System.Collections.Generic.Dictionary<CentralHub.Api.Services.LocalizationService.FilterCriteria, CentralHub.Api.Model.Measurement>.ValueCollection' for improved performance (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1859)
{
var filteredMeasurements = new Dictionary<FilterCriteria, Measurement>();
foreach (var (filterCriteria, measurement) in measurements.Select(m => (new FilterCriteria(m.MacAddress, m.Type), m)))
Expand Down
Loading