Skip to content

Commit

Permalink
Fix Measurements with multiple trackers
Browse files Browse the repository at this point in the history
  • Loading branch information
mads256h committed Dec 4, 2023
1 parent 6cd66a0 commit 3aca672
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 390 deletions.
Original file line number Diff line number Diff line change
@@ -1,67 +1,21 @@
using System.Text.Json.Serialization;

namespace CentralHub.Api.Model.Responses.AggregatedMeasurements;

public sealed class AggregatedMeasurements
namespace CentralHub.Api.Model.Responses.Measurements;

[method: JsonConstructor]
public sealed class AggregatedMeasurements(int aggregatedMeasurementsId,
DateTime startTime,
DateTime endTime,
int bluetoothCount,
int wifiCount)
{
[JsonConstructor]
public AggregatedMeasurements(
int aggregatedMeasurementDtoId,
DateTime startTime,
DateTime endTime,
int measurementGroupCount,
int bluetoothMedian,
double bluetoothMean,
int bluetoothMin,
int bluetoothMax,
int totalBluetoothDeviceCount,
int wifiMedian,
double wifiMean,
int wifiMin,
int wifiMax,
int totalWifiDeviceCount)
{
AggregatedMeasurementDtoId = aggregatedMeasurementDtoId;
StartTime = startTime;
EndTime = endTime;
MeasurementGroupCount = measurementGroupCount;
BluetoothMedian = bluetoothMedian;
BluetoothMean = bluetoothMean;
BluetoothMin = bluetoothMin;
BluetoothMax = bluetoothMax;
TotalBluetoothDeviceCount = totalBluetoothDeviceCount;
WifiMedian = wifiMedian;
WifiMean = wifiMean;
WifiMin = wifiMin;
WifiMax = wifiMax;
TotalWifiDeviceCount = totalWifiDeviceCount;
}

public int AggregatedMeasurementDtoId { get; }

public DateTime StartTime { get; }

public DateTime EndTime { get; }

public int MeasurementGroupCount { get; }

public int BluetoothMedian { get; }

public double BluetoothMean { get; }

public int BluetoothMin { get; }

public int BluetoothMax { get; }

public int TotalBluetoothDeviceCount { get; }

public int WifiMedian { get; }
public int AggregatedMeasurementId { get; } = aggregatedMeasurementsId;

public double WifiMean { get; }
public DateTime StartTime { get; } = startTime;

public int WifiMin { get; }
public DateTime EndTime { get; } = endTime;

public int WifiMax { get; }
public int BluetoothCount { get; } = bluetoothCount;

public int TotalWifiDeviceCount { get; }
public int WifiCount { get; } = wifiCount;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace CentralHub.Api.Model.Responses.Measurements;
public sealed class GetAggregatedMeasurementsResponse
{
[JsonConstructor]
public GetAggregatedMeasurementsResponse(bool success, IReadOnlyCollection<AggregatedMeasurements.AggregatedMeasurements>? aggregatedMeasurements)
public GetAggregatedMeasurementsResponse(bool success, IReadOnlyCollection<AggregatedMeasurements>? aggregatedMeasurements)
{
Success = success;
AggregatedMeasurements = aggregatedMeasurements;
Expand All @@ -14,14 +14,14 @@ public GetAggregatedMeasurementsResponse(bool success, IReadOnlyCollection<Aggre
public bool Success { get; }

// Null when unsuccessful
public IReadOnlyCollection<AggregatedMeasurements.AggregatedMeasurements>? AggregatedMeasurements { get; }
public IReadOnlyCollection<AggregatedMeasurements>? AggregatedMeasurements { get; }

public static GetAggregatedMeasurementsResponse CreateUnsuccessful()
{
return new GetAggregatedMeasurementsResponse(false, null);
}

public static GetAggregatedMeasurementsResponse CreateSuccessful(IReadOnlyCollection<AggregatedMeasurements.AggregatedMeasurements> aggregatedMeasurements)
public static GetAggregatedMeasurementsResponse CreateSuccessful(IReadOnlyCollection<AggregatedMeasurements> aggregatedMeasurements)
{
return new GetAggregatedMeasurementsResponse(true, aggregatedMeasurements);
}
Expand Down
41 changes: 13 additions & 28 deletions CentralHub.Api.Tests/MeasurementControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class MeasurementControllerTests
private MeasurementController _measurementController;
private IRoomRepository _roomRepository;
private ITrackerRepository _trackerRepository;
private IMeasurementRepository _aggregatedMeasurementRepository;
private IMeasurementRepository _measurementRepository;

private LocalizationService _localizationService;

Expand Down Expand Up @@ -45,26 +45,28 @@ public void Setup()
_trackerRepository = new MockTrackerRepository(roomDto);
_trackerId = _trackerRepository.AddTrackerAsync(trackerDto, default).GetAwaiter().GetResult();

_aggregatedMeasurementRepository = new MockAggregatedMeasurementRepository();
_measurementRepository = new MockAggregatedMeasurementRepository();
_measurementController = new MeasurementController(
NullLogger<MeasurementController>.Instance,
_aggregatedMeasurementRepository,
_trackerRepository);
_roomRepository,
_trackerRepository,
_measurementRepository
);

_localizationService = new LocalizationService(
NullLogger<LocalizationService>.Instance,
_aggregatedMeasurementRepository,
_measurementRepository,
_roomRepository
);
}

[TearDown]
public void TearDown()
{
var aggregatedMeasurements = _aggregatedMeasurementRepository.GetAggregatedMeasurementsAsync(_roomId, default).GetAwaiter().GetResult();
var aggregatedMeasurements = _measurementRepository.GetAggregatedMeasurementsAsync(_roomId, default).GetAwaiter().GetResult();
foreach (var measurement in aggregatedMeasurements)
{
_aggregatedMeasurementRepository.RemoveAggregatedMeasurementAsync(measurement, default).GetAwaiter().GetResult();
_measurementRepository.RemoveAggregatedMeasurementAsync(measurement, default).GetAwaiter().GetResult();
}
}

Expand All @@ -78,7 +80,7 @@ public async Task TestAddMeasurements()

var addMeasurementsRequest = new AddMeasurementsRequest(_trackerId, measurements);
await _measurementController.AddMeasurements(addMeasurementsRequest, default);
var addedMeasurements = (await _aggregatedMeasurementRepository.GetRoomMeasurementGroupsAsync(default))[_roomId][0].Measurements;
var addedMeasurements = (await _measurementRepository.GetRoomMeasurementGroupsAsync(default))[_roomId][0].Measurements;

Assert.That(addedMeasurements, Has.Count.EqualTo(measurements.Length));
Assert.That(addedMeasurements, Does.Contain(measurements[0]));
Expand All @@ -95,7 +97,7 @@ public async Task TrackersFilteredFromMeasurements()

var addMeasurementsRequest = new AddMeasurementsRequest(_trackerId, measurements);
await _measurementController.AddMeasurements(addMeasurementsRequest, default);
var addedMeasurements = (await _aggregatedMeasurementRepository.GetRoomMeasurementGroupsAsync(default))[_roomId][0].Measurements;
var addedMeasurements = (await _measurementRepository.GetRoomMeasurementGroupsAsync(default))[_roomId][0].Measurements;

Assert.That(addedMeasurements, Has.Count.EqualTo(0));
}
Expand All @@ -111,15 +113,6 @@ public async Task CalibrateMeasurements()
var addMeasurementsRequest = new AddMeasurementsRequest(_trackerId, measurements);
await _measurementController.AddMeasurements(addMeasurementsRequest, default);

var measurements2 = new Measurement[] {
new Measurement("13:22:33:44:55:66", Measurement.Protocol.Bluetooth, 10),
new Measurement("ac:bb:cc:dd:ee:ff", Measurement.Protocol.Wifi, 20),
new Measurement("14:22:33:44:55:66", Measurement.Protocol.Bluetooth, 10),
new Measurement("ab:bb:cc:dd:ee:ff", Measurement.Protocol.Wifi, 20)
};

var addMeasurementsRequest2 = new AddMeasurementsRequest(_trackerId, measurements2);
await _measurementController.AddMeasurements(addMeasurementsRequest2, default);
await _localizationService.AggregateMeasurementsAsync(default);

var measurements3 = new Measurement[] {
Expand All @@ -135,15 +128,7 @@ public async Task CalibrateMeasurements()

var lastAggregatedMeasurements = (await _measurementController.GetAggregateMeasurements(_roomId, default)).AggregatedMeasurements.Last();

Assert.That(lastAggregatedMeasurements.BluetoothMedian, Is.EqualTo(1));
Assert.That(lastAggregatedMeasurements.WifiMedian, Is.EqualTo(1));
Assert.That(lastAggregatedMeasurements.BluetoothMean, Is.EqualTo(1f));
Assert.That(lastAggregatedMeasurements.WifiMean, Is.EqualTo(1f));
Assert.That(lastAggregatedMeasurements.BluetoothMax, Is.EqualTo(1));
Assert.That(lastAggregatedMeasurements.WifiMax, Is.EqualTo(1));
Assert.That(lastAggregatedMeasurements.BluetoothMin, Is.EqualTo(1));
Assert.That(lastAggregatedMeasurements.WifiMin, Is.EqualTo(1));
Assert.That(lastAggregatedMeasurements.TotalBluetoothDeviceCount, Is.EqualTo(1));
Assert.That(lastAggregatedMeasurements.TotalWifiDeviceCount, Is.EqualTo(1));
Assert.That(lastAggregatedMeasurements.BluetoothCount, Is.EqualTo(1));
Assert.That(lastAggregatedMeasurements.WifiCount, Is.EqualTo(1));
}
}
44 changes: 8 additions & 36 deletions CentralHub.Api.Tests/ServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,8 @@ public async Task TestAggregateSingleGroupMeasurements()

var aggregatedMeasurement = aggregatedMeasurements.First();

Assert.That(aggregatedMeasurement.MeasurementGroupCount, Is.EqualTo(1));
Assert.That(aggregatedMeasurement.BluetoothMedianDeviceCount, Is.EqualTo(1));
Assert.That(aggregatedMeasurement.WifiMedianDeviceCount, Is.EqualTo(1));
Assert.That(aggregatedMeasurement.BluetoothMeanDeviceCount, Is.EqualTo(1f));
Assert.That(aggregatedMeasurement.WifiMeanDeviceCount, Is.EqualTo(1f));
Assert.That(aggregatedMeasurement.BluetoothMaxDeviceCount, Is.EqualTo(1));
Assert.That(aggregatedMeasurement.WifiMaxDeviceCount, Is.EqualTo(1));
Assert.That(aggregatedMeasurement.BluetoothMinDeviceCount, Is.EqualTo(1));
Assert.That(aggregatedMeasurement.WifiMinDeviceCount, Is.EqualTo(1));
Assert.That(aggregatedMeasurement.TotalBluetoothDeviceCount, Is.EqualTo(1));
Assert.That(aggregatedMeasurement.TotalWifiDeviceCount, Is.EqualTo(1));
Assert.That(aggregatedMeasurement.BluetoothCount, Is.EqualTo(1));
Assert.That(aggregatedMeasurement.WifiCount, Is.EqualTo(1));
}

[Test]
Expand Down Expand Up @@ -116,17 +107,8 @@ public async Task TestAggregateMultipleGroupsOfMeasurements()

var aggregatedMeasurement = aggregatedMeasurements.First();

Assert.That(aggregatedMeasurement.MeasurementGroupCount, Is.EqualTo(2));
Assert.That(aggregatedMeasurement.BluetoothMedianDeviceCount, Is.EqualTo(3));
Assert.That(aggregatedMeasurement.WifiMedianDeviceCount, Is.EqualTo(3));
Assert.That(aggregatedMeasurement.BluetoothMeanDeviceCount, Is.EqualTo(2.5f));
Assert.That(aggregatedMeasurement.WifiMeanDeviceCount, Is.EqualTo(2.5f));
Assert.That(aggregatedMeasurement.BluetoothMaxDeviceCount, Is.EqualTo(3));
Assert.That(aggregatedMeasurement.WifiMaxDeviceCount, Is.EqualTo(3));
Assert.That(aggregatedMeasurement.BluetoothMinDeviceCount, Is.EqualTo(2));
Assert.That(aggregatedMeasurement.WifiMinDeviceCount, Is.EqualTo(2));
Assert.That(aggregatedMeasurement.TotalBluetoothDeviceCount, Is.EqualTo(5));
Assert.That(aggregatedMeasurement.TotalWifiDeviceCount, Is.EqualTo(5));
Assert.That(aggregatedMeasurement.BluetoothCount, Is.EqualTo(5));
Assert.That(aggregatedMeasurement.WifiCount, Is.EqualTo(5));
}

[Test]
Expand All @@ -148,9 +130,8 @@ public async Task TestAggregateDuplicateMeasurements()

var aggregatedMeasurement = aggregatedMeasurements.First();

Assert.That(aggregatedMeasurement.MeasurementGroupCount, Is.EqualTo(1));
Assert.That(aggregatedMeasurement.TotalBluetoothDeviceCount, Is.EqualTo(1));
Assert.That(aggregatedMeasurement.TotalWifiDeviceCount, Is.EqualTo(1));
Assert.That(aggregatedMeasurement.BluetoothCount, Is.EqualTo(1));
Assert.That(aggregatedMeasurement.WifiCount, Is.EqualTo(1));
}

[Test]
Expand All @@ -165,16 +146,7 @@ public async Task TestNoMeasurements()

var aggregatedMeasurement = aggregatedMeasurements.First();

Assert.That(aggregatedMeasurement.MeasurementGroupCount, Is.EqualTo(1));
Assert.That(aggregatedMeasurement.BluetoothMedianDeviceCount, Is.EqualTo(0));
Assert.That(aggregatedMeasurement.WifiMedianDeviceCount, Is.EqualTo(0));
Assert.That(aggregatedMeasurement.BluetoothMeanDeviceCount, Is.EqualTo(0));
Assert.That(aggregatedMeasurement.WifiMeanDeviceCount, Is.EqualTo(0));
Assert.That(aggregatedMeasurement.BluetoothMaxDeviceCount, Is.EqualTo(0));
Assert.That(aggregatedMeasurement.WifiMaxDeviceCount, Is.EqualTo(0));
Assert.That(aggregatedMeasurement.BluetoothMinDeviceCount, Is.EqualTo(0));
Assert.That(aggregatedMeasurement.WifiMinDeviceCount, Is.EqualTo(0));
Assert.That(aggregatedMeasurement.TotalBluetoothDeviceCount, Is.EqualTo(0));
Assert.That(aggregatedMeasurement.TotalWifiDeviceCount, Is.EqualTo(0));
Assert.That(aggregatedMeasurement.BluetoothCount, Is.EqualTo(0));
Assert.That(aggregatedMeasurement.WifiCount, Is.EqualTo(0));
}
}
Loading

0 comments on commit 3aca672

Please sign in to comment.