from datetime import datetime
import re
def getTagPaths(location=None):
	"""
		Get all the relvant tagpaths needed for the historian query
	"""
	baseTagPaths = reports.MBR.common.util.getBaseTagPaths(reports.MBR.common.static.getUPSBase(), location)
	return baseTagPaths
	
def getPowerPaths(location):
	paths = []
	for ups1path, ups2path in getTagPaths(location):
		paths.extend(["%s/Total kW"%ups1path, "%s/Total kW"%ups2path])
	return paths
def getPowerHist(startDate,endDate, location=None):
	tagpaths = getPowerPaths(location)
		
	hist = reports.MBR.common.util.getHistory(tagpaths, startDate, endDate, customArgs={})
	return reports.MBR.common.util.generateSumCol(hist, sumColName="SumTotalkW"), tagpaths
	

def averageHistData(histData, inclColumns, interval=None):
	"""
		Calculate the Average, Min Average, Max Average
		Args:
			data: historian data, expected in minute intervals
			inclColumns: list of strings reprsenting the tagpath/column name from queryTagHistory
			interval: grouping by minutes, None if daily
		Returns:
			dictionary containing calculated data
	"""
	data = system.dataset.toPyDataSet(histData)
	res = {"MinAvg":-1,"MaxAvg":-1,"ResultData":None}
	dailyGroup= {}
	resultData = []
	allAvgs = []
	def getUPSName(inclNames):
		newNames = []
		for name in inclNames:
			newNames.append(name.split("_")[-1].split("/")[0])
		return newNames
	if interval is None:
		for row in data:
	#		dayStr = system.date.format(system.date.fromMillis(row["t_stamp"]),"yyyy-MM-dd")
			dayStr = system.date.format(row["t_stamp"],"yyyy-MM-dd")
			dailyGroup.setdefault(dayStr,{c:[] for c in inclColumns})
			for c in inclColumns:
				dailyGroup[dayStr][c].append(row[c])
				
			
		
		sortedDateKeys = sorted(dailyGroup.keys(), key=lambda x: datetime.strptime(x, '%Y-%m-%d'))
		for date_str in sortedDateKeys:
			oneRow= [system.date.parse(date_str, "yyyy-MM-dd")]
			for c in inclColumns:
				cleanValues = [v for v in dailyGroup[date_str][c] if v is not None] 
				dailyAvg = sum(cleanValues)/len(cleanValues) if len(cleanValues) > 0 else 0
				allAvgs.append(dailyAvg)
				oneRow.append(dailyAvg)
			resultData.append(oneRow)		

	else:		
		quarterHrStr = ""
		for i,row in enumerate(data):
			if i%interval == 0: 
				quarterHrStr = system.date.format(row["t_stamp"],"yyyy-MM-dd HH:mm")
				dailyGroup.setdefault(quarterHrStr,{c:[] for c in inclColumns})
			for c in inclColumns:
				dailyGroup[quarterHrStr][c].append(row[c])
		sortedDateKeys = sorted(dailyGroup.keys(), key=lambda x: datetime.strptime(x, '%Y-%m-%d %H:%M'))
		for dt_str in sortedDateKeys:
			oneRow = [system.date.parse(dt_str, "yyyy-MM-dd HH:mm")]
			for c in inclColumns:
#				print dailyGroup[dt_str]
				cleanValues = [v for v in dailyGroup[dt_str][c] if v is not None] 
				dtAvg = sum(cleanValues)/len(cleanValues) if len(cleanValues) > 0 else 0
				allAvgs.append(dtAvg)
				oneRow.append(dtAvg)
			resultData.append(oneRow)
	
	res["MinAvg"] = min(allAvgs) if len(allAvgs) > 0 and not all(x == 0 for x in allAvgs) else -1
	res["MaxAvg"] = max(allAvgs) if len(allAvgs) > 0 and not all(x == 0 for x in allAvgs) else 1
	res["SimpAvg"] = sum(allAvgs)/len(allAvgs) if len(allAvgs) > 0 else 0
	res["Paths"] = inclColumns

	# we're going to inject two new columns to resultData
	for row in resultData:
		row.extend([res["MinAvg"], res["MaxAvg"]])
		
	res["ResultDS"] = system.dataset.toDataSet(["Date"]+["Unit%s"%(i+1) for i in range(len(inclColumns))]+["Min", "Max"], resultData)
#	res["RawDS"] = data
	return res

def getEqNamesByLoc(location):
	mEqPaths = []
	for ups1path, ups2path in getTagPaths(location):
		mEqPaths.extend(["%s/Meta/EqName"%ups1path, "%s/Meta/EqName"%ups2path])
	return [qv.value for qv in system.tag.readBlocking(mEqPaths)]

def getEqNamesByPath(paths):
	mEqPaths = ["/".join(p.split("/")[:-1]) +"/Meta/EqName" for p in paths]
	return [qv.value for qv in system.tag.readBlocking(mEqPaths)]
	
def processAverage(startDate, endDate, location, interval=None):
	finalRes= {}
	def getUPSName(tagpath):		
		return tagpath.split("_")[-1].split("/")[0][:-2] if "-"in tagpath else 	tagpath.split("_")[-1].split("/")[0][:-1]
	def getRoomName(tagpath):
#		return tagpath.split("/")[1].split("_")[1]
		if "]" in tagpath:
			return tagpath.split("/")[0].split("]")[1]
		else:
			return tagpath.split("/")[0]

	
	histData, rawTagPaths = getPowerHist(startDate,endDate, location)
	header = system.dataset.getColumnHeaders(histData)
	# clone the header for actual tagpaths
	rawTagPaths = [""]+rawTagPaths
#	print header
	allRows = []
	newHeader = ["ChartName","MinAvg","MaxAvg","ChartDS", "SimpAvg"]
	
	res = averageHistData(histData, ["SumTotalkW"], interval)
	allRows.append(["Data Hall Total Loading", res["MinAvg"], res["MaxAvg"], res["ResultDS"], res["SimpAvg"]])
	finalRes["TotalLoading"] = system.dataset.toDataSet(newHeader, allRows)
	
	allRows= []
	
	# Format the result
	for i in range(1, len(header)-2, 2):  # Start at index 1, step by 2 to get pairs
		tag1, tag2 = header[i], header[i+1]
		res = averageHistData(histData, [tag1,tag2], interval)
		eq1,eq2 = reports.MBR.criticalpower.getEqNamesByPath([rawTagPaths[i], rawTagPaths[i+1]])
		allRows.append([getRoomName(tag1)+" Loading", res["MinAvg"], res["MaxAvg"], res["ResultDS"], res["SimpAvg"], getUPSName(tag1), eq1, eq2])	    
	
	finalRes["reportDS"] = system.dataset.toDataSet(newHeader+["ChartCategory", "Eq1", "Eq2"], allRows)
	finalRes["rawDS"] = histData
#	finalRes["NameLists"] = reports.MBR.criticalpower.getEqNames(location)
	
	return finalRes
	
def doDailyReport(startDate, endDate, location=None):
	return processAverage(startDate, endDate, location, None)
	
def doReportInterval(startDate, endDate, location=None, interval=15):
	return processAverage(startDate, endDate, location, interval)