Skip to content

Commit

Permalink
EM datasource didn't filter out-of-range emission data (#572)
Browse files Browse the repository at this point in the history
Issue #570

Signed-off-by: Yasumasa Suenaga <[email protected]>
  • Loading branch information
YaSuenag authored Oct 15, 2024
1 parent 0afba03 commit 0cd1998
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,18 @@ private async Task<IEnumerable<CarbonIntensity>> GetRecentCarbonInstensityData(L

private IEnumerable<EmissionsData> HistoryCarbonIntensityToEmissionsData(Location location, IEnumerable<CarbonIntensity> data, DateTimeOffset startTime, DateTimeOffset endTime)
{
IEnumerable<EmissionsData> 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<CarbonIntensity> carbonIntensityDataPoints, TimeSpan defaultValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
}
}
Expand All @@ -162,10 +165,11 @@ public async Task GetCarbonIntensity_DateRangeWithin24Hours_ReturnsResultsWhenRe
var result = await this._dataSource.GetCarbonIntensityAsync(new List<Location>() { _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));

Expand All @@ -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;

Expand All @@ -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,
}
}
Expand All @@ -206,10 +213,11 @@ public async Task GetCarbonIntensity_DateRangeMore24Hours_ReturnsResultsWhenReco
var result = await this._dataSource.GetCarbonIntensityAsync(new List<Location>() { _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));

Expand Down Expand Up @@ -261,7 +269,9 @@ public async Task GetDurationBetweenHistoryDataPoints_ReturnsDefaultDuration_Whe
{
HistoryData = new List<CarbonIntensity>()
{
new CarbonIntensity()
new CarbonIntensity(){
DateTime = startDate.AddMinutes(30),
},
}
};

Expand All @@ -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));

Expand Down

0 comments on commit 0cd1998

Please sign in to comment.