def on_get(req, resp): print(req.params) combined_equipment_id = req.params.get('combinedequipmentid') period_type = req.params.get('periodtype') base_start_datetime_local = req.params.get('baseperiodstartdatetime') base_end_datetime_local = req.params.get('baseperiodenddatetime') reporting_start_datetime_local = req.params.get( 'reportingperiodstartdatetime') reporting_end_datetime_local = req.params.get( 'reportingperiodenddatetime') quick_mode = req.params.get('quickmode') ################################################################################################################ # Step 1: valid parameters ################################################################################################################ if combined_equipment_id is None: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_COMBINED_EQUIPMENT_ID') else: combined_equipment_id = str.strip(combined_equipment_id) if not combined_equipment_id.isdigit() or int( combined_equipment_id) <= 0: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_COMBINED_EQUIPMENT_ID') if period_type is None: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE') else: period_type = str.strip(period_type) if period_type not in ['hourly', 'daily', 'monthly', 'yearly']: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE') timezone_offset = int(config.utc_offset[1:3]) * 60 + int( config.utc_offset[4:6]) if config.utc_offset[0] == '-': timezone_offset = -timezone_offset base_start_datetime_utc = None if base_start_datetime_local is not None and len( str.strip(base_start_datetime_local)) > 0: base_start_datetime_local = str.strip(base_start_datetime_local) try: base_start_datetime_utc = datetime.strptime(base_start_datetime_local, '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ timedelta(minutes=timezone_offset) except ValueError: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description="API.INVALID_BASE_PERIOD_START_DATETIME") base_end_datetime_utc = None if base_end_datetime_local is not None and len( str.strip(base_end_datetime_local)) > 0: base_end_datetime_local = str.strip(base_end_datetime_local) try: base_end_datetime_utc = datetime.strptime(base_end_datetime_local, '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ timedelta(minutes=timezone_offset) except ValueError: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description="API.INVALID_BASE_PERIOD_END_DATETIME") if base_start_datetime_utc is not None and base_end_datetime_utc is not None and \ base_start_datetime_utc >= base_end_datetime_utc: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_BASE_PERIOD_END_DATETIME') if reporting_start_datetime_local is None: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description="API.INVALID_REPORTING_PERIOD_START_DATETIME") else: reporting_start_datetime_local = str.strip( reporting_start_datetime_local) try: reporting_start_datetime_utc = datetime.strptime(reporting_start_datetime_local, '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ timedelta(minutes=timezone_offset) except ValueError: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description="API.INVALID_REPORTING_PERIOD_START_DATETIME") if reporting_end_datetime_local is None: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description="API.INVALID_REPORTING_PERIOD_END_DATETIME") else: reporting_end_datetime_local = str.strip( reporting_end_datetime_local) try: reporting_end_datetime_utc = datetime.strptime(reporting_end_datetime_local, '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ timedelta(minutes=timezone_offset) except ValueError: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description="API.INVALID_REPORTING_PERIOD_END_DATETIME") if reporting_start_datetime_utc >= reporting_end_datetime_utc: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_REPORTING_PERIOD_END_DATETIME') # if turn quick mode on, do not return parameters data and excel file is_quick_mode = False if quick_mode is not None and \ len(str.strip(quick_mode)) > 0 and \ str.lower(str.strip(quick_mode)) in ('true', 't', 'on', 'yes', 'y'): is_quick_mode = True ################################################################################################################ # Step 2: query the combined equipment ################################################################################################################ 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_historical = mysql.connector.connect(**config.myems_historical_db) cursor_historical = cnx_historical.cursor() cursor_system.execute( " SELECT id, name, cost_center_id " " FROM tbl_combined_equipments " " WHERE id = %s ", (combined_equipment_id, )) row_combined_equipment = cursor_system.fetchone() if row_combined_equipment 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_historical: cursor_historical.close() if cnx_historical: cnx_historical.disconnect() raise falcon.HTTPError( falcon.HTTP_404, title='API.NOT_FOUND', description='API.COMBINED_EQUIPMENT_NOT_FOUND') combined_equipment = dict() combined_equipment['id'] = row_combined_equipment[0] combined_equipment['name'] = row_combined_equipment[1] combined_equipment['cost_center_id'] = row_combined_equipment[2] ################################################################################################################ # Step 3: query associated points ################################################################################################################ point_list = list() cursor_system.execute( " SELECT p.id, p.name, p.units, p.object_type " " FROM tbl_combined_equipments e, tbl_combined_equipments_parameters ep, tbl_points p " " WHERE e.id = %s AND e.id = ep.combined_equipment_id AND ep.parameter_type = 'point' " " AND ep.point_id = p.id " " ORDER BY p.id ", (combined_equipment['id'], )) rows_points = cursor_system.fetchall() if rows_points is not None and len(rows_points) > 0: for row in rows_points: point_list.append({ "id": row[0], "name": row[1], "units": row[2], "object_type": row[3] }) print(point_list) ################################################################################################################ # Step 4: query associated fractions ################################################################################################################ fraction_list = list() cursor_system.execute( " SELECT id, name, numerator_meter_uuid, denominator_meter_uuid " " FROM tbl_combined_equipments_parameters " " WHERE combined_equipment_id = %s AND parameter_type = 'fraction' ", (combined_equipment['id'], )) rows_fractions = cursor_system.fetchall() if rows_fractions is not None and len(rows_fractions) > 0: for row in rows_fractions: fraction_list.append({ "id": row[0], "name": row[1], "numerator_meter_uuid": row[2], "denominator_meter_uuid": row[3], }) print(fraction_list) ################################################################################################################ # Step 5: query fractions' numerator and denominator ################################################################################################################ # get all meters meter_dict = dict() query = (" SELECT m.id, m.uuid, ec.unit_of_measure " " FROM tbl_meters m, tbl_energy_categories ec " " WHERE m.energy_category_id = ec.id ") cursor_system.execute(query) rows_meters = cursor_system.fetchall() if rows_meters is not None and len(rows_meters) > 0: for row in rows_meters: meter_dict[row[1]] = {'id': row[0], 'unit': row[2]} # get all offline meters offline_meter_dict = dict() query = (" SELECT m.id, m.uuid, ec.unit_of_measure " " FROM tbl_offline_meters m, tbl_energy_categories ec " " WHERE m.energy_category_id = ec.id ") cursor_system.execute(query) rows_offline_meters = cursor_system.fetchall() if rows_offline_meters is not None and len(rows_offline_meters) > 0: for row in rows_offline_meters: offline_meter_dict[row[1]] = {'id': row[0], 'unit': row[2]} # get all virtual meters virtual_meter_dict = dict() query = (" SELECT m.id, m.uuid, ec.unit_of_measure " " FROM tbl_virtual_meters m, tbl_energy_categories ec " " WHERE m.energy_category_id = ec.id ") cursor_system.execute(query) rows_virtual_meters = cursor_system.fetchall() if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: for row in rows_virtual_meters: virtual_meter_dict[row[1]] = {'id': row[0], 'unit': row[2]} if fraction_list is not None and len(fraction_list) > 0: for fraction in fraction_list: if fraction['numerator_meter_uuid'] in offline_meter_dict: fraction['numerator_meter_id'] = offline_meter_dict[ fraction['numerator_meter_uuid']]['id'] fraction['numerator_meter_unit'] = offline_meter_dict[ fraction['numerator_meter_uuid']]['unit'] fraction['numerator_meter_type'] = 'offline_meter' elif fraction['numerator_meter_uuid'] in virtual_meter_dict: fraction['numerator_meter_id'] = virtual_meter_dict[ fraction['numerator_meter_uuid']]['id'] fraction['numerator_meter_unit'] = virtual_meter_dict[ fraction['numerator_meter_uuid']]['unit'] fraction['numerator_meter_type'] = 'virtual_meter' elif fraction['numerator_meter_uuid'] in meter_dict: fraction['numerator_meter_id'] = meter_dict[ fraction['numerator_meter_uuid']]['id'] fraction['numerator_meter_unit'] = meter_dict[ fraction['numerator_meter_uuid']]['unit'] fraction['numerator_meter_type'] = 'meter' if fraction['denominator_meter_uuid'] in offline_meter_dict: fraction['denominator_meter_id'] = offline_meter_dict[ fraction['denominator_meter_uuid']]['id'] fraction['denominator_meter_unit'] = offline_meter_dict[ fraction['denominator_meter_uuid']]['unit'] fraction['denominator_meter_type'] = 'offline_meter' elif fraction['denominator_meter_uuid'] in virtual_meter_dict: fraction['denominator_meter_id'] = virtual_meter_dict[ fraction['denominator_meter_uuid']]['id'] fraction['denominator_meter_unit'] = virtual_meter_dict[ fraction['denominator_meter_uuid']]['unit'] fraction['denominator_meter_type'] = 'virtual_meter' elif fraction['denominator_meter_uuid'] in meter_dict: fraction['denominator_meter_id'] = meter_dict[ fraction['denominator_meter_uuid']]['id'] fraction['denominator_meter_unit'] = meter_dict[ fraction['denominator_meter_uuid']]['unit'] fraction['denominator_meter_type'] = 'meter' print(fraction_list) ################################################################################################################ # Step 5: calculate base period fractions ################################################################################################################ base = dict() if fraction_list is not None and len(fraction_list) > 0: for fraction in fraction_list: base[fraction['id']] = dict() base[fraction['id']]['name'] = fraction['name'] base[fraction['id']]['unit'] = fraction['numerator_meter_unit'] + '/' + \ fraction['denominator_meter_unit'] base[fraction['id']]['numerator_timestamps'] = list() base[fraction['id']]['numerator_values'] = list() base[fraction['id']]['numerator_cumulation'] = Decimal(0.0) base[fraction['id']]['denominator_timestamps'] = list() base[fraction['id']]['denominator_values'] = list() base[fraction['id']]['denominator_cumulation'] = Decimal(0.0) base[fraction['id']]['timestamps'] = list() base[fraction['id']]['values'] = list() base[fraction['id']]['cumulation'] = Decimal(0.0) # query numerator meter output if fraction['numerator_meter_type'] == 'meter': 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 ") elif fraction['numerator_meter_type'] == 'offline_meter': query = (" SELECT start_datetime_utc, actual_value " " FROM tbl_offline_meter_hourly " " WHERE offline_meter_id = %s " " AND start_datetime_utc >= %s " " AND start_datetime_utc < %s " " ORDER BY start_datetime_utc ") elif fraction['numerator_meter_type'] == 'virtual_meter': query = (" SELECT start_datetime_utc, actual_value " " FROM tbl_virtual_meter_hourly " " WHERE virtual_meter_id = %s " " AND start_datetime_utc >= %s " " AND start_datetime_utc < %s " " ORDER BY start_datetime_utc ") cursor_energy.execute( query, (fraction['numerator_meter_id'], base_start_datetime_utc, base_end_datetime_utc)) rows_numerator_meter_hourly = cursor_energy.fetchall() rows_numerator_meter_periodically = \ utilities.aggregate_hourly_data_by_period(rows_numerator_meter_hourly, base_start_datetime_utc, base_end_datetime_utc, period_type) # query denominator meter input if fraction['denominator_meter_type'] == 'meter': 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 ") elif fraction['denominator_meter_type'] == 'offline_meter': query = (" SELECT start_datetime_utc, actual_value " " FROM tbl_offline_meter_hourly " " WHERE offline_meter_id = %s " " AND start_datetime_utc >= %s " " AND start_datetime_utc < %s " " ORDER BY start_datetime_utc ") elif fraction['denominator_meter_type'] == 'virtual_meter': query = (" SELECT start_datetime_utc, actual_value " " FROM tbl_virtual_meter_hourly " " WHERE virtual_meter_id = %s " " AND start_datetime_utc >= %s " " AND start_datetime_utc < %s " " ORDER BY start_datetime_utc ") cursor_energy.execute( query, (fraction['denominator_meter_id'], base_start_datetime_utc, base_end_datetime_utc)) rows_denominator_meter_hourly = cursor_energy.fetchall() rows_denominator_meter_periodically = \ utilities.aggregate_hourly_data_by_period(rows_denominator_meter_hourly, base_start_datetime_utc, base_end_datetime_utc, period_type) for row_numerator_meter_periodically in rows_numerator_meter_periodically: current_datetime_local = row_numerator_meter_periodically[0].replace(tzinfo=timezone.utc) + \ timedelta(minutes=timezone_offset) if period_type == 'hourly': current_datetime = current_datetime_local.strftime( '%Y-%m-%dT%H:%M:%S') elif period_type == 'daily': current_datetime = current_datetime_local.strftime( '%Y-%m-%d') elif period_type == 'monthly': current_datetime = current_datetime_local.strftime( '%Y-%m') elif period_type == 'yearly': current_datetime = current_datetime_local.strftime( '%Y') actual_value = Decimal(0.0) if row_numerator_meter_periodically[1] is None \ else row_numerator_meter_periodically[1] base[fraction['id']]['numerator_timestamps'].append( current_datetime) base[fraction['id']]['numerator_values'].append( actual_value) base[ fraction['id']]['numerator_cumulation'] += actual_value for row_denominator_meter_periodically in rows_denominator_meter_periodically: current_datetime_local = row_denominator_meter_periodically[0].replace(tzinfo=timezone.utc) + \ timedelta(minutes=timezone_offset) if period_type == 'hourly': current_datetime = current_datetime_local.strftime( '%Y-%m-%dT%H:%M:%S') elif period_type == 'daily': current_datetime = current_datetime_local.strftime( '%Y-%m-%d') elif period_type == 'monthly': current_datetime = current_datetime_local.strftime( '%Y-%m') elif period_type == 'yearly': current_datetime = current_datetime_local.strftime( '%Y') actual_value = Decimal(0.0) if row_denominator_meter_periodically[1] is None \ else row_denominator_meter_periodically[1] base[fraction['id']]['denominator_timestamps'].append( current_datetime) base[fraction['id']]['denominator_values'].append( actual_value) base[fraction['id']][ 'denominator_cumulation'] += actual_value for i in range( len(base[fraction['id']]['denominator_timestamps'])): timestamp = base[ fraction['id']]['denominator_timestamps'][i] base[fraction['id']]['timestamps'].append(timestamp) value = (base[fraction['id']]['numerator_values'][i] / base[fraction['id']]['denominator_values'][i]) \ if base[fraction['id']]['denominator_values'][i] > Decimal(0.0) else Decimal(0.0) base[fraction['id']]['values'].append(value) cumulation = (base[fraction['id']]['numerator_cumulation'] / base[fraction['id']]['denominator_cumulation']) \ if base[fraction['id']]['denominator_cumulation'] > Decimal(0.0) else Decimal(0.0) base[fraction['id']]['cumulation'] = cumulation ################################################################################################################ # Step 6: calculate reporting period fractions ################################################################################################################ reporting = dict() if fraction_list is not None and len(fraction_list) > 0: for fraction in fraction_list: reporting[fraction['id']] = dict() reporting[fraction['id']]['name'] = fraction['name'] reporting[fraction['id']]['unit'] = fraction['numerator_meter_unit'] + '/' + \ fraction['denominator_meter_unit'] reporting[fraction['id']]['numerator_timestamps'] = list() reporting[fraction['id']]['numerator_values'] = list() reporting[fraction['id']]['numerator_cumulation'] = Decimal( 0.0) reporting[fraction['id']]['denominator_timestamps'] = list() reporting[fraction['id']]['denominator_values'] = list() reporting[fraction['id']]['denominator_cumulation'] = Decimal( 0.0) reporting[fraction['id']]['timestamps'] = list() reporting[fraction['id']]['values'] = list() reporting[fraction['id']]['cumulation'] = Decimal(0.0) # query numerator meter output if fraction['numerator_meter_type'] == 'meter': 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 ") elif fraction['numerator_meter_type'] == 'offline_meter': query = (" SELECT start_datetime_utc, actual_value " " FROM tbl_offline_meter_hourly " " WHERE offline_meter_id = %s " " AND start_datetime_utc >= %s " " AND start_datetime_utc < %s " " ORDER BY start_datetime_utc ") elif fraction['numerator_meter_type'] == 'virtual_meter': query = (" SELECT start_datetime_utc, actual_value " " FROM tbl_virtual_meter_hourly " " WHERE virtual_meter_id = %s " " AND start_datetime_utc >= %s " " AND start_datetime_utc < %s " " ORDER BY start_datetime_utc ") cursor_energy.execute( query, (fraction['numerator_meter_id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) rows_numerator_meter_hourly = cursor_energy.fetchall() rows_numerator_meter_periodically = \ utilities.aggregate_hourly_data_by_period(rows_numerator_meter_hourly, reporting_start_datetime_utc, reporting_end_datetime_utc, period_type) # query denominator meter input if fraction['denominator_meter_type'] == 'meter': 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 ") elif fraction['denominator_meter_type'] == 'offline_meter': query = (" SELECT start_datetime_utc, actual_value " " FROM tbl_offline_meter_hourly " " WHERE offline_meter_id = %s " " AND start_datetime_utc >= %s " " AND start_datetime_utc < %s " " ORDER BY start_datetime_utc ") elif fraction['denominator_meter_type'] == 'virtual_meter': query = (" SELECT start_datetime_utc, actual_value " " FROM tbl_virtual_meter_hourly " " WHERE virtual_meter_id = %s " " AND start_datetime_utc >= %s " " AND start_datetime_utc < %s " " ORDER BY start_datetime_utc ") cursor_energy.execute( query, (fraction['denominator_meter_id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) rows_denominator_meter_hourly = cursor_energy.fetchall() rows_denominator_meter_periodically = \ utilities.aggregate_hourly_data_by_period(rows_denominator_meter_hourly, reporting_start_datetime_utc, reporting_end_datetime_utc, period_type) for row_numerator_meter_periodically in rows_numerator_meter_periodically: current_datetime_local = row_numerator_meter_periodically[0].replace(tzinfo=timezone.utc) + \ timedelta(minutes=timezone_offset) if period_type == 'hourly': current_datetime = current_datetime_local.strftime( '%Y-%m-%dT%H:%M:%S') elif period_type == 'daily': current_datetime = current_datetime_local.strftime( '%Y-%m-%d') elif period_type == 'monthly': current_datetime = current_datetime_local.strftime( '%Y-%m') elif period_type == 'yearly': current_datetime = current_datetime_local.strftime( '%Y') actual_value = Decimal(0.0) if row_numerator_meter_periodically[1] is None \ else row_numerator_meter_periodically[1] reporting[fraction['id']]['numerator_timestamps'].append( current_datetime) reporting[fraction['id']]['numerator_values'].append( actual_value) reporting[ fraction['id']]['numerator_cumulation'] += actual_value for row_denominator_meter_periodically in rows_denominator_meter_periodically: current_datetime_local = row_denominator_meter_periodically[0].replace(tzinfo=timezone.utc) + \ timedelta(minutes=timezone_offset) if period_type == 'hourly': current_datetime = current_datetime_local.strftime( '%Y-%m-%dT%H:%M:%S') elif period_type == 'daily': current_datetime = current_datetime_local.strftime( '%Y-%m-%d') elif period_type == 'monthly': current_datetime = current_datetime_local.strftime( '%Y-%m') elif period_type == 'yearly': current_datetime = current_datetime_local.strftime( '%Y') actual_value = Decimal(0.0) if row_denominator_meter_periodically[1] is None \ else row_denominator_meter_periodically[1] reporting[fraction['id']]['denominator_timestamps'].append( current_datetime) reporting[fraction['id']]['denominator_values'].append( actual_value) reporting[fraction['id']][ 'denominator_cumulation'] += actual_value for i in range( len(reporting[fraction['id']] ['denominator_timestamps'])): timestamp = reporting[ fraction['id']]['denominator_timestamps'][i] reporting[fraction['id']]['timestamps'].append(timestamp) value = reporting[fraction['id']]['numerator_values'][i] / \ reporting[fraction['id']]['denominator_values'][i] \ if reporting[fraction['id']]['denominator_values'][i] > Decimal(0.0) else Decimal(0.0) reporting[fraction['id']]['values'].append(value) cumulation = (reporting[fraction['id']]['numerator_cumulation'] / reporting[fraction['id']]['denominator_cumulation']) \ if reporting[fraction['id']]['denominator_cumulation'] > Decimal(0.0) else Decimal(0.0) reporting[fraction['id']]['cumulation'] = cumulation ################################################################################################################ # Step 7: query associated points data ################################################################################################################ parameters_data = dict() parameters_data['names'] = list() parameters_data['timestamps'] = list() parameters_data['values'] = list() if not is_quick_mode: for point in point_list: point_values = [] point_timestamps = [] if point['object_type'] == 'ANALOG_VALUE': query = (" SELECT utc_date_time, actual_value " " FROM tbl_analog_value " " WHERE point_id = %s " " AND utc_date_time BETWEEN %s AND %s " " ORDER BY utc_date_time ") cursor_historical.execute( query, (point['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) rows = cursor_historical.fetchall() if rows is not None and len(rows) > 0: for row in rows: current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ timedelta(minutes=timezone_offset) current_datetime = current_datetime_local.strftime( '%Y-%m-%dT%H:%M:%S') point_timestamps.append(current_datetime) point_values.append(row[1]) elif point['object_type'] == 'ENERGY_VALUE': query = (" SELECT utc_date_time, actual_value " " FROM tbl_energy_value " " WHERE point_id = %s " " AND utc_date_time BETWEEN %s AND %s " " ORDER BY utc_date_time ") cursor_historical.execute( query, (point['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) rows = cursor_historical.fetchall() if rows is not None and len(rows) > 0: for row in rows: current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ timedelta(minutes=timezone_offset) current_datetime = current_datetime_local.strftime( '%Y-%m-%dT%H:%M:%S') point_timestamps.append(current_datetime) point_values.append(row[1]) elif point['object_type'] == 'DIGITAL_VALUE': query = (" SELECT utc_date_time, actual_value " " FROM tbl_digital_value " " WHERE point_id = %s " " AND utc_date_time BETWEEN %s AND %s ") cursor_historical.execute( query, (point['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) rows = cursor_historical.fetchall() if rows is not None and len(rows) > 0: for row in rows: current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ timedelta(minutes=timezone_offset) current_datetime = current_datetime_local.strftime( '%Y-%m-%dT%H:%M:%S') point_timestamps.append(current_datetime) point_values.append(row[1]) parameters_data['names'].append(point['name'] + ' (' + point['units'] + ')') parameters_data['timestamps'].append(point_timestamps) parameters_data['values'].append(point_values) ################################################################################################################ # Step 8: construct the report ################################################################################################################ if cursor_system: cursor_system.close() if cnx_system: cnx_system.disconnect() if cursor_energy: cursor_energy.close() if cnx_energy: cnx_energy.disconnect() result = dict() result['combined_equipment'] = dict() result['combined_equipment']['name'] = combined_equipment['name'] result['base_period_efficiency'] = dict() result['base_period_efficiency']['timestamps'] = list() result['base_period_efficiency']['values'] = list() result['base_period_efficiency']['cumulations'] = list() result['reporting_period_efficiency'] = dict() result['reporting_period_efficiency']['names'] = list() result['reporting_period_efficiency']['units'] = list() result['reporting_period_efficiency']['timestamps'] = list() result['reporting_period_efficiency']['values'] = list() result['reporting_period_efficiency']['cumulations'] = list() result['reporting_period_efficiency']['increment_rates'] = list() if fraction_list is not None and len(fraction_list) > 0: for fraction in fraction_list: result['base_period_efficiency']['timestamps'].append( base[fraction['id']]['timestamps']) result['base_period_efficiency']['values'].append( base[fraction['id']]['values']) result['base_period_efficiency']['cumulations'].append( base[fraction['id']]['cumulation']) result['reporting_period_efficiency']['names'].append( reporting[fraction['id']]['name']) result['reporting_period_efficiency']['units'].append( reporting[fraction['id']]['unit']) result['reporting_period_efficiency']['timestamps'].append( reporting[fraction['id']]['timestamps']) result['reporting_period_efficiency']['values'].append( reporting[fraction['id']]['values']) result['reporting_period_efficiency']['cumulations'].append( reporting[fraction['id']]['cumulation']) result['reporting_period_efficiency']['increment_rates'].append( (reporting[fraction['id']]['cumulation'] - base[fraction['id']]['cumulation']) / base[fraction['id']]['cumulation'] if base[ fraction['id']]['cumulation'] > Decimal(0.0) else None) result['parameters'] = { "names": parameters_data['names'], "timestamps": parameters_data['timestamps'], "values": parameters_data['values'] } # export result to Excel file and then encode the file to base64 string result['excel_bytes_base64'] = None if not is_quick_mode: result['excel_bytes_base64'] = \ excelexporters.combinedequipmentefficiency.export(result, combined_equipment['name'], reporting_start_datetime_local, reporting_end_datetime_local, period_type) resp.body = json.dumps(result)
def on_get(req, resp): print(req.params) store_id = req.params.get('storeid') period_type = req.params.get('periodtype') base_start_datetime_local = req.params.get('baseperiodstartdatetime') base_end_datetime_local = req.params.get('baseperiodenddatetime') reporting_start_datetime_local = req.params.get( 'reportingperiodstartdatetime') reporting_end_datetime_local = req.params.get( 'reportingperiodenddatetime') ################################################################################################################ # Step 1: valid parameters ################################################################################################################ if store_id is None: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_STORE_ID') else: store_id = str.strip(store_id) if not store_id.isdigit() or int(store_id) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_STORE_ID') if period_type is None: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE') else: period_type = str.strip(period_type) if period_type not in ['hourly', 'daily', 'monthly', 'yearly']: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE') timezone_offset = int(config.utc_offset[1:3]) * 60 + int( config.utc_offset[4:6]) if config.utc_offset[0] == '-': timezone_offset = -timezone_offset base_start_datetime_utc = None if base_start_datetime_local is not None and len( str.strip(base_start_datetime_local)) > 0: base_start_datetime_local = str.strip(base_start_datetime_local) try: base_start_datetime_utc = datetime.strptime(base_start_datetime_local, '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ timedelta(minutes=timezone_offset) except ValueError: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description="API.INVALID_BASE_PERIOD_START_DATETIME") base_end_datetime_utc = None if base_end_datetime_local is not None and len( str.strip(base_end_datetime_local)) > 0: base_end_datetime_local = str.strip(base_end_datetime_local) try: base_end_datetime_utc = datetime.strptime(base_end_datetime_local, '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ timedelta(minutes=timezone_offset) except ValueError: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description="API.INVALID_BASE_PERIOD_END_DATETIME") if base_start_datetime_utc is not None and base_end_datetime_utc is not None and \ base_start_datetime_utc >= base_end_datetime_utc: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_BASE_PERIOD_END_DATETIME') if reporting_start_datetime_local is None: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description="API.INVALID_REPORTING_PERIOD_START_DATETIME") else: reporting_start_datetime_local = str.strip( reporting_start_datetime_local) try: reporting_start_datetime_utc = datetime.strptime(reporting_start_datetime_local, '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ timedelta(minutes=timezone_offset) except ValueError: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description="API.INVALID_REPORTING_PERIOD_START_DATETIME") if reporting_end_datetime_local is None: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description="API.INVALID_REPORTING_PERIOD_END_DATETIME") else: reporting_end_datetime_local = str.strip( reporting_end_datetime_local) try: reporting_end_datetime_utc = datetime.strptime(reporting_end_datetime_local, '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ timedelta(minutes=timezone_offset) except ValueError: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description="API.INVALID_REPORTING_PERIOD_END_DATETIME") if reporting_start_datetime_utc >= reporting_end_datetime_utc: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_REPORTING_PERIOD_END_DATETIME') ################################################################################################################ # Step 2: query the store ################################################################################################################ 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_historical = mysql.connector.connect(**config.myems_historical_db) cursor_historical = cnx_historical.cursor() cursor_system.execute( " SELECT id, name, area, cost_center_id " " FROM tbl_stores " " WHERE id = %s ", (store_id, )) row_store = cursor_system.fetchone() if row_store 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_historical: cursor_historical.close() if cnx_historical: cnx_historical.disconnect() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.STORE_NOT_FOUND') store = dict() store['id'] = row_store[0] store['name'] = row_store[1] store['area'] = row_store[2] store['cost_center_id'] = row_store[3] ################################################################################################################ # Step 3: query energy categories ################################################################################################################ energy_category_set = set() # query energy categories in base period cursor_energy.execute( " SELECT DISTINCT(energy_category_id) " " FROM tbl_store_input_category_hourly " " WHERE store_id = %s " " AND start_datetime_utc >= %s " " AND start_datetime_utc < %s ", (store['id'], base_start_datetime_utc, base_end_datetime_utc)) rows_energy_categories = cursor_energy.fetchall() if rows_energy_categories is not None or len( rows_energy_categories) > 0: for row_energy_category in rows_energy_categories: energy_category_set.add(row_energy_category[0]) # query energy categories in reporting period cursor_energy.execute( " SELECT DISTINCT(energy_category_id) " " FROM tbl_store_input_category_hourly " " WHERE store_id = %s " " AND start_datetime_utc >= %s " " AND start_datetime_utc < %s ", (store['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) rows_energy_categories = cursor_energy.fetchall() if rows_energy_categories is not None or len( rows_energy_categories) > 0: for row_energy_category in rows_energy_categories: energy_category_set.add(row_energy_category[0]) # query all energy categories in base period and reporting period cursor_system.execute( " SELECT id, name, unit_of_measure, kgce, kgco2e " " FROM tbl_energy_categories " " ORDER BY id ", ) rows_energy_categories = cursor_system.fetchall() if rows_energy_categories is None or len(rows_energy_categories) == 0: 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_historical: cursor_historical.close() if cnx_historical: cnx_historical.disconnect() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.ENERGY_CATEGORY_NOT_FOUND') energy_category_dict = dict() for row_energy_category in rows_energy_categories: if row_energy_category[0] in energy_category_set: energy_category_dict[row_energy_category[0]] = { "name": row_energy_category[1], "unit_of_measure": row_energy_category[2], "kgce": row_energy_category[3], "kgco2e": row_energy_category[4] } ################################################################################################################ # Step 4: query associated sensors ################################################################################################################ point_list = list() cursor_system.execute( " SELECT p.id, p.name, p.units, p.object_type " " FROM tbl_stores st, tbl_sensors se, tbl_stores_sensors ss, " " tbl_points p, tbl_sensors_points sp " " WHERE st.id = %s AND st.id = ss.store_id AND ss.sensor_id = se.id " " AND se.id = sp.sensor_id AND sp.point_id = p.id " " ORDER BY p.id ", (store['id'], )) rows_points = cursor_system.fetchall() if rows_points is not None and len(rows_points) > 0: for row in rows_points: point_list.append({ "id": row[0], "name": row[1], "units": row[2], "object_type": row[3] }) ################################################################################################################ # Step 5: query associated points ################################################################################################################ cursor_system.execute( " SELECT p.id, p.name, p.units, p.object_type " " FROM tbl_stores s, tbl_stores_points sp, tbl_points p " " WHERE s.id = %s AND s.id = sp.store_id AND sp.point_id = p.id " " ORDER BY p.id ", (store['id'], )) rows_points = cursor_system.fetchall() if rows_points is not None and len(rows_points) > 0: for row in rows_points: point_list.append({ "id": row[0], "name": row[1], "units": row[2], "object_type": row[3] }) ################################################################################################################ # Step 6: query base period energy input ################################################################################################################ base = dict() if energy_category_set is not None and len(energy_category_set) > 0: for energy_category_id in energy_category_set: kgce = energy_category_dict[energy_category_id]['kgce'] kgco2e = energy_category_dict[energy_category_id]['kgco2e'] base[energy_category_id] = dict() base[energy_category_id]['timestamps'] = list() base[energy_category_id]['values'] = list() base[energy_category_id]['subtotal'] = Decimal(0.0) base[energy_category_id]['subtotal_in_kgce'] = Decimal(0.0) base[energy_category_id]['subtotal_in_kgco2e'] = Decimal(0.0) cursor_energy.execute( " SELECT start_datetime_utc, actual_value " " FROM tbl_store_input_category_hourly " " WHERE store_id = %s " " AND energy_category_id = %s " " AND start_datetime_utc >= %s " " AND start_datetime_utc < %s " " ORDER BY start_datetime_utc ", (store['id'], energy_category_id, base_start_datetime_utc, base_end_datetime_utc)) rows_store_hourly = cursor_energy.fetchall() rows_store_periodically = utilities.aggregate_hourly_data_by_period( rows_store_hourly, base_start_datetime_utc, base_end_datetime_utc, period_type) for row_store_periodically in rows_store_periodically: current_datetime_local = row_store_periodically[0].replace(tzinfo=timezone.utc) + \ timedelta(minutes=timezone_offset) if period_type == 'hourly': current_datetime = current_datetime_local.strftime( '%Y-%m-%dT%H:%M:%S') elif period_type == 'daily': current_datetime = current_datetime_local.strftime( '%Y-%m-%d') elif period_type == 'monthly': current_datetime = current_datetime_local.strftime( '%Y-%m') elif period_type == 'yearly': current_datetime = current_datetime_local.strftime( '%Y') actual_value = Decimal(0.0) if row_store_periodically[ 1] is None else row_store_periodically[1] base[energy_category_id]['timestamps'].append( current_datetime) base[energy_category_id]['values'].append(actual_value) base[energy_category_id]['subtotal'] += actual_value base[energy_category_id][ 'subtotal_in_kgce'] += actual_value * kgce base[energy_category_id][ 'subtotal_in_kgco2e'] += actual_value * kgco2e ################################################################################################################ # Step 8: query reporting period energy input ################################################################################################################ reporting = dict() if energy_category_set is not None and len(energy_category_set) > 0: for energy_category_id in energy_category_set: kgce = energy_category_dict[energy_category_id]['kgce'] kgco2e = energy_category_dict[energy_category_id]['kgco2e'] reporting[energy_category_id] = dict() reporting[energy_category_id]['timestamps'] = list() reporting[energy_category_id]['values'] = list() reporting[energy_category_id]['subtotal'] = Decimal(0.0) reporting[energy_category_id]['subtotal_in_kgce'] = Decimal( 0.0) reporting[energy_category_id]['subtotal_in_kgco2e'] = Decimal( 0.0) reporting[energy_category_id]['toppeak'] = Decimal(0.0) reporting[energy_category_id]['onpeak'] = Decimal(0.0) reporting[energy_category_id]['midpeak'] = Decimal(0.0) reporting[energy_category_id]['offpeak'] = Decimal(0.0) cursor_energy.execute( " SELECT start_datetime_utc, actual_value " " FROM tbl_store_input_category_hourly " " WHERE store_id = %s " " AND energy_category_id = %s " " AND start_datetime_utc >= %s " " AND start_datetime_utc < %s " " ORDER BY start_datetime_utc ", (store['id'], energy_category_id, reporting_start_datetime_utc, reporting_end_datetime_utc)) rows_store_hourly = cursor_energy.fetchall() rows_store_periodically = utilities.aggregate_hourly_data_by_period( rows_store_hourly, reporting_start_datetime_utc, reporting_end_datetime_utc, period_type) for row_store_periodically in rows_store_periodically: current_datetime_local = row_store_periodically[0].replace(tzinfo=timezone.utc) + \ timedelta(minutes=timezone_offset) if period_type == 'hourly': current_datetime = current_datetime_local.strftime( '%Y-%m-%dT%H:%M:%S') elif period_type == 'daily': current_datetime = current_datetime_local.strftime( '%Y-%m-%d') elif period_type == 'monthly': current_datetime = current_datetime_local.strftime( '%Y-%m') elif period_type == 'yearly': current_datetime = current_datetime_local.strftime( '%Y') actual_value = Decimal(0.0) if row_store_periodically[ 1] is None else row_store_periodically[1] reporting[energy_category_id]['timestamps'].append( current_datetime) reporting[energy_category_id]['values'].append( actual_value) reporting[energy_category_id]['subtotal'] += actual_value reporting[energy_category_id][ 'subtotal_in_kgce'] += actual_value * kgce reporting[energy_category_id][ 'subtotal_in_kgco2e'] += actual_value * kgco2e energy_category_tariff_dict = utilities.get_energy_category_peak_types( store['cost_center_id'], energy_category_id, reporting_start_datetime_utc, reporting_end_datetime_utc) for row in rows_store_hourly: peak_type = energy_category_tariff_dict.get(row[0], None) if peak_type == 'toppeak': reporting[energy_category_id]['toppeak'] += row[1] elif peak_type == 'onpeak': reporting[energy_category_id]['onpeak'] += row[1] elif peak_type == 'midpeak': reporting[energy_category_id]['midpeak'] += row[1] elif peak_type == 'offpeak': reporting[energy_category_id]['offpeak'] += row[1] ################################################################################################################ # Step 9: query tariff data ################################################################################################################ parameters_data = dict() parameters_data['names'] = list() parameters_data['timestamps'] = list() parameters_data['values'] = list() if energy_category_set is not None and len(energy_category_set) > 0: for energy_category_id in energy_category_set: energy_category_tariff_dict = utilities.get_energy_category_tariffs( store['cost_center_id'], energy_category_id, reporting_start_datetime_utc, reporting_end_datetime_utc) tariff_timestamp_list = list() tariff_value_list = list() for k, v in energy_category_tariff_dict.items(): # convert k from utc to local k = k + timedelta(minutes=timezone_offset) tariff_timestamp_list.append(k.isoformat()[0:19][0:19]) tariff_value_list.append(v) parameters_data['names'].append( 'TARIFF-' + energy_category_dict[energy_category_id]['name']) parameters_data['timestamps'].append(tariff_timestamp_list) parameters_data['values'].append(tariff_value_list) ################################################################################################################ # Step 10: query associated sensors and points data ################################################################################################################ for point in point_list: point_values = [] point_timestamps = [] if point['object_type'] == 'ANALOG_VALUE': query = (" SELECT utc_date_time, actual_value " " FROM tbl_analog_value " " WHERE point_id = %s " " AND utc_date_time BETWEEN %s AND %s " " ORDER BY utc_date_time ") cursor_historical.execute( query, (point['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) rows = cursor_historical.fetchall() if rows is not None and len(rows) > 0: for row in rows: current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ timedelta(minutes=timezone_offset) current_datetime = current_datetime_local.strftime( '%Y-%m-%dT%H:%M:%S') point_timestamps.append(current_datetime) point_values.append(row[1]) elif point['object_type'] == 'ENERGY_VALUE': query = (" SELECT utc_date_time, actual_value " " FROM tbl_energy_value " " WHERE point_id = %s " " AND utc_date_time BETWEEN %s AND %s " " ORDER BY utc_date_time ") cursor_historical.execute( query, (point['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) rows = cursor_historical.fetchall() if rows is not None and len(rows) > 0: for row in rows: current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ timedelta(minutes=timezone_offset) current_datetime = current_datetime_local.strftime( '%Y-%m-%dT%H:%M:%S') point_timestamps.append(current_datetime) point_values.append(row[1]) elif point['object_type'] == 'DIGITAL_VALUE': query = (" SELECT utc_date_time, actual_value " " FROM tbl_digital_value " " WHERE point_id = %s " " AND utc_date_time BETWEEN %s AND %s ") cursor_historical.execute( query, (point['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) rows = cursor_historical.fetchall() if rows is not None and len(rows) > 0: for row in rows: current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ timedelta(minutes=timezone_offset) current_datetime = current_datetime_local.strftime( '%Y-%m-%dT%H:%M:%S') point_timestamps.append(current_datetime) point_values.append(row[1]) parameters_data['names'].append(point['name'] + ' (' + point['units'] + ')') parameters_data['timestamps'].append(point_timestamps) parameters_data['values'].append(point_values) ################################################################################################################ # Step 12: construct the report ################################################################################################################ 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_historical: cursor_historical.close() if cnx_historical: cnx_historical.disconnect() result = dict() result['store'] = dict() result['store']['name'] = store['name'] result['store']['area'] = store['area'] result['base_period'] = dict() result['base_period']['names'] = list() result['base_period']['units'] = list() result['base_period']['timestamps'] = list() result['base_period']['values'] = list() result['base_period']['subtotals'] = list() result['base_period']['subtotals_in_kgce'] = list() result['base_period']['subtotals_in_kgco2e'] = list() result['base_period']['total_in_kgce'] = Decimal(0.0) result['base_period']['total_in_kgco2e'] = Decimal(0.0) if energy_category_set is not None and len(energy_category_set) > 0: for energy_category_id in energy_category_set: result['base_period']['names'].append( energy_category_dict[energy_category_id]['name']) result['base_period']['units'].append( energy_category_dict[energy_category_id] ['unit_of_measure']) result['base_period']['timestamps'].append( base[energy_category_id]['timestamps']) result['base_period']['values'].append( base[energy_category_id]['values']) result['base_period']['subtotals'].append( base[energy_category_id]['subtotal']) result['base_period']['subtotals_in_kgce'].append( base[energy_category_id]['subtotal_in_kgce']) result['base_period']['subtotals_in_kgco2e'].append( base[energy_category_id]['subtotal_in_kgco2e']) result['base_period']['total_in_kgce'] += base[ energy_category_id]['subtotal_in_kgce'] result['base_period']['total_in_kgco2e'] += base[ energy_category_id]['subtotal_in_kgco2e'] result['reporting_period'] = dict() result['reporting_period']['names'] = list() result['reporting_period']['energy_category_ids'] = list() result['reporting_period']['units'] = list() result['reporting_period']['timestamps'] = list() result['reporting_period']['values'] = list() result['reporting_period']['subtotals'] = list() result['reporting_period']['subtotals_in_kgce'] = list() result['reporting_period']['subtotals_in_kgco2e'] = list() result['reporting_period']['subtotals_per_unit_area'] = list() result['reporting_period']['toppeaks'] = list() result['reporting_period']['onpeaks'] = list() result['reporting_period']['midpeaks'] = list() result['reporting_period']['offpeaks'] = list() result['reporting_period']['increment_rates'] = list() result['reporting_period']['total_in_kgce'] = Decimal(0.0) result['reporting_period']['total_in_kgco2e'] = Decimal(0.0) result['reporting_period']['increment_rate_in_kgce'] = Decimal(0.0) result['reporting_period']['increment_rate_in_kgco2e'] = Decimal(0.0) if energy_category_set is not None and len(energy_category_set) > 0: for energy_category_id in energy_category_set: result['reporting_period']['names'].append( energy_category_dict[energy_category_id]['name']) result['reporting_period']['energy_category_ids'].append( energy_category_id) result['reporting_period']['units'].append( energy_category_dict[energy_category_id] ['unit_of_measure']) result['reporting_period']['timestamps'].append( reporting[energy_category_id]['timestamps']) result['reporting_period']['values'].append( reporting[energy_category_id]['values']) result['reporting_period']['subtotals'].append( reporting[energy_category_id]['subtotal']) result['reporting_period']['subtotals_in_kgce'].append( reporting[energy_category_id]['subtotal_in_kgce']) result['reporting_period']['subtotals_in_kgco2e'].append( reporting[energy_category_id]['subtotal_in_kgco2e']) result['reporting_period']['subtotals_per_unit_area'].append( reporting[energy_category_id]['subtotal'] / store['area'] if store['area'] > 0.0 else None) result['reporting_period']['toppeaks'].append( reporting[energy_category_id]['toppeak']) result['reporting_period']['onpeaks'].append( reporting[energy_category_id]['onpeak']) result['reporting_period']['midpeaks'].append( reporting[energy_category_id]['midpeak']) result['reporting_period']['offpeaks'].append( reporting[energy_category_id]['offpeak']) result['reporting_period']['increment_rates'].append( (reporting[energy_category_id]['subtotal'] - base[energy_category_id]['subtotal']) / base[energy_category_id]['subtotal'] if base[energy_category_id]['subtotal'] > 0.0 else None) result['reporting_period']['total_in_kgce'] += reporting[ energy_category_id]['subtotal_in_kgce'] result['reporting_period']['total_in_kgco2e'] += reporting[ energy_category_id]['subtotal_in_kgco2e'] result['reporting_period']['total_in_kgco2e_per_unit_area'] = \ result['reporting_period']['total_in_kgce'] / store['area'] if store['area'] > 0.0 else None result['reporting_period']['increment_rate_in_kgce'] = \ (result['reporting_period']['total_in_kgce'] - result['base_period']['total_in_kgce']) / \ result['base_period']['total_in_kgce'] \ if result['base_period']['total_in_kgce'] > Decimal(0.0) else None result['reporting_period']['total_in_kgce_per_unit_area'] = \ result['reporting_period']['total_in_kgco2e'] / store['area'] if store['area'] > 0.0 else None result['reporting_period']['increment_rate_in_kgco2e'] = \ (result['reporting_period']['total_in_kgco2e'] - result['base_period']['total_in_kgco2e']) / \ result['base_period']['total_in_kgco2e'] \ if result['base_period']['total_in_kgco2e'] > Decimal(0.0) else None result['parameters'] = { "names": parameters_data['names'], "timestamps": parameters_data['timestamps'], "values": parameters_data['values'] } # export result to Excel file and then encode the file to base64 string result[ 'excel_bytes_base64'] = excelexporters.storeenergycategory.export( result, store['name'], reporting_start_datetime_local, reporting_end_datetime_local, period_type) resp.body = json.dumps(result)
def on_get(req, resp): print(req.params) meter_id = req.params.get('meterid') period_type = req.params.get('periodtype') base_period_start_datetime_local = req.params.get( 'baseperiodstartdatetime') base_period_end_datetime_local = req.params.get( 'baseperiodenddatetime') reporting_period_start_datetime_local = req.params.get( 'reportingperiodstartdatetime') reporting_period_end_datetime_local = req.params.get( 'reportingperiodenddatetime') ################################################################################################################ # Step 1: valid parameters ################################################################################################################ if meter_id is None: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_METER_ID') else: meter_id = str.strip(meter_id) if not meter_id.isdigit() or int(meter_id) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_METER_ID') if period_type is None: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE') else: period_type = str.strip(period_type) if period_type not in ['hourly', 'daily', 'monthly', 'yearly']: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE') timezone_offset = int(config.utc_offset[1:3]) * 60 + int( config.utc_offset[4:6]) if config.utc_offset[0] == '-': timezone_offset = -timezone_offset base_start_datetime_utc = None if base_period_start_datetime_local is not None and len( str.strip(base_period_start_datetime_local)) > 0: base_period_start_datetime_local = str.strip( base_period_start_datetime_local) try: base_start_datetime_utc = datetime.strptime( base_period_start_datetime_local, '%Y-%m-%dT%H:%M:%S') except ValueError: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description="API.INVALID_BASE_PERIOD_START_DATETIME") base_start_datetime_utc = base_start_datetime_utc.replace(tzinfo=timezone.utc) - \ timedelta(minutes=timezone_offset) base_end_datetime_utc = None if base_period_end_datetime_local is not None and len( str.strip(base_period_end_datetime_local)) > 0: base_period_end_datetime_local = str.strip( base_period_end_datetime_local) try: base_end_datetime_utc = datetime.strptime( base_period_end_datetime_local, '%Y-%m-%dT%H:%M:%S') except ValueError: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description="API.INVALID_BASE_PERIOD_END_DATETIME") base_end_datetime_utc = base_end_datetime_utc.replace(tzinfo=timezone.utc) - \ timedelta(minutes=timezone_offset) if base_start_datetime_utc is not None and base_end_datetime_utc is not None and \ base_start_datetime_utc >= base_end_datetime_utc: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_BASE_PERIOD_END_DATETIME') if reporting_period_start_datetime_local is None: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description="API.INVALID_REPORTING_PERIOD_START_DATETIME") else: reporting_period_start_datetime_local = str.strip( reporting_period_start_datetime_local) try: reporting_start_datetime_utc = datetime.strptime( reporting_period_start_datetime_local, '%Y-%m-%dT%H:%M:%S') except ValueError: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description="API.INVALID_REPORTING_PERIOD_START_DATETIME") reporting_start_datetime_utc = reporting_start_datetime_utc.replace(tzinfo=timezone.utc) - \ timedelta(minutes=timezone_offset) if reporting_period_end_datetime_local is None: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description="API.INVALID_REPORTING_PERIOD_END_DATETIME") else: reporting_period_end_datetime_local = str.strip( reporting_period_end_datetime_local) try: reporting_end_datetime_utc = datetime.strptime( reporting_period_end_datetime_local, '%Y-%m-%dT%H:%M:%S') except ValueError: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description="API.INVALID_REPORTING_PERIOD_END_DATETIME") reporting_end_datetime_utc = reporting_end_datetime_utc.replace(tzinfo=timezone.utc) - \ timedelta(minutes=timezone_offset) if reporting_start_datetime_utc >= reporting_end_datetime_utc: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_REPORTING_PERIOD_END_DATETIME') ################################################################################################################ # 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_billing = mysql.connector.connect(**config.myems_billing_db) cursor_billing = cnx_billing.cursor() cnx_historical = mysql.connector.connect(**config.myems_historical_db) cursor_historical = cnx_historical.cursor() cursor_system.execute( " SELECT m.id, m.name, m.cost_center_id, m.energy_category_id, " " ec.name, ec.unit_of_measure, ec.kgce, ec.kgco2e " " FROM tbl_meters m, tbl_energy_categories ec " " WHERE m.id = %s AND m.energy_category_id = ec.id ", (meter_id, )) 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_billing: cursor_billing.close() if cnx_billing: cnx_billing.disconnect() if cursor_historical: cursor_historical.close() if cnx_historical: cnx_historical.disconnect() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.METER_NOT_FOUND') meter = dict() meter['id'] = row_meter[0] meter['name'] = row_meter[1] meter['cost_center_id'] = row_meter[2] meter['energy_category_id'] = row_meter[3] meter['energy_category_name'] = row_meter[4] meter['unit_of_measure'] = config.currency_unit meter['kgce'] = row_meter[6] meter['kgco2e'] = row_meter[7] ################################################################################################################ # Step 3: query associated points ################################################################################################################ point_list = list() cursor_system.execute( " SELECT p.id, p.name, p.units, p.object_type " " FROM tbl_meters m, tbl_meters_points mp, tbl_points p " " WHERE m.id = %s AND m.id = mp.meter_id AND mp.point_id = p.id " " ORDER BY p.id ", (meter['id'], )) rows_points = cursor_system.fetchall() if rows_points is not None and len(rows_points) > 0: for row in rows_points: point_list.append({ "id": row[0], "name": row[1], "units": row[2], "object_type": row[3] }) ################################################################################################################ # Step 4: query base 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_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, base_end_datetime_utc, period_type) base = dict() base['timestamps'] = list() base['values'] = list() base['total_in_category'] = Decimal(0.0) base['total_in_kgce'] = Decimal(0.0) base['total_in_kgco2e'] = Decimal(0.0) for row_meter_periodically in rows_meter_periodically: current_datetime_local = row_meter_periodically[0].replace(tzinfo=timezone.utc) + \ timedelta(minutes=timezone_offset) if period_type == 'hourly': current_datetime = current_datetime_local.strftime( '%Y-%m-%dT%H:%M:%S') elif period_type == 'daily': current_datetime = current_datetime_local.strftime('%Y-%m-%d') elif period_type == 'monthly': current_datetime = current_datetime_local.strftime('%Y-%m') elif period_type == 'yearly': current_datetime = current_datetime_local.strftime('%Y') actual_value = Decimal(0.0) if row_meter_periodically[1] is None \ else row_meter_periodically[1] base['timestamps'].append(current_datetime) base['total_in_kgce'] += actual_value * meter['kgce'] base['total_in_kgco2e'] += actual_value * meter['kgco2e'] ################################################################################################################ # Step 5: query base period energy cost ################################################################################################################ 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_billing.execute( query, (meter['id'], base_start_datetime_utc, base_end_datetime_utc)) rows_meter_hourly = cursor_billing.fetchall() rows_meter_periodically = utilities.aggregate_hourly_data_by_period( rows_meter_hourly, base_start_datetime_utc, base_end_datetime_utc, period_type) base['values'] = list() base['total_in_category'] = Decimal(0.0) for row_meter_periodically in rows_meter_periodically: actual_value = Decimal(0.0) if row_meter_periodically[1] is None \ else row_meter_periodically[1] base['values'].append(actual_value) base['total_in_category'] += actual_value ################################################################################################################ # 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_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, reporting_end_datetime_utc, period_type) reporting = dict() reporting['timestamps'] = list() reporting['values'] = list() reporting['total_in_category'] = Decimal(0.0) reporting['total_in_kgce'] = Decimal(0.0) reporting['total_in_kgco2e'] = Decimal(0.0) for row_meter_periodically in rows_meter_periodically: current_datetime_local = row_meter_periodically[0].replace(tzinfo=timezone.utc) + \ timedelta(minutes=timezone_offset) if period_type == 'hourly': current_datetime = current_datetime_local.strftime( '%Y-%m-%dT%H:%M:%S') elif period_type == 'daily': current_datetime = current_datetime_local.strftime('%Y-%m-%d') elif period_type == 'monthly': current_datetime = current_datetime_local.strftime('%Y-%m') elif period_type == 'yearly': current_datetime = current_datetime_local.strftime('%Y') actual_value = Decimal(0.0) if row_meter_periodically[1] is None \ else row_meter_periodically[1] reporting['timestamps'].append(current_datetime) reporting['total_in_kgce'] += actual_value * meter['kgce'] reporting['total_in_kgco2e'] += actual_value * meter['kgco2e'] ################################################################################################################ # Step 7: query reporting period energy cost ################################################################################################################ 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_billing.execute(query, (meter['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) rows_meter_hourly = cursor_billing.fetchall() rows_meter_periodically = utilities.aggregate_hourly_data_by_period( rows_meter_hourly, reporting_start_datetime_utc, reporting_end_datetime_utc, period_type) for row_meter_periodically in rows_meter_periodically: actual_value = Decimal(0.0) if row_meter_periodically[1] is None \ else row_meter_periodically[1] reporting['values'].append(actual_value) reporting['total_in_category'] += actual_value ################################################################################################################ # Step 8: query tariff data ################################################################################################################ parameters_data = dict() parameters_data['names'] = list() parameters_data['timestamps'] = list() parameters_data['values'] = list() tariff_dict = utilities.get_energy_category_tariffs( meter['cost_center_id'], meter['energy_category_id'], reporting_start_datetime_utc, reporting_end_datetime_utc) tariff_timestamp_list = list() tariff_value_list = list() for k, v in tariff_dict.items(): # convert k from utc to local k = k + timedelta(minutes=timezone_offset) tariff_timestamp_list.append(k.isoformat()[0:19]) tariff_value_list.append(v) parameters_data['names'].append('TARIFF-' + meter['energy_category_name']) parameters_data['timestamps'].append(tariff_timestamp_list) parameters_data['values'].append(tariff_value_list) ################################################################################################################ # Step 9: query associated points data ################################################################################################################ for point in point_list: point_values = [] point_timestamps = [] if point['object_type'] == 'ANALOG_VALUE': query = (" SELECT utc_date_time, actual_value " " FROM tbl_analog_value " " WHERE point_id = %s " " AND utc_date_time BETWEEN %s AND %s " " ORDER BY utc_date_time ") cursor_historical.execute( query, (point['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) rows = cursor_historical.fetchall() if rows is not None and len(rows) > 0: for row in rows: current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ timedelta(minutes=timezone_offset) current_datetime = current_datetime_local.strftime( '%Y-%m-%dT%H:%M:%S') point_timestamps.append(current_datetime) point_values.append(row[1]) elif point['object_type'] == 'ENERGY_VALUE': query = (" SELECT utc_date_time, actual_value " " FROM tbl_energy_value " " WHERE point_id = %s " " AND utc_date_time BETWEEN %s AND %s " " ORDER BY utc_date_time ") cursor_historical.execute( query, (point['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) rows = cursor_historical.fetchall() if rows is not None and len(rows) > 0: for row in rows: current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ timedelta(minutes=timezone_offset) current_datetime = current_datetime_local.strftime( '%Y-%m-%dT%H:%M:%S') point_timestamps.append(current_datetime) point_values.append(row[1]) elif point['object_type'] == 'DIGITAL_VALUE': query = (" SELECT utc_date_time, actual_value " " FROM tbl_digital_value " " WHERE point_id = %s " " AND utc_date_time BETWEEN %s AND %s ") cursor_historical.execute( query, (point['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) rows = cursor_historical.fetchall() if rows is not None and len(rows) > 0: for row in rows: current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ timedelta(minutes=timezone_offset) current_datetime = current_datetime_local.strftime( '%Y-%m-%dT%H:%M:%S') point_timestamps.append(current_datetime) point_values.append(row[1]) parameters_data['names'].append(point['name'] + ' (' + point['units'] + ')') parameters_data['timestamps'].append(point_timestamps) parameters_data['values'].append(point_values) ################################################################################################################ # Step 10: construct the report ################################################################################################################ 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_billing: cursor_billing.close() if cnx_billing: cnx_billing.disconnect() if cursor_historical: cursor_historical.close() if cnx_historical: cnx_historical.disconnect() result = { "meter": { "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, "kgce": meter['kgce'], "kgco2e": meter['kgco2e'], }, "base_period": { "total_in_category": base['total_in_category'], "total_in_kgce": base['total_in_kgce'], "total_in_kgco2e": base['total_in_kgco2e'], "timestamps": base['timestamps'], "values": base['values'], }, "reporting_period": { "increment_rate": (reporting['total_in_category'] - base['total_in_category']) / base['total_in_category'] if base['total_in_category'] > 0 else None, "total_in_category": reporting['total_in_category'], "total_in_kgce": reporting['total_in_kgce'], "total_in_kgco2e": reporting['total_in_kgco2e'], "timestamps": reporting['timestamps'], "values": reporting['values'], }, "parameters": { "names": parameters_data['names'], "timestamps": parameters_data['timestamps'], "values": parameters_data['values'] }, } # export result to Excel file and then encode the file to base64 string result['excel_bytes_base64'] = \ excelexporters.metercost.export(result, meter['name'], reporting_period_start_datetime_local, reporting_period_end_datetime_local, period_type) resp.body = json.dumps(result)
def on_get(req, resp): print(req.params) combined_equipment_id = req.params.get('combinedequipmentid') combined_equipment_uuid = req.params.get('combinedequipmentuuid') period_type = req.params.get('periodtype') base_start_datetime_local = req.params.get('baseperiodstartdatetime') base_end_datetime_local = req.params.get('baseperiodenddatetime') reporting_start_datetime_local = req.params.get( 'reportingperiodstartdatetime') reporting_end_datetime_local = req.params.get( 'reportingperiodenddatetime') ################################################################################################################ # Step 1: valid parameters ################################################################################################################ if combined_equipment_id is None and combined_equipment_uuid is None: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_COMBINED_EQUIPMENT_ID') if combined_equipment_id is not None: combined_equipment_id = str.strip(combined_equipment_id) if not combined_equipment_id.isdigit() or int( combined_equipment_id) <= 0: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_COMBINED_EQUIPMENT_ID') if combined_equipment_uuid is not None: regex = re.compile( '^[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}\Z', re.I) match = regex.match(str.strip(combined_equipment_uuid)) if not bool(match): raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_COMBINED_EQUIPMENT_UUID') if period_type is None: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE') else: period_type = str.strip(period_type) if period_type not in [ 'hourly', 'daily', 'weekly', 'monthly', 'yearly' ]: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE') timezone_offset = int(config.utc_offset[1:3]) * 60 + int( config.utc_offset[4:6]) if config.utc_offset[0] == '-': timezone_offset = -timezone_offset base_start_datetime_utc = None if base_start_datetime_local is not None and len( str.strip(base_start_datetime_local)) > 0: base_start_datetime_local = str.strip(base_start_datetime_local) try: base_start_datetime_utc = datetime.strptime(base_start_datetime_local, '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ timedelta(minutes=timezone_offset) except ValueError: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description="API.INVALID_BASE_PERIOD_START_DATETIME") base_end_datetime_utc = None if base_end_datetime_local is not None and len( str.strip(base_end_datetime_local)) > 0: base_end_datetime_local = str.strip(base_end_datetime_local) try: base_end_datetime_utc = datetime.strptime(base_end_datetime_local, '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ timedelta(minutes=timezone_offset) except ValueError: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description="API.INVALID_BASE_PERIOD_END_DATETIME") if base_start_datetime_utc is not None and base_end_datetime_utc is not None and \ base_start_datetime_utc >= base_end_datetime_utc: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_BASE_PERIOD_END_DATETIME') if reporting_start_datetime_local is None: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description="API.INVALID_REPORTING_PERIOD_START_DATETIME") else: reporting_start_datetime_local = str.strip( reporting_start_datetime_local) try: reporting_start_datetime_utc = datetime.strptime(reporting_start_datetime_local, '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ timedelta(minutes=timezone_offset) except ValueError: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description="API.INVALID_REPORTING_PERIOD_START_DATETIME") if reporting_end_datetime_local is None: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description="API.INVALID_REPORTING_PERIOD_END_DATETIME") else: reporting_end_datetime_local = str.strip( reporting_end_datetime_local) try: reporting_end_datetime_utc = datetime.strptime(reporting_end_datetime_local, '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ timedelta(minutes=timezone_offset) except ValueError: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description="API.INVALID_REPORTING_PERIOD_END_DATETIME") if reporting_start_datetime_utc >= reporting_end_datetime_utc: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_REPORTING_PERIOD_END_DATETIME') ################################################################################################################ # Step 2: query the combined equipment ################################################################################################################ 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_historical = mysql.connector.connect(**config.myems_historical_db) cursor_historical = cnx_historical.cursor() if combined_equipment_id is not None: cursor_system.execute( " SELECT id, name, cost_center_id " " FROM tbl_combined_equipments " " WHERE id = %s ", (combined_equipment_id, )) row_combined_equipment = cursor_system.fetchone() elif combined_equipment_uuid is not None: cursor_system.execute( " SELECT id, name, cost_center_id " " FROM tbl_combined_equipments " " WHERE uuid = %s ", (combined_equipment_uuid, )) row_combined_equipment = cursor_system.fetchone() if row_combined_equipment is None: if cursor_system: cursor_system.close() if cnx_system: cnx_system.close() if cursor_energy: cursor_energy.close() if cnx_energy: cnx_energy.close() if cursor_historical: cursor_historical.close() if cnx_historical: cnx_historical.close() raise falcon.HTTPError( falcon.HTTP_404, title='API.NOT_FOUND', description='API.COMBINED_EQUIPMENT_NOT_FOUND') combined_equipment = dict() combined_equipment['id'] = row_combined_equipment[0] combined_equipment['name'] = row_combined_equipment[1] combined_equipment['cost_center_id'] = row_combined_equipment[2] ################################################################################################################ # Step 3: query energy categories ################################################################################################################ energy_category_set = set() # query energy categories in base period cursor_energy.execute( " SELECT DISTINCT(energy_category_id) " " FROM tbl_combined_equipment_output_category_hourly " " WHERE combined_equipment_id = %s " " AND start_datetime_utc >= %s " " AND start_datetime_utc < %s ", (combined_equipment['id'], base_start_datetime_utc, base_end_datetime_utc)) rows_energy_categories = cursor_energy.fetchall() if rows_energy_categories is not None or len( rows_energy_categories) > 0: for row_energy_category in rows_energy_categories: energy_category_set.add(row_energy_category[0]) # query energy categories in reporting period cursor_energy.execute( " SELECT DISTINCT(energy_category_id) " " FROM tbl_combined_equipment_output_category_hourly " " WHERE combined_equipment_id = %s " " AND start_datetime_utc >= %s " " AND start_datetime_utc < %s ", (combined_equipment['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) rows_energy_categories = cursor_energy.fetchall() if rows_energy_categories is not None or len( rows_energy_categories) > 0: for row_energy_category in rows_energy_categories: energy_category_set.add(row_energy_category[0]) # query all energy categories in base period and reporting period cursor_system.execute( " SELECT id, name, unit_of_measure, kgce, kgco2e " " FROM tbl_energy_categories " " ORDER BY id ", ) rows_energy_categories = cursor_system.fetchall() if rows_energy_categories is None or len(rows_energy_categories) == 0: if cursor_system: cursor_system.close() if cnx_system: cnx_system.close() if cursor_energy: cursor_energy.close() if cnx_energy: cnx_energy.close() if cursor_historical: cursor_historical.close() if cnx_historical: cnx_historical.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.ENERGY_CATEGORY_NOT_FOUND') energy_category_dict = dict() for row_energy_category in rows_energy_categories: if row_energy_category[0] in energy_category_set: energy_category_dict[row_energy_category[0]] = { "name": row_energy_category[1], "unit_of_measure": row_energy_category[2], "kgce": row_energy_category[3], "kgco2e": row_energy_category[4] } ################################################################################################################ # Step 4: query associated points ################################################################################################################ point_list = list() cursor_system.execute( " SELECT p.id, ep.name, p.units, p.object_type " " FROM tbl_combined_equipments e, tbl_combined_equipments_parameters ep, tbl_points p " " WHERE e.id = %s AND e.id = ep.combined_equipment_id AND ep.parameter_type = 'point' " " AND ep.point_id = p.id " " ORDER BY p.id ", (combined_equipment['id'], )) rows_points = cursor_system.fetchall() if rows_points is not None and len(rows_points) > 0: for row in rows_points: point_list.append({ "id": row[0], "name": row[1], "units": row[2], "object_type": row[3] }) ################################################################################################################ # 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] }) ################################################################################################################ # Step 6: query base period energy output ################################################################################################################ base = dict() if energy_category_set is not None and len(energy_category_set) > 0: for energy_category_id in energy_category_set: base[energy_category_id] = dict() base[energy_category_id]['timestamps'] = list() base[energy_category_id]['values'] = list() base[energy_category_id]['subtotal'] = Decimal(0.0) cursor_energy.execute( " SELECT start_datetime_utc, actual_value " " FROM tbl_combined_equipment_output_category_hourly " " WHERE combined_equipment_id = %s " " AND energy_category_id = %s " " AND start_datetime_utc >= %s " " AND start_datetime_utc < %s " " ORDER BY start_datetime_utc ", (combined_equipment['id'], energy_category_id, base_start_datetime_utc, base_end_datetime_utc)) rows_combined_equipment_hourly = cursor_energy.fetchall() rows_combined_equipment_periodically = \ utilities.aggregate_hourly_data_by_period(rows_combined_equipment_hourly, base_start_datetime_utc, base_end_datetime_utc, period_type) for row_combined_equipment_periodically in rows_combined_equipment_periodically: current_datetime_local = row_combined_equipment_periodically[0].replace(tzinfo=timezone.utc) + \ timedelta(minutes=timezone_offset) if period_type == 'hourly': current_datetime = current_datetime_local.strftime( '%Y-%m-%dT%H:%M:%S') elif period_type == 'daily': current_datetime = current_datetime_local.strftime( '%Y-%m-%d') elif period_type == 'weekly': current_datetime = current_datetime_local.strftime( '%Y-%m-%d') elif period_type == 'monthly': current_datetime = current_datetime_local.strftime( '%Y-%m') elif period_type == 'yearly': current_datetime = current_datetime_local.strftime( '%Y') actual_value = Decimal(0.0) if row_combined_equipment_periodically[1] is None \ else row_combined_equipment_periodically[1] base[energy_category_id]['timestamps'].append( current_datetime) base[energy_category_id]['values'].append(actual_value) base[energy_category_id]['subtotal'] += actual_value ################################################################################################################ # Step 7: query reporting period energy output ################################################################################################################ reporting = dict() if energy_category_set is not None and len(energy_category_set) > 0: for energy_category_id in energy_category_set: reporting[energy_category_id] = dict() reporting[energy_category_id]['timestamps'] = list() reporting[energy_category_id]['values'] = list() reporting[energy_category_id]['subtotal'] = Decimal(0.0) cursor_energy.execute( " SELECT start_datetime_utc, actual_value " " FROM tbl_combined_equipment_output_category_hourly " " WHERE combined_equipment_id = %s " " AND energy_category_id = %s " " AND start_datetime_utc >= %s " " AND start_datetime_utc < %s " " ORDER BY start_datetime_utc ", (combined_equipment['id'], energy_category_id, reporting_start_datetime_utc, reporting_end_datetime_utc)) rows_combined_equipment_hourly = cursor_energy.fetchall() rows_combined_equipment_periodically = \ utilities.aggregate_hourly_data_by_period(rows_combined_equipment_hourly, reporting_start_datetime_utc, reporting_end_datetime_utc, period_type) for row_combined_equipment_periodically in rows_combined_equipment_periodically: current_datetime_local = row_combined_equipment_periodically[0].replace(tzinfo=timezone.utc) + \ timedelta(minutes=timezone_offset) if period_type == 'hourly': current_datetime = current_datetime_local.strftime( '%Y-%m-%dT%H:%M:%S') elif period_type == 'daily': current_datetime = current_datetime_local.strftime( '%Y-%m-%d') elif period_type == 'weekly': current_datetime = current_datetime_local.strftime( '%Y-%m-%d') elif period_type == 'monthly': current_datetime = current_datetime_local.strftime( '%Y-%m') elif period_type == 'yearly': current_datetime = current_datetime_local.strftime( '%Y') actual_value = Decimal(0.0) if row_combined_equipment_periodically[1] is None \ else row_combined_equipment_periodically[1] reporting[energy_category_id]['timestamps'].append( current_datetime) reporting[energy_category_id]['values'].append( actual_value) reporting[energy_category_id]['subtotal'] += actual_value ################################################################################################################ # Step 8: query tariff data ################################################################################################################ parameters_data = dict() parameters_data['names'] = list() parameters_data['timestamps'] = list() parameters_data['values'] = list() if energy_category_set is not None and len(energy_category_set) > 0: for energy_category_id in energy_category_set: energy_category_tariff_dict = \ utilities.get_energy_category_tariffs(combined_equipment['cost_center_id'], energy_category_id, reporting_start_datetime_utc, reporting_end_datetime_utc) tariff_timestamp_list = list() tariff_value_list = list() for k, v in energy_category_tariff_dict.items(): # convert k from utc to local k = k + timedelta(minutes=timezone_offset) tariff_timestamp_list.append(k.isoformat()[0:19][0:19]) tariff_value_list.append(v) parameters_data['names'].append( 'TARIFF-' + energy_category_dict[energy_category_id]['name']) parameters_data['timestamps'].append(tariff_timestamp_list) parameters_data['values'].append(tariff_value_list) ################################################################################################################ # Step 9: query associated points data ################################################################################################################ for point in point_list: point_values = [] point_timestamps = [] if point['object_type'] == 'ANALOG_VALUE': query = (" SELECT utc_date_time, actual_value " " FROM tbl_analog_value " " WHERE point_id = %s " " AND utc_date_time BETWEEN %s AND %s " " ORDER BY utc_date_time ") cursor_historical.execute( query, (point['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) rows = cursor_historical.fetchall() if rows is not None and len(rows) > 0: for row in rows: current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ timedelta(minutes=timezone_offset) current_datetime = current_datetime_local.strftime( '%Y-%m-%dT%H:%M:%S') point_timestamps.append(current_datetime) point_values.append(row[1]) elif point['object_type'] == 'ENERGY_VALUE': query = (" SELECT utc_date_time, actual_value " " FROM tbl_energy_value " " WHERE point_id = %s " " AND utc_date_time BETWEEN %s AND %s " " ORDER BY utc_date_time ") cursor_historical.execute( query, (point['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) rows = cursor_historical.fetchall() if rows is not None and len(rows) > 0: for row in rows: current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ timedelta(minutes=timezone_offset) current_datetime = current_datetime_local.strftime( '%Y-%m-%dT%H:%M:%S') point_timestamps.append(current_datetime) point_values.append(row[1]) elif point['object_type'] == 'DIGITAL_VALUE': query = (" SELECT utc_date_time, actual_value " " FROM tbl_digital_value " " WHERE point_id = %s " " AND utc_date_time BETWEEN %s AND %s " " ORDER BY utc_date_time ") cursor_historical.execute( query, (point['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) rows = cursor_historical.fetchall() if rows is not None and len(rows) > 0: for row in rows: current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ timedelta(minutes=timezone_offset) current_datetime = current_datetime_local.strftime( '%Y-%m-%dT%H:%M:%S') point_timestamps.append(current_datetime) point_values.append(row[1]) parameters_data['names'].append(point['name'] + ' (' + point['units'] + ')') parameters_data['timestamps'].append(point_timestamps) parameters_data['values'].append(point_values) ################################################################################################################ # Step 10: query associated equipments energy output ################################################################################################################ associated_equipment_data = dict() if energy_category_set is not None and len(energy_category_set) > 0: for energy_category_id in energy_category_set: associated_equipment_data[energy_category_id] = dict() associated_equipment_data[energy_category_id][ 'associated_equipment_names'] = list() associated_equipment_data[energy_category_id][ 'subtotals'] = list() for associated_equipment in associated_equipment_list: associated_equipment_data[energy_category_id][ 'associated_equipment_names'].append( associated_equipment['name']) cursor_energy.execute( " SELECT SUM(actual_value) " " FROM tbl_equipment_output_category_hourly " " WHERE equipment_id = %s " " AND energy_category_id = %s " " AND start_datetime_utc >= %s " " AND start_datetime_utc < %s " " ORDER BY start_datetime_utc ", (associated_equipment['id'], energy_category_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_category_id][ 'subtotals'].append(subtotal) ################################################################################################################ # Step 11: construct the report ################################################################################################################ if cursor_system: cursor_system.close() if cnx_system: cnx_system.close() if cursor_energy: cursor_energy.close() if cnx_energy: cnx_energy.close() if cursor_historical: cursor_historical.close() if cnx_historical: cnx_historical.close() result = dict() result['combined_equipment'] = dict() result['combined_equipment']['name'] = combined_equipment['name'] result['base_period'] = dict() result['base_period']['names'] = list() result['base_period']['units'] = list() result['base_period']['timestamps'] = list() result['base_period']['values'] = list() result['base_period']['subtotals'] = list() if energy_category_set is not None and len(energy_category_set) > 0: for energy_category_id in energy_category_set: result['base_period']['names'].append( energy_category_dict[energy_category_id]['name']) result['base_period']['units'].append( energy_category_dict[energy_category_id] ['unit_of_measure']) result['base_period']['timestamps'].append( base[energy_category_id]['timestamps']) result['base_period']['values'].append( base[energy_category_id]['values']) result['base_period']['subtotals'].append( base[energy_category_id]['subtotal']) result['reporting_period'] = dict() result['reporting_period']['names'] = list() result['reporting_period']['energy_category_ids'] = list() result['reporting_period']['units'] = list() result['reporting_period']['timestamps'] = list() result['reporting_period']['values'] = list() result['reporting_period']['subtotals'] = list() result['reporting_period']['increment_rates'] = list() if energy_category_set is not None and len(energy_category_set) > 0: for energy_category_id in energy_category_set: result['reporting_period']['names'].append( energy_category_dict[energy_category_id]['name']) result['reporting_period']['energy_category_ids'].append( energy_category_id) result['reporting_period']['units'].append( energy_category_dict[energy_category_id] ['unit_of_measure']) result['reporting_period']['timestamps'].append( reporting[energy_category_id]['timestamps']) result['reporting_period']['values'].append( reporting[energy_category_id]['values']) result['reporting_period']['subtotals'].append( reporting[energy_category_id]['subtotal']) result['reporting_period']['increment_rates'].append( (reporting[energy_category_id]['subtotal'] - base[energy_category_id]['subtotal']) / base[energy_category_id]['subtotal'] if base[energy_category_id]['subtotal'] > 0.0 else None) result['parameters'] = { "names": parameters_data['names'], "timestamps": parameters_data['timestamps'], "values": parameters_data['values'] } result['associated_equipment'] = dict() result['associated_equipment']['energy_category_names'] = list() result['associated_equipment']['units'] = list() result['associated_equipment'][ 'associated_equipment_names_array'] = list() result['associated_equipment']['subtotals_array'] = list() if energy_category_set is not None and len(energy_category_set) > 0: for energy_category_id in energy_category_set: result['associated_equipment']['energy_category_names'].append( energy_category_dict[energy_category_id]['name']) result['associated_equipment']['units'].append( energy_category_dict[energy_category_id] ['unit_of_measure']) result['associated_equipment'][ 'associated_equipment_names_array'].append( associated_equipment_data[energy_category_id] ['associated_equipment_names']) result['associated_equipment']['subtotals_array'].append( associated_equipment_data[energy_category_id]['subtotals']) # export result to Excel file and then encode the file to base64 string result[ 'excel_bytes_base64'] = excelexporters.combinedequipmentoutput.export( result, combined_equipment['name'], reporting_start_datetime_local, reporting_end_datetime_local, period_type) resp.text = json.dumps(result)
def on_get(req, resp): print(req.params) offline_meter_id = req.params.get('offlinemeterid') period_type = req.params.get('periodtype') base_period_start_datetime = req.params.get('baseperiodstartdatetime') base_period_end_datetime = req.params.get('baseperiodenddatetime') reporting_period_start_datetime = req.params.get( 'reportingperiodstartdatetime') reporting_period_end_datetime = req.params.get( 'reportingperiodenddatetime') ################################################################################################################ # Step 1: valid parameters ################################################################################################################ if offline_meter_id is None: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_OFFLINE_METER_ID') else: offline_meter_id = str.strip(offline_meter_id) if not offline_meter_id.isdigit() or int(offline_meter_id) <= 0: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_OFFLINE_METER_ID') if period_type is None: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE') else: period_type = str.strip(period_type) if period_type not in ['hourly', 'daily', 'monthly', 'yearly']: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE') timezone_offset = int(config.utc_offset[1:3]) * 60 + int( config.utc_offset[4:6]) if config.utc_offset[0] == '-': timezone_offset = -timezone_offset base_start_datetime_utc = None if base_period_start_datetime is not None and len( str.strip(base_period_start_datetime)) > 0: base_period_start_datetime = str.strip(base_period_start_datetime) try: base_start_datetime_utc = datetime.strptime( base_period_start_datetime, '%Y-%m-%dT%H:%M:%S') except ValueError: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description="API.INVALID_BASE_PERIOD_BEGINS_DATETIME") base_start_datetime_utc = base_start_datetime_utc.replace(tzinfo=timezone.utc) - \ timedelta(minutes=timezone_offset) base_end_datetime_utc = None if base_period_end_datetime is not None and len( str.strip(base_period_end_datetime)) > 0: base_period_end_datetime = str.strip(base_period_end_datetime) try: base_end_datetime_utc = datetime.strptime( base_period_end_datetime, '%Y-%m-%dT%H:%M:%S') except ValueError: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description="API.INVALID_BASE_PERIOD_ENDS_DATETIME") base_end_datetime_utc = base_end_datetime_utc.replace(tzinfo=timezone.utc) - \ timedelta(minutes=timezone_offset) if base_start_datetime_utc is not None and base_end_datetime_utc is not None and \ base_start_datetime_utc >= base_end_datetime_utc: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_BASE_PERIOD_ENDS_DATETIME') if reporting_period_start_datetime is None: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description="API.INVALID_REPORTING_PERIOD_BEGINS_DATETIME") else: reporting_period_start_datetime = str.strip( reporting_period_start_datetime) try: reporting_start_datetime_utc = datetime.strptime( reporting_period_start_datetime, '%Y-%m-%dT%H:%M:%S') except ValueError: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description="API.INVALID_REPORTING_PERIOD_BEGINS_DATETIME") reporting_start_datetime_utc = reporting_start_datetime_utc.replace(tzinfo=timezone.utc) - \ timedelta(minutes=timezone_offset) if reporting_period_end_datetime is None: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description="API.INVALID_REPORTING_PERIOD_ENDS_DATETIME") else: reporting_period_end_datetime = str.strip( reporting_period_end_datetime) try: reporting_end_datetime_utc = datetime.strptime( reporting_period_end_datetime, '%Y-%m-%dT%H:%M:%S') except ValueError: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description="API.INVALID_REPORTING_PERIOD_ENDS_DATETIME") reporting_end_datetime_utc = reporting_end_datetime_utc.replace(tzinfo=timezone.utc) - \ timedelta(minutes=timezone_offset) if reporting_start_datetime_utc >= reporting_end_datetime_utc: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_REPORTING_PERIOD_ENDS_DATETIME') ################################################################################################################ # Step 2: query the offline 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() cursor_system.execute( " SELECT m.id, m.name, m.cost_center_id, m.energy_category_id, " " ec.name, ec.unit_of_measure, ec.kgce, ec.kgco2e " " FROM tbl_offline_meters m, tbl_energy_categories ec " " WHERE m.id = %s AND m.energy_category_id = ec.id ", (offline_meter_id, )) row_offline_meter = cursor_system.fetchone() if row_offline_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() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.OFFLINE_METER_NOT_FOUND') offline_meter = dict() offline_meter['id'] = row_offline_meter[0] offline_meter['name'] = row_offline_meter[1] offline_meter['cost_center_id'] = row_offline_meter[2] offline_meter['energy_category_id'] = row_offline_meter[3] offline_meter['energy_category_name'] = row_offline_meter[4] offline_meter['unit_of_measure'] = row_offline_meter[5] offline_meter['kgce'] = row_offline_meter[6] offline_meter['kgco2e'] = row_offline_meter[7] ################################################################################################################ # Step 3: query base period energy consumption ################################################################################################################ query = (" SELECT start_datetime_utc, actual_value " " FROM tbl_offline_meter_hourly " " WHERE offline_meter_id = %s " " AND start_datetime_utc >= %s " " AND start_datetime_utc < %s " " ORDER BY start_datetime_utc ") cursor_energy.execute(query, (offline_meter['id'], base_start_datetime_utc, base_end_datetime_utc)) rows_offline_meter_hourly = cursor_energy.fetchall() rows_offline_meter_periodically = utilities.aggregate_hourly_data_by_period( rows_offline_meter_hourly, base_start_datetime_utc, base_end_datetime_utc, period_type) base = dict() base['timestamps'] = list() base['values'] = list() base['total_in_category'] = Decimal(0.0) base['total_in_kgce'] = Decimal(0.0) base['total_in_kgco2e'] = Decimal(0.0) for row_offline_meter_periodically in rows_offline_meter_periodically: current_datetime_local = row_offline_meter_periodically[0].replace(tzinfo=timezone.utc) + \ timedelta(minutes=timezone_offset) if period_type == 'hourly': current_datetime = current_datetime_local.strftime( '%Y-%m-%dT%H:%M:%S') elif period_type == 'daily': current_datetime = current_datetime_local.strftime('%Y-%m-%d') elif period_type == 'monthly': current_datetime = current_datetime_local.strftime('%Y-%m') elif period_type == 'yearly': current_datetime = current_datetime_local.strftime('%Y') actual_value = Decimal(0.0) if row_offline_meter_periodically[1] is None \ else row_offline_meter_periodically[1] base['timestamps'].append(current_datetime) base['values'].append(actual_value) base['total_in_category'] += actual_value base['total_in_kgce'] += actual_value * offline_meter['kgce'] base['total_in_kgco2e'] += actual_value * offline_meter['kgco2e'] ################################################################################################################ # Step 4: query reporting period energy consumption ################################################################################################################ query = (" SELECT start_datetime_utc, actual_value " " FROM tbl_offline_meter_hourly " " WHERE offline_meter_id = %s " " AND start_datetime_utc >= %s " " AND start_datetime_utc < %s " " ORDER BY start_datetime_utc ") cursor_energy.execute( query, (offline_meter['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) rows_offline_meter_hourly = cursor_energy.fetchall() rows_offline_meter_periodically = utilities.aggregate_hourly_data_by_period( rows_offline_meter_hourly, reporting_start_datetime_utc, reporting_end_datetime_utc, period_type) reporting = dict() reporting['timestamps'] = list() reporting['values'] = list() reporting['total_in_category'] = Decimal(0.0) reporting['total_in_kgce'] = Decimal(0.0) reporting['total_in_kgco2e'] = Decimal(0.0) for row_offline_meter_periodically in rows_offline_meter_periodically: current_datetime_local = row_offline_meter_periodically[0].replace(tzinfo=timezone.utc) + \ timedelta(minutes=timezone_offset) if period_type == 'hourly': current_datetime = current_datetime_local.strftime( '%Y-%m-%dT%H:%M:%S') elif period_type == 'daily': current_datetime = current_datetime_local.strftime('%Y-%m-%d') elif period_type == 'monthly': current_datetime = current_datetime_local.strftime('%Y-%m') elif period_type == 'yearly': current_datetime = current_datetime_local.strftime('%Y') actual_value = Decimal(0.0) if row_offline_meter_periodically[1] is None \ else row_offline_meter_periodically[1] reporting['timestamps'].append(current_datetime) reporting['values'].append(actual_value) reporting['total_in_category'] += actual_value reporting['total_in_kgce'] += actual_value * offline_meter['kgce'] reporting[ 'total_in_kgco2e'] += actual_value * offline_meter['kgco2e'] ################################################################################################################ # Step 5: query tariff data ################################################################################################################ parameters_data = dict() parameters_data['names'] = list() parameters_data['timestamps'] = list() parameters_data['values'] = list() tariff_dict = utilities.get_energy_category_tariffs( offline_meter['cost_center_id'], offline_meter['energy_category_id'], reporting_start_datetime_utc, reporting_end_datetime_utc) tariff_timestamp_list = list() tariff_value_list = list() for k, v in tariff_dict.items(): # convert k from utc to local k = k + timedelta(minutes=timezone_offset) tariff_timestamp_list.append(k.isoformat()[0:19]) tariff_value_list.append(v) parameters_data['names'].append('TARIFF-' + offline_meter['energy_category_name']) parameters_data['timestamps'].append(tariff_timestamp_list) parameters_data['values'].append(tariff_value_list) ################################################################################################################ # Step 6: construct the report ################################################################################################################ if cursor_system: cursor_system.close() if cnx_system: cnx_system.disconnect() if cursor_energy: cursor_energy.close() if cnx_energy: cnx_energy.disconnect() result = { "offline_meter": { "cost_center_id": offline_meter['cost_center_id'], "energy_category_id": offline_meter['energy_category_id'], "energy_category_name": offline_meter['energy_category_name'], "unit_of_measure": offline_meter['unit_of_measure'], "kgce": offline_meter['kgce'], "kgco2e": offline_meter['kgco2e'], }, "base_period": { "total_in_category": base['total_in_category'], "total_in_kgce": base['total_in_kgce'], "total_in_kgco2e": base['total_in_kgco2e'], "timestamps": base['timestamps'], "values": base['values'], }, "reporting_period": { "increment_rate": (reporting['total_in_category'] - base['total_in_category']) / base['total_in_category'] if base['total_in_category'] > 0 else None, "total_in_category": reporting['total_in_category'], "total_in_kgce": reporting['total_in_kgce'], "total_in_kgco2e": reporting['total_in_kgco2e'], "timestamps": reporting['timestamps'], "values": reporting['values'], }, "parameters": { "names": parameters_data['names'], "timestamps": parameters_data['timestamps'], "values": parameters_data['values'] }, } resp.body = json.dumps(result)
def on_get(req, resp): print(req.params) tenant_id = req.params.get('tenantid') tenant_uuid = req.params.get('tenantuuid') reporting_start_datetime_local = req.params.get('reportingperiodstartdatetime') reporting_end_datetime_local = req.params.get('reportingperiodenddatetime') # This value is intentionally left daily period_type = 'daily' ################################################################################################################ # Step 1: valid parameters ################################################################################################################ if tenant_id is None and tenant_uuid is None: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_TENANT_ID') if tenant_id is not None: tenant_id = str.strip(tenant_id) if not tenant_id.isdigit() or int(tenant_id) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_TENANT_ID') if tenant_uuid is not None: regex = re.compile('^[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}\Z', re.I) match = regex.match(str.strip(tenant_uuid)) if not bool(match): raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_TENANT_UUID') timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) if config.utc_offset[0] == '-': timezone_offset = -timezone_offset if reporting_start_datetime_local is None: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description="API.INVALID_REPORTING_PERIOD_START_DATETIME") else: reporting_start_datetime_local = str.strip(reporting_start_datetime_local) try: reporting_start_datetime_utc = datetime.strptime(reporting_start_datetime_local, '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ timedelta(minutes=timezone_offset) except ValueError: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description="API.INVALID_REPORTING_PERIOD_START_DATETIME") if reporting_end_datetime_local is None: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description="API.INVALID_REPORTING_PERIOD_END_DATETIME") else: reporting_end_datetime_local = str.strip(reporting_end_datetime_local) try: reporting_end_datetime_utc = datetime.strptime(reporting_end_datetime_local, '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ timedelta(minutes=timezone_offset) except ValueError: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description="API.INVALID_REPORTING_PERIOD_END_DATETIME") if reporting_start_datetime_utc >= reporting_end_datetime_utc: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_REPORTING_PERIOD_END_DATETIME') ################################################################################################################ # Step 2: query the tenant ################################################################################################################ 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_billing = mysql.connector.connect(**config.myems_billing_db) cursor_billing = cnx_billing.cursor() if tenant_id is not None: cursor_system.execute(" SELECT t.id, t.name, t.buildings, t.floors, t.rooms, t.lease_number, " " c.email, c.phone, cost_center_id " " FROM tbl_tenants t, tbl_contacts c " " WHERE t.id = %s AND t.contact_id = c.id ", (tenant_id,)) row_tenant = cursor_system.fetchone() elif tenant_uuid is not None: cursor_system.execute(" SELECT t.id, t.name, t.buildings, t.floors, t.rooms, t.lease_number, " " c.email, c.phone, cost_center_id " " FROM tbl_tenants t, tbl_contacts c " " WHERE t.uuid = %s AND t.contact_id = c.id ", (tenant_uuid,)) row_tenant = cursor_system.fetchone() if row_tenant is None: if cursor_system: cursor_system.close() if cnx_system: cnx_system.close() if cursor_energy: cursor_energy.close() if cnx_energy: cnx_energy.close() if cursor_billing: cursor_billing.close() if cnx_billing: cnx_billing.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.TENANT_NOT_FOUND') tenant = dict() tenant['id'] = row_tenant[0] tenant['name'] = row_tenant[1] tenant['buildings'] = row_tenant[2] tenant['floors'] = row_tenant[3] tenant['rooms'] = row_tenant[4] tenant['lease_number'] = row_tenant[5] tenant['email'] = row_tenant[6] tenant['phone'] = row_tenant[7] tenant['cost_center_id'] = row_tenant[8] ################################################################################################################ # Step 3: query energy categories ################################################################################################################ energy_category_set = set() # query energy categories in reporting period cursor_billing.execute(" SELECT DISTINCT(energy_category_id) " " FROM tbl_tenant_input_category_hourly " " WHERE tenant_id = %s " " AND start_datetime_utc >= %s " " AND start_datetime_utc < %s ", (tenant['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) rows_energy_categories = cursor_billing.fetchall() if rows_energy_categories is not None or len(rows_energy_categories) > 0: for row_energy_category in rows_energy_categories: energy_category_set.add(row_energy_category[0]) # query all energy categories cursor_system.execute(" SELECT id, name, unit_of_measure, kgce, kgco2e " " FROM tbl_energy_categories " " ORDER BY id ", ) rows_energy_categories = cursor_system.fetchall() if rows_energy_categories is None or len(rows_energy_categories) == 0: if cursor_system: cursor_system.close() if cnx_system: cnx_system.close() if cursor_energy: cursor_energy.close() if cnx_energy: cnx_energy.close() if cursor_billing: cursor_billing.close() if cnx_billing: cnx_billing.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.ENERGY_CATEGORY_NOT_FOUND') energy_category_dict = dict() for row_energy_category in rows_energy_categories: if row_energy_category[0] in energy_category_set: energy_category_dict[row_energy_category[0]] = {"name": row_energy_category[1], "unit_of_measure": row_energy_category[2], "kgce": row_energy_category[3], "kgco2e": row_energy_category[4]} ################################################################################################################ # Step 4: query reporting period energy input ################################################################################################################ reporting_input = dict() if energy_category_set is not None and len(energy_category_set) > 0: for energy_category_id in energy_category_set: reporting_input[energy_category_id] = dict() reporting_input[energy_category_id]['timestamps'] = list() reporting_input[energy_category_id]['values'] = list() reporting_input[energy_category_id]['subtotal'] = Decimal(0.0) cursor_energy.execute(" SELECT start_datetime_utc, actual_value " " FROM tbl_tenant_input_category_hourly " " WHERE tenant_id = %s " " AND energy_category_id = %s " " AND start_datetime_utc >= %s " " AND start_datetime_utc < %s " " ORDER BY start_datetime_utc ", (tenant['id'], energy_category_id, reporting_start_datetime_utc, reporting_end_datetime_utc)) rows_tenant_hourly = cursor_energy.fetchall() rows_tenant_periodically = utilities.aggregate_hourly_data_by_period(rows_tenant_hourly, reporting_start_datetime_utc, reporting_end_datetime_utc, period_type) for row_tenant_periodically in rows_tenant_periodically: current_datetime_local = row_tenant_periodically[0].replace(tzinfo=timezone.utc) + \ timedelta(minutes=timezone_offset) if period_type == 'hourly': current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') elif period_type == 'daily': current_datetime = current_datetime_local.strftime('%Y-%m-%d') elif period_type == 'weekly': current_datetime = current_datetime_local.strftime('%Y-%m-%d') elif period_type == 'monthly': current_datetime = current_datetime_local.strftime('%Y-%m') elif period_type == 'yearly': current_datetime = current_datetime_local.strftime('%Y') actual_value = Decimal(0.0) if row_tenant_periodically[1] is None else row_tenant_periodically[1] reporting_input[energy_category_id]['timestamps'].append(current_datetime) reporting_input[energy_category_id]['values'].append(actual_value) reporting_input[energy_category_id]['subtotal'] += actual_value ################################################################################################################ # Step 5: query reporting period energy cost ################################################################################################################ reporting_cost = dict() if energy_category_set is not None and len(energy_category_set) > 0: for energy_category_id in energy_category_set: reporting_cost[energy_category_id] = dict() reporting_cost[energy_category_id]['timestamps'] = list() reporting_cost[energy_category_id]['values'] = list() reporting_cost[energy_category_id]['subtotal'] = Decimal(0.0) cursor_billing.execute(" SELECT start_datetime_utc, actual_value " " FROM tbl_tenant_input_category_hourly " " WHERE tenant_id = %s " " AND energy_category_id = %s " " AND start_datetime_utc >= %s " " AND start_datetime_utc < %s " " ORDER BY start_datetime_utc ", (tenant['id'], energy_category_id, reporting_start_datetime_utc, reporting_end_datetime_utc)) rows_tenant_hourly = cursor_billing.fetchall() rows_tenant_periodically = utilities.aggregate_hourly_data_by_period(rows_tenant_hourly, reporting_start_datetime_utc, reporting_end_datetime_utc, period_type) for row_tenant_periodically in rows_tenant_periodically: current_datetime_local = row_tenant_periodically[0].replace(tzinfo=timezone.utc) + \ timedelta(minutes=timezone_offset) if period_type == 'hourly': current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S') elif period_type == 'daily': current_datetime = current_datetime_local.strftime('%Y-%m-%d') elif period_type == 'weekly': current_datetime = current_datetime_local.strftime('%Y-%m-%d') elif period_type == 'monthly': current_datetime = current_datetime_local.strftime('%Y-%m') elif period_type == 'yearly': current_datetime = current_datetime_local.strftime('%Y') actual_value = Decimal(0.0) if row_tenant_periodically[1] is None else row_tenant_periodically[1] reporting_cost[energy_category_id]['timestamps'].append(current_datetime) reporting_cost[energy_category_id]['values'].append(actual_value) reporting_cost[energy_category_id]['subtotal'] += actual_value ################################################################################################################ # Step 6: query tariff data ################################################################################################################ parameters_data = dict() parameters_data['names'] = list() parameters_data['timestamps'] = list() parameters_data['values'] = list() if energy_category_set is not None and len(energy_category_set) > 0: for energy_category_id in energy_category_set: energy_category_tariff_dict = utilities.get_energy_category_tariffs(tenant['cost_center_id'], energy_category_id, reporting_start_datetime_utc, reporting_end_datetime_utc) tariff_timestamp_list = list() tariff_value_list = list() for k, v in energy_category_tariff_dict.items(): # convert k from utc to local k = k + timedelta(minutes=timezone_offset) tariff_timestamp_list.append(k.isoformat()[0:19][0:19]) tariff_value_list.append(v) parameters_data['names'].append('TARIFF-' + energy_category_dict[energy_category_id]['name']) parameters_data['timestamps'].append(tariff_timestamp_list) parameters_data['values'].append(tariff_value_list) ################################################################################################################ # Step 7: construct the report ################################################################################################################ if cursor_system: cursor_system.close() if cnx_system: cnx_system.close() if cursor_energy: cursor_energy.close() if cnx_energy: cnx_energy.close() if cursor_billing: cursor_billing.close() if cnx_billing: cnx_billing.close() result = dict() result['tenant'] = dict() result['tenant']['name'] = tenant['name'] result['tenant']['buildings'] = tenant['buildings'] result['tenant']['floors'] = tenant['floors'] result['tenant']['rooms'] = tenant['rooms'] result['tenant']['lease_number'] = tenant['lease_number'] result['tenant']['email'] = tenant['email'] result['tenant']['phone'] = tenant['phone'] result['reporting_period'] = dict() result['reporting_period']['names'] = list() result['reporting_period']['energy_category_ids'] = list() result['reporting_period']['units'] = list() result['reporting_period']['subtotals_input'] = list() result['reporting_period']['subtotals_cost'] = list() result['reporting_period']['total_cost'] = Decimal(0.0) result['reporting_period']['currency_unit'] = config.currency_unit if energy_category_set is not None and len(energy_category_set) > 0: for energy_category_id in energy_category_set: result['reporting_period']['names'].append(energy_category_dict[energy_category_id]['name']) result['reporting_period']['energy_category_ids'].append(energy_category_id) result['reporting_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) result['reporting_period']['subtotals_input'].append(reporting_input[energy_category_id]['subtotal']) result['reporting_period']['subtotals_cost'].append(reporting_cost[energy_category_id]['subtotal']) result['reporting_period']['total_cost'] += reporting_cost[energy_category_id]['subtotal'] result['parameters'] = { "names": parameters_data['names'], "timestamps": parameters_data['timestamps'], "values": parameters_data['values'] } # export result to Excel file and then encode the file to base64 string result['excel_bytes_base64'] = excelexporters.tenantbill.export(result, tenant['name'], reporting_start_datetime_local, reporting_end_datetime_local, period_type) resp.text = json.dumps(result)
def on_get(req, resp): print(req.params) meter_id = req.params.get('meterid') period_type = req.params.get('periodtype') reporting_period_start_datetime_local = req.params.get( 'reportingperiodstartdatetime') reporting_period_end_datetime_local = req.params.get( 'reportingperiodenddatetime') ################################################################################################################ # Step 1: valid parameters ################################################################################################################ if meter_id is None: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_METER_ID') else: meter_id = str.strip(meter_id) if not meter_id.isdigit() or int(meter_id) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_METER_ID') if period_type is None: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE') else: period_type = str.strip(period_type) if period_type not in ['hourly', 'daily', 'monthly', 'yearly']: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_PERIOD_TYPE') timezone_offset = int(config.utc_offset[1:3]) * 60 + int( config.utc_offset[4:6]) if config.utc_offset[0] == '-': timezone_offset = -timezone_offset if reporting_period_start_datetime_local is None: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description="API.INVALID_REPORTING_PERIOD_START_DATETIME") else: reporting_period_start_datetime_local = str.strip( reporting_period_start_datetime_local) try: reporting_start_datetime_utc = datetime.strptime( reporting_period_start_datetime_local, '%Y-%m-%dT%H:%M:%S') except ValueError: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description="API.INVALID_REPORTING_PERIOD_START_DATETIME") reporting_start_datetime_utc = reporting_start_datetime_utc.replace(tzinfo=timezone.utc) - \ timedelta(minutes=timezone_offset) if reporting_period_end_datetime_local is None: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description="API.INVALID_REPORTING_PERIOD_END_DATETIME") else: reporting_period_end_datetime_local = str.strip( reporting_period_end_datetime_local) try: reporting_end_datetime_utc = datetime.strptime( reporting_period_end_datetime_local, '%Y-%m-%dT%H:%M:%S') except ValueError: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description="API.INVALID_REPORTING_PERIOD_END_DATETIME") reporting_end_datetime_utc = reporting_end_datetime_utc.replace(tzinfo=timezone.utc) - \ timedelta(minutes=timezone_offset) if reporting_start_datetime_utc >= reporting_end_datetime_utc: raise falcon.HTTPError( falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_REPORTING_PERIOD_END_DATETIME') ################################################################################################################ # 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() cursor_system.execute( " SELECT m.id, m.name, m.cost_center_id, m.energy_category_id, " " ec.name, ec.unit_of_measure " " FROM tbl_meters m, tbl_energy_categories ec " " WHERE m.id = %s AND m.energy_category_id = ec.id ", (meter_id, )) 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() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.METER_NOT_FOUND') master_meter = dict() master_meter['id'] = row_meter[0] master_meter['name'] = row_meter[1] master_meter['cost_center_id'] = row_meter[2] master_meter['energy_category_id'] = row_meter[3] master_meter['energy_category_name'] = row_meter[4] master_meter['unit_of_measure'] = row_meter[5] ################################################################################################################ # Step 3: query associated submeters ################################################################################################################ submeter_list = list() submeter_id_set = set() cursor_system.execute( " SELECT id, name, energy_category_id " " FROM tbl_meters " " WHERE master_meter_id = %s ", (master_meter['id'], )) rows_meters = cursor_system.fetchall() if rows_meters is not None and len(rows_meters) > 0: for row in rows_meters: submeter_list.append({ "id": row[0], "name": row[1], "energy_category_id": row[2] }) submeter_id_set.add(row[0]) ################################################################################################################ # Step 4: query reporting period master meter energy consumption ################################################################################################################ reporting = dict() reporting['master_meter_total_in_category'] = Decimal(0.0) reporting['submeters_total_in_category'] = Decimal(0.0) reporting['total_difference_in_category'] = Decimal(0.0) reporting['percentage_difference'] = Decimal(0.0) reporting['timestamps'] = list() reporting['master_meter_values'] = list() reporting['submeters_values'] = list() reporting['difference_values'] = list() parameters_data = dict() parameters_data['names'] = list() parameters_data['timestamps'] = list() parameters_data['values'] = list() 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_energy.execute( query, (master_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, reporting_end_datetime_utc, period_type) for row_meter_periodically in rows_meter_periodically: current_datetime_local = row_meter_periodically[0].replace(tzinfo=timezone.utc) + \ timedelta(minutes=timezone_offset) if period_type == 'hourly': current_datetime = current_datetime_local.strftime( '%Y-%m-%dT%H:%M:%S') elif period_type == 'daily': current_datetime = current_datetime_local.strftime('%Y-%m-%d') elif period_type == 'monthly': current_datetime = current_datetime_local.strftime('%Y-%m') elif period_type == 'yearly': current_datetime = current_datetime_local.strftime('%Y') actual_value = Decimal(0.0) if row_meter_periodically[ 1] is None else row_meter_periodically[1] reporting['timestamps'].append(current_datetime) reporting['master_meter_values'].append(actual_value) reporting['master_meter_total_in_category'] += actual_value # add master meter values to parameter data parameters_data['names'].append(master_meter['name']) parameters_data['timestamps'].append(reporting['timestamps']) parameters_data['values'].append(reporting['master_meter_values']) ################################################################################################################ # Step 5: query reporting period submeters energy consumption ################################################################################################################ if len(submeter_list) > 0: query = (" SELECT start_datetime_utc, SUM(actual_value) " " FROM tbl_meter_hourly " " WHERE meter_id IN ( " + ', '.join(map(str, submeter_id_set)) + ") " " AND start_datetime_utc >= %s " " AND start_datetime_utc < %s " " GROUP BY start_datetime_utc " " ORDER BY start_datetime_utc ") cursor_energy.execute( query, (reporting_start_datetime_utc, reporting_end_datetime_utc)) rows_submeters_hourly = cursor_energy.fetchall() rows_submeters_periodically = utilities.aggregate_hourly_data_by_period( rows_submeters_hourly, reporting_start_datetime_utc, reporting_end_datetime_utc, period_type) for row_submeters_periodically in rows_submeters_periodically: current_datetime_local = row_submeters_periodically[0].replace(tzinfo=timezone.utc) + \ timedelta(minutes=timezone_offset) if period_type == 'hourly': current_datetime = current_datetime_local.strftime( '%Y-%m-%dT%H:%M:%S') elif period_type == 'daily': current_datetime = current_datetime_local.strftime( '%Y-%m-%d') elif period_type == 'monthly': current_datetime = current_datetime_local.strftime('%Y-%m') elif period_type == 'yearly': current_datetime = current_datetime_local.strftime('%Y') actual_value = Decimal(0.0) if row_submeters_periodically[ 1] is None else row_submeters_periodically[1] reporting['submeters_values'].append(actual_value) reporting['submeters_total_in_category'] += actual_value ################################################################################################################ # Step 6: calculate reporting period difference between master meter and submeters ################################################################################################################ if len(submeter_list) > 0: for i in range(len(reporting['timestamps'])): reporting['difference_values'].append( reporting['master_meter_values'][i] - reporting['submeters_values'][i]) else: for i in range(len(reporting['timestamps'])): reporting['difference_values'].append( reporting['master_meter_values'][i]) reporting['total_difference_in_category'] = \ reporting['master_meter_total_in_category'] - reporting['submeters_total_in_category'] if abs(reporting['master_meter_total_in_category']) > Decimal(0.0): reporting['percentage_difference'] = \ reporting['total_difference_in_category'] / reporting['master_meter_total_in_category'] elif abs(reporting['master_meter_total_in_category']) == Decimal(0.0) and \ abs(reporting['submeters_total_in_category']) > Decimal(0.0): reporting['percentage_difference'] = Decimal(-1.0) ################################################################################################################ # Step 7: query submeter values as parameter data ################################################################################################################ for submeter in submeter_list: submeter_timestamps = list() submeter_values = list() 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_energy.execute( query, (submeter['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, reporting_end_datetime_utc, period_type) for row_meter_periodically in rows_meter_periodically: current_datetime_local = row_meter_periodically[0].replace(tzinfo=timezone.utc) + \ timedelta(minutes=timezone_offset) if period_type == 'hourly': current_datetime = current_datetime_local.strftime( '%Y-%m-%dT%H:%M:%S') elif period_type == 'daily': current_datetime = current_datetime_local.strftime( '%Y-%m-%d') elif period_type == 'monthly': current_datetime = current_datetime_local.strftime('%Y-%m') elif period_type == 'yearly': current_datetime = current_datetime_local.strftime('%Y') actual_value = Decimal(0.0) if row_meter_periodically[ 1] is None else row_meter_periodically[1] submeter_timestamps.append(current_datetime) submeter_values.append(actual_value) parameters_data['names'].append(submeter['name']) parameters_data['timestamps'].append(submeter_timestamps) parameters_data['values'].append(submeter_values) ################################################################################################################ # Step 8: construct the report ################################################################################################################ if cursor_system: cursor_system.close() if cnx_system: cnx_system.disconnect() if cursor_energy: cursor_energy.close() if cnx_energy: cnx_energy.disconnect() result = { "meter": { "cost_center_id": master_meter['cost_center_id'], "energy_category_id": master_meter['energy_category_id'], "energy_category_name": master_meter['energy_category_name'], "unit_of_measure": master_meter['unit_of_measure'], }, "reporting_period": { "master_meter_consumption_in_category": reporting['master_meter_total_in_category'], "submeters_consumption_in_category": reporting['submeters_total_in_category'], "difference_in_category": reporting['total_difference_in_category'], "percentage_difference": reporting['percentage_difference'], "timestamps": reporting['timestamps'], "difference_values": reporting['difference_values'], }, "parameters": { "names": parameters_data['names'], "timestamps": parameters_data['timestamps'], "values": parameters_data['values'] }, } # export result to Excel file and then encode the file to base64 string result[ 'excel_bytes_base64'] = excelexporters.metersubmetersbalance.export( result, master_meter['name'], reporting_period_start_datetime_local, reporting_period_end_datetime_local, period_type) resp.body = json.dumps(result)