added monthly reports and sheet name changes
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
"deviceTypes": [
|
||||
{
|
||||
"deviceType": "rigpump",
|
||||
"sheetName": "Rig Pump",
|
||||
"dataPoints": [
|
||||
"vfd_current",
|
||||
"vfd_frequency"
|
||||
@@ -20,6 +21,7 @@
|
||||
},
|
||||
{
|
||||
"deviceType": "cqwatertanks",
|
||||
"sheetName": "Water Tanks",
|
||||
"dataPoints": [
|
||||
"fm_1_flow_rate",
|
||||
"tank_1_level",
|
||||
@@ -33,6 +35,7 @@
|
||||
},
|
||||
{
|
||||
"deviceType": "piflow",
|
||||
"sheetName": "Water Wells",
|
||||
"dataPoints": [
|
||||
"avgFrequency30Days",
|
||||
"percentRunTime30Days",
|
||||
@@ -48,6 +51,7 @@
|
||||
},
|
||||
{
|
||||
"deviceType": "advvfdipp",
|
||||
"sheetName": "Adv VFD IPP",
|
||||
"dataPoints": [
|
||||
"flowtotalyesterday",
|
||||
"fluidlevel",
|
||||
@@ -68,32 +72,37 @@
|
||||
},
|
||||
"filterDevicesIn": [],
|
||||
"filterDevicesOut": [],
|
||||
"name": "OxyRock-Daily-Report"
|
||||
"name": "OxyRock-Daily-Report",
|
||||
"period": "Daily"
|
||||
},
|
||||
{
|
||||
"emails": [
|
||||
|
||||
"chris@rrigenergy.com",
|
||||
"corey@rrigenergy.com",
|
||||
"parker@rrigenergy.com",
|
||||
"zack@rrigenergy.com",
|
||||
"claire@rrigenergy.com"
|
||||
],
|
||||
"customers": {
|
||||
"81083430-6988-11ec-a919-556e8dbef35c": {
|
||||
"name": "Henry-Petroleum",
|
||||
"4d4058a0-b25d-11ef-861c-8dbe77c636e1": {
|
||||
"name": "RRig-Energy",
|
||||
"deviceTypes": [
|
||||
{
|
||||
"deviceType": "abbflow",
|
||||
"deviceType": "rr_well",
|
||||
"sheetName": "Water Wells",
|
||||
"dataPoints": [
|
||||
"accumulated_volume"
|
||||
"last_month_total"
|
||||
],
|
||||
"labels":{
|
||||
"accumulated_volume": "Accumulated Volume"
|
||||
"last_month_total": "Month Volume"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"filterDevicesIn": [
|
||||
"0090dbd0-abb0-11ec-90c2-ad8278896f52"
|
||||
],
|
||||
"filterDevicesIn": [],
|
||||
"filterDevicesOut": [],
|
||||
"name": "Henry-Petroleum-Daily-Report"
|
||||
"name": "RRig-Energy-Water-Production-Monthly",
|
||||
"period": "Monthly"
|
||||
}
|
||||
]
|
||||
@@ -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