From ca4fdc087289eee94bda4d28f1b64a0149f01b5c Mon Sep 17 00:00:00 2001 From: Caozhenhui <823914102@qq.com> Date: Tue, 23 Mar 2021 17:19:03 +0800 Subject: [PATCH 1/3] 2021/3/23 #86 --- .../combinedequipmentenergyitem.py | 58 +++++++++++++- .../reports/combinedequipmentenergyitem.py | 80 ++++++++++++++++--- 2 files changed, 127 insertions(+), 11 deletions(-) diff --git a/myems-api/excelexporters/combinedequipmentenergyitem.py b/myems-api/excelexporters/combinedequipmentenergyitem.py index ece85f12..690dd4d7 100644 --- a/myems-api/excelexporters/combinedequipmentenergyitem.py +++ b/myems-api/excelexporters/combinedequipmentenergyitem.py @@ -4,7 +4,6 @@ import os from openpyxl.chart import ( PieChart, LineChart, - BarChart, Reference, ) from openpyxl.styles import PatternFill, Border, Side, Alignment, Font @@ -432,6 +431,63 @@ def generate_excel(report, chart_start_row_number += 6 ws.add_chart(line, chart_cell) + ##################################### + + has_associated_equipment_flag = True + + if "associated_equipment" not in report.keys() or \ + "energy_item_names" not in report['associated_equipment'].keys() or \ + len(report['associated_equipment']["energy_item_names"]) == 0 \ + or 'associated_equipment_names_array' not in report['associated_equipment'].keys() \ + or report['associated_equipment']['associated_equipment_names_array'] is None \ + or len(report['associated_equipment']['associated_equipment_names_array']) == 0 \ + or len(report['associated_equipment']['associated_equipment_names_array'][0]) == 0: + has_associated_equipment_flag = False + + if has_associated_equipment_flag: + associated_equipment = report['associated_equipment'] + + ws['B' + str(current_row_number)].font = title_font + ws['B' + str(current_row_number)] = name + ' 相关设备数据' + + current_row_number += 1 + table_start_row_number = current_row_number + + ws.row_dimensions[current_row_number].height = 60 + ws['B' + str(current_row_number)].fill = table_fill + ws['B' + str(current_row_number)].font = name_font + ws['B' + str(current_row_number)].alignment = c_c_alignment + ws['B' + str(current_row_number)].border = f_border + ws['B' + str(current_row_number)] = '相关设备' + ca_len = len(associated_equipment['energy_item_names']) + + for i in range(0, ca_len): + row = chr(ord('C') + i) + ws[row + str(current_row_number)].fill = table_fill + ws[row + str(current_row_number)].font = name_font + ws[row + str(current_row_number)].alignment = c_c_alignment + ws[row + str(current_row_number)].border = f_border + ws[row + str(current_row_number)] = \ + reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" + + associated_equipment_len = len(associated_equipment['associated_equipment_names_array'][0]) + + for i in range(0, associated_equipment_len): + current_row_number += 1 + row = str(current_row_number) + + ws['B' + row].font = title_font + ws['B' + row].alignment = c_c_alignment + ws['B' + row] = associated_equipment['associated_equipment_names_array'][0][i] + ws['B' + row].border = f_border + + for j in range(0, ca_len): + col = chr(ord('C') + j) + ws[col + row].font = name_font + ws[col + row].alignment = c_c_alignment + ws[col + row] = round(associated_equipment['subtotals_array'][j][i], 2) + ws[col + row].border = f_border + filename = str(uuid.uuid4()) + '.xlsx' wb.save(filename) diff --git a/myems-api/reports/combinedequipmentenergyitem.py b/myems-api/reports/combinedequipmentenergyitem.py index ced3add1..7b1e8541 100644 --- a/myems-api/reports/combinedequipmentenergyitem.py +++ b/myems-api/reports/combinedequipmentenergyitem.py @@ -23,11 +23,13 @@ class Reporting: # Step 2: query the combined equipment # Step 3: query energy items # Step 4: query associated points - # Step 5: query base period energy input - # Step 6: query reporting period energy input - # Step 7: query tariff data - # Step 8: query associated points data - # Step 9: construct the report + # Step 5: query associated equipments + # Step 6: query base period energy input + # Step 7: query reporting period energy input + # Step 8: query tariff data + # Step 9: query associated points data + # Step 10: query associated equipments energy input + # Step 11: construct the report #################################################################################################################### @staticmethod def on_get(req, resp): @@ -239,7 +241,21 @@ class Reporting: point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]}) ################################################################################################################ - # Step 5: query base period energy input + # Step 5: query associated equipments + ################################################################################################################ + associated_equipment_list = list() + cursor_system.execute(" SELECT e.id, e.name " + " FROM tbl_equipments e,tbl_combined_equipments_equipments ee" + " WHERE ee.combined_equipment_id = %s AND e.id = ee.equipment_id" + " ORDER BY id ", (combined_equipment['id'],)) + rows_associated_equipments = cursor_system.fetchall() + if rows_associated_equipments is not None and len(rows_associated_equipments) > 0: + for row in rows_associated_equipments: + associated_equipment_list.append({"id": row[0], "name": row[1]}) + + print(associated_equipment_list) + ################################################################################################################ + # Step 6: query base period energy input ################################################################################################################ base = dict() if energy_item_set is not None and len(energy_item_set) > 0: @@ -286,7 +302,7 @@ class Reporting: base[energy_item_id]['subtotal'] += actual_value ################################################################################################################ - # Step 6: query reporting period energy input + # Step 7: query reporting period energy input ################################################################################################################ reporting = dict() if energy_item_set is not None and len(energy_item_set) > 0: @@ -353,7 +369,7 @@ class Reporting: reporting[energy_item_id]['offpeak'] += row[1] ################################################################################################################ - # Step 7: query tariff data + # Step 8: query tariff data ################################################################################################################ parameters_data = dict() parameters_data['names'] = list() @@ -379,7 +395,7 @@ class Reporting: parameters_data['values'].append(tariff_value_list) ################################################################################################################ - # Step 8: query associated points data + # Step 9: query associated points data ################################################################################################################ for point in point_list: point_values = [] @@ -444,7 +460,37 @@ class Reporting: parameters_data['values'].append(point_values) ################################################################################################################ - # Step 9: construct the report + # Step 10: query associated equipments energy input + ################################################################################################################ + associated_equipment_data = dict() + + if energy_item_set is not None and len(energy_item_set) > 0: + for energy_item_id in energy_item_set: + associated_equipment_data[energy_item_id] = dict() + associated_equipment_data[energy_item_id]['associated_equipment_names'] = list() + associated_equipment_data[energy_item_id]['subtotals'] = list() + for associated_equipment in associated_equipment_list: + associated_equipment_data[energy_item_id]['associated_equipment_names'].append( + associated_equipment['name']) + + cursor_energy.execute(" SELECT SUM(actual_value) " + " FROM tbl_combined_equipment_input_item_hourly " + " WHERE combined_equipment_id = %s " + " AND energy_item_id = %s " + " AND start_datetime_utc >= %s " + " AND start_datetime_utc < %s " + " ORDER BY start_datetime_utc ", + (associated_equipment['id'], + energy_item_id, + reporting_start_datetime_utc, + reporting_end_datetime_utc)) + row_subtotal = cursor_energy.fetchone() + + subtotal = Decimal(0.0) if (row_subtotal is None or row_subtotal[0] is None) else row_subtotal[0] + associated_equipment_data[energy_item_id]['subtotals'].append(subtotal) + + ################################################################################################################ + # Step 11: construct the report ################################################################################################################ if cursor_system: cursor_system.close() @@ -522,6 +568,20 @@ class Reporting: "values": parameters_data['values'] } + result['associated_equipment'] = dict() + result['associated_equipment']['energy_item_names'] = list() + result['associated_equipment']['units'] = list() + result['associated_equipment']['associated_equipment_names_array'] = list() + result['associated_equipment']['subtotals_array'] = list() + if energy_item_set is not None and len(energy_item_set) > 0: + for energy_item_id in energy_item_set: + result['associated_equipment']['energy_item_names'].append(energy_item_dict[energy_item_id]['name']) + result['associated_equipment']['units'].append(energy_item_dict[energy_item_id]['unit_of_measure']) + result['associated_equipment']['associated_equipment_names_array'].append( + associated_equipment_data[energy_item_id]['associated_equipment_names']) + result['associated_equipment']['subtotals_array'].append( + associated_equipment_data[energy_item_id]['subtotals']) + # export result to Excel file and then encode the file to base64 string result['excel_bytes_base64'] = excelexporters.combinedequipmentenergyitem.export(result, combined_equipment['name'], From 3ccce287ee8a59dcea933ad65bcecc604157d450 Mon Sep 17 00:00:00 2001 From: Caozhenhui <823914102@qq.com> Date: Thu, 25 Mar 2021 17:10:37 +0800 Subject: [PATCH 2/3] added associated equipment data to CombinedEquipmentEnergyItem report & excel reporter --- myems-api/reports/combinedequipmentenergyitem.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/myems-api/reports/combinedequipmentenergyitem.py b/myems-api/reports/combinedequipmentenergyitem.py index 7b1e8541..06053f2e 100644 --- a/myems-api/reports/combinedequipmentenergyitem.py +++ b/myems-api/reports/combinedequipmentenergyitem.py @@ -474,8 +474,8 @@ class Reporting: associated_equipment['name']) cursor_energy.execute(" SELECT SUM(actual_value) " - " FROM tbl_combined_equipment_input_item_hourly " - " WHERE combined_equipment_id = %s " + " FROM tbl_equipment_input_item_hourly " + " WHERE equipment_id = %s " " AND energy_item_id = %s " " AND start_datetime_utc >= %s " " AND start_datetime_utc < %s " From 1296e69231178c27f247e6e22e8eba867f373d15 Mon Sep 17 00:00:00 2001 From: "13621160019@163.com" <13621160019@163.com> Date: Fri, 26 Mar 2021 10:31:05 +0800 Subject: [PATCH 3/3] fixed typo in combinedequipmentenergyitem report in API --- myems-api/reports/combinedequipmentenergyitem.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/myems-api/reports/combinedequipmentenergyitem.py b/myems-api/reports/combinedequipmentenergyitem.py index 06053f2e..710376be 100644 --- a/myems-api/reports/combinedequipmentenergyitem.py +++ b/myems-api/reports/combinedequipmentenergyitem.py @@ -247,7 +247,7 @@ class Reporting: cursor_system.execute(" SELECT e.id, e.name " " FROM tbl_equipments e,tbl_combined_equipments_equipments ee" " WHERE ee.combined_equipment_id = %s AND e.id = ee.equipment_id" - " ORDER BY id ", (combined_equipment['id'],)) + " ORDER BY e.id ", (combined_equipment['id'],)) rows_associated_equipments = cursor_system.fetchall() if rows_associated_equipments is not None and len(rows_associated_equipments) > 0: for row in rows_associated_equipments: