Endpoint for EPU data per month
Closed this issue · 8 comments
For the overview chart (see #27), I need EPU data per month. Instead of querying for all data per day and then aggregating on the frontend, I assume it would be better to create an endpoint/option for average EPU data per month? E.g.:
https://epu-index.herokuapp.com/api/epu/?format=json&aggregate=month
I don't require the start/end
options for this query.
Hi guys, it's now implemented at https://epu-index.herokuapp.com/epu-per-month/ !
@niconoe @bartaelterman the API currently gives me unordered (as an object), rather than array:
{
2003-08: 0.684210526263158,
2003-09: 1.0144927535652173,
2003-01: 0.8787878786818183,
2003-02: 0.8070175437894737,
2003-03: 0.8095238095714288,
2003-04: 0.99999999995,
2003-05: 1.1363636363636365,
2003-06: 1.3,
...
}
I might be able to work around this (as the charting library C3 will probably place time data in order), but it would be more convenient if I get the data as an array, just like the day API does. Bart, maybe you can help me figure out how the mapping function should work with the current data I get? How do I get the current data into two arrays?
One solution would be (not tested):
First create an array of the dates:
var dates = [];
for (var key in response_data) {
if (response_data.hasOwnProperty(key)) {
dates.push(key)
}
}
Now sort it (either as strings, or convert it to Dates first)
dates.sort(); // I think this sorts the array, and does not return a new sorted array so no need to assign the result to a new variable
And create a new array with the epu-index data in the same order
var epu_data = dates.map(function (x) {return response_data[x];}); // map will respect the order of the dates array
This should give you 2 arrays, sorted by date.
Having said all that, I would plead for letting the API return an array to keep our API more consistent.
@niconoe, thanks for the API. After some discussion with @bartaelterman, we think it's better to structure this API similar to the epu API. Here are the new requirements:
- Return the data as an array:
[
{
"month": "2010-08",
"epu": 3.141592
},
{
"month": "2010-09",
"epu": 12.49
},
]
- I would include
api
in the endpoint URL: https://epu-index.herokuapp.com/api/epu-per-month/ (cf. https://epu-index.herokuapp.com/api/epu/)
Question: I noticed that the epu API does not sort chronologically. @bartaelterman, do you think that is required? The chart can handle unsorted data for now.
Technically, sorting the API data chronologically will make no difference. The charting libraries will always sort the data before plotting them. Nevertheless, I would also vote for a sorted API.
Sorted is also consistent with the requirements in #45.
That's implemented, please test!
Works great!