Skip navigation
All People > Jerome Lefebvre > Jerome Lefebvre のブログ > 2019 > August
Jerome Lefebvre

Looping over arrays

Posted by Jerome Lefebvre Aug 26, 2019

Looping arrays

the technique

AF Analysis has supported arrays for a while, but doesn't support looping them with a typical for loop statement. A lot of use cases for loops are converted by the map, filter and aggregate methods such as sum.

In this post, I want to explore an other way to loop over arrays.

The idea is quite simple, say you have an array of 5 element and would like to loop over it over one minute. We can schedule an analysis to run every 12 seconds (60/5 = 12). We can then use the number of seconds method to pick an element of an array.

Here is the AF structure, in which I have my array: Array

Here is the analysis that picks a different element of the array each time it runs:


The analysis will have to be modified if you want to loop over something different than 1 minute.

Building up an XY-plot

Here is an example of using such technique. I was recently asked how to monitor pressure across a pipeline. The pipeline had manholes every few kilometers, at each manholes inlet or outlet (sometime both) pressure were collected. In between manholes, we assume the pressure just drops linearly. Visualizing this in PI Vision can be a bit tricky as the x-axis is not time, but distance along the pipeline. An X=Y plot is much closer, but the raw data is made up of 1-2 tag per manhole. Thus, there is the need to combine the data before displaying it.


The sensors array is just a list of tag names. AF2

Now, I built up an analysis similar to the one above, but looping over both arrays.


Now that with this done we can display everything in PI Vision: XY


We are forced to pick a timestamp for this display, I couldn't find a good choice for this graph, thus I picked midnight of each day. Thus, the red line will show the latest values for the particular display. If somebody can think of a better output time, please let me know!

I am just sharing an experiment, this is in no way is the following usage of PI Vision a supported usage of PI vision. The comments are also disabled as indication that you are on your own if you want to try this. This uses internal APIs for PI Vision to create displays, creating displays in such a way may lead to corruptions of your PI Vision displays. Do not try for reason other than fun and exploration.



PI Vision Event Frame overlay displays are very useful, to look into the details of a particular event and how they differ from other events. One of the feature that they lack is support for URL parameters, it is not possible to simply update your URL and view different set of event frames. The way this is dealt with is by creating many different event frame overlay pages, all with different queries.


This has been requested here:


In this experiment, I play around with the creation of such displays by using the internal requests made by the browser to the PI Vision service.This is done via Chrome, Postman and PowerShell.


The web browser, Chrome, offers a lot of tools to debug websites. We can use them to explore the iteration between the browser and the PI Vision server. Here, we want to look at the network logs, to see the exactly happens when we hit save on an overlay page. Hitting F12, we do have a very promising requests (screenshot directly below).



Chrome is very good at looking at the details of the requests that have already been made. But, to make further requests I prefer to use a tool such as Postman. Let's export this request and import it back in Postman.


Export the request:


Import the request:




In postman, you now free to play around with the requests, see what happens when you change a value.

In particular, you can make new displays by making new requests.


If for example, the creation went well, you receive the ID for the new displays.


You can try to tweak the request until you create event frame overlay displays with exactly the search parameters you want.


Postman also has the ability to create code snippets in various language. This means, it will turn your snippet into a working Powershell, python, etc. requests, 


In my case I picked PowerShell.




In here, there is nothing really to explain. I want to create displays, and postman give me all the code I needed to make that request. From PowerShell, all I need is the for loop construct.