5 Replies Latest reply on May 29, 2018 3:51 PM by Roger Palmen

    How to get the 20 last events from an attribute

    Roger Palmen

      Hi All,


      I need to solve this using either Analytics or by a call to the PI Web API. I need to retrieve the data from PI (using the PI Web API) for the last 20 Events of an AF attribute, and of course i don't know the timeframe involved. That could be hours, weeks, months.


      • Using Analytics i can only think of 20 nested PrevEvent functions to find the 20th event back from now, and then retrieve all the events from that 20th event back. The nestig is of course an eyesore.
      • Using PI Web API i could just request the data for e.g. 1 year backward, but limit the result to the last 20 events(using maxCount in this call: Request Rejected ). As i'm not sure how many events this could return, and when the filtering is done, i'm a bit wary that requesting 1 years data for a data-dense point will cause trouble. But on the other hand i also need to support very sparse data that updates only once per month.


      Any ideas?

        • Re: How to get the 20 last events from an attribute
          Roger Palmen

          Digging slightly deeper... on the PI Web API GetRecorded call for the Stream controller, starttime and endtime are optional, and maxCount has a default of 1000. So the option #2 the key question would be how the PI Web API Translates a call to AFSDK calls. Will it use RecordedValuesByCount: Request Rejected ? As that will only retrieve the 20 Events, no matter what time period. So mainly lookingfor some confirmation that it does not do the filtering on the end of the PI Web API, but on the PI Data Archive.

            • Re: How to get the 20 last events from an attribute

              Hi Roger,


              I was looking into this but GetRecorded under the hood calls RecordedValuesAsync on the AF object, but its unclear how the MaxCount parameter will affect the actual RPC that goes out to the Data Archive and how the Data Archive actually handles that RPC. Let me do a bit of digging and get a better answer out to you.



              1 of 1 people found this helpful
              • Re: How to get the 20 last events from an attribute

                Hi Roger,


                As Vince said, Web API is not going to use RecordedValuesByCount. However, you can get RecordedValues[Async] to be functionally equivalent to RecordedValuesByCount. The key is that the AFTimeRange passed to the method needs to be constructed such that start time of the range > end time of the range. For example,


                new AFTimeRange("*","1-Jan-2018")


                When the AFTimeRange is constructed like this, then the navigation direction on the PI Data Archive is backwards, allowing you to retrieve the most recent n events, where n is what is passed as the maxCount argument. On the PI Data Archive, navigation is ended once the maxCount parameter is fulfilled, so you do not need to worry about it continuing further back all the way to the end of your time range.


                So to translate this back to how the Web API call should be made, make sure your startTime > endTime (and that your endTime reasonably encompasses your 20 events), and set MaxReturnedItems/maxCount to 20.



                The optimization of navigation is not as tightly optimized when using a filter, so for best performance you should leave this as null/empty string.

                5 of 5 people found this helpful