From 0cd1998ae8a502890302025493e48cd36f5c9fe1 Mon Sep 17 00:00:00 2001 From: Yasumasa Suenaga Date: Tue, 15 Oct 2024 15:38:30 +0900 Subject: [PATCH] EM datasource didn't filter out-of-range emission data (#572) Issue #570 Signed-off-by: Yasumasa Suenaga --- .../src/ElectricityMapsDataSource.cs | 22 +++++++++---------- .../test/ElectricityMapsDataSourceTests.cs | 22 ++++++++++++++----- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/CarbonAware.DataSources/CarbonAware.DataSources.ElectricityMaps/src/ElectricityMapsDataSource.cs b/src/CarbonAware.DataSources/CarbonAware.DataSources.ElectricityMaps/src/ElectricityMapsDataSource.cs index acfc1339e..56bf25b46 100644 --- a/src/CarbonAware.DataSources/CarbonAware.DataSources.ElectricityMaps/src/ElectricityMapsDataSource.cs +++ b/src/CarbonAware.DataSources/CarbonAware.DataSources.ElectricityMaps/src/ElectricityMapsDataSource.cs @@ -138,18 +138,18 @@ private async Task> GetRecentCarbonInstensityData(L private IEnumerable HistoryCarbonIntensityToEmissionsData(Location location, IEnumerable data, DateTimeOffset startTime, DateTimeOffset endTime) { - IEnumerable emissions; var duration = GetDurationFromHistoryDataPointsOrDefault(data, TimeSpan.Zero); - emissions = data.Select(d => - { - var emission = (EmissionsData) d; - emission.Location = location.Name ?? string.Empty; - emission.Time = d.DateTime; - emission.Duration = duration; - return emission; - }); - - return emissions; + return data.Where(d => ((startTime <= d.DateTime) && (d.DateTime < endTime)) || + ((startTime <= d.DateTime.Add(duration)) && (d.DateTime.Add(duration) < endTime)) || + ((d.DateTime < startTime) && (endTime < d.DateTime.Add(duration)))) + .Select(d => + { + var emission = (EmissionsData)d; + emission.Location = location.Name ?? string.Empty; + emission.Time = d.DateTime; + emission.Duration = duration; + return emission; + }); } private TimeSpan GetDurationFromHistoryDataPointsOrDefault(IEnumerable carbonIntensityDataPoints, TimeSpan defaultValue) diff --git a/src/CarbonAware.DataSources/CarbonAware.DataSources.ElectricityMaps/test/ElectricityMapsDataSourceTests.cs b/src/CarbonAware.DataSources/CarbonAware.DataSources.ElectricityMaps/test/ElectricityMapsDataSourceTests.cs index ff1dcc56a..58bddc973 100644 --- a/src/CarbonAware.DataSources/CarbonAware.DataSources.ElectricityMaps/test/ElectricityMapsDataSourceTests.cs +++ b/src/CarbonAware.DataSources/CarbonAware.DataSources.ElectricityMaps/test/ElectricityMapsDataSourceTests.cs @@ -134,6 +134,7 @@ public async Task GetCarbonIntensity_CallsExpectedClientEndpoint(int startTimeOf public async Task GetCarbonIntensity_DateRangeWithin24Hours_ReturnsResultsWhenRecordsFound() { var startDate = DateTimeOffset.UtcNow.AddHours(-10); + var expectedDate = startDate.AddMinutes(-10); var endDate = startDate.AddHours(1); var expectedCarbonIntensity = 100; @@ -145,10 +146,12 @@ public async Task GetCarbonIntensity_DateRangeWithin24Hours_ReturnsResultsWhenRe { new CarbonIntensity() { - Value = expectedCarbonIntensity, + DateTime = startDate.AddMinutes(-40), // out of range + Value = 0, }, new CarbonIntensity() { + DateTime = expectedDate, Value = expectedCarbonIntensity, } } @@ -162,10 +165,11 @@ public async Task GetCarbonIntensity_DateRangeWithin24Hours_ReturnsResultsWhenRe var result = await this._dataSource.GetCarbonIntensityAsync(new List() { _defaultLocation }, startDate, endDate); Assert.IsNotNull(result); - Assert.That(result.Count(), Is.EqualTo(2)); + Assert.That(result.Count(), Is.EqualTo(1)); var first = result.First(); Assert.IsNotNull(first); + Assert.That(expectedDate, Is.EqualTo(first.Time)); Assert.That(first.Rating, Is.EqualTo(expectedCarbonIntensity)); Assert.That(first.Location, Is.EqualTo(_defaultLocation.Name)); @@ -176,6 +180,7 @@ public async Task GetCarbonIntensity_DateRangeWithin24Hours_ReturnsResultsWhenRe public async Task GetCarbonIntensity_DateRangeMore24Hours_ReturnsResultsWhenRecordsFound() { var startDate = _defaultDataStartTime; + var expectedDate = startDate.AddMinutes(-10); var endDate = startDate.AddHours(1); var expectedCarbonIntensity = 100; @@ -187,10 +192,12 @@ public async Task GetCarbonIntensity_DateRangeMore24Hours_ReturnsResultsWhenReco { new CarbonIntensity() { - Value = expectedCarbonIntensity, + DateTime = startDate.AddMinutes(-40), // out of range + Value = 0, }, new CarbonIntensity() { + DateTime = expectedDate, Value = expectedCarbonIntensity, } } @@ -206,10 +213,11 @@ public async Task GetCarbonIntensity_DateRangeMore24Hours_ReturnsResultsWhenReco var result = await this._dataSource.GetCarbonIntensityAsync(new List() { _defaultLocation }, startDate, endDate); Assert.IsNotNull(result); - Assert.That(result.Count(), Is.EqualTo(2)); + Assert.That(result.Count(), Is.EqualTo(1)); var first = result.First(); Assert.IsNotNull(first); + Assert.That(expectedDate, Is.EqualTo(first.Time)); Assert.That(first.Rating, Is.EqualTo(expectedCarbonIntensity)); Assert.That(first.Location, Is.EqualTo(_defaultLocation.Name)); @@ -261,7 +269,9 @@ public async Task GetDurationBetweenHistoryDataPoints_ReturnsDefaultDuration_Whe { HistoryData = new List() { - new CarbonIntensity() + new CarbonIntensity(){ + DateTime = startDate.AddMinutes(30), + }, } }; @@ -285,7 +295,7 @@ public async Task GetDurationBetweenHistoryDataPoints_WhenMultipleDataPoints_Ret { var startDate = DateTimeOffset.UtcNow.AddHours(-8); var endDate = startDate.AddHours(1); - var expectedDuration = TimeSpan.FromHours(1); + var expectedDuration = TimeSpan.FromMinutes(59); // Arrange _locationSource.Setup(l => l.ToGeopositionLocationAsync(_defaultLocation)).Returns(Task.FromResult(_defaultLocation));