Min,Max,Avg Tag Calculation Query

Hey, I am trying to use the system.tag.queryTagCalculation() to pull the minimum, maximum and average of a tag and have ran into some inconsistency.

  1. The Maximum Value is not matching the historical data (Station 9 State/Station 9 Valve)

    Script Implementation:
    Tag Binding
def transform(self, value, quality, timestamp):
	"""
	Transform the incoming value and return a result.

	Arguments:
		self: A reference to the component this binding is configured on.
		value: The incoming value from the binding or the previous transform.
		quality: The quality code of the incoming value.
		timestamp: The timestamp of the incoming value as a java.util.Date
	"""
	tag =  [value["tag"]]
	startDate = ""
	endDate = ""
	timeDiff = 0
	if value["historicalMode"]:
		try:
			endDate = system.date.parse(value["endDate"])
			startDate = system.date.parse(value["startDate"])
			timeDiff =  system.date.secondsBetween(startDate, endDate)
		except:
			endDate = ""
			startDate = ""
	if timeDiff>0 and value["historicalMode"]:
		result = ScriptLib.DashBoards.analytics.getMinMaxAvg(tag, returnSize = 10,interval = 1,intervalUnit = "hr" ,jsonFormat = True, endDate = endDate,startDate=startDate)
		type = 1
	elif timeDiff <= 0 and value["historicalMode"]:
		result =  [{"Minimum":0,"Maximum":0,"Average":0}]
		type = 2
	else:
		result = ScriptLib.DashBoards.analytics.getMinMaxAvg(tag, returnSize = 500,interval = 5,intervalUnit = "min" ,jsonFormat = True)
		type = 3
	return result

Script Library

def getMinMaxAvg(tags, returnSize = 100,interval = 1,intervalUnit = "hr" ,jsonFormat = True, endDate ="",startDate = ""):
	#Set the interval to all lower case
	log = system.util.getLogger("MinMaxAVG")
	info = tags[0]
	intervalUnit = intervalUnit.lower()
	if endDate == "":
		endDate = system.date.now()
	if interval < 1:
		interval = 1
	if startDate == "":
		if intervalUnit in ["hr","h","hours","hour"]:
			startDate = system.date.addHours(endDate,-interval)
		elif intervalUnit in ["sec","s","seconds","second"]:
			startDate = system.date.addSeconds(endDate,-interval)
		elif intervalUnit in ["min","m","minutes","minute"]:
				startDate = system.date.addMinutes(endDate,-interval)
		else:
			startDate = system.date.addDays(endDate,-interval)
	info = info+", Time - StartDate: " + str(startDate) + ", EndDate: "+ str(endDate)
	data = system.tag.queryTagCalculations(tags,["Minimum","Maximum","Average"],startDate,endDate)
	if jsonFormat:
		data = ScriptLib.Framework.utility.datasetToJSON(data)
	info= info +", Minimum: "+str(data[0]["Minimum"])
	info=info +", Maximum: "+str(data[0]["Maximum"])
	log.info(info)
	return data
  1. When start and end dates are equal, perspective fails to load data and freezes the screens, I made a workaround for this issue.

Maybe I need to get the data and apply secondary min max average

Can anyone please help?