Skip to content

Commit

Permalink
added meter carbon dioxide emission report
Browse files Browse the repository at this point in the history
  • Loading branch information
guangyuzhang committed Jan 24, 2022
1 parent 6e93bbd commit 24a0677
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 32 deletions.
7 changes: 7 additions & 0 deletions myems-api/example.env
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ MYEMS_REPORTING_DB_DATABASE=myems_reporting_db
MYEMS_REPORTING_DB_USER=root
MYEMS_REPORTING_DB_PASSWORD=!MyEMS1

# config for myems_carbon_db
MYEMS_CARBON_DB_HOST=127.0.0.1
MYEMS_CARBON_DB_PORT=3306
MYEMS_CARBON_DB_DATABASE=myems_carbon_db
MYEMS_CARBON_DB_USER=root
MYEMS_CARBON_DB_PASSWORD=!MyEMS1

# indicated in how many minutes to calculate meter energy consumption
# 30 for half hourly period
# 60 for hourly period
Expand Down
20 changes: 10 additions & 10 deletions myems-api/excelexporters/metercarbon.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def generate_excel(report, name, reporting_start_datetime_local, reporting_end_d

# todo
ws = wb.active
ws.title = "MeterCarbonEmission"
ws.title = "MeterCarbon"
# Row height
ws.row_dimensions[1].height = 102
for i in range(2, 2000 + 1):
Expand Down Expand Up @@ -147,14 +147,14 @@ def generate_excel(report, name, reporting_start_datetime_local, reporting_end_d

####################################################################################################################

has_cost_data_flag = True
has_carbon_data_flag = True

if "values" not in report['reporting_period'].keys() or len(report['reporting_period']['values']) == 0:
has_cost_data_flag = False
has_carbon_data_flag = False

if has_cost_data_flag:
if has_carbon_data_flag:
ws['B6'].font = title_font
ws['B6'] = name + 'Reporting Period Carbon Emission'
ws['B6'] = name + 'Reporting Period Carbon Dioxide Emissions'

reporting_period_data = report['reporting_period']
category = report['meter']['energy_category_name']
Expand All @@ -166,7 +166,7 @@ def generate_excel(report, name, reporting_start_datetime_local, reporting_end_d

ws['B8'].font = title_font
ws['B8'].alignment = c_c_alignment
ws['B8'] = 'Carbon Emission'
ws['B8'] = 'Carbon Dioxide Emission'
ws['B8'].border = f_border

ws['B9'].font = title_font
Expand Down Expand Up @@ -242,7 +242,7 @@ def generate_excel(report, name, reporting_start_datetime_local, reporting_end_d

####################################################################################################################

has_cost_datail_flag = True
has_carbon_detail_flag = True
reporting_period_data = report['reporting_period']
category = report['meter']['energy_category_name']
ca_len = len(category)
Expand All @@ -256,9 +256,9 @@ def generate_excel(report, name, reporting_start_datetime_local, reporting_end_d
parameters_parameters_datas_len += 1

if "values" not in reporting_period_data.keys() or len(reporting_period_data['values']) == 0:
has_cost_datail_flag = False
has_carbon_detail_flag = False

if has_cost_datail_flag:
if has_carbon_detail_flag:
start_detail_data_row_number = 13 + (parameters_parameters_datas_len + ca_len) * 6

ws['B11'].font = title_font
Expand Down Expand Up @@ -324,7 +324,7 @@ def generate_excel(report, name, reporting_start_datetime_local, reporting_end_d
ws[col + str(end_data_row_number + 1)].border = f_border

line = LineChart()
line.title = 'Reporting Period Costs - ' + report['meter']['energy_category_name'] + \
line.title = 'Reporting Period Carbon Dioxide Emissions - ' + report['meter']['energy_category_name'] + \
" (" + report['meter']['unit_of_measure'] + ")"
line_data = Reference(ws, min_col=3, min_row=start_detail_data_row_number, max_row=max_row)
line.series.append(Series(line_data, title_from_data=True))
Expand Down
6 changes: 3 additions & 3 deletions myems-api/excelexporters/metercost.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def generate_excel(report, name, reporting_start_datetime_local, reporting_end_d

####################################################################################################################

has_cost_datail_flag = True
has_cost_detail_flag = True
reporting_period_data = report['reporting_period']
category = report['meter']['energy_category_name']
ca_len = len(category)
Expand All @@ -256,9 +256,9 @@ def generate_excel(report, name, reporting_start_datetime_local, reporting_end_d
parameters_parameters_datas_len += 1

if "values" not in reporting_period_data.keys() or len(reporting_period_data['values']) == 0:
has_cost_datail_flag = False
has_cost_detail_flag = False

if has_cost_datail_flag:
if has_cost_detail_flag:
start_detail_data_row_number = 13 + (parameters_parameters_datas_len + ca_len) * 6

ws['B11'].font = title_font
Expand Down
6 changes: 3 additions & 3 deletions myems-api/excelexporters/offlinemetercost.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,16 +241,16 @@ def generate_excel(report, name, reporting_start_datetime_local, reporting_end_d

####################################################################################################################

has_cost_datail_flag = True
has_cost_detail_flag = True
reporting_period_data = report['reporting_period']
category = report['offline_meter']['energy_category_name']
ca_len = len(category)
times = reporting_period_data['timestamps']

if "values" not in reporting_period_data.keys() or len(reporting_period_data['values']) == 0:
has_cost_datail_flag = False
has_cost_detail_flag = False

if has_cost_datail_flag:
if has_cost_detail_flag:
ws['B11'].font = title_font
ws['B11'] = name + 'Detailed Data'

Expand Down
48 changes: 32 additions & 16 deletions myems-api/reports/metercarbon.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ def on_options(req, resp):
####################################################################################################################
# PROCEDURES
# Step 1: valid parameters
# Step 2: query the meter and carbon
# Step 2: query the meter and energy category
# Step 3: query associated points
# Step 4: query base period energy consumption
# Step 5: query base period carbon Emission
# Step 5: query base period carbon dioxide emission
# Step 6: query reporting period energy consumption
# Step 7: query reporting period carbon emission
# Step 7: query reporting period carbon dioxide emission
# Step 8: query tariff data
# Step 9: query associated points data
# Step 10: construct the report
Expand Down Expand Up @@ -132,11 +132,14 @@ def on_get(req, resp):
description='API.INVALID_REPORTING_PERIOD_END_DATETIME')

################################################################################################################
# Step 2: query the meter and carbon
# Step 2: query the meter and energy category
################################################################################################################
cnx_system = mysql.connector.connect(**config.myems_system_db)
cursor_system = cnx_system.cursor()

cnx_energy = mysql.connector.connect(**config.myems_energy_db)
cursor_energy = cnx_energy.cursor()

cnx_carbon = mysql.connector.connect(**config.myems_carbon_db)
cursor_carbon = cnx_carbon.cursor()

Expand All @@ -154,16 +157,23 @@ def on_get(req, resp):
" FROM tbl_meters m, tbl_energy_categories ec "
" WHERE m.uuid = %s AND m.energy_category_id = ec.id ", (meter_uuid,))
row_meter = cursor_system.fetchone()

if row_meter is None:
if cursor_system:
cursor_system.close()
if cnx_system:
cnx_system.disconnect()

if cursor_energy:
cursor_energy.close()
if cnx_energy:
cnx_energy.disconnect()

if cursor_carbon:
cursor_carbon.close()
if cnx_carbon:
cnx_carbon.disconnect()

if cursor_historical:
cursor_historical.close()
if cnx_historical:
Expand Down Expand Up @@ -203,8 +213,8 @@ def on_get(req, resp):
" AND start_datetime_utc >= %s "
" AND start_datetime_utc < %s "
" ORDER BY start_datetime_utc ")
cursor_carbon.execute(query, (meter['id'], base_start_datetime_utc, base_end_datetime_utc))
rows_meter_hourly = cursor_carbon.fetchall()
cursor_energy.execute(query, (meter['id'], base_start_datetime_utc, base_end_datetime_utc))
rows_meter_hourly = cursor_energy.fetchall()

rows_meter_periodically = utilities.aggregate_hourly_data_by_period(rows_meter_hourly,
base_start_datetime_utc,
Expand Down Expand Up @@ -238,7 +248,7 @@ def on_get(req, resp):
base['total_in_kgco2e'] += actual_value * meter['kgco2e']

################################################################################################################
# Step 5: query base period Carbon Emission
# Step 5: query base period carbon dioxide emission
################################################################################################################
query = (" SELECT start_datetime_utc, actual_value "
" FROM tbl_meter_hourly "
Expand All @@ -264,16 +274,16 @@ def on_get(req, resp):
base['total_in_category'] += actual_value

################################################################################################################
# Step 6: query reporting period carbon consumption
# Step 6: query reporting period energy consumption
################################################################################################################
query = (" SELECT start_datetime_utc, actual_value "
" FROM tbl_meter_hourly "
" WHERE meter_id = %s "
" AND start_datetime_utc >= %s "
" AND start_datetime_utc < %s "
" ORDER BY start_datetime_utc ")
cursor_carbon.execute(query, (meter['id'], reporting_start_datetime_utc, reporting_end_datetime_utc))
rows_meter_hourly = cursor_carbon.fetchall()
cursor_energy.execute(query, (meter['id'], reporting_start_datetime_utc, reporting_end_datetime_utc))
rows_meter_hourly = cursor_energy.fetchall()

rows_meter_periodically = utilities.aggregate_hourly_data_by_period(rows_meter_hourly,
reporting_start_datetime_utc,
Expand Down Expand Up @@ -308,7 +318,7 @@ def on_get(req, resp):
reporting['total_in_kgco2e'] += actual_value * meter['kgco2e']

################################################################################################################
# Step 7: query reporting period carbon emission
# Step 7: query reporting period carbon dioxide emission
################################################################################################################
query = (" SELECT start_datetime_utc, actual_value "
" FROM tbl_meter_hourly "
Expand Down Expand Up @@ -429,10 +439,16 @@ def on_get(req, resp):
if cnx_system:
cnx_system.disconnect()

if cursor_energy:
cursor_energy.close()
if cnx_energy:
cnx_energy.disconnect()

if cursor_carbon:
cursor_carbon.close()
if cnx_carbon:
cnx_carbon.disconnect()

if cursor_historical:
cursor_historical.close()
if cnx_historical:
Expand All @@ -442,7 +458,7 @@ def on_get(req, resp):
"cost_center_id": meter['cost_center_id'],
"energy_category_id": meter['energy_category_id'],
"energy_category_name": meter['energy_category_name'],
"unit_of_measure": config.currency_unit,
"unit_of_measure": 'KG',
"kgce": meter['kgce'],
"kgco2e": meter['kgco2e'],
},
Expand Down Expand Up @@ -472,9 +488,9 @@ def on_get(req, resp):
# export result to Excel file and then encode the file to base64 string
result['excel_bytes_base64'] = \
excelexporters.metercarbon.export(result,
meter['name'],
reporting_period_start_datetime_local,
reporting_period_end_datetime_local,
period_type)
meter['name'],
reporting_period_start_datetime_local,
reporting_period_end_datetime_local,
period_type)

resp.text = json.dumps(result)

0 comments on commit 24a0677

Please sign in to comment.