-
Notifications
You must be signed in to change notification settings - Fork 99
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
EMFree data source should regard specified time range #389
EMFree data source should regard specified time range #389
Conversation
Signed-off-by: Yasumasa Suenaga <suenaga@oss.nttdata.com>
Codecov Report
❗ Your organization needs to install the Codecov GitHub app to enable full functionality. @@ Coverage Diff @@
## dev #389 +/- ##
==========================================
+ Coverage 74.21% 74.67% +0.46%
==========================================
Files 77 77
Lines 2637 2646 +9
Branches 266 268 +2
==========================================
+ Hits 1957 1976 +19
+ Misses 598 589 -9
+ Partials 82 81 -1
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this all looks good, left a single comment to review on tomorrows meeting - otherwise happy to merge.
@@ -86,15 +86,27 @@ public async Task<IEnumerable<EmissionsData>> GetCarbonIntensityAsync(Location l | |||
} | |||
|
|||
List<EmissionsData> EmissionsDataList = new List<EmissionsData>(); | |||
var emissionDateTime = gridEmissionData.Data.Datetime; | |||
var shouldReturn = true; | |||
if (emissionDateTime != null && periodStartTime < periodEndTime) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we check below regardless of whether periodStartTime < periodEndTime?
This does catch any cases where the emissionsDateTime is outside of the range periodStartTime and periodEndTime if both are specified, but e.g. if only periodStartTime is specified for a time thats in the future, then it will still return the emission at current time.
Again, we might want to disregard this as this endpoint is only for current/historical data and no one should use dates for forecasting here, but maybe we should not return any data in that case as
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree, so is it better to throw exception (e.g. ArgumentOutOfRangeException ) instead of returning empty list in that case? I think it is an illegal case if GetCarbonIntensityAsync()
receives future datetime, so I think it is appropriate to throw an exception.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think other datasources (WattTimeDataSource
, ElectricityMapsDataSource
) have same problems. If it is proper to throw exception, it is better to handle another issue/PR to fix all of datasources.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Below is for WattTime (I requested 1 hour into the future, on the emissions/bylocation endpoint), so it is returning 204 (which is to be expected?)
The way this is implemented in the WattTimeDataSource.cs, is that they use a helper class IntervalHelper
which is just a friendly wrapper around a clamping filter:
/~https://github.com/Green-Software-Foundation/carbon-aware-sdk/blob/dev/src/CarbonAware.DataSources/CarbonAware.DataSources.WattTime/src/WattTimeDataSource.cs#L72
Its simple, but maybe we can reuse it here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So as discussed in #394 - for now align to the WattTIme DataSource (so return empty array on invalid date range/future date range) using IntervalHelper. Create a new issue to consider throwing exception on requesting future data on the historical endpoints
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I pushed new commit to return empty list when ElectricityMapsFree.GetCarbonIntensityAsync() receives request for forecast. AFAICS IntervalHelper
expects array of EmissionsData
which includes emission datetime, however EMFree seems to be possible to lack it.
Lines 90 to 96 in cfd76fe
var emissionData = new EmissionsData() | |
{ | |
Location = location.Name ?? "", | |
Time = gridEmissionData.Data.Datetime ?? new DateTimeOffset(), | |
Rating = gridEmissionData.Data.CarbonIntensity ?? 0.0, | |
Duration = new TimeSpan(0, 0, 0) | |
}; |
And also EmissionsHandler
would set same value (UtcNow
) when startTime
and endTime
is null
.
carbon-aware-sdk/src/GSF.CarbonAware/src/Handlers/EmissionsHandler.cs
Lines 50 to 51 in cfd76fe
var startTime = parameters.GetStartOrDefault(DateTimeOffset.UtcNow); | |
var endTime = parameters.GetEndOrDefault(startTime); |
Hence I fixed to check whether periodStartTime
is less than current time, and returns empty list if so.
…receives request for forecast Signed-off-by: Yasumasa Suenaga <suenaga@oss.nttdata.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, let's discuss how we handle invalid time ranges further in #396
Pull Request
Issue Number: N/A
Summary
Make ElectricityMapsFree data source aware of specifying time range.
Currently WebAPI would return data even if it is out of time range which is specified in below:
It should be returned empty (HTTP 204 or empty JSON array) or an error in this case.
Changes
Z
to time string because time is treated as UTC. (Let me know if this change should be happened in other issue)Checklist
Are there API Changes?
No
Is this a breaking change?
EMFree data source would not return any data when it does not exist within specified time range. But it is expected behavior I think.