Skip to content
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

How to query last Observations for Thing in single GET? #145

Open
justb4 opened this issue Feb 7, 2018 · 10 comments
Open

How to query last Observations for Thing in single GET? #145

justb4 opened this issue Feb 7, 2018 · 10 comments

Comments

@justb4
Copy link

justb4 commented Feb 7, 2018

Did not find a public STA mailing list, so posing the question here: need to get the last Observations for a single Thing (or better from a series of Things from a properties.project query). Question was posed on our SE issue tracker:
smartemission/smartemission#79

Started a SE STA Cookbook where I will add any recipes.

I know this can be solved in multiple steps:

  • GET list of Things with $expanded Datastreams
  • for each Datastream GET the last Observation (think $top=1)

But I am seeking to do the 2nd step in one GET. Scenario: typically a web viewer that shows a map with AQ Stations, clicking on a Station will show all last station's last sensor (Observation) values. Example: http://data.smartemission.nl/smartapp/.

@bertt
Copy link
Contributor

bertt commented Feb 7, 2018

@tebben
Copy link
Contributor

tebben commented Feb 7, 2018

Getting only the last observation for a thing is not possible, you can however request the related datastreams and the latest observation for the datastream.

Example GET a list of Things based on a property with expanded datastreams and last observation:

https://gost.geodan.nl/v1.0/Things?$expand=Datastreams($select=id),Datastreams/Observations($select=id,phenomenonTime,result;$top=1)&$filter=properties/owner eq 'TimE'&$select=id,name,description

You can findout more about SensorThings and ODATA here: https://gost1.docs.apiary.io/#reference/odata

@justb4
Copy link
Author

justb4 commented Feb 7, 2018

Thanks for the quick responses! I've adapted the SE STA Cookbook after experimenting with various GET options.

Example 1

Get the last Observation for each Datastream from a Thing queried by device_id in a single GET ( @bertt suggestion): http://data.smartemission.nl/gost/v1.0/Things?$filter=name eq ‘20010001’&$expand=Datastreams/Observations($top=1))

Example 2

Using $select, to receive less data attributes. Here query for Device id 20010001 last Observations showing only id and name of each Datastream (@tebben suggestion):

http://data.smartemission.nl/gost/v1.0/Things?$filter=name eq ‘20010001’&$select=id,name&$expand=Datastreams($select=id,name),Datastreams/Observations($top=1)

Somehow the part Observations($select=id,phenomenonTime,result;$top=1) did not work (complains about parenthesis mismatch). Using Docker geodan/gost:0.5.

@tebben
Copy link
Contributor

tebben commented Feb 7, 2018

It's possible that some queries do not work on version 0.5, latest version has better query support.

@justb4
Copy link
Author

justb4 commented Feb 7, 2018

Ok, I see e.g. #129 fixed separated inline options.

One last Thing: need to find a query to get all Observations related to single Thing for time interval in particular last 48 hours. Tried ISO 8601 intervals without success.

This (phenomenonTime with gt, lt) works on all Observations:

http://data.smartemission.nl/gost/v1.0/Observations?$filter=phenomenonTime gt ‘2018-01-29T11:00:00.000Z’ and phenomenonTime le ‘2018-01-29T12:00:00.000Z’&$select=result,phenomenonTime,parameters

but how to realize for single Thing?

@tebben
Copy link
Contributor

tebben commented Feb 8, 2018

Should be something like

http://data.smartemission.nl/gost/v1.0/Things(1)?$expand=Datastreams/Observations($filter=date(phenomenonTime) le date(now()) and date(phenomenonTime) ge date('2018-01-01');$select=result,phenomenonTime,parameters)&$select=id,name 

however, I found a problem with parsing of string/date/time on inline queries, I'm looking into it now

@justb4
Copy link
Author

justb4 commented Feb 8, 2018

Yes, I tried similar $filter constructs and found date/time parsing issues as well, but was using ISO 8601 strings directly, not using the date() function like date('2018-01-01') . At some point I was not sure if $filter could even be used inside Entity() brackets. Remember we are using 0.5.

@tebben
Copy link
Contributor

tebben commented Feb 9, 2018

Using ISO 8601 strings should also be fine I just used date() as an example, The odata code is now updated to get the mentioned example working, strings weren't parsed properly inside $expand queries. There is still some work to do to get all queries working inside the $expand operation, an issue is created for this: #147

@justb4
Copy link
Author

justb4 commented Feb 9, 2018

Tried gost:latest Docker image (on 09.02.2018, 16:00). Can confirm that multiple inline options work like ;$top=1 and that it is possible to get all Things with their Location and last Observation for each Datastream:

http://data.smartemission.nl/gost/v1.0/Things?$expand=Locations($select=location),Datastreams($select=id,name),Datastreams/Observations($select=id,phenomenonTime,result;$top=1)&$select=id,name,properties

Only this returns 13 Things out of 184 with no nextLink. This possibly related to issue #146.

Also in theory getting the last Observations for a time period works, but with same nextLink issue, getting one Thing, possibly not all Observations, but on the other hand I can imagine that keeping a nextLink here would be quite complicated, maybe even a flaw in the standard..:

http://data.smartemission.nl/gost/v1.0/Things?$expand=Locations($select=location),Datastreams($select=id,name),Datastreams/Observations($select=phenomenonTime,result;$filter=date(phenomenonTime) le date(now()) and date(phenomenonTime) ge date('2018-02-08');$select=result,phenomenonTime,parameters)&$select=id,name,properties

@tebben
Copy link
Contributor

tebben commented Feb 28, 2018

Related to issue #146, should be fixed. We are aware of possible complications around not having a nextlink on expanded entity arrays, the STA standard does not specify this and is indeed complicated to implement, however we are planning to implement this and making this an optional feature in the feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants