8 Replies Latest reply on Nov 24, 2015 8:14 PM by I.Berry

    Queries with PI WebAPI?

    I.Berry

      Hi,

       

      I am having some trouble imagining how some common application queries could be implemented using the PI WebAPI. These types of queries are possible with OLEDB or even the discontinued OData interface. But it seems to be difficult to do them with the PI WebAPI. One example is "get the values for a specific attribute(s) from all elements of a certain template".

       

      e.g. The current value of the efficiency attribute on all elements of type "Pump"

       

      I can see that this could be done as follows

      1)

      Query with the Search interface to find all Pumps

      https://host/piwebapi/search/query?q=afelementtemplate:Pump

       

      Or query with the WebAPI to find all pumps:

      https://host/piwebapi/assetdatabases/<webID>/elements?templateName=Pump&searchFullHierarchy=true

       

      2)

      For each of the pumps, need to find the WebID of each of the efficiency attribute

       

      This is a HTTP GET for each pump

      https://HOST/piwebapi/elements/<webID>/attributes?nameFilter=Efficiency

       

      3)

      Build a URL using the WebIDs from  that will return all the current values for each efficiency attribute with one request (I believe this is possible in PI WebAPI 2015R2)

       

       

      My question is: Is this the best way to do it with PI WebAPI, or is there a more efficient way?

       

      Many Thanks!

      Ivan

        • Re: Queries with PI WebAPI?
          bshang

          Hi Ivan,

           

          There is probably a way to do this with two server calls.

           

          1) Use the Search interface to find all Pumps

          https://host/piwebapi/search/query?q=afelementtemplate:Pump&fields=attributes

           

          I added fields=attributes to your query above so the attribute webIds are returned.

           

          2) Parse out the webIds client-side and pass them into GetRecordedAdHoc (or similar) which accepts multiple instances of webIds.

           

          Regarding 1), we have an enhancement to expose AF SDK's FindElementAttributes so you can use a non-Indexed Search query to obtain the webIds of desired attributes, instead of going through them element by element.

           

          What is the client programming language you are using? Would you find client wrapper libraries for PI Web API helpful? These wrappers would try to abstract out details of the URL scheme, query format, serialization/deserilization, etc. so application can be concerned with business logic rather than HTTP request/response logic.

          2 of 2 people found this helpful
            • Re: Queries with PI WebAPI?
              I.Berry

              Thanks Barry!

               

              This is promising. Two requests is not bad.

               

              when I add these paremters to the URL &fields=name;webid;attributes

               

              I find that I don't get the WebIDs for the attributes back, only those of the elements. Do you get the webids for attributes also?

               

              This is the type of result I get:

               

              "Name": "Pump101",

                    "Attributes": [

                      {

                        "Name": "Efficiency",

                        "Description": "",

                        "DataType": "System.Double",

                        "UoM": "",

                        "Value": "PI Point",

                        "Categories": []

                      }

              ],

                "WebID": "E0UhOqOjJsd0qH16rQ6ijcegAhbfTRWE5RGB9gBQVgAMowQU1TREMxLVMtNDIwNDJcU0hFTExcU0hFTExEU0xcVUlcT1BFUkFURURcSVJBUVxNQUpOT09OXE1BSk5PT05cRFMtMlxEUy0yX1dFTExfSE9PS1VQX1NPVVJcTUotMDhcQU5OVUxVU0JQUkVTU1VSRQ",

                "Score": 3.52727962

               

              There is no WebID for the Efficiency attribute, so I will need to do another request to get the webid for the attribute.

               

              In this case I am using javascript, but I am also interested in doing things in one request so that we can use the PI WebAPI as a datasource for tools like PowerBI/Excel 2013. I am not sure if client libraries are required, they might be useful, but I didn't have too much trouble getting things working with Kendo UI. I think more examples in the online help would be helpful.

               

              The planned enhancement sounds great!

               

              Regards,

              Ivan