added monthly reports and sheet name changes
This commit is contained in:
@@ -134,45 +134,55 @@ def lambda_handler(event, context):
|
||||
reportToList = {}
|
||||
# Loop through each item in config, each item represents a report
|
||||
for report in config:
|
||||
reportToList[report["name"]] = report["emails"]
|
||||
for customer in report["customers"].keys():
|
||||
reportDeviceTypes = report["customers"][customer]["deviceTypes"]
|
||||
devices = get_all_customer_devices(token, customer)
|
||||
if report["filterDevicesIn"]:
|
||||
devices = [device for device in devices if device["id"]["id"] in report["filterDevicesIn"]]
|
||||
if report["filterDevicesOut"]:
|
||||
devices = [device for device in devices if device["id"]["id"] not in report["filterDevicesOut"]]
|
||||
if not reportData.get(report["name"], None):
|
||||
reportData[report["name"]] = {}
|
||||
for device in devices:
|
||||
deviceId = device["id"]["id"]
|
||||
deviceType = device["type"]
|
||||
deviceName = device["name"]
|
||||
for reportDeviceType in reportDeviceTypes:
|
||||
if reportDeviceType["deviceType"] == deviceType:
|
||||
keys = get_timeseries_keys(token, deviceId)
|
||||
keys = list(filter(lambda x: x in reportDeviceType["dataPoints"], keys))
|
||||
#Check for report customer
|
||||
if not reportData[report["name"]].get(report["customers"][customer]["name"], None):
|
||||
reportData[report["name"]][report["customers"][customer]["name"]] = {}
|
||||
#Check for device type in config
|
||||
if deviceType in list(map(lambda x: x["deviceType"], reportDeviceTypes)):
|
||||
#Check if deviceType in report
|
||||
if not reportData[report["name"]][report["customers"][customer]["name"]].get(deviceType, None):
|
||||
reportData[report["name"]][report["customers"][customer]["name"]][deviceType] = {}
|
||||
if keys:
|
||||
deviceData = get_latest_telemetry(token, deviceId, keys)
|
||||
for x in reportDeviceTypes:
|
||||
if x["deviceType"] == deviceType:
|
||||
labels = x["labels"]
|
||||
labelled_data = {}
|
||||
for k,v in labels.items():
|
||||
labelled_data[v] = {}
|
||||
for k,v in deviceData.items():
|
||||
labelled_data[labels[k]] = v
|
||||
reportData[report["name"]][report["customers"][customer]["name"]][deviceType][deviceName] = labelled_data
|
||||
else:
|
||||
reportData[report["name"]][report["customers"][customer]["name"]][deviceType][deviceName] = {}
|
||||
runThisReport = True
|
||||
if report["period"] == "Monthly" and dt.now().day != 1:
|
||||
runThisReport = False
|
||||
if not report["emails"]:
|
||||
runThisReport = False
|
||||
if runThisReport:
|
||||
reportToList[report["name"]] = report["emails"]
|
||||
for customer in report["customers"].keys():
|
||||
reportDeviceTypes = report["customers"][customer]["deviceTypes"]
|
||||
devices = get_all_customer_devices(token, customer)
|
||||
if report["filterDevicesIn"]:
|
||||
devices = [device for device in devices if device["id"]["id"] in report["filterDevicesIn"]]
|
||||
if report["filterDevicesOut"]:
|
||||
devices = [device for device in devices if device["id"]["id"] not in report["filterDevicesOut"]]
|
||||
if not reportData.get(report["name"], None):
|
||||
reportData[report["name"]] = {}
|
||||
for device in devices:
|
||||
deviceId = device["id"]["id"]
|
||||
deviceType = device["type"]
|
||||
deviceName = device["name"]
|
||||
sheetName = deviceType
|
||||
for x in reportDeviceTypes:
|
||||
if x["deviceType"] == deviceType:
|
||||
sheetName = x["sheetName"]
|
||||
for reportDeviceType in reportDeviceTypes:
|
||||
if reportDeviceType["deviceType"] == deviceType:
|
||||
keys = get_timeseries_keys(token, deviceId)
|
||||
keys = list(filter(lambda x: x in reportDeviceType["dataPoints"], keys))
|
||||
#Check for report customer
|
||||
if not reportData[report["name"]].get(report["customers"][customer]["name"], None):
|
||||
reportData[report["name"]][report["customers"][customer]["name"]] = {}
|
||||
#Check for device type in config
|
||||
if deviceType in list(map(lambda x: x["deviceType"], reportDeviceTypes)):
|
||||
#Check if deviceType in report
|
||||
if not reportData[report["name"]][report["customers"][customer]["name"]].get(sheetName, None):
|
||||
reportData[report["name"]][report["customers"][customer]["name"]][sheetName] = {}
|
||||
if keys:
|
||||
deviceData = get_latest_telemetry(token, deviceId, keys)
|
||||
for x in reportDeviceTypes:
|
||||
if x["deviceType"] == deviceType:
|
||||
labels = x["labels"]
|
||||
labelled_data = {}
|
||||
for k,v in labels.items():
|
||||
labelled_data[v] = {}
|
||||
for k,v in deviceData.items():
|
||||
labelled_data[labels[k]] = v
|
||||
reportData[report["name"]][report["customers"][customer]["name"]][sheetName][deviceName] = labelled_data
|
||||
else:
|
||||
reportData[report["name"]][report["customers"][customer]["name"]][sheetName][deviceName] = {}
|
||||
#Sort Data
|
||||
reportDataSorted = sort_dict_keys(reportData)
|
||||
#print(json.dumps(reportDataSorted,indent=4))
|
||||
|
||||
Reference in New Issue
Block a user