fablabbcn / smartcitizen-api

The Smart Citizen Engine
https://developer.smartcitizen.me
GNU Affero General Public License v3.0
10 stars 4 forks source link

Return something sensible when searching readings for non-existent components #276

Closed timcowlishaw closed 11 months ago

timcowlishaw commented 11 months ago

Fixes #275

Currently, we 500 when searching for readings for a sensor which isn't present on a device. This PR instead returns a meaningful empty response:

{
  "device_id": "15333",
  "sensor_key": null,
  "sensor_id": 3,
  "component_id": null,
  "rollup": "20m",
  "function": "avg",
  "from": "2023-10-25",
  "to": "2023-10-26",
  "sample_size": 0,
  "readings": []
}
oscgonfer commented 11 months ago

Tested in staging. Very good work!

https://staging-api.smartcitizen.me/v0/devices/16602/readings?sensor_id=550&rollup=1m&from=2010-01-01

{"device_id":"16602","sensor_key":null,"sensor_id":550,"component_id":null,"rollup":"1m","function":"avg","from":"2010-01-01","to":null,"sample_size":0,"readings":[]}

https://staging-api.smartcitizen.me/v0/devices/16602/readings?sensor_id=55&rollup=1m&from=2010-01-01

{"device_id":16602,"sensor_key":"t","sensor_id":55,"component_id":240,"rollup":"1m","function":"avg","from":"2010-01-01T00:00:00Z","to":"2023-10-27T09:38:52Z","sample_size":9,"readings":[["2023-10-27T09:38:20Z",23.92],["2023-10-27T09:37:20Z",24.07],["2023-10-27T09:36:20Z",24.05],["2023-10-27T09:35:20Z",23.98],["2023-10-27T09:34:20Z",24.05],["2023-10-27T09:33:20Z",24.02],["2023-10-27T09:32:20Z",24.08],["2023-10-27T09:31:20Z",24.02],["2023-10-27T09:30:20Z",24.25]]}