We're creating a PIthon program (PI AF SDK using Python) that queries time series data from a Web API and writes to AF Attributes using AFListData.UpdateValues.
The program performs a separate call for each of four measurement points, and the first call takes far longer than the second one.
Below is a heavily redacted code sample and a screenshot of the output.
Code sample:
import requests import os import sys import clr import configparser import time from datetime import datetime, timedelta from System.Collections.Generic import List as ListNet from System import DateTime as datetimeNET start_time = time.time() # afobjsmeasidlookup is a Python dictionary<integer, AFAttribute>. [print(measid, attribute) for measid, attribute in afobjsmeasidlookup.items()] for measid in afobjsmeasidlookup: apiURL = apiURLBase + r"/api/measurements/{}/data?start={}".format(str(measid), dateLookback.strftime("%Y-%m-%d")) apiResponse = requests.get(apiURL, headers = headers, verify=sslVerify) vals = apiResponse.json() milestones.append((round((time.time() - start_time), 1), " API call completed.")) # values, tsnet, stats and uoms will be used in AFValues constructor. values = [meas['avg'] for meas in vals] tsnet = [datetimeNET.Parse(str(meas['timestamp'])) for meas in vals] stats = [AFValueStatus.Good]*len(values) for afatt in afobjsmeasidlookup[measid]: uoms = [afatt.DefaultUOM] * len(values) theseafvals = AFValues(values, tsnet, stats, uoms) theseafvals.Attribute = afatt AFListData.UpdateValues(theseafvals, AFUpdateOption.Replace) milestones.append((round((time.time() - start_time), 1), " {} AFValues written.".format(str(theseafvals.Count)))) [print ("{}: {}".format(str(ts), info)) for (ts, info) in milestones]
Output from line 32:
A look at the afobjsmeasidlookup dictionary:
Is the problem related to the initial call having to first establish a connection to the PI Data Archive or AF server?