Skip to content

Commit

Permalink
Improved performance of the measurement range endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Casper-NS committed Dec 4, 2023
1 parent abc12c7 commit a9d21ed
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions CentralHub.Api/Controllers/MeasurementController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Immutable;
using CentralHub.Api.Dtos;
using CentralHub.Api.Model;
using CentralHub.Api.Model.Requests.Localization;
using CentralHub.Api.Model.Responses.AggregatedMeasurements;
Expand Down Expand Up @@ -60,12 +61,31 @@ public async Task<GetAggregatedMeasurementsResponse> GetAggregateMeasurements(in
return GetAggregatedMeasurementsResponse.CreateUnsuccessful();
}

return GetAggregatedMeasurementsResponse
.CreateSuccessful(CreateMeasurements(aggregatedMeasurements));
}

[HttpGet("range")]
public async Task<GetAggregatedMeasurementsResponse> GetAggregateMeasurements(int roomId, DateTime timeStart, DateTime timeEnd, CancellationToken token)
{
var aggregatedMeasurements = await _aggregatorRepository.GetAggregatedMeasurementsAsync(roomId, timeStart, timeEnd, token);

if (aggregatedMeasurements == null)
{
return GetAggregatedMeasurementsResponse.CreateUnsuccessful();
}

return GetAggregatedMeasurementsResponse
.CreateSuccessful(CreateMeasurements(aggregatedMeasurements));
}

private static IReadOnlyList<AggregatedMeasurements> CreateMeasurements(IEnumerable<AggregatedMeasurementDto> aggregatedMeasurements)
{
var recentAggregatedMeasurements = aggregatedMeasurements.Where(am => am.EndTime > (DateTime.UtcNow - TimeSpan.FromDays(1)));
var wifiCalibrationNumber = recentAggregatedMeasurements.Min(am => am.WifiMinDeviceCount);
var bluetoothCalibrationNumber = recentAggregatedMeasurements.Min(am => am.BluetoothMinDeviceCount);

return GetAggregatedMeasurementsResponse.CreateSuccessful(
aggregatedMeasurements.Select(am => new AggregatedMeasurements(
return aggregatedMeasurements.Select(am => new AggregatedMeasurements(
am.AggregatedMeasurementDtoId,
am.StartTime,
am.EndTime,
Expand All @@ -80,22 +100,6 @@ public async Task<GetAggregatedMeasurementsResponse> GetAggregateMeasurements(in
am.WifiMaxDeviceCount - wifiCalibrationNumber,
am.WifiMinDeviceCount - wifiCalibrationNumber,
am.TotalWifiDeviceCount - wifiCalibrationNumber)
).ToImmutableArray());
}

[HttpGet("range")]
public async Task<GetAggregatedMeasurementsResponse> GetAggregateMeasurements(int roomId, DateTime timeStart, DateTime timeEnd, CancellationToken token)
{
var aggregatedMeasurements = await GetAggregateMeasurements(roomId, token);

if (!aggregatedMeasurements.Success)
{
return GetAggregatedMeasurementsResponse.CreateUnsuccessful();
}

return GetAggregatedMeasurementsResponse
.CreateSuccessful(aggregatedMeasurements.AggregatedMeasurements!
.Where(am => am.StartTime >= timeStart && am.EndTime <= timeEnd)
.ToImmutableArray());
).ToImmutableArray();
}
}

0 comments on commit a9d21ed

Please sign in to comment.