Filter Most Recent Records by Time (mins) in Dynamic Data

3140
12
02-25-2019 03:07 PM
by Anonymous User
Not applicable

Has anyone needed to get the most current record by filtering on day/time of a dynamic data set (it is currently a hosted feature service in AGO)?  The data is refreshed about every 10 mins. I am trying to use filtering or Arcade expressions.  Thank you.  Jake Skinner

0 Kudos
12 Replies
KhaledHassen
Esri Contributor

Please check queryTopFeaature API on the feature service layer and it is available on online feature service. It is the same as query API but it will return you the most recent or oldest record/feature(s).

The key to this API is to pass the topFilter param which can specify the orderBy and groupBy fields.

topFilter:

{
"groupByFields" : "your-groupby_field(s)",
"orderByFields" : "sometime_field ASC",
"topCount" : 1 // You can return the most recent 1 or more record
}

Khaled Hassen

Online Feature Service Dev. Lead

by Anonymous User
Not applicable

Hi Khaled, Thanks so much for the response and I was able to get it to return the correct records. How do I use this as a layer in my web map?

Thanks!

Tonya

0 Kudos
KhaledHassen
Esri Contributor

One way to do this is by creating a view layer from this service.

You can then set the topFilter in the view. Once this is done, then you should be able to view the results like any other filter in the layer.

To set the topFilter in the view, you will need to use the layer admin API and pass the followings:

https://servicesurl/ArcGIS/rest/admin/services/view-name/FeatureServer/1/updateDefinition

 {

    "viewLayerDefinition" : {

        "topFilter" : {

          "orderByFields" : "<yourfileldName> ASC",

          "groupByFields" : "your _groupBy_fields",

          "topCount" : 1

        }

    }

}

AndrewHargreaves1
New Contributor III

Hello Tonya Kauhi & Khaled Hassen

I'm struggling to form my queryTopFeatures correctly. I am attempting to query the most recent record for each FacilityID in a table using the below:

https://xxxxx.com/arcgis/rest/services/xxx/xxx/FeatureServer/101/queryTopFeatures?where=&objectIds=&time=&topFilter={"groupByFields": 'FacilityID', "topCount": 1, "orderByFields": 'workdate'}&geometry=&geometryType=esriGeometryEnvelope&inSR=&spatialRel=esriSpatialRelIntersects&distance=&units=esriSRUnit_Foot&outFields=*&returnGeometry=true&maxAllowableOffset=&geometryPrecision=&outSR=&returnIdsOnly=false&returnCountOnly=false&returnExtentOnly=false&returnZ=false&returnM=false&resultType=&f=pjson

I'm unsure if it matters but know that 'workdate' is a date field. Please can I ask for help on how to form this query correctly?

Thanks

0 Kudos
RonParis
Occasional Contributor

Hi Khaled,

Is it possible to use topFilter to group by on geometry? My issue is that have a point feature class which is constantly being updated, and I want to be able display the most recently created features. At the moment, AGOL draws randomly, so it isn't possible to display the most recent.

Many thanks

Josh

0 Kudos
KhaledHassen
Esri Contributor

Hi Josh,

No we do not support group by on geometry. Not sure I understand the issue with the display you have. You should be able to get ht most recent based on your groupby and order by you are using with the topfilter. If this is not happening, we might have an issue.

Khaled

0 Kudos
RonParis
Occasional Contributor

Hi Khaled,

I essentially have a point dataset of assets (fire hydrants) and a separate point dataset (inspections). The inspections sit on top of the asset dataset. Each asset has >1 inspection sat on top of it. The inspections are symbolised on an attribute called "Defect". I have an inspection with a CreationDate August 1st that says the asset is defective (coloured red), and then I have an inspection with a CreationDate of August 2nd that says the defect has been rectified (coloured green). Unfortunately in AGOL, it is drawing the August 1st feature first, as opposed to the August 2nd feature.

Ideally, I want these inspection layers to draw the most recent inspection first (on the screen, the first inspection you see the one most recently created). I do not have any common attributes in either point dataset (e.g an asset ID). The only relating factor is the spatial element I hope this makes sense!

0 Kudos
KhaledHassen
Esri Contributor

If I understand this correctly, it looks like you need an order by in the view definition in addition to the top filter. We are working on this enhancement for Sept. release. Once you have the order by in the view definition, you can order by the creation date DESC which should display the features ordered by the creation date (ASC or DESC).

Is this what are you looking for?

Khaled

RonParis
Occasional Contributor

That sounds like what I'm after. Thanks Khaled

0 Kudos