4 Replies Latest reply on May 2, 2016 10:21 AM by gregor

    Is there a way to calculate the number of points between a specified starttime and endtime using  python?

    unagaswamy

      I would like to validate data uploaded to PI server by getting the total counts of data points in a specified duration.  Is there a way to get this count using pi web api using python?

       

      I am looking for something like

       

      SELECT COUNT(*) FROM table_name
      WHERE date between "2015-01-01" and "2015-12-31"
      
        • Re: Is there a way to calculate the number of points between a specified starttime and endtime using  python?
          gregor

          Hello Uma,

           

          Please see this other discussion on how to query Summaries through PI Web API.

          For a list of available Summary Types, please see the corresponding chapter in PI Web API Help.

           

          I believe you will remember Barry's blog about Using PI Web API with Python

          • Re: Is there a way to calculate the number of points between a specified starttime and endtime using  python?
            mtippett

            Hi Uma,

             

            The resources that Gregor pointed out, based on that I wrote the following short script. Note that in my script I am using basic authentication (for simplicity) and I am not verifying the SSL certificate (as mine is a self-signed one), so this is not best practice!  Still it should give you an idea about how to go about this.

             

            """
            A little script which connects to a PI server and returns the number of events in the last hour
            """
            
            
            import getpass # for password entry
            import requests # for sending http(s) requests
            
            
            # The next two lines disable warnings about not verifying SSL certificates
            from requests.packages.urllib3.exceptions import InsecureRequestWarning
            requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
            
            
            def getPIPoint(webapiServer, dataArchiveServer,tagname,username,password,ver=False):
              """Returns a dictionary with PI point information given the PI point name
              """
              response = requests.get('https://' + webapiServer + '/piwebapi/points?path=' + dataArchiveServer + '\\' + tagname,verify = ver, auth = requests.auth.HTTPBasicAuth(username,password))
            
            
              responseJson = response.json()
              return responseJson
            
            def getSummary(PIPoint,username,password,starttime = '*-1h',endtime = '*',summary = 'Count',calcbasis = 'EventWeighted',ver=False):
              """Given a PI point this returns the summary data
              """
              URL = PIPoint['Links']['SummaryData']
              parameters = '?starttime=' + starttime + '&endtime=' + endtime + '&summaryType=' + summary + '&CalculationBasis=' + calcbasis
            
            
              response = requests.get(URL + parameters,verify = ver, auth = requests.auth.HTTPBasicAuth(username,password))
            
            
              responseJson = response.json()
              return responseJson
            
            
            if __name__ ==  "__main__":
            
              webapiServer = raw_input('\nEnter your PI webAPI server: ')
              dataArchive = raw_input('\nEnter your PI Data Archive: ')
              username = raw_input('\nEnter the domain\username to connect to the PI Data Archive: ')
              password = getpass.getpass('\nEnter your password: ')
            
            
              while True:
            
                   tagname = raw_input('\nEnter a tag: ')
            
                   try:
                        tag = getPIPoint(webapiServer, dataArchive,tagname,username,password)
                   except requests.exceptions.RequestException as e:
                        print('Error requesting data from PI webAPI server')
                        print('\n{}').format(e)
                   else:
                        if(tag.get('Errors',False)):
                             print("{}").format(tag['Errors'][0])
                             continue
            
            
                   tagSummary = getSummary(tag,username,password)
            
            
                   print('A total of {} events').format(int(tagSummary['Items'][0]['Value']['Value']))
            
            
            
            
            2 of 2 people found this helpful