| @@ 45-812 (lines=768) @@ | ||
| 42 | from core.useractivity import access_control, api_key_control |
|
| 43 | ||
| 44 | ||
| 45 | class Reporting: |
|
| 46 | def __init__(self): |
|
| 47 | """"Initializes Reporting""" |
|
| 48 | pass |
|
| 49 | ||
| 50 | @staticmethod |
|
| 51 | def on_options(req, resp): |
|
| 52 | _ = req |
|
| 53 | resp.status = falcon.HTTP_200 |
|
| 54 | ||
| 55 | #################################################################################################################### |
|
| 56 | # PROCEDURES |
|
| 57 | # Step 1: valid parameters |
|
| 58 | # Step 2: query the shopfloor |
|
| 59 | # Step 3: query energy categories |
|
| 60 | # Step 4: query associated sensors |
|
| 61 | # Step 5: query associated points |
|
| 62 | # Step 6: query base period energy saving |
|
| 63 | # Step 7: query reporting period energy saving |
|
| 64 | # Step 8: query tariff data |
|
| 65 | # Step 9: query associated sensors and points data |
|
| 66 | # Step 10: construct the report |
|
| 67 | #################################################################################################################### |
|
| 68 | @staticmethod |
|
| 69 | def on_get(req, resp): |
|
| 70 | if 'API-KEY' not in req.headers or \ |
|
| 71 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 72 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 73 | access_control(req) |
|
| 74 | else: |
|
| 75 | api_key_control(req) |
|
| 76 | print(req.params) |
|
| 77 | shopfloor_id = req.params.get('shopfloorid') |
|
| 78 | shopfloor_uuid = req.params.get('shopflooruuid') |
|
| 79 | period_type = req.params.get('periodtype') |
|
| 80 | base_period_start_datetime_local = req.params.get('baseperiodstartdatetime') |
|
| 81 | base_period_end_datetime_local = req.params.get('baseperiodenddatetime') |
|
| 82 | reporting_period_start_datetime_local = req.params.get('reportingperiodstartdatetime') |
|
| 83 | reporting_period_end_datetime_local = req.params.get('reportingperiodenddatetime') |
|
| 84 | language = req.params.get('language') |
|
| 85 | quick_mode = req.params.get('quickmode') |
|
| 86 | ||
| 87 | ################################################################################################################ |
|
| 88 | # Step 1: valid parameters |
|
| 89 | ################################################################################################################ |
|
| 90 | if shopfloor_id is None and shopfloor_uuid is None: |
|
| 91 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 92 | title='API.BAD_REQUEST', |
|
| 93 | description='API.INVALID_SHOPFLOOR_ID') |
|
| 94 | ||
| 95 | if shopfloor_id is not None: |
|
| 96 | shopfloor_id = str.strip(shopfloor_id) |
|
| 97 | if not shopfloor_id.isdigit() or int(shopfloor_id) <= 0: |
|
| 98 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 99 | title='API.BAD_REQUEST', |
|
| 100 | description='API.INVALID_SHOPFLOOR_ID') |
|
| 101 | ||
| 102 | if shopfloor_uuid is not None: |
|
| 103 | regex = re.compile(r'^[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) |
|
| 104 | match = regex.match(str.strip(shopfloor_uuid)) |
|
| 105 | if not bool(match): |
|
| 106 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 107 | title='API.BAD_REQUEST', |
|
| 108 | description='API.INVALID_SHOPFLOOR_UUID') |
|
| 109 | ||
| 110 | if period_type is None: |
|
| 111 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 112 | description='API.INVALID_PERIOD_TYPE') |
|
| 113 | else: |
|
| 114 | period_type = str.strip(period_type) |
|
| 115 | if period_type not in ['hourly', 'daily', 'weekly', 'monthly', 'yearly']: |
|
| 116 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 117 | description='API.INVALID_PERIOD_TYPE') |
|
| 118 | ||
| 119 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
|
| 120 | if config.utc_offset[0] == '-': |
|
| 121 | timezone_offset = -timezone_offset |
|
| 122 | ||
| 123 | base_start_datetime_utc = None |
|
| 124 | if base_period_start_datetime_local is not None and len(str.strip(base_period_start_datetime_local)) > 0: |
|
| 125 | base_period_start_datetime_local = str.strip(base_period_start_datetime_local) |
|
| 126 | try: |
|
| 127 | base_start_datetime_utc = datetime.strptime(base_period_start_datetime_local, '%Y-%m-%dT%H:%M:%S') |
|
| 128 | except ValueError: |
|
| 129 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 130 | description="API.INVALID_BASE_PERIOD_START_DATETIME") |
|
| 131 | base_start_datetime_utc = \ |
|
| 132 | base_start_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset) |
|
| 133 | # nomalize the start datetime |
|
| 134 | if config.minutes_to_count == 30 and base_start_datetime_utc.minute >= 30: |
|
| 135 | base_start_datetime_utc = base_start_datetime_utc.replace(minute=30, second=0, microsecond=0) |
|
| 136 | else: |
|
| 137 | base_start_datetime_utc = base_start_datetime_utc.replace(minute=0, second=0, microsecond=0) |
|
| 138 | ||
| 139 | base_end_datetime_utc = None |
|
| 140 | if base_period_end_datetime_local is not None and len(str.strip(base_period_end_datetime_local)) > 0: |
|
| 141 | base_period_end_datetime_local = str.strip(base_period_end_datetime_local) |
|
| 142 | try: |
|
| 143 | base_end_datetime_utc = datetime.strptime(base_period_end_datetime_local, '%Y-%m-%dT%H:%M:%S') |
|
| 144 | except ValueError: |
|
| 145 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 146 | description="API.INVALID_BASE_PERIOD_END_DATETIME") |
|
| 147 | base_end_datetime_utc = \ |
|
| 148 | base_end_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset) |
|
| 149 | ||
| 150 | if base_start_datetime_utc is not None and base_end_datetime_utc is not None and \ |
|
| 151 | base_start_datetime_utc >= base_end_datetime_utc: |
|
| 152 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 153 | description='API.INVALID_BASE_PERIOD_END_DATETIME') |
|
| 154 | ||
| 155 | if reporting_period_start_datetime_local is None: |
|
| 156 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 157 | description="API.INVALID_REPORTING_PERIOD_START_DATETIME") |
|
| 158 | else: |
|
| 159 | reporting_period_start_datetime_local = str.strip(reporting_period_start_datetime_local) |
|
| 160 | try: |
|
| 161 | reporting_start_datetime_utc = datetime.strptime(reporting_period_start_datetime_local, |
|
| 162 | '%Y-%m-%dT%H:%M:%S') |
|
| 163 | except ValueError: |
|
| 164 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 165 | description="API.INVALID_REPORTING_PERIOD_START_DATETIME") |
|
| 166 | reporting_start_datetime_utc = \ |
|
| 167 | reporting_start_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset) |
|
| 168 | # nomalize the start datetime |
|
| 169 | if config.minutes_to_count == 30 and reporting_start_datetime_utc.minute >= 30: |
|
| 170 | reporting_start_datetime_utc = reporting_start_datetime_utc.replace(minute=30, second=0, microsecond=0) |
|
| 171 | else: |
|
| 172 | reporting_start_datetime_utc = reporting_start_datetime_utc.replace(minute=0, second=0, microsecond=0) |
|
| 173 | ||
| 174 | if reporting_period_end_datetime_local is None: |
|
| 175 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 176 | description="API.INVALID_REPORTING_PERIOD_END_DATETIME") |
|
| 177 | else: |
|
| 178 | reporting_period_end_datetime_local = str.strip(reporting_period_end_datetime_local) |
|
| 179 | try: |
|
| 180 | reporting_end_datetime_utc = datetime.strptime(reporting_period_end_datetime_local, |
|
| 181 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
|
| 182 | timedelta(minutes=timezone_offset) |
|
| 183 | except ValueError: |
|
| 184 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 185 | description="API.INVALID_REPORTING_PERIOD_END_DATETIME") |
|
| 186 | ||
| 187 | if reporting_start_datetime_utc >= reporting_end_datetime_utc: |
|
| 188 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 189 | description='API.INVALID_REPORTING_PERIOD_END_DATETIME') |
|
| 190 | ||
| 191 | # if turn quick mode on, do not return parameters data and excel file |
|
| 192 | is_quick_mode = False |
|
| 193 | if quick_mode is not None and \ |
|
| 194 | len(str.strip(quick_mode)) > 0 and \ |
|
| 195 | str.lower(str.strip(quick_mode)) in ('true', 't', 'on', 'yes', 'y'): |
|
| 196 | is_quick_mode = True |
|
| 197 | ||
| 198 | trans = utilities.get_translation(language) |
|
| 199 | trans.install() |
|
| 200 | _ = trans.gettext |
|
| 201 | ||
| 202 | ################################################################################################################ |
|
| 203 | # Step 2: query the shopfloor |
|
| 204 | ################################################################################################################ |
|
| 205 | cnx_system = mysql.connector.connect(**config.myems_system_db) |
|
| 206 | cursor_system = cnx_system.cursor() |
|
| 207 | ||
| 208 | cnx_energy = mysql.connector.connect(**config.myems_energy_db) |
|
| 209 | cursor_energy = cnx_energy.cursor() |
|
| 210 | ||
| 211 | cnx_energy_baseline = mysql.connector.connect(**config.myems_energy_baseline_db) |
|
| 212 | cursor_energy_baseline = cnx_energy_baseline.cursor() |
|
| 213 | ||
| 214 | cnx_historical = mysql.connector.connect(**config.myems_historical_db) |
|
| 215 | cursor_historical = cnx_historical.cursor() |
|
| 216 | ||
| 217 | if shopfloor_id is not None: |
|
| 218 | cursor_system.execute(" SELECT id, name, area, cost_center_id " |
|
| 219 | " FROM tbl_shopfloors " |
|
| 220 | " WHERE id = %s ", (shopfloor_id,)) |
|
| 221 | row_shopfloor = cursor_system.fetchone() |
|
| 222 | elif shopfloor_uuid is not None: |
|
| 223 | cursor_system.execute(" SELECT id, name, area, cost_center_id " |
|
| 224 | " FROM tbl_shopfloors " |
|
| 225 | " WHERE uuid = %s ", (shopfloor_uuid,)) |
|
| 226 | row_shopfloor = cursor_system.fetchone() |
|
| 227 | ||
| 228 | if row_shopfloor is None: |
|
| 229 | if cursor_system: |
|
| 230 | cursor_system.close() |
|
| 231 | if cnx_system: |
|
| 232 | cnx_system.close() |
|
| 233 | ||
| 234 | if cursor_energy: |
|
| 235 | cursor_energy.close() |
|
| 236 | if cnx_energy: |
|
| 237 | cnx_energy.close() |
|
| 238 | ||
| 239 | if cursor_energy_baseline: |
|
| 240 | cursor_energy_baseline.close() |
|
| 241 | if cnx_energy_baseline: |
|
| 242 | cnx_energy_baseline.close() |
|
| 243 | ||
| 244 | if cursor_historical: |
|
| 245 | cursor_historical.close() |
|
| 246 | if cnx_historical: |
|
| 247 | cnx_historical.close() |
|
| 248 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', description='API.SHOPFLOOR_NOT_FOUND') |
|
| 249 | ||
| 250 | shopfloor = dict() |
|
| 251 | shopfloor['id'] = row_shopfloor[0] |
|
| 252 | shopfloor['name'] = row_shopfloor[1] |
|
| 253 | shopfloor['area'] = row_shopfloor[2] |
|
| 254 | shopfloor['cost_center_id'] = row_shopfloor[3] |
|
| 255 | ||
| 256 | ################################################################################################################ |
|
| 257 | # Step 3: query energy categories |
|
| 258 | ################################################################################################################ |
|
| 259 | energy_category_set = set() |
|
| 260 | # query energy categories in base period |
|
| 261 | cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
|
| 262 | " FROM tbl_shopfloor_input_category_hourly " |
|
| 263 | " WHERE shopfloor_id = %s " |
|
| 264 | " AND start_datetime_utc >= %s " |
|
| 265 | " AND start_datetime_utc < %s ", |
|
| 266 | (shopfloor['id'], base_start_datetime_utc, base_end_datetime_utc)) |
|
| 267 | rows_energy_categories = cursor_energy.fetchall() |
|
| 268 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
| 269 | for row_energy_category in rows_energy_categories: |
|
| 270 | energy_category_set.add(row_energy_category[0]) |
|
| 271 | ||
| 272 | # query energy categories in reporting period |
|
| 273 | cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
|
| 274 | " FROM tbl_shopfloor_input_category_hourly " |
|
| 275 | " WHERE shopfloor_id = %s " |
|
| 276 | " AND start_datetime_utc >= %s " |
|
| 277 | " AND start_datetime_utc < %s ", |
|
| 278 | (shopfloor['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) |
|
| 279 | rows_energy_categories = cursor_energy.fetchall() |
|
| 280 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
| 281 | for row_energy_category in rows_energy_categories: |
|
| 282 | energy_category_set.add(row_energy_category[0]) |
|
| 283 | ||
| 284 | # query all energy categories in base period and reporting period |
|
| 285 | cursor_system.execute(" SELECT id, name, unit_of_measure, kgce, kgco2e " |
|
| 286 | " FROM tbl_energy_categories " |
|
| 287 | " ORDER BY id ", ) |
|
| 288 | rows_energy_categories = cursor_system.fetchall() |
|
| 289 | if rows_energy_categories is None or len(rows_energy_categories) == 0: |
|
| 290 | if cursor_system: |
|
| 291 | cursor_system.close() |
|
| 292 | if cnx_system: |
|
| 293 | cnx_system.close() |
|
| 294 | ||
| 295 | if cursor_energy: |
|
| 296 | cursor_energy.close() |
|
| 297 | if cnx_energy: |
|
| 298 | cnx_energy.close() |
|
| 299 | ||
| 300 | if cursor_energy_baseline: |
|
| 301 | cursor_energy_baseline.close() |
|
| 302 | if cnx_energy_baseline: |
|
| 303 | cnx_energy_baseline.close() |
|
| 304 | ||
| 305 | if cursor_historical: |
|
| 306 | cursor_historical.close() |
|
| 307 | if cnx_historical: |
|
| 308 | cnx_historical.close() |
|
| 309 | raise falcon.HTTPError(status=falcon.HTTP_404, |
|
| 310 | title='API.NOT_FOUND', |
|
| 311 | description='API.ENERGY_CATEGORY_NOT_FOUND') |
|
| 312 | energy_category_dict = dict() |
|
| 313 | for row_energy_category in rows_energy_categories: |
|
| 314 | if row_energy_category[0] in energy_category_set: |
|
| 315 | energy_category_dict[row_energy_category[0]] = {"name": row_energy_category[1], |
|
| 316 | "unit_of_measure": row_energy_category[2], |
|
| 317 | "kgce": row_energy_category[3], |
|
| 318 | "kgco2e": row_energy_category[4]} |
|
| 319 | ||
| 320 | ################################################################################################################ |
|
| 321 | # Step 4: query associated sensors |
|
| 322 | ################################################################################################################ |
|
| 323 | point_list = list() |
|
| 324 | cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type " |
|
| 325 | " FROM tbl_shopfloors st, tbl_sensors se, tbl_shopfloors_sensors ss, " |
|
| 326 | " tbl_points p, tbl_sensors_points sp " |
|
| 327 | " WHERE st.id = %s AND st.id = ss.shopfloor_id AND ss.sensor_id = se.id " |
|
| 328 | " AND se.id = sp.sensor_id AND sp.point_id = p.id " |
|
| 329 | " ORDER BY p.id ", (shopfloor['id'],)) |
|
| 330 | rows_points = cursor_system.fetchall() |
|
| 331 | if rows_points is not None and len(rows_points) > 0: |
|
| 332 | for row in rows_points: |
|
| 333 | point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]}) |
|
| 334 | ||
| 335 | ################################################################################################################ |
|
| 336 | # Step 5: query associated points |
|
| 337 | ################################################################################################################ |
|
| 338 | cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type " |
|
| 339 | " FROM tbl_shopfloors s, tbl_shopfloors_points sp, tbl_points p " |
|
| 340 | " WHERE s.id = %s AND s.id = sp.shopfloor_id AND sp.point_id = p.id " |
|
| 341 | " ORDER BY p.id ", (shopfloor['id'],)) |
|
| 342 | rows_points = cursor_system.fetchall() |
|
| 343 | if rows_points is not None and len(rows_points) > 0: |
|
| 344 | for row in rows_points: |
|
| 345 | point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]}) |
|
| 346 | ||
| 347 | ################################################################################################################ |
|
| 348 | # Step 6: query base period energy saving |
|
| 349 | ################################################################################################################ |
|
| 350 | base = dict() |
|
| 351 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
| 352 | for energy_category_id in energy_category_set: |
|
| 353 | kgce = energy_category_dict[energy_category_id]['kgce'] |
|
| 354 | kgco2e = energy_category_dict[energy_category_id]['kgco2e'] |
|
| 355 | ||
| 356 | base[energy_category_id] = dict() |
|
| 357 | base[energy_category_id]['timestamps'] = list() |
|
| 358 | base[energy_category_id]['values_baseline'] = list() |
|
| 359 | base[energy_category_id]['values_actual'] = list() |
|
| 360 | base[energy_category_id]['values_saving'] = list() |
|
| 361 | base[energy_category_id]['subtotal_baseline'] = Decimal(0.0) |
|
| 362 | base[energy_category_id]['subtotal_actual'] = Decimal(0.0) |
|
| 363 | base[energy_category_id]['subtotal_saving'] = Decimal(0.0) |
|
| 364 | base[energy_category_id]['subtotal_in_kgce_baseline'] = Decimal(0.0) |
|
| 365 | base[energy_category_id]['subtotal_in_kgce_actual'] = Decimal(0.0) |
|
| 366 | base[energy_category_id]['subtotal_in_kgce_saving'] = Decimal(0.0) |
|
| 367 | base[energy_category_id]['subtotal_in_kgco2e_baseline'] = Decimal(0.0) |
|
| 368 | base[energy_category_id]['subtotal_in_kgco2e_actual'] = Decimal(0.0) |
|
| 369 | base[energy_category_id]['subtotal_in_kgco2e_saving'] = Decimal(0.0) |
|
| 370 | # query base period's energy baseline |
|
| 371 | cursor_energy_baseline.execute(" SELECT start_datetime_utc, actual_value " |
|
| 372 | " FROM tbl_shopfloor_input_category_hourly " |
|
| 373 | " WHERE shopfloor_id = %s " |
|
| 374 | " AND energy_category_id = %s " |
|
| 375 | " AND start_datetime_utc >= %s " |
|
| 376 | " AND start_datetime_utc < %s " |
|
| 377 | " ORDER BY start_datetime_utc ", |
|
| 378 | (shopfloor['id'], |
|
| 379 | energy_category_id, |
|
| 380 | base_start_datetime_utc, |
|
| 381 | base_end_datetime_utc)) |
|
| 382 | rows_shopfloor_hourly = cursor_energy_baseline.fetchall() |
|
| 383 | ||
| 384 | rows_shopfloor_periodically = utilities.aggregate_hourly_data_by_period(rows_shopfloor_hourly, |
|
| 385 | base_start_datetime_utc, |
|
| 386 | base_end_datetime_utc, |
|
| 387 | period_type) |
|
| 388 | for row_shopfloor_periodically in rows_shopfloor_periodically: |
|
| 389 | current_datetime_local = row_shopfloor_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
| 390 | timedelta(minutes=timezone_offset) |
|
| 391 | if period_type == 'hourly': |
|
| 392 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 393 | elif period_type == 'daily': |
|
| 394 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 395 | elif period_type == 'weekly': |
|
| 396 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 397 | elif period_type == 'monthly': |
|
| 398 | current_datetime = current_datetime_local.isoformat()[0:7] |
|
| 399 | elif period_type == 'yearly': |
|
| 400 | current_datetime = current_datetime_local.isoformat()[0:4] |
|
| 401 | ||
| 402 | baseline_value = Decimal(0.0) if row_shopfloor_periodically[1] is None \ |
|
| 403 | else row_shopfloor_periodically[1] |
|
| 404 | base[energy_category_id]['timestamps'].append(current_datetime) |
|
| 405 | base[energy_category_id]['values_baseline'].append(baseline_value) |
|
| 406 | base[energy_category_id]['subtotal_baseline'] += baseline_value |
|
| 407 | base[energy_category_id]['subtotal_in_kgce_baseline'] += baseline_value * kgce |
|
| 408 | base[energy_category_id]['subtotal_in_kgco2e_baseline'] += baseline_value * kgco2e |
|
| 409 | ||
| 410 | # query base period's energy actual |
|
| 411 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
|
| 412 | " FROM tbl_shopfloor_input_category_hourly " |
|
| 413 | " WHERE shopfloor_id = %s " |
|
| 414 | " AND energy_category_id = %s " |
|
| 415 | " AND start_datetime_utc >= %s " |
|
| 416 | " AND start_datetime_utc < %s " |
|
| 417 | " ORDER BY start_datetime_utc ", |
|
| 418 | (shopfloor['id'], |
|
| 419 | energy_category_id, |
|
| 420 | base_start_datetime_utc, |
|
| 421 | base_end_datetime_utc)) |
|
| 422 | rows_shopfloor_hourly = cursor_energy.fetchall() |
|
| 423 | ||
| 424 | rows_shopfloor_periodically = utilities.aggregate_hourly_data_by_period(rows_shopfloor_hourly, |
|
| 425 | base_start_datetime_utc, |
|
| 426 | base_end_datetime_utc, |
|
| 427 | period_type) |
|
| 428 | for row_shopfloor_periodically in rows_shopfloor_periodically: |
|
| 429 | current_datetime_local = row_shopfloor_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
| 430 | timedelta(minutes=timezone_offset) |
|
| 431 | if period_type == 'hourly': |
|
| 432 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 433 | elif period_type == 'daily': |
|
| 434 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 435 | elif period_type == 'weekly': |
|
| 436 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 437 | elif period_type == 'monthly': |
|
| 438 | current_datetime = current_datetime_local.isoformat()[0:7] |
|
| 439 | elif period_type == 'yearly': |
|
| 440 | current_datetime = current_datetime_local.isoformat()[0:4] |
|
| 441 | ||
| 442 | actual_value = Decimal(0.0) if row_shopfloor_periodically[1] is None \ |
|
| 443 | else row_shopfloor_periodically[1] |
|
| 444 | base[energy_category_id]['values_actual'].append(actual_value) |
|
| 445 | base[energy_category_id]['subtotal_actual'] += actual_value |
|
| 446 | base[energy_category_id]['subtotal_in_kgce_actual'] += actual_value * kgce |
|
| 447 | base[energy_category_id]['subtotal_in_kgco2e_actual'] += actual_value * kgco2e |
|
| 448 | ||
| 449 | # calculate base period's energy savings |
|
| 450 | for i in range(len(base[energy_category_id]['values_baseline'])): |
|
| 451 | base[energy_category_id]['values_saving'].append( |
|
| 452 | base[energy_category_id]['values_baseline'][i] - |
|
| 453 | base[energy_category_id]['values_actual'][i]) |
|
| 454 | ||
| 455 | base[energy_category_id]['subtotal_saving'] = \ |
|
| 456 | base[energy_category_id]['subtotal_baseline'] - \ |
|
| 457 | base[energy_category_id]['subtotal_actual'] |
|
| 458 | base[energy_category_id]['subtotal_in_kgce_saving'] = \ |
|
| 459 | base[energy_category_id]['subtotal_in_kgce_baseline'] - \ |
|
| 460 | base[energy_category_id]['subtotal_in_kgce_actual'] |
|
| 461 | base[energy_category_id]['subtotal_in_kgco2e_saving'] = \ |
|
| 462 | base[energy_category_id]['subtotal_in_kgco2e_baseline'] - \ |
|
| 463 | base[energy_category_id]['subtotal_in_kgco2e_actual'] |
|
| 464 | ################################################################################################################ |
|
| 465 | # Step 7: query reporting period energy saving |
|
| 466 | ################################################################################################################ |
|
| 467 | reporting = dict() |
|
| 468 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
| 469 | for energy_category_id in energy_category_set: |
|
| 470 | kgce = energy_category_dict[energy_category_id]['kgce'] |
|
| 471 | kgco2e = energy_category_dict[energy_category_id]['kgco2e'] |
|
| 472 | ||
| 473 | reporting[energy_category_id] = dict() |
|
| 474 | reporting[energy_category_id]['timestamps'] = list() |
|
| 475 | reporting[energy_category_id]['values_baseline'] = list() |
|
| 476 | reporting[energy_category_id]['values_actual'] = list() |
|
| 477 | reporting[energy_category_id]['values_saving'] = list() |
|
| 478 | reporting[energy_category_id]['subtotal_baseline'] = Decimal(0.0) |
|
| 479 | reporting[energy_category_id]['subtotal_actual'] = Decimal(0.0) |
|
| 480 | reporting[energy_category_id]['subtotal_saving'] = Decimal(0.0) |
|
| 481 | reporting[energy_category_id]['subtotal_in_kgce_baseline'] = Decimal(0.0) |
|
| 482 | reporting[energy_category_id]['subtotal_in_kgce_actual'] = Decimal(0.0) |
|
| 483 | reporting[energy_category_id]['subtotal_in_kgce_saving'] = Decimal(0.0) |
|
| 484 | reporting[energy_category_id]['subtotal_in_kgco2e_baseline'] = Decimal(0.0) |
|
| 485 | reporting[energy_category_id]['subtotal_in_kgco2e_actual'] = Decimal(0.0) |
|
| 486 | reporting[energy_category_id]['subtotal_in_kgco2e_saving'] = Decimal(0.0) |
|
| 487 | # query reporting period's energy baseline |
|
| 488 | cursor_energy_baseline.execute(" SELECT start_datetime_utc, actual_value " |
|
| 489 | " FROM tbl_shopfloor_input_category_hourly " |
|
| 490 | " WHERE shopfloor_id = %s " |
|
| 491 | " AND energy_category_id = %s " |
|
| 492 | " AND start_datetime_utc >= %s " |
|
| 493 | " AND start_datetime_utc < %s " |
|
| 494 | " ORDER BY start_datetime_utc ", |
|
| 495 | (shopfloor['id'], |
|
| 496 | energy_category_id, |
|
| 497 | reporting_start_datetime_utc, |
|
| 498 | reporting_end_datetime_utc)) |
|
| 499 | rows_shopfloor_hourly = cursor_energy_baseline.fetchall() |
|
| 500 | ||
| 501 | rows_shopfloor_periodically = utilities.aggregate_hourly_data_by_period(rows_shopfloor_hourly, |
|
| 502 | reporting_start_datetime_utc, |
|
| 503 | reporting_end_datetime_utc, |
|
| 504 | period_type) |
|
| 505 | for row_shopfloor_periodically in rows_shopfloor_periodically: |
|
| 506 | current_datetime_local = row_shopfloor_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
| 507 | timedelta(minutes=timezone_offset) |
|
| 508 | if period_type == 'hourly': |
|
| 509 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 510 | elif period_type == 'daily': |
|
| 511 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 512 | elif period_type == 'weekly': |
|
| 513 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 514 | elif period_type == 'monthly': |
|
| 515 | current_datetime = current_datetime_local.isoformat()[0:7] |
|
| 516 | elif period_type == 'yearly': |
|
| 517 | current_datetime = current_datetime_local.isoformat()[0:4] |
|
| 518 | ||
| 519 | baseline_value = Decimal(0.0) if row_shopfloor_periodically[1] is None \ |
|
| 520 | else row_shopfloor_periodically[1] |
|
| 521 | reporting[energy_category_id]['timestamps'].append(current_datetime) |
|
| 522 | reporting[energy_category_id]['values_baseline'].append(baseline_value) |
|
| 523 | reporting[energy_category_id]['subtotal_baseline'] += baseline_value |
|
| 524 | reporting[energy_category_id]['subtotal_in_kgce_baseline'] += baseline_value * kgce |
|
| 525 | reporting[energy_category_id]['subtotal_in_kgco2e_baseline'] += baseline_value * kgco2e |
|
| 526 | ||
| 527 | # query reporting period's energy actual |
|
| 528 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
|
| 529 | " FROM tbl_shopfloor_input_category_hourly " |
|
| 530 | " WHERE shopfloor_id = %s " |
|
| 531 | " AND energy_category_id = %s " |
|
| 532 | " AND start_datetime_utc >= %s " |
|
| 533 | " AND start_datetime_utc < %s " |
|
| 534 | " ORDER BY start_datetime_utc ", |
|
| 535 | (shopfloor['id'], |
|
| 536 | energy_category_id, |
|
| 537 | reporting_start_datetime_utc, |
|
| 538 | reporting_end_datetime_utc)) |
|
| 539 | rows_shopfloor_hourly = cursor_energy.fetchall() |
|
| 540 | ||
| 541 | rows_shopfloor_periodically = utilities.aggregate_hourly_data_by_period(rows_shopfloor_hourly, |
|
| 542 | reporting_start_datetime_utc, |
|
| 543 | reporting_end_datetime_utc, |
|
| 544 | period_type) |
|
| 545 | for row_shopfloor_periodically in rows_shopfloor_periodically: |
|
| 546 | current_datetime_local = row_shopfloor_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
| 547 | timedelta(minutes=timezone_offset) |
|
| 548 | if period_type == 'hourly': |
|
| 549 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 550 | elif period_type == 'daily': |
|
| 551 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 552 | elif period_type == 'weekly': |
|
| 553 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 554 | elif period_type == 'monthly': |
|
| 555 | current_datetime = current_datetime_local.isoformat()[0:7] |
|
| 556 | elif period_type == 'yearly': |
|
| 557 | current_datetime = current_datetime_local.isoformat()[0:4] |
|
| 558 | ||
| 559 | actual_value = Decimal(0.0) if row_shopfloor_periodically[1] is None \ |
|
| 560 | else row_shopfloor_periodically[1] |
|
| 561 | reporting[energy_category_id]['values_actual'].append(actual_value) |
|
| 562 | reporting[energy_category_id]['subtotal_actual'] += actual_value |
|
| 563 | reporting[energy_category_id]['subtotal_in_kgce_actual'] += actual_value * kgce |
|
| 564 | reporting[energy_category_id]['subtotal_in_kgco2e_actual'] += actual_value * kgco2e |
|
| 565 | ||
| 566 | # calculate reporting period's energy savings |
|
| 567 | for i in range(len(reporting[energy_category_id]['values_baseline'])): |
|
| 568 | reporting[energy_category_id]['values_saving'].append( |
|
| 569 | reporting[energy_category_id]['values_baseline'][i] - |
|
| 570 | reporting[energy_category_id]['values_actual'][i]) |
|
| 571 | ||
| 572 | reporting[energy_category_id]['subtotal_saving'] = \ |
|
| 573 | reporting[energy_category_id]['subtotal_baseline'] - \ |
|
| 574 | reporting[energy_category_id]['subtotal_actual'] |
|
| 575 | reporting[energy_category_id]['subtotal_in_kgce_saving'] = \ |
|
| 576 | reporting[energy_category_id]['subtotal_in_kgce_baseline'] - \ |
|
| 577 | reporting[energy_category_id]['subtotal_in_kgce_actual'] |
|
| 578 | reporting[energy_category_id]['subtotal_in_kgco2e_saving'] = \ |
|
| 579 | reporting[energy_category_id]['subtotal_in_kgco2e_baseline'] - \ |
|
| 580 | reporting[energy_category_id]['subtotal_in_kgco2e_actual'] |
|
| 581 | ################################################################################################################ |
|
| 582 | # Step 8: query tariff data |
|
| 583 | ################################################################################################################ |
|
| 584 | parameters_data = dict() |
|
| 585 | parameters_data['names'] = list() |
|
| 586 | parameters_data['timestamps'] = list() |
|
| 587 | parameters_data['values'] = list() |
|
| 588 | if config.is_tariff_appended and energy_category_set is not None and len(energy_category_set) > 0 \ |
|
| 589 | and not is_quick_mode: |
|
| 590 | for energy_category_id in energy_category_set: |
|
| 591 | energy_category_tariff_dict = utilities.get_energy_category_tariffs(shopfloor['cost_center_id'], |
|
| 592 | energy_category_id, |
|
| 593 | reporting_start_datetime_utc, |
|
| 594 | reporting_end_datetime_utc) |
|
| 595 | tariff_timestamp_list = list() |
|
| 596 | tariff_value_list = list() |
|
| 597 | for k, v in energy_category_tariff_dict.items(): |
|
| 598 | # convert k from utc to local |
|
| 599 | k = k + timedelta(minutes=timezone_offset) |
|
| 600 | tariff_timestamp_list.append(k.isoformat()[0:19]) |
|
| 601 | tariff_value_list.append(v) |
|
| 602 | ||
| 603 | parameters_data['names'].append(_('Tariff') + '-' + energy_category_dict[energy_category_id]['name']) |
|
| 604 | parameters_data['timestamps'].append(tariff_timestamp_list) |
|
| 605 | parameters_data['values'].append(tariff_value_list) |
|
| 606 | ||
| 607 | ################################################################################################################ |
|
| 608 | # Step 9: query associated sensors and points data |
|
| 609 | ################################################################################################################ |
|
| 610 | if not is_quick_mode: |
|
| 611 | for point in point_list: |
|
| 612 | point_values = [] |
|
| 613 | point_timestamps = [] |
|
| 614 | if point['object_type'] == 'ENERGY_VALUE': |
|
| 615 | query = (" SELECT utc_date_time, actual_value " |
|
| 616 | " FROM tbl_energy_value " |
|
| 617 | " WHERE point_id = %s " |
|
| 618 | " AND utc_date_time BETWEEN %s AND %s " |
|
| 619 | " ORDER BY utc_date_time ") |
|
| 620 | cursor_historical.execute(query, (point['id'], |
|
| 621 | reporting_start_datetime_utc, |
|
| 622 | reporting_end_datetime_utc)) |
|
| 623 | rows = cursor_historical.fetchall() |
|
| 624 | ||
| 625 | if rows is not None and len(rows) > 0: |
|
| 626 | for row in rows: |
|
| 627 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
| 628 | timedelta(minutes=timezone_offset) |
|
| 629 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 630 | point_timestamps.append(current_datetime) |
|
| 631 | point_values.append(row[1]) |
|
| 632 | elif point['object_type'] == 'ANALOG_VALUE': |
|
| 633 | query = (" SELECT utc_date_time, actual_value " |
|
| 634 | " FROM tbl_analog_value " |
|
| 635 | " WHERE point_id = %s " |
|
| 636 | " AND utc_date_time BETWEEN %s AND %s " |
|
| 637 | " ORDER BY utc_date_time ") |
|
| 638 | cursor_historical.execute(query, (point['id'], |
|
| 639 | reporting_start_datetime_utc, |
|
| 640 | reporting_end_datetime_utc)) |
|
| 641 | rows = cursor_historical.fetchall() |
|
| 642 | ||
| 643 | if rows is not None and len(rows) > 0: |
|
| 644 | for row in rows: |
|
| 645 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
| 646 | timedelta(minutes=timezone_offset) |
|
| 647 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 648 | point_timestamps.append(current_datetime) |
|
| 649 | point_values.append(row[1]) |
|
| 650 | elif point['object_type'] == 'DIGITAL_VALUE': |
|
| 651 | query = (" SELECT utc_date_time, actual_value " |
|
| 652 | " FROM tbl_digital_value " |
|
| 653 | " WHERE point_id = %s " |
|
| 654 | " AND utc_date_time BETWEEN %s AND %s " |
|
| 655 | " ORDER BY utc_date_time ") |
|
| 656 | cursor_historical.execute(query, (point['id'], |
|
| 657 | reporting_start_datetime_utc, |
|
| 658 | reporting_end_datetime_utc)) |
|
| 659 | rows = cursor_historical.fetchall() |
|
| 660 | ||
| 661 | if rows is not None and len(rows) > 0: |
|
| 662 | for row in rows: |
|
| 663 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
| 664 | timedelta(minutes=timezone_offset) |
|
| 665 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 666 | point_timestamps.append(current_datetime) |
|
| 667 | point_values.append(row[1]) |
|
| 668 | ||
| 669 | parameters_data['names'].append(point['name'] + ' (' + point['units'] + ')') |
|
| 670 | parameters_data['timestamps'].append(point_timestamps) |
|
| 671 | parameters_data['values'].append(point_values) |
|
| 672 | ||
| 673 | ################################################################################################################ |
|
| 674 | # Step 10: construct the report |
|
| 675 | ################################################################################################################ |
|
| 676 | if cursor_system: |
|
| 677 | cursor_system.close() |
|
| 678 | if cnx_system: |
|
| 679 | cnx_system.close() |
|
| 680 | ||
| 681 | if cursor_energy: |
|
| 682 | cursor_energy.close() |
|
| 683 | if cnx_energy: |
|
| 684 | cnx_energy.close() |
|
| 685 | ||
| 686 | if cursor_energy_baseline: |
|
| 687 | cursor_energy_baseline.close() |
|
| 688 | if cnx_energy_baseline: |
|
| 689 | cnx_energy_baseline.close() |
|
| 690 | ||
| 691 | if cursor_historical: |
|
| 692 | cursor_historical.close() |
|
| 693 | if cnx_historical: |
|
| 694 | cnx_historical.close() |
|
| 695 | ||
| 696 | result = dict() |
|
| 697 | ||
| 698 | result['shopfloor'] = dict() |
|
| 699 | result['shopfloor']['name'] = shopfloor['name'] |
|
| 700 | result['shopfloor']['area'] = shopfloor['area'] |
|
| 701 | ||
| 702 | result['base_period'] = dict() |
|
| 703 | result['base_period']['names'] = list() |
|
| 704 | result['base_period']['units'] = list() |
|
| 705 | result['base_period']['timestamps'] = list() |
|
| 706 | result['base_period']['values_saving'] = list() |
|
| 707 | result['base_period']['subtotals_saving'] = list() |
|
| 708 | result['base_period']['subtotals_in_kgce_saving'] = list() |
|
| 709 | result['base_period']['subtotals_in_kgco2e_saving'] = list() |
|
| 710 | result['base_period']['total_in_kgce_saving'] = Decimal(0.0) |
|
| 711 | result['base_period']['total_in_kgco2e_saving'] = Decimal(0.0) |
|
| 712 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
| 713 | for energy_category_id in energy_category_set: |
|
| 714 | result['base_period']['names'].append(energy_category_dict[energy_category_id]['name']) |
|
| 715 | result['base_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) |
|
| 716 | result['base_period']['timestamps'].append(base[energy_category_id]['timestamps']) |
|
| 717 | result['base_period']['values_saving'].append(base[energy_category_id]['values_saving']) |
|
| 718 | result['base_period']['subtotals_saving'].append(base[energy_category_id]['subtotal_saving']) |
|
| 719 | result['base_period']['subtotals_in_kgce_saving'].append( |
|
| 720 | base[energy_category_id]['subtotal_in_kgce_saving']) |
|
| 721 | result['base_period']['subtotals_in_kgco2e_saving'].append( |
|
| 722 | base[energy_category_id]['subtotal_in_kgco2e_saving']) |
|
| 723 | result['base_period']['total_in_kgce_saving'] += base[energy_category_id]['subtotal_in_kgce_saving'] |
|
| 724 | result['base_period']['total_in_kgco2e_saving'] += base[energy_category_id]['subtotal_in_kgco2e_saving'] |
|
| 725 | ||
| 726 | result['reporting_period'] = dict() |
|
| 727 | result['reporting_period']['names'] = list() |
|
| 728 | result['reporting_period']['energy_category_ids'] = list() |
|
| 729 | result['reporting_period']['units'] = list() |
|
| 730 | result['reporting_period']['timestamps'] = list() |
|
| 731 | result['reporting_period']['values_saving'] = list() |
|
| 732 | result['reporting_period']['rates_saving'] = list() |
|
| 733 | result['reporting_period']['subtotals_saving'] = list() |
|
| 734 | result['reporting_period']['subtotals_in_kgce_saving'] = list() |
|
| 735 | result['reporting_period']['subtotals_in_kgco2e_saving'] = list() |
|
| 736 | result['reporting_period']['subtotals_per_unit_area_saving'] = list() |
|
| 737 | result['reporting_period']['increment_rates_saving'] = list() |
|
| 738 | result['reporting_period']['total_in_kgce_saving'] = Decimal(0.0) |
|
| 739 | result['reporting_period']['total_in_kgco2e_saving'] = Decimal(0.0) |
|
| 740 | result['reporting_period']['increment_rate_in_kgce_saving'] = Decimal(0.0) |
|
| 741 | result['reporting_period']['increment_rate_in_kgco2e_saving'] = Decimal(0.0) |
|
| 742 | ||
| 743 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
| 744 | for energy_category_id in energy_category_set: |
|
| 745 | result['reporting_period']['names'].append(energy_category_dict[energy_category_id]['name']) |
|
| 746 | result['reporting_period']['energy_category_ids'].append(energy_category_id) |
|
| 747 | result['reporting_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) |
|
| 748 | result['reporting_period']['timestamps'].append(reporting[energy_category_id]['timestamps']) |
|
| 749 | result['reporting_period']['values_saving'].append(reporting[energy_category_id]['values_saving']) |
|
| 750 | result['reporting_period']['subtotals_saving'].append(reporting[energy_category_id]['subtotal_saving']) |
|
| 751 | result['reporting_period']['subtotals_in_kgce_saving'].append( |
|
| 752 | reporting[energy_category_id]['subtotal_in_kgce_saving']) |
|
| 753 | result['reporting_period']['subtotals_in_kgco2e_saving'].append( |
|
| 754 | reporting[energy_category_id]['subtotal_in_kgco2e_saving']) |
|
| 755 | result['reporting_period']['subtotals_per_unit_area_saving'].append( |
|
| 756 | reporting[energy_category_id]['subtotal_saving'] / shopfloor['area'] |
|
| 757 | if shopfloor['area'] > Decimal(0.0) else None) |
|
| 758 | result['reporting_period']['increment_rates_saving'].append( |
|
| 759 | (reporting[energy_category_id]['subtotal_saving'] - base[energy_category_id]['subtotal_saving']) / |
|
| 760 | base[energy_category_id]['subtotal_saving'] |
|
| 761 | if base[energy_category_id]['subtotal_saving'] != Decimal(0.0) else None) |
|
| 762 | result['reporting_period']['total_in_kgce_saving'] += \ |
|
| 763 | reporting[energy_category_id]['subtotal_in_kgce_saving'] |
|
| 764 | result['reporting_period']['total_in_kgco2e_saving'] += \ |
|
| 765 | reporting[energy_category_id]['subtotal_in_kgco2e_saving'] |
|
| 766 | ||
| 767 | rate = list() |
|
| 768 | for index, value in enumerate(reporting[energy_category_id]['values_saving']): |
|
| 769 | if index < len(base[energy_category_id]['values_saving']) \ |
|
| 770 | and base[energy_category_id]['values_saving'][index] != 0 and value != 0: |
|
| 771 | rate.append((value - base[energy_category_id]['values_saving'][index]) |
|
| 772 | / base[energy_category_id]['values_saving'][index]) |
|
| 773 | else: |
|
| 774 | rate.append(None) |
|
| 775 | result['reporting_period']['rates_saving'].append(rate) |
|
| 776 | ||
| 777 | result['reporting_period']['total_in_kgco2e_per_unit_area_saving'] = \ |
|
| 778 | result['reporting_period']['total_in_kgce_saving'] / shopfloor['area'] \ |
|
| 779 | if shopfloor['area'] > 0.0 else None |
|
| 780 | ||
| 781 | result['reporting_period']['increment_rate_in_kgce_saving'] = \ |
|
| 782 | (result['reporting_period']['total_in_kgce_saving'] - result['base_period']['total_in_kgce_saving']) / \ |
|
| 783 | result['base_period']['total_in_kgce_saving'] \ |
|
| 784 | if result['base_period']['total_in_kgce_saving'] != Decimal(0.0) else None |
|
| 785 | ||
| 786 | result['reporting_period']['total_in_kgce_per_unit_area_saving'] = \ |
|
| 787 | result['reporting_period']['total_in_kgco2e_saving'] / shopfloor['area'] \ |
|
| 788 | if shopfloor['area'] > Decimal(0.0) else None |
|
| 789 | ||
| 790 | result['reporting_period']['increment_rate_in_kgco2e_saving'] = \ |
|
| 791 | (result['reporting_period']['total_in_kgco2e_saving'] - result['base_period']['total_in_kgco2e_saving']) / \ |
|
| 792 | result['base_period']['total_in_kgco2e_saving'] \ |
|
| 793 | if result['base_period']['total_in_kgco2e_saving'] != Decimal(0.0) else None |
|
| 794 | ||
| 795 | result['parameters'] = { |
|
| 796 | "names": parameters_data['names'], |
|
| 797 | "timestamps": parameters_data['timestamps'], |
|
| 798 | "values": parameters_data['values'] |
|
| 799 | } |
|
| 800 | ||
| 801 | result['excel_bytes_base64'] = None |
|
| 802 | if not is_quick_mode: |
|
| 803 | result['excel_bytes_base64'] = excelexporters.shopfloorsaving.export(result, |
|
| 804 | shopfloor['name'], |
|
| 805 | base_period_start_datetime_local, |
|
| 806 | base_period_end_datetime_local, |
|
| 807 | reporting_period_start_datetime_local, |
|
| 808 | reporting_period_end_datetime_local, |
|
| 809 | period_type, |
|
| 810 | language) |
|
| 811 | ||
| 812 | resp.text = json.dumps(result) |
|
| 813 | ||
| @@ 45-812 (lines=768) @@ | ||
| 42 | from core.useractivity import access_control, api_key_control |
|
| 43 | ||
| 44 | ||
| 45 | class Reporting: |
|
| 46 | def __init__(self): |
|
| 47 | """"Initializes Reporting""" |
|
| 48 | pass |
|
| 49 | ||
| 50 | @staticmethod |
|
| 51 | def on_options(req, resp): |
|
| 52 | _ = req |
|
| 53 | resp.status = falcon.HTTP_200 |
|
| 54 | ||
| 55 | #################################################################################################################### |
|
| 56 | # PROCEDURES |
|
| 57 | # Step 1: valid parameters |
|
| 58 | # Step 2: query the shopfloor |
|
| 59 | # Step 3: query energy categories |
|
| 60 | # Step 4: query associated sensors |
|
| 61 | # Step 5: query associated points |
|
| 62 | # Step 6: query base period energy saving |
|
| 63 | # Step 7: query reporting period energy saving |
|
| 64 | # Step 8: query tariff data |
|
| 65 | # Step 9: query associated sensors and points data |
|
| 66 | # Step 10: construct the report |
|
| 67 | #################################################################################################################### |
|
| 68 | @staticmethod |
|
| 69 | def on_get(req, resp): |
|
| 70 | if 'API-KEY' not in req.headers or \ |
|
| 71 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 72 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 73 | access_control(req) |
|
| 74 | else: |
|
| 75 | api_key_control(req) |
|
| 76 | print(req.params) |
|
| 77 | shopfloor_id = req.params.get('shopfloorid') |
|
| 78 | shopfloor_uuid = req.params.get('shopflooruuid') |
|
| 79 | period_type = req.params.get('periodtype') |
|
| 80 | base_period_start_datetime_local = req.params.get('baseperiodstartdatetime') |
|
| 81 | base_period_end_datetime_local = req.params.get('baseperiodenddatetime') |
|
| 82 | reporting_period_start_datetime_local = req.params.get('reportingperiodstartdatetime') |
|
| 83 | reporting_period_end_datetime_local = req.params.get('reportingperiodenddatetime') |
|
| 84 | language = req.params.get('language') |
|
| 85 | quick_mode = req.params.get('quickmode') |
|
| 86 | ||
| 87 | ################################################################################################################ |
|
| 88 | # Step 1: valid parameters |
|
| 89 | ################################################################################################################ |
|
| 90 | if shopfloor_id is None and shopfloor_uuid is None: |
|
| 91 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 92 | title='API.BAD_REQUEST', |
|
| 93 | description='API.INVALID_SHOPFLOOR_ID') |
|
| 94 | ||
| 95 | if shopfloor_id is not None: |
|
| 96 | shopfloor_id = str.strip(shopfloor_id) |
|
| 97 | if not shopfloor_id.isdigit() or int(shopfloor_id) <= 0: |
|
| 98 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 99 | title='API.BAD_REQUEST', |
|
| 100 | description='API.INVALID_SHOPFLOOR_ID') |
|
| 101 | ||
| 102 | if shopfloor_uuid is not None: |
|
| 103 | regex = re.compile(r'^[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) |
|
| 104 | match = regex.match(str.strip(shopfloor_uuid)) |
|
| 105 | if not bool(match): |
|
| 106 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 107 | title='API.BAD_REQUEST', |
|
| 108 | description='API.INVALID_SHOPFLOOR_UUID') |
|
| 109 | ||
| 110 | if period_type is None: |
|
| 111 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 112 | description='API.INVALID_PERIOD_TYPE') |
|
| 113 | else: |
|
| 114 | period_type = str.strip(period_type) |
|
| 115 | if period_type not in ['hourly', 'daily', 'weekly', 'monthly', 'yearly']: |
|
| 116 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 117 | description='API.INVALID_PERIOD_TYPE') |
|
| 118 | ||
| 119 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
|
| 120 | if config.utc_offset[0] == '-': |
|
| 121 | timezone_offset = -timezone_offset |
|
| 122 | ||
| 123 | base_start_datetime_utc = None |
|
| 124 | if base_period_start_datetime_local is not None and len(str.strip(base_period_start_datetime_local)) > 0: |
|
| 125 | base_period_start_datetime_local = str.strip(base_period_start_datetime_local) |
|
| 126 | try: |
|
| 127 | base_start_datetime_utc = datetime.strptime(base_period_start_datetime_local, '%Y-%m-%dT%H:%M:%S') |
|
| 128 | except ValueError: |
|
| 129 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 130 | description="API.INVALID_BASE_PERIOD_START_DATETIME") |
|
| 131 | base_start_datetime_utc = \ |
|
| 132 | base_start_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset) |
|
| 133 | # nomalize the start datetime |
|
| 134 | if config.minutes_to_count == 30 and base_start_datetime_utc.minute >= 30: |
|
| 135 | base_start_datetime_utc = base_start_datetime_utc.replace(minute=30, second=0, microsecond=0) |
|
| 136 | else: |
|
| 137 | base_start_datetime_utc = base_start_datetime_utc.replace(minute=0, second=0, microsecond=0) |
|
| 138 | ||
| 139 | base_end_datetime_utc = None |
|
| 140 | if base_period_end_datetime_local is not None and len(str.strip(base_period_end_datetime_local)) > 0: |
|
| 141 | base_period_end_datetime_local = str.strip(base_period_end_datetime_local) |
|
| 142 | try: |
|
| 143 | base_end_datetime_utc = datetime.strptime(base_period_end_datetime_local, '%Y-%m-%dT%H:%M:%S') |
|
| 144 | except ValueError: |
|
| 145 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 146 | description="API.INVALID_BASE_PERIOD_END_DATETIME") |
|
| 147 | base_end_datetime_utc = \ |
|
| 148 | base_end_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset) |
|
| 149 | ||
| 150 | if base_start_datetime_utc is not None and base_end_datetime_utc is not None and \ |
|
| 151 | base_start_datetime_utc >= base_end_datetime_utc: |
|
| 152 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 153 | description='API.INVALID_BASE_PERIOD_END_DATETIME') |
|
| 154 | ||
| 155 | if reporting_period_start_datetime_local is None: |
|
| 156 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 157 | description="API.INVALID_REPORTING_PERIOD_START_DATETIME") |
|
| 158 | else: |
|
| 159 | reporting_period_start_datetime_local = str.strip(reporting_period_start_datetime_local) |
|
| 160 | try: |
|
| 161 | reporting_start_datetime_utc = datetime.strptime(reporting_period_start_datetime_local, |
|
| 162 | '%Y-%m-%dT%H:%M:%S') |
|
| 163 | except ValueError: |
|
| 164 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 165 | description="API.INVALID_REPORTING_PERIOD_START_DATETIME") |
|
| 166 | reporting_start_datetime_utc = \ |
|
| 167 | reporting_start_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset) |
|
| 168 | # nomalize the start datetime |
|
| 169 | if config.minutes_to_count == 30 and reporting_start_datetime_utc.minute >= 30: |
|
| 170 | reporting_start_datetime_utc = reporting_start_datetime_utc.replace(minute=30, second=0, microsecond=0) |
|
| 171 | else: |
|
| 172 | reporting_start_datetime_utc = reporting_start_datetime_utc.replace(minute=0, second=0, microsecond=0) |
|
| 173 | ||
| 174 | if reporting_period_end_datetime_local is None: |
|
| 175 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 176 | description="API.INVALID_REPORTING_PERIOD_END_DATETIME") |
|
| 177 | else: |
|
| 178 | reporting_period_end_datetime_local = str.strip(reporting_period_end_datetime_local) |
|
| 179 | try: |
|
| 180 | reporting_end_datetime_utc = datetime.strptime(reporting_period_end_datetime_local, |
|
| 181 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
|
| 182 | timedelta(minutes=timezone_offset) |
|
| 183 | except ValueError: |
|
| 184 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 185 | description="API.INVALID_REPORTING_PERIOD_END_DATETIME") |
|
| 186 | ||
| 187 | if reporting_start_datetime_utc >= reporting_end_datetime_utc: |
|
| 188 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 189 | description='API.INVALID_REPORTING_PERIOD_END_DATETIME') |
|
| 190 | ||
| 191 | # if turn quick mode on, do not return parameters data and excel file |
|
| 192 | is_quick_mode = False |
|
| 193 | if quick_mode is not None and \ |
|
| 194 | len(str.strip(quick_mode)) > 0 and \ |
|
| 195 | str.lower(str.strip(quick_mode)) in ('true', 't', 'on', 'yes', 'y'): |
|
| 196 | is_quick_mode = True |
|
| 197 | ||
| 198 | trans = utilities.get_translation(language) |
|
| 199 | trans.install() |
|
| 200 | _ = trans.gettext |
|
| 201 | ||
| 202 | ################################################################################################################ |
|
| 203 | # Step 2: query the shopfloor |
|
| 204 | ################################################################################################################ |
|
| 205 | cnx_system = mysql.connector.connect(**config.myems_system_db) |
|
| 206 | cursor_system = cnx_system.cursor() |
|
| 207 | ||
| 208 | cnx_energy = mysql.connector.connect(**config.myems_energy_db) |
|
| 209 | cursor_energy = cnx_energy.cursor() |
|
| 210 | ||
| 211 | cnx_energy_plan = mysql.connector.connect(**config.myems_energy_plan_db) |
|
| 212 | cursor_energy_plan = cnx_energy_plan.cursor() |
|
| 213 | ||
| 214 | cnx_historical = mysql.connector.connect(**config.myems_historical_db) |
|
| 215 | cursor_historical = cnx_historical.cursor() |
|
| 216 | ||
| 217 | if shopfloor_id is not None: |
|
| 218 | cursor_system.execute(" SELECT id, name, area, cost_center_id " |
|
| 219 | " FROM tbl_shopfloors " |
|
| 220 | " WHERE id = %s ", (shopfloor_id,)) |
|
| 221 | row_shopfloor = cursor_system.fetchone() |
|
| 222 | elif shopfloor_uuid is not None: |
|
| 223 | cursor_system.execute(" SELECT id, name, area, cost_center_id " |
|
| 224 | " FROM tbl_shopfloors " |
|
| 225 | " WHERE uuid = %s ", (shopfloor_uuid,)) |
|
| 226 | row_shopfloor = cursor_system.fetchone() |
|
| 227 | ||
| 228 | if row_shopfloor is None: |
|
| 229 | if cursor_system: |
|
| 230 | cursor_system.close() |
|
| 231 | if cnx_system: |
|
| 232 | cnx_system.close() |
|
| 233 | ||
| 234 | if cursor_energy: |
|
| 235 | cursor_energy.close() |
|
| 236 | if cnx_energy: |
|
| 237 | cnx_energy.close() |
|
| 238 | ||
| 239 | if cursor_energy_plan: |
|
| 240 | cursor_energy_plan.close() |
|
| 241 | if cnx_energy_plan: |
|
| 242 | cnx_energy_plan.close() |
|
| 243 | ||
| 244 | if cursor_historical: |
|
| 245 | cursor_historical.close() |
|
| 246 | if cnx_historical: |
|
| 247 | cnx_historical.close() |
|
| 248 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', description='API.SHOPFLOOR_NOT_FOUND') |
|
| 249 | ||
| 250 | shopfloor = dict() |
|
| 251 | shopfloor['id'] = row_shopfloor[0] |
|
| 252 | shopfloor['name'] = row_shopfloor[1] |
|
| 253 | shopfloor['area'] = row_shopfloor[2] |
|
| 254 | shopfloor['cost_center_id'] = row_shopfloor[3] |
|
| 255 | ||
| 256 | ################################################################################################################ |
|
| 257 | # Step 3: query energy categories |
|
| 258 | ################################################################################################################ |
|
| 259 | energy_category_set = set() |
|
| 260 | # query energy categories in base period |
|
| 261 | cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
|
| 262 | " FROM tbl_shopfloor_input_category_hourly " |
|
| 263 | " WHERE shopfloor_id = %s " |
|
| 264 | " AND start_datetime_utc >= %s " |
|
| 265 | " AND start_datetime_utc < %s ", |
|
| 266 | (shopfloor['id'], base_start_datetime_utc, base_end_datetime_utc)) |
|
| 267 | rows_energy_categories = cursor_energy.fetchall() |
|
| 268 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
| 269 | for row_energy_category in rows_energy_categories: |
|
| 270 | energy_category_set.add(row_energy_category[0]) |
|
| 271 | ||
| 272 | # query energy categories in reporting period |
|
| 273 | cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
|
| 274 | " FROM tbl_shopfloor_input_category_hourly " |
|
| 275 | " WHERE shopfloor_id = %s " |
|
| 276 | " AND start_datetime_utc >= %s " |
|
| 277 | " AND start_datetime_utc < %s ", |
|
| 278 | (shopfloor['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) |
|
| 279 | rows_energy_categories = cursor_energy.fetchall() |
|
| 280 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
| 281 | for row_energy_category in rows_energy_categories: |
|
| 282 | energy_category_set.add(row_energy_category[0]) |
|
| 283 | ||
| 284 | # query all energy categories in base period and reporting period |
|
| 285 | cursor_system.execute(" SELECT id, name, unit_of_measure, kgce, kgco2e " |
|
| 286 | " FROM tbl_energy_categories " |
|
| 287 | " ORDER BY id ", ) |
|
| 288 | rows_energy_categories = cursor_system.fetchall() |
|
| 289 | if rows_energy_categories is None or len(rows_energy_categories) == 0: |
|
| 290 | if cursor_system: |
|
| 291 | cursor_system.close() |
|
| 292 | if cnx_system: |
|
| 293 | cnx_system.close() |
|
| 294 | ||
| 295 | if cursor_energy: |
|
| 296 | cursor_energy.close() |
|
| 297 | if cnx_energy: |
|
| 298 | cnx_energy.close() |
|
| 299 | ||
| 300 | if cursor_energy_plan: |
|
| 301 | cursor_energy_plan.close() |
|
| 302 | if cnx_energy_plan: |
|
| 303 | cnx_energy_plan.close() |
|
| 304 | ||
| 305 | if cursor_historical: |
|
| 306 | cursor_historical.close() |
|
| 307 | if cnx_historical: |
|
| 308 | cnx_historical.close() |
|
| 309 | raise falcon.HTTPError(status=falcon.HTTP_404, |
|
| 310 | title='API.NOT_FOUND', |
|
| 311 | description='API.ENERGY_CATEGORY_NOT_FOUND') |
|
| 312 | energy_category_dict = dict() |
|
| 313 | for row_energy_category in rows_energy_categories: |
|
| 314 | if row_energy_category[0] in energy_category_set: |
|
| 315 | energy_category_dict[row_energy_category[0]] = {"name": row_energy_category[1], |
|
| 316 | "unit_of_measure": row_energy_category[2], |
|
| 317 | "kgce": row_energy_category[3], |
|
| 318 | "kgco2e": row_energy_category[4]} |
|
| 319 | ||
| 320 | ################################################################################################################ |
|
| 321 | # Step 4: query associated sensors |
|
| 322 | ################################################################################################################ |
|
| 323 | point_list = list() |
|
| 324 | cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type " |
|
| 325 | " FROM tbl_shopfloors st, tbl_sensors se, tbl_shopfloors_sensors ss, " |
|
| 326 | " tbl_points p, tbl_sensors_points sp " |
|
| 327 | " WHERE st.id = %s AND st.id = ss.shopfloor_id AND ss.sensor_id = se.id " |
|
| 328 | " AND se.id = sp.sensor_id AND sp.point_id = p.id " |
|
| 329 | " ORDER BY p.id ", (shopfloor['id'],)) |
|
| 330 | rows_points = cursor_system.fetchall() |
|
| 331 | if rows_points is not None and len(rows_points) > 0: |
|
| 332 | for row in rows_points: |
|
| 333 | point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]}) |
|
| 334 | ||
| 335 | ################################################################################################################ |
|
| 336 | # Step 5: query associated points |
|
| 337 | ################################################################################################################ |
|
| 338 | cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type " |
|
| 339 | " FROM tbl_shopfloors s, tbl_shopfloors_points sp, tbl_points p " |
|
| 340 | " WHERE s.id = %s AND s.id = sp.shopfloor_id AND sp.point_id = p.id " |
|
| 341 | " ORDER BY p.id ", (shopfloor['id'],)) |
|
| 342 | rows_points = cursor_system.fetchall() |
|
| 343 | if rows_points is not None and len(rows_points) > 0: |
|
| 344 | for row in rows_points: |
|
| 345 | point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]}) |
|
| 346 | ||
| 347 | ################################################################################################################ |
|
| 348 | # Step 6: query base period energy saving |
|
| 349 | ################################################################################################################ |
|
| 350 | base = dict() |
|
| 351 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
| 352 | for energy_category_id in energy_category_set: |
|
| 353 | kgce = energy_category_dict[energy_category_id]['kgce'] |
|
| 354 | kgco2e = energy_category_dict[energy_category_id]['kgco2e'] |
|
| 355 | ||
| 356 | base[energy_category_id] = dict() |
|
| 357 | base[energy_category_id]['timestamps'] = list() |
|
| 358 | base[energy_category_id]['values_plan'] = list() |
|
| 359 | base[energy_category_id]['values_actual'] = list() |
|
| 360 | base[energy_category_id]['values_saving'] = list() |
|
| 361 | base[energy_category_id]['subtotal_plan'] = Decimal(0.0) |
|
| 362 | base[energy_category_id]['subtotal_actual'] = Decimal(0.0) |
|
| 363 | base[energy_category_id]['subtotal_saving'] = Decimal(0.0) |
|
| 364 | base[energy_category_id]['subtotal_in_kgce_plan'] = Decimal(0.0) |
|
| 365 | base[energy_category_id]['subtotal_in_kgce_actual'] = Decimal(0.0) |
|
| 366 | base[energy_category_id]['subtotal_in_kgce_saving'] = Decimal(0.0) |
|
| 367 | base[energy_category_id]['subtotal_in_kgco2e_plan'] = Decimal(0.0) |
|
| 368 | base[energy_category_id]['subtotal_in_kgco2e_actual'] = Decimal(0.0) |
|
| 369 | base[energy_category_id]['subtotal_in_kgco2e_saving'] = Decimal(0.0) |
|
| 370 | # query base period's energy plan |
|
| 371 | cursor_energy_plan.execute(" SELECT start_datetime_utc, actual_value " |
|
| 372 | " FROM tbl_shopfloor_input_category_hourly " |
|
| 373 | " WHERE shopfloor_id = %s " |
|
| 374 | " AND energy_category_id = %s " |
|
| 375 | " AND start_datetime_utc >= %s " |
|
| 376 | " AND start_datetime_utc < %s " |
|
| 377 | " ORDER BY start_datetime_utc ", |
|
| 378 | (shopfloor['id'], |
|
| 379 | energy_category_id, |
|
| 380 | base_start_datetime_utc, |
|
| 381 | base_end_datetime_utc)) |
|
| 382 | rows_shopfloor_hourly = cursor_energy_plan.fetchall() |
|
| 383 | ||
| 384 | rows_shopfloor_periodically = utilities.aggregate_hourly_data_by_period(rows_shopfloor_hourly, |
|
| 385 | base_start_datetime_utc, |
|
| 386 | base_end_datetime_utc, |
|
| 387 | period_type) |
|
| 388 | for row_shopfloor_periodically in rows_shopfloor_periodically: |
|
| 389 | current_datetime_local = row_shopfloor_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
| 390 | timedelta(minutes=timezone_offset) |
|
| 391 | if period_type == 'hourly': |
|
| 392 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 393 | elif period_type == 'daily': |
|
| 394 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 395 | elif period_type == 'weekly': |
|
| 396 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 397 | elif period_type == 'monthly': |
|
| 398 | current_datetime = current_datetime_local.isoformat()[0:7] |
|
| 399 | elif period_type == 'yearly': |
|
| 400 | current_datetime = current_datetime_local.isoformat()[0:4] |
|
| 401 | ||
| 402 | plan_value = Decimal(0.0) if row_shopfloor_periodically[1] is None \ |
|
| 403 | else row_shopfloor_periodically[1] |
|
| 404 | base[energy_category_id]['timestamps'].append(current_datetime) |
|
| 405 | base[energy_category_id]['values_plan'].append(plan_value) |
|
| 406 | base[energy_category_id]['subtotal_plan'] += plan_value |
|
| 407 | base[energy_category_id]['subtotal_in_kgce_plan'] += plan_value * kgce |
|
| 408 | base[energy_category_id]['subtotal_in_kgco2e_plan'] += plan_value * kgco2e |
|
| 409 | ||
| 410 | # query base period's energy actual |
|
| 411 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
|
| 412 | " FROM tbl_shopfloor_input_category_hourly " |
|
| 413 | " WHERE shopfloor_id = %s " |
|
| 414 | " AND energy_category_id = %s " |
|
| 415 | " AND start_datetime_utc >= %s " |
|
| 416 | " AND start_datetime_utc < %s " |
|
| 417 | " ORDER BY start_datetime_utc ", |
|
| 418 | (shopfloor['id'], |
|
| 419 | energy_category_id, |
|
| 420 | base_start_datetime_utc, |
|
| 421 | base_end_datetime_utc)) |
|
| 422 | rows_shopfloor_hourly = cursor_energy.fetchall() |
|
| 423 | ||
| 424 | rows_shopfloor_periodically = utilities.aggregate_hourly_data_by_period(rows_shopfloor_hourly, |
|
| 425 | base_start_datetime_utc, |
|
| 426 | base_end_datetime_utc, |
|
| 427 | period_type) |
|
| 428 | for row_shopfloor_periodically in rows_shopfloor_periodically: |
|
| 429 | current_datetime_local = row_shopfloor_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
| 430 | timedelta(minutes=timezone_offset) |
|
| 431 | if period_type == 'hourly': |
|
| 432 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 433 | elif period_type == 'daily': |
|
| 434 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 435 | elif period_type == 'weekly': |
|
| 436 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 437 | elif period_type == 'monthly': |
|
| 438 | current_datetime = current_datetime_local.isoformat()[0:7] |
|
| 439 | elif period_type == 'yearly': |
|
| 440 | current_datetime = current_datetime_local.isoformat()[0:4] |
|
| 441 | ||
| 442 | actual_value = Decimal(0.0) if row_shopfloor_periodically[1] is None \ |
|
| 443 | else row_shopfloor_periodically[1] |
|
| 444 | base[energy_category_id]['values_actual'].append(actual_value) |
|
| 445 | base[energy_category_id]['subtotal_actual'] += actual_value |
|
| 446 | base[energy_category_id]['subtotal_in_kgce_actual'] += actual_value * kgce |
|
| 447 | base[energy_category_id]['subtotal_in_kgco2e_actual'] += actual_value * kgco2e |
|
| 448 | ||
| 449 | # calculate base period's energy savings |
|
| 450 | for i in range(len(base[energy_category_id]['values_plan'])): |
|
| 451 | base[energy_category_id]['values_saving'].append( |
|
| 452 | base[energy_category_id]['values_plan'][i] - |
|
| 453 | base[energy_category_id]['values_actual'][i]) |
|
| 454 | ||
| 455 | base[energy_category_id]['subtotal_saving'] = \ |
|
| 456 | base[energy_category_id]['subtotal_plan'] - \ |
|
| 457 | base[energy_category_id]['subtotal_actual'] |
|
| 458 | base[energy_category_id]['subtotal_in_kgce_saving'] = \ |
|
| 459 | base[energy_category_id]['subtotal_in_kgce_plan'] - \ |
|
| 460 | base[energy_category_id]['subtotal_in_kgce_actual'] |
|
| 461 | base[energy_category_id]['subtotal_in_kgco2e_saving'] = \ |
|
| 462 | base[energy_category_id]['subtotal_in_kgco2e_plan'] - \ |
|
| 463 | base[energy_category_id]['subtotal_in_kgco2e_actual'] |
|
| 464 | ################################################################################################################ |
|
| 465 | # Step 7: query reporting period energy saving |
|
| 466 | ################################################################################################################ |
|
| 467 | reporting = dict() |
|
| 468 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
| 469 | for energy_category_id in energy_category_set: |
|
| 470 | kgce = energy_category_dict[energy_category_id]['kgce'] |
|
| 471 | kgco2e = energy_category_dict[energy_category_id]['kgco2e'] |
|
| 472 | ||
| 473 | reporting[energy_category_id] = dict() |
|
| 474 | reporting[energy_category_id]['timestamps'] = list() |
|
| 475 | reporting[energy_category_id]['values_plan'] = list() |
|
| 476 | reporting[energy_category_id]['values_actual'] = list() |
|
| 477 | reporting[energy_category_id]['values_saving'] = list() |
|
| 478 | reporting[energy_category_id]['subtotal_plan'] = Decimal(0.0) |
|
| 479 | reporting[energy_category_id]['subtotal_actual'] = Decimal(0.0) |
|
| 480 | reporting[energy_category_id]['subtotal_saving'] = Decimal(0.0) |
|
| 481 | reporting[energy_category_id]['subtotal_in_kgce_plan'] = Decimal(0.0) |
|
| 482 | reporting[energy_category_id]['subtotal_in_kgce_actual'] = Decimal(0.0) |
|
| 483 | reporting[energy_category_id]['subtotal_in_kgce_saving'] = Decimal(0.0) |
|
| 484 | reporting[energy_category_id]['subtotal_in_kgco2e_plan'] = Decimal(0.0) |
|
| 485 | reporting[energy_category_id]['subtotal_in_kgco2e_actual'] = Decimal(0.0) |
|
| 486 | reporting[energy_category_id]['subtotal_in_kgco2e_saving'] = Decimal(0.0) |
|
| 487 | # query reporting period's energy plan |
|
| 488 | cursor_energy_plan.execute(" SELECT start_datetime_utc, actual_value " |
|
| 489 | " FROM tbl_shopfloor_input_category_hourly " |
|
| 490 | " WHERE shopfloor_id = %s " |
|
| 491 | " AND energy_category_id = %s " |
|
| 492 | " AND start_datetime_utc >= %s " |
|
| 493 | " AND start_datetime_utc < %s " |
|
| 494 | " ORDER BY start_datetime_utc ", |
|
| 495 | (shopfloor['id'], |
|
| 496 | energy_category_id, |
|
| 497 | reporting_start_datetime_utc, |
|
| 498 | reporting_end_datetime_utc)) |
|
| 499 | rows_shopfloor_hourly = cursor_energy_plan.fetchall() |
|
| 500 | ||
| 501 | rows_shopfloor_periodically = utilities.aggregate_hourly_data_by_period(rows_shopfloor_hourly, |
|
| 502 | reporting_start_datetime_utc, |
|
| 503 | reporting_end_datetime_utc, |
|
| 504 | period_type) |
|
| 505 | for row_shopfloor_periodically in rows_shopfloor_periodically: |
|
| 506 | current_datetime_local = row_shopfloor_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
| 507 | timedelta(minutes=timezone_offset) |
|
| 508 | if period_type == 'hourly': |
|
| 509 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 510 | elif period_type == 'daily': |
|
| 511 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 512 | elif period_type == 'weekly': |
|
| 513 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 514 | elif period_type == 'monthly': |
|
| 515 | current_datetime = current_datetime_local.isoformat()[0:7] |
|
| 516 | elif period_type == 'yearly': |
|
| 517 | current_datetime = current_datetime_local.isoformat()[0:4] |
|
| 518 | ||
| 519 | plan_value = Decimal(0.0) if row_shopfloor_periodically[1] is None \ |
|
| 520 | else row_shopfloor_periodically[1] |
|
| 521 | reporting[energy_category_id]['timestamps'].append(current_datetime) |
|
| 522 | reporting[energy_category_id]['values_plan'].append(plan_value) |
|
| 523 | reporting[energy_category_id]['subtotal_plan'] += plan_value |
|
| 524 | reporting[energy_category_id]['subtotal_in_kgce_plan'] += plan_value * kgce |
|
| 525 | reporting[energy_category_id]['subtotal_in_kgco2e_plan'] += plan_value * kgco2e |
|
| 526 | ||
| 527 | # query reporting period's energy actual |
|
| 528 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
|
| 529 | " FROM tbl_shopfloor_input_category_hourly " |
|
| 530 | " WHERE shopfloor_id = %s " |
|
| 531 | " AND energy_category_id = %s " |
|
| 532 | " AND start_datetime_utc >= %s " |
|
| 533 | " AND start_datetime_utc < %s " |
|
| 534 | " ORDER BY start_datetime_utc ", |
|
| 535 | (shopfloor['id'], |
|
| 536 | energy_category_id, |
|
| 537 | reporting_start_datetime_utc, |
|
| 538 | reporting_end_datetime_utc)) |
|
| 539 | rows_shopfloor_hourly = cursor_energy.fetchall() |
|
| 540 | ||
| 541 | rows_shopfloor_periodically = utilities.aggregate_hourly_data_by_period(rows_shopfloor_hourly, |
|
| 542 | reporting_start_datetime_utc, |
|
| 543 | reporting_end_datetime_utc, |
|
| 544 | period_type) |
|
| 545 | for row_shopfloor_periodically in rows_shopfloor_periodically: |
|
| 546 | current_datetime_local = row_shopfloor_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
| 547 | timedelta(minutes=timezone_offset) |
|
| 548 | if period_type == 'hourly': |
|
| 549 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 550 | elif period_type == 'daily': |
|
| 551 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 552 | elif period_type == 'weekly': |
|
| 553 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 554 | elif period_type == 'monthly': |
|
| 555 | current_datetime = current_datetime_local.isoformat()[0:7] |
|
| 556 | elif period_type == 'yearly': |
|
| 557 | current_datetime = current_datetime_local.isoformat()[0:4] |
|
| 558 | ||
| 559 | actual_value = Decimal(0.0) if row_shopfloor_periodically[1] is None \ |
|
| 560 | else row_shopfloor_periodically[1] |
|
| 561 | reporting[energy_category_id]['values_actual'].append(actual_value) |
|
| 562 | reporting[energy_category_id]['subtotal_actual'] += actual_value |
|
| 563 | reporting[energy_category_id]['subtotal_in_kgce_actual'] += actual_value * kgce |
|
| 564 | reporting[energy_category_id]['subtotal_in_kgco2e_actual'] += actual_value * kgco2e |
|
| 565 | ||
| 566 | # calculate reporting period's energy savings |
|
| 567 | for i in range(len(reporting[energy_category_id]['values_plan'])): |
|
| 568 | reporting[energy_category_id]['values_saving'].append( |
|
| 569 | reporting[energy_category_id]['values_plan'][i] - |
|
| 570 | reporting[energy_category_id]['values_actual'][i]) |
|
| 571 | ||
| 572 | reporting[energy_category_id]['subtotal_saving'] = \ |
|
| 573 | reporting[energy_category_id]['subtotal_plan'] - \ |
|
| 574 | reporting[energy_category_id]['subtotal_actual'] |
|
| 575 | reporting[energy_category_id]['subtotal_in_kgce_saving'] = \ |
|
| 576 | reporting[energy_category_id]['subtotal_in_kgce_plan'] - \ |
|
| 577 | reporting[energy_category_id]['subtotal_in_kgce_actual'] |
|
| 578 | reporting[energy_category_id]['subtotal_in_kgco2e_saving'] = \ |
|
| 579 | reporting[energy_category_id]['subtotal_in_kgco2e_plan'] - \ |
|
| 580 | reporting[energy_category_id]['subtotal_in_kgco2e_actual'] |
|
| 581 | ################################################################################################################ |
|
| 582 | # Step 8: query tariff data |
|
| 583 | ################################################################################################################ |
|
| 584 | parameters_data = dict() |
|
| 585 | parameters_data['names'] = list() |
|
| 586 | parameters_data['timestamps'] = list() |
|
| 587 | parameters_data['values'] = list() |
|
| 588 | if config.is_tariff_appended and energy_category_set is not None and len(energy_category_set) > 0 \ |
|
| 589 | and not is_quick_mode: |
|
| 590 | for energy_category_id in energy_category_set: |
|
| 591 | energy_category_tariff_dict = utilities.get_energy_category_tariffs(shopfloor['cost_center_id'], |
|
| 592 | energy_category_id, |
|
| 593 | reporting_start_datetime_utc, |
|
| 594 | reporting_end_datetime_utc) |
|
| 595 | tariff_timestamp_list = list() |
|
| 596 | tariff_value_list = list() |
|
| 597 | for k, v in energy_category_tariff_dict.items(): |
|
| 598 | # convert k from utc to local |
|
| 599 | k = k + timedelta(minutes=timezone_offset) |
|
| 600 | tariff_timestamp_list.append(k.isoformat()[0:19]) |
|
| 601 | tariff_value_list.append(v) |
|
| 602 | ||
| 603 | parameters_data['names'].append(_('Tariff') + '-' + energy_category_dict[energy_category_id]['name']) |
|
| 604 | parameters_data['timestamps'].append(tariff_timestamp_list) |
|
| 605 | parameters_data['values'].append(tariff_value_list) |
|
| 606 | ||
| 607 | ################################################################################################################ |
|
| 608 | # Step 9: query associated sensors and points data |
|
| 609 | ################################################################################################################ |
|
| 610 | if not is_quick_mode: |
|
| 611 | for point in point_list: |
|
| 612 | point_values = [] |
|
| 613 | point_timestamps = [] |
|
| 614 | if point['object_type'] == 'ENERGY_VALUE': |
|
| 615 | query = (" SELECT utc_date_time, actual_value " |
|
| 616 | " FROM tbl_energy_value " |
|
| 617 | " WHERE point_id = %s " |
|
| 618 | " AND utc_date_time BETWEEN %s AND %s " |
|
| 619 | " ORDER BY utc_date_time ") |
|
| 620 | cursor_historical.execute(query, (point['id'], |
|
| 621 | reporting_start_datetime_utc, |
|
| 622 | reporting_end_datetime_utc)) |
|
| 623 | rows = cursor_historical.fetchall() |
|
| 624 | ||
| 625 | if rows is not None and len(rows) > 0: |
|
| 626 | for row in rows: |
|
| 627 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
| 628 | timedelta(minutes=timezone_offset) |
|
| 629 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 630 | point_timestamps.append(current_datetime) |
|
| 631 | point_values.append(row[1]) |
|
| 632 | elif point['object_type'] == 'ANALOG_VALUE': |
|
| 633 | query = (" SELECT utc_date_time, actual_value " |
|
| 634 | " FROM tbl_analog_value " |
|
| 635 | " WHERE point_id = %s " |
|
| 636 | " AND utc_date_time BETWEEN %s AND %s " |
|
| 637 | " ORDER BY utc_date_time ") |
|
| 638 | cursor_historical.execute(query, (point['id'], |
|
| 639 | reporting_start_datetime_utc, |
|
| 640 | reporting_end_datetime_utc)) |
|
| 641 | rows = cursor_historical.fetchall() |
|
| 642 | ||
| 643 | if rows is not None and len(rows) > 0: |
|
| 644 | for row in rows: |
|
| 645 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
| 646 | timedelta(minutes=timezone_offset) |
|
| 647 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 648 | point_timestamps.append(current_datetime) |
|
| 649 | point_values.append(row[1]) |
|
| 650 | elif point['object_type'] == 'DIGITAL_VALUE': |
|
| 651 | query = (" SELECT utc_date_time, actual_value " |
|
| 652 | " FROM tbl_digital_value " |
|
| 653 | " WHERE point_id = %s " |
|
| 654 | " AND utc_date_time BETWEEN %s AND %s " |
|
| 655 | " ORDER BY utc_date_time ") |
|
| 656 | cursor_historical.execute(query, (point['id'], |
|
| 657 | reporting_start_datetime_utc, |
|
| 658 | reporting_end_datetime_utc)) |
|
| 659 | rows = cursor_historical.fetchall() |
|
| 660 | ||
| 661 | if rows is not None and len(rows) > 0: |
|
| 662 | for row in rows: |
|
| 663 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
| 664 | timedelta(minutes=timezone_offset) |
|
| 665 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 666 | point_timestamps.append(current_datetime) |
|
| 667 | point_values.append(row[1]) |
|
| 668 | ||
| 669 | parameters_data['names'].append(point['name'] + ' (' + point['units'] + ')') |
|
| 670 | parameters_data['timestamps'].append(point_timestamps) |
|
| 671 | parameters_data['values'].append(point_values) |
|
| 672 | ||
| 673 | ################################################################################################################ |
|
| 674 | # Step 10: construct the report |
|
| 675 | ################################################################################################################ |
|
| 676 | if cursor_system: |
|
| 677 | cursor_system.close() |
|
| 678 | if cnx_system: |
|
| 679 | cnx_system.close() |
|
| 680 | ||
| 681 | if cursor_energy: |
|
| 682 | cursor_energy.close() |
|
| 683 | if cnx_energy: |
|
| 684 | cnx_energy.close() |
|
| 685 | ||
| 686 | if cursor_energy_plan: |
|
| 687 | cursor_energy_plan.close() |
|
| 688 | if cnx_energy_plan: |
|
| 689 | cnx_energy_plan.close() |
|
| 690 | ||
| 691 | if cursor_historical: |
|
| 692 | cursor_historical.close() |
|
| 693 | if cnx_historical: |
|
| 694 | cnx_historical.close() |
|
| 695 | ||
| 696 | result = dict() |
|
| 697 | ||
| 698 | result['shopfloor'] = dict() |
|
| 699 | result['shopfloor']['name'] = shopfloor['name'] |
|
| 700 | result['shopfloor']['area'] = shopfloor['area'] |
|
| 701 | ||
| 702 | result['base_period'] = dict() |
|
| 703 | result['base_period']['names'] = list() |
|
| 704 | result['base_period']['units'] = list() |
|
| 705 | result['base_period']['timestamps'] = list() |
|
| 706 | result['base_period']['values_saving'] = list() |
|
| 707 | result['base_period']['subtotals_saving'] = list() |
|
| 708 | result['base_period']['subtotals_in_kgce_saving'] = list() |
|
| 709 | result['base_period']['subtotals_in_kgco2e_saving'] = list() |
|
| 710 | result['base_period']['total_in_kgce_saving'] = Decimal(0.0) |
|
| 711 | result['base_period']['total_in_kgco2e_saving'] = Decimal(0.0) |
|
| 712 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
| 713 | for energy_category_id in energy_category_set: |
|
| 714 | result['base_period']['names'].append(energy_category_dict[energy_category_id]['name']) |
|
| 715 | result['base_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) |
|
| 716 | result['base_period']['timestamps'].append(base[energy_category_id]['timestamps']) |
|
| 717 | result['base_period']['values_saving'].append(base[energy_category_id]['values_saving']) |
|
| 718 | result['base_period']['subtotals_saving'].append(base[energy_category_id]['subtotal_saving']) |
|
| 719 | result['base_period']['subtotals_in_kgce_saving'].append( |
|
| 720 | base[energy_category_id]['subtotal_in_kgce_saving']) |
|
| 721 | result['base_period']['subtotals_in_kgco2e_saving'].append( |
|
| 722 | base[energy_category_id]['subtotal_in_kgco2e_saving']) |
|
| 723 | result['base_period']['total_in_kgce_saving'] += base[energy_category_id]['subtotal_in_kgce_saving'] |
|
| 724 | result['base_period']['total_in_kgco2e_saving'] += base[energy_category_id]['subtotal_in_kgco2e_saving'] |
|
| 725 | ||
| 726 | result['reporting_period'] = dict() |
|
| 727 | result['reporting_period']['names'] = list() |
|
| 728 | result['reporting_period']['energy_category_ids'] = list() |
|
| 729 | result['reporting_period']['units'] = list() |
|
| 730 | result['reporting_period']['timestamps'] = list() |
|
| 731 | result['reporting_period']['values_saving'] = list() |
|
| 732 | result['reporting_period']['rates_saving'] = list() |
|
| 733 | result['reporting_period']['subtotals_saving'] = list() |
|
| 734 | result['reporting_period']['subtotals_in_kgce_saving'] = list() |
|
| 735 | result['reporting_period']['subtotals_in_kgco2e_saving'] = list() |
|
| 736 | result['reporting_period']['subtotals_per_unit_area_saving'] = list() |
|
| 737 | result['reporting_period']['increment_rates_saving'] = list() |
|
| 738 | result['reporting_period']['total_in_kgce_saving'] = Decimal(0.0) |
|
| 739 | result['reporting_period']['total_in_kgco2e_saving'] = Decimal(0.0) |
|
| 740 | result['reporting_period']['increment_rate_in_kgce_saving'] = Decimal(0.0) |
|
| 741 | result['reporting_period']['increment_rate_in_kgco2e_saving'] = Decimal(0.0) |
|
| 742 | ||
| 743 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
| 744 | for energy_category_id in energy_category_set: |
|
| 745 | result['reporting_period']['names'].append(energy_category_dict[energy_category_id]['name']) |
|
| 746 | result['reporting_period']['energy_category_ids'].append(energy_category_id) |
|
| 747 | result['reporting_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) |
|
| 748 | result['reporting_period']['timestamps'].append(reporting[energy_category_id]['timestamps']) |
|
| 749 | result['reporting_period']['values_saving'].append(reporting[energy_category_id]['values_saving']) |
|
| 750 | result['reporting_period']['subtotals_saving'].append(reporting[energy_category_id]['subtotal_saving']) |
|
| 751 | result['reporting_period']['subtotals_in_kgce_saving'].append( |
|
| 752 | reporting[energy_category_id]['subtotal_in_kgce_saving']) |
|
| 753 | result['reporting_period']['subtotals_in_kgco2e_saving'].append( |
|
| 754 | reporting[energy_category_id]['subtotal_in_kgco2e_saving']) |
|
| 755 | result['reporting_period']['subtotals_per_unit_area_saving'].append( |
|
| 756 | reporting[energy_category_id]['subtotal_saving'] / shopfloor['area'] |
|
| 757 | if shopfloor['area'] > Decimal(0.0) else None) |
|
| 758 | result['reporting_period']['increment_rates_saving'].append( |
|
| 759 | (reporting[energy_category_id]['subtotal_saving'] - base[energy_category_id]['subtotal_saving']) / |
|
| 760 | base[energy_category_id]['subtotal_saving'] |
|
| 761 | if base[energy_category_id]['subtotal_saving'] != Decimal(0.0) else None) |
|
| 762 | result['reporting_period']['total_in_kgce_saving'] += \ |
|
| 763 | reporting[energy_category_id]['subtotal_in_kgce_saving'] |
|
| 764 | result['reporting_period']['total_in_kgco2e_saving'] += \ |
|
| 765 | reporting[energy_category_id]['subtotal_in_kgco2e_saving'] |
|
| 766 | ||
| 767 | rate = list() |
|
| 768 | for index, value in enumerate(reporting[energy_category_id]['values_saving']): |
|
| 769 | if index < len(base[energy_category_id]['values_saving']) \ |
|
| 770 | and base[energy_category_id]['values_saving'][index] != 0 and value != 0: |
|
| 771 | rate.append((value - base[energy_category_id]['values_saving'][index]) |
|
| 772 | / base[energy_category_id]['values_saving'][index]) |
|
| 773 | else: |
|
| 774 | rate.append(None) |
|
| 775 | result['reporting_period']['rates_saving'].append(rate) |
|
| 776 | ||
| 777 | result['reporting_period']['total_in_kgco2e_per_unit_area_saving'] = \ |
|
| 778 | result['reporting_period']['total_in_kgce_saving'] / shopfloor['area'] \ |
|
| 779 | if shopfloor['area'] > 0.0 else None |
|
| 780 | ||
| 781 | result['reporting_period']['increment_rate_in_kgce_saving'] = \ |
|
| 782 | (result['reporting_period']['total_in_kgce_saving'] - result['base_period']['total_in_kgce_saving']) / \ |
|
| 783 | result['base_period']['total_in_kgce_saving'] \ |
|
| 784 | if result['base_period']['total_in_kgce_saving'] != Decimal(0.0) else None |
|
| 785 | ||
| 786 | result['reporting_period']['total_in_kgce_per_unit_area_saving'] = \ |
|
| 787 | result['reporting_period']['total_in_kgco2e_saving'] / shopfloor['area'] \ |
|
| 788 | if shopfloor['area'] > Decimal(0.0) else None |
|
| 789 | ||
| 790 | result['reporting_period']['increment_rate_in_kgco2e_saving'] = \ |
|
| 791 | (result['reporting_period']['total_in_kgco2e_saving'] - result['base_period']['total_in_kgco2e_saving']) / \ |
|
| 792 | result['base_period']['total_in_kgco2e_saving'] \ |
|
| 793 | if result['base_period']['total_in_kgco2e_saving'] != Decimal(0.0) else None |
|
| 794 | ||
| 795 | result['parameters'] = { |
|
| 796 | "names": parameters_data['names'], |
|
| 797 | "timestamps": parameters_data['timestamps'], |
|
| 798 | "values": parameters_data['values'] |
|
| 799 | } |
|
| 800 | ||
| 801 | result['excel_bytes_base64'] = None |
|
| 802 | if not is_quick_mode: |
|
| 803 | result['excel_bytes_base64'] = excelexporters.shopfloorplan.export(result, |
|
| 804 | shopfloor['name'], |
|
| 805 | base_period_start_datetime_local, |
|
| 806 | base_period_end_datetime_local, |
|
| 807 | reporting_period_start_datetime_local, |
|
| 808 | reporting_period_end_datetime_local, |
|
| 809 | period_type, |
|
| 810 | language) |
|
| 811 | ||
| 812 | resp.text = json.dumps(result) |
|
| 813 | ||
| @@ 46-809 (lines=764) @@ | ||
| 43 | from core.useractivity import access_control, api_key_control |
|
| 44 | ||
| 45 | ||
| 46 | class Reporting: |
|
| 47 | def __init__(self): |
|
| 48 | """"Initializes Reporting""" |
|
| 49 | pass |
|
| 50 | ||
| 51 | @staticmethod |
|
| 52 | def on_options(req, resp): |
|
| 53 | _ = req |
|
| 54 | resp.status = falcon.HTTP_200 |
|
| 55 | ||
| 56 | #################################################################################################################### |
|
| 57 | # PROCEDURES |
|
| 58 | # Step 1: valid parameters |
|
| 59 | # Step 2: query the tenant |
|
| 60 | # Step 3: query energy categories |
|
| 61 | # Step 4: query associated sensors |
|
| 62 | # Step 5: query associated points |
|
| 63 | # Step 6: query base period energy saving |
|
| 64 | # Step 7: query reporting period energy saving |
|
| 65 | # Step 8: query tariff data |
|
| 66 | # Step 9: query associated sensors and points data |
|
| 67 | # Step 10: construct the report |
|
| 68 | #################################################################################################################### |
|
| 69 | @staticmethod |
|
| 70 | def on_get(req, resp): |
|
| 71 | if 'API-KEY' not in req.headers or \ |
|
| 72 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 73 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 74 | access_control(req) |
|
| 75 | else: |
|
| 76 | api_key_control(req) |
|
| 77 | print(req.params) |
|
| 78 | tenant_id = req.params.get('tenantid') |
|
| 79 | tenant_uuid = req.params.get('tenantuuid') |
|
| 80 | period_type = req.params.get('periodtype') |
|
| 81 | base_period_start_datetime_local = req.params.get('baseperiodstartdatetime') |
|
| 82 | base_period_end_datetime_local = req.params.get('baseperiodenddatetime') |
|
| 83 | reporting_period_start_datetime_local = req.params.get('reportingperiodstartdatetime') |
|
| 84 | reporting_period_end_datetime_local = req.params.get('reportingperiodenddatetime') |
|
| 85 | language = req.params.get('language') |
|
| 86 | quick_mode = req.params.get('quickmode') |
|
| 87 | ||
| 88 | ################################################################################################################ |
|
| 89 | # Step 1: valid parameters |
|
| 90 | ################################################################################################################ |
|
| 91 | if tenant_id is None and tenant_uuid is None: |
|
| 92 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 93 | title='API.BAD_REQUEST', |
|
| 94 | description='API.INVALID_TENANT_ID') |
|
| 95 | ||
| 96 | if tenant_id is not None: |
|
| 97 | tenant_id = str.strip(tenant_id) |
|
| 98 | if not tenant_id.isdigit() or int(tenant_id) <= 0: |
|
| 99 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 100 | title='API.BAD_REQUEST', |
|
| 101 | description='API.INVALID_TENANT_ID') |
|
| 102 | ||
| 103 | if tenant_uuid is not None: |
|
| 104 | regex = re.compile(r'^[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) |
|
| 105 | match = regex.match(str.strip(tenant_uuid)) |
|
| 106 | if not bool(match): |
|
| 107 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 108 | title='API.BAD_REQUEST', |
|
| 109 | description='API.INVALID_TENANT_UUID') |
|
| 110 | ||
| 111 | if period_type is None: |
|
| 112 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 113 | description='API.INVALID_PERIOD_TYPE') |
|
| 114 | else: |
|
| 115 | period_type = str.strip(period_type) |
|
| 116 | if period_type not in ['hourly', 'daily', 'weekly', 'monthly', 'yearly']: |
|
| 117 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 118 | description='API.INVALID_PERIOD_TYPE') |
|
| 119 | ||
| 120 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
|
| 121 | if config.utc_offset[0] == '-': |
|
| 122 | timezone_offset = -timezone_offset |
|
| 123 | ||
| 124 | base_start_datetime_utc = None |
|
| 125 | if base_period_start_datetime_local is not None and len(str.strip(base_period_start_datetime_local)) > 0: |
|
| 126 | base_period_start_datetime_local = str.strip(base_period_start_datetime_local) |
|
| 127 | try: |
|
| 128 | base_start_datetime_utc = datetime.strptime(base_period_start_datetime_local, '%Y-%m-%dT%H:%M:%S') |
|
| 129 | except ValueError: |
|
| 130 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 131 | description="API.INVALID_BASE_PERIOD_START_DATETIME") |
|
| 132 | base_start_datetime_utc = \ |
|
| 133 | base_start_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset) |
|
| 134 | # nomalize the start datetime |
|
| 135 | if config.minutes_to_count == 30 and base_start_datetime_utc.minute >= 30: |
|
| 136 | base_start_datetime_utc = base_start_datetime_utc.replace(minute=30, second=0, microsecond=0) |
|
| 137 | else: |
|
| 138 | base_start_datetime_utc = base_start_datetime_utc.replace(minute=0, second=0, microsecond=0) |
|
| 139 | ||
| 140 | base_end_datetime_utc = None |
|
| 141 | if base_period_end_datetime_local is not None and len(str.strip(base_period_end_datetime_local)) > 0: |
|
| 142 | base_period_end_datetime_local = str.strip(base_period_end_datetime_local) |
|
| 143 | try: |
|
| 144 | base_end_datetime_utc = datetime.strptime(base_period_end_datetime_local, '%Y-%m-%dT%H:%M:%S') |
|
| 145 | except ValueError: |
|
| 146 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 147 | description="API.INVALID_BASE_PERIOD_END_DATETIME") |
|
| 148 | base_end_datetime_utc = \ |
|
| 149 | base_end_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset) |
|
| 150 | ||
| 151 | if base_start_datetime_utc is not None and base_end_datetime_utc is not None and \ |
|
| 152 | base_start_datetime_utc >= base_end_datetime_utc: |
|
| 153 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 154 | description='API.INVALID_BASE_PERIOD_END_DATETIME') |
|
| 155 | ||
| 156 | if reporting_period_start_datetime_local is None: |
|
| 157 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 158 | description="API.INVALID_REPORTING_PERIOD_START_DATETIME") |
|
| 159 | else: |
|
| 160 | reporting_period_start_datetime_local = str.strip(reporting_period_start_datetime_local) |
|
| 161 | try: |
|
| 162 | reporting_start_datetime_utc = datetime.strptime(reporting_period_start_datetime_local, |
|
| 163 | '%Y-%m-%dT%H:%M:%S') |
|
| 164 | except ValueError: |
|
| 165 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 166 | description="API.INVALID_REPORTING_PERIOD_START_DATETIME") |
|
| 167 | reporting_start_datetime_utc = \ |
|
| 168 | reporting_start_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset) |
|
| 169 | # nomalize the start datetime |
|
| 170 | if config.minutes_to_count == 30 and reporting_start_datetime_utc.minute >= 30: |
|
| 171 | reporting_start_datetime_utc = reporting_start_datetime_utc.replace(minute=30, second=0, microsecond=0) |
|
| 172 | else: |
|
| 173 | reporting_start_datetime_utc = reporting_start_datetime_utc.replace(minute=0, second=0, microsecond=0) |
|
| 174 | ||
| 175 | if reporting_period_end_datetime_local is None: |
|
| 176 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 177 | description="API.INVALID_REPORTING_PERIOD_END_DATETIME") |
|
| 178 | else: |
|
| 179 | reporting_period_end_datetime_local = str.strip(reporting_period_end_datetime_local) |
|
| 180 | try: |
|
| 181 | reporting_end_datetime_utc = datetime.strptime(reporting_period_end_datetime_local, |
|
| 182 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
|
| 183 | timedelta(minutes=timezone_offset) |
|
| 184 | except ValueError: |
|
| 185 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 186 | description="API.INVALID_REPORTING_PERIOD_END_DATETIME") |
|
| 187 | ||
| 188 | if reporting_start_datetime_utc >= reporting_end_datetime_utc: |
|
| 189 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 190 | description='API.INVALID_REPORTING_PERIOD_END_DATETIME') |
|
| 191 | ||
| 192 | # if turn quick mode on, do not return parameters data and excel file |
|
| 193 | is_quick_mode = False |
|
| 194 | if quick_mode is not None and \ |
|
| 195 | len(str.strip(quick_mode)) > 0 and \ |
|
| 196 | str.lower(str.strip(quick_mode)) in ('true', 't', 'on', 'yes', 'y'): |
|
| 197 | is_quick_mode = True |
|
| 198 | ||
| 199 | trans = utilities.get_translation(language) |
|
| 200 | trans.install() |
|
| 201 | _ = trans.gettext |
|
| 202 | ||
| 203 | ################################################################################################################ |
|
| 204 | # Step 2: query the tenant |
|
| 205 | ################################################################################################################ |
|
| 206 | cnx_system = mysql.connector.connect(**config.myems_system_db) |
|
| 207 | cursor_system = cnx_system.cursor() |
|
| 208 | ||
| 209 | cnx_energy = mysql.connector.connect(**config.myems_energy_db) |
|
| 210 | cursor_energy = cnx_energy.cursor() |
|
| 211 | ||
| 212 | cnx_energy_baseline = mysql.connector.connect(**config.myems_energy_baseline_db) |
|
| 213 | cursor_energy_baseline = cnx_energy_baseline.cursor() |
|
| 214 | ||
| 215 | cnx_historical = mysql.connector.connect(**config.myems_historical_db) |
|
| 216 | cursor_historical = cnx_historical.cursor() |
|
| 217 | ||
| 218 | if tenant_id is not None: |
|
| 219 | cursor_system.execute(" SELECT id, name, area, cost_center_id " |
|
| 220 | " FROM tbl_tenants " |
|
| 221 | " WHERE id = %s ", (tenant_id,)) |
|
| 222 | row_tenant = cursor_system.fetchone() |
|
| 223 | elif tenant_uuid is not None: |
|
| 224 | cursor_system.execute(" SELECT id, name, area, cost_center_id " |
|
| 225 | " FROM tbl_tenants " |
|
| 226 | " WHERE uuid = %s ", (tenant_uuid,)) |
|
| 227 | row_tenant = cursor_system.fetchone() |
|
| 228 | ||
| 229 | if row_tenant is None: |
|
| 230 | if cursor_system: |
|
| 231 | cursor_system.close() |
|
| 232 | if cnx_system: |
|
| 233 | cnx_system.close() |
|
| 234 | ||
| 235 | if cursor_energy: |
|
| 236 | cursor_energy.close() |
|
| 237 | if cnx_energy: |
|
| 238 | cnx_energy.close() |
|
| 239 | ||
| 240 | if cursor_energy_baseline: |
|
| 241 | cursor_energy_baseline.close() |
|
| 242 | if cnx_energy_baseline: |
|
| 243 | cnx_energy_baseline.close() |
|
| 244 | ||
| 245 | if cursor_historical: |
|
| 246 | cursor_historical.close() |
|
| 247 | if cnx_historical: |
|
| 248 | cnx_historical.close() |
|
| 249 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', description='API.TENANT_NOT_FOUND') |
|
| 250 | ||
| 251 | tenant = dict() |
|
| 252 | tenant['id'] = row_tenant[0] |
|
| 253 | tenant['name'] = row_tenant[1] |
|
| 254 | tenant['area'] = row_tenant[2] |
|
| 255 | tenant['cost_center_id'] = row_tenant[3] |
|
| 256 | ||
| 257 | ################################################################################################################ |
|
| 258 | # Step 3: query energy categories |
|
| 259 | ################################################################################################################ |
|
| 260 | energy_category_set = set() |
|
| 261 | # query energy categories in base period |
|
| 262 | cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
|
| 263 | " FROM tbl_tenant_input_category_hourly " |
|
| 264 | " WHERE tenant_id = %s " |
|
| 265 | " AND start_datetime_utc >= %s " |
|
| 266 | " AND start_datetime_utc < %s ", |
|
| 267 | (tenant['id'], base_start_datetime_utc, base_end_datetime_utc)) |
|
| 268 | rows_energy_categories = cursor_energy.fetchall() |
|
| 269 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
| 270 | for row_energy_category in rows_energy_categories: |
|
| 271 | energy_category_set.add(row_energy_category[0]) |
|
| 272 | ||
| 273 | # query energy categories in reporting period |
|
| 274 | cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
|
| 275 | " FROM tbl_tenant_input_category_hourly " |
|
| 276 | " WHERE tenant_id = %s " |
|
| 277 | " AND start_datetime_utc >= %s " |
|
| 278 | " AND start_datetime_utc < %s ", |
|
| 279 | (tenant['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) |
|
| 280 | rows_energy_categories = cursor_energy.fetchall() |
|
| 281 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
| 282 | for row_energy_category in rows_energy_categories: |
|
| 283 | energy_category_set.add(row_energy_category[0]) |
|
| 284 | ||
| 285 | # query all energy categories in base period and reporting period |
|
| 286 | cursor_system.execute(" SELECT id, name, unit_of_measure, kgce, kgco2e " |
|
| 287 | " FROM tbl_energy_categories " |
|
| 288 | " ORDER BY id ", ) |
|
| 289 | rows_energy_categories = cursor_system.fetchall() |
|
| 290 | if rows_energy_categories is None or len(rows_energy_categories) == 0: |
|
| 291 | if cursor_system: |
|
| 292 | cursor_system.close() |
|
| 293 | if cnx_system: |
|
| 294 | cnx_system.close() |
|
| 295 | ||
| 296 | if cursor_energy: |
|
| 297 | cursor_energy.close() |
|
| 298 | if cnx_energy: |
|
| 299 | cnx_energy.close() |
|
| 300 | ||
| 301 | if cursor_energy_baseline: |
|
| 302 | cursor_energy_baseline.close() |
|
| 303 | if cnx_energy_baseline: |
|
| 304 | cnx_energy_baseline.close() |
|
| 305 | ||
| 306 | if cursor_historical: |
|
| 307 | cursor_historical.close() |
|
| 308 | if cnx_historical: |
|
| 309 | cnx_historical.close() |
|
| 310 | raise falcon.HTTPError(status=falcon.HTTP_404, |
|
| 311 | title='API.NOT_FOUND', |
|
| 312 | description='API.ENERGY_CATEGORY_NOT_FOUND') |
|
| 313 | energy_category_dict = dict() |
|
| 314 | for row_energy_category in rows_energy_categories: |
|
| 315 | if row_energy_category[0] in energy_category_set: |
|
| 316 | energy_category_dict[row_energy_category[0]] = {"name": row_energy_category[1], |
|
| 317 | "unit_of_measure": row_energy_category[2], |
|
| 318 | "kgce": row_energy_category[3], |
|
| 319 | "kgco2e": row_energy_category[4]} |
|
| 320 | ||
| 321 | ################################################################################################################ |
|
| 322 | # Step 4: query associated sensors |
|
| 323 | ################################################################################################################ |
|
| 324 | point_list = list() |
|
| 325 | cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type " |
|
| 326 | " FROM tbl_tenants t, tbl_sensors s, tbl_tenants_sensors ts, " |
|
| 327 | " tbl_points p, tbl_sensors_points sp " |
|
| 328 | " WHERE t.id = %s AND t.id = ts.tenant_id AND ts.sensor_id = s.id " |
|
| 329 | " AND s.id = sp.sensor_id AND sp.point_id = p.id " |
|
| 330 | " ORDER BY p.id ", (tenant['id'],)) |
|
| 331 | rows_points = cursor_system.fetchall() |
|
| 332 | if rows_points is not None and len(rows_points) > 0: |
|
| 333 | for row in rows_points: |
|
| 334 | point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]}) |
|
| 335 | ||
| 336 | ################################################################################################################ |
|
| 337 | # Step 5: query associated points |
|
| 338 | ################################################################################################################ |
|
| 339 | cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type " |
|
| 340 | " FROM tbl_tenants t, tbl_tenants_points tp, tbl_points p " |
|
| 341 | " WHERE t.id = %s AND t.id = tp.tenant_id AND tp.point_id = p.id " |
|
| 342 | " ORDER BY p.id ", (tenant['id'],)) |
|
| 343 | rows_points = cursor_system.fetchall() |
|
| 344 | if rows_points is not None and len(rows_points) > 0: |
|
| 345 | for row in rows_points: |
|
| 346 | point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]}) |
|
| 347 | ||
| 348 | ################################################################################################################ |
|
| 349 | # Step 6: query base period energy saving |
|
| 350 | ################################################################################################################ |
|
| 351 | base = dict() |
|
| 352 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
| 353 | for energy_category_id in energy_category_set: |
|
| 354 | kgce = energy_category_dict[energy_category_id]['kgce'] |
|
| 355 | kgco2e = energy_category_dict[energy_category_id]['kgco2e'] |
|
| 356 | ||
| 357 | base[energy_category_id] = dict() |
|
| 358 | base[energy_category_id]['timestamps'] = list() |
|
| 359 | base[energy_category_id]['values_baseline'] = list() |
|
| 360 | base[energy_category_id]['values_actual'] = list() |
|
| 361 | base[energy_category_id]['values_saving'] = list() |
|
| 362 | base[energy_category_id]['subtotal_baseline'] = Decimal(0.0) |
|
| 363 | base[energy_category_id]['subtotal_actual'] = Decimal(0.0) |
|
| 364 | base[energy_category_id]['subtotal_saving'] = Decimal(0.0) |
|
| 365 | base[energy_category_id]['subtotal_in_kgce_baseline'] = Decimal(0.0) |
|
| 366 | base[energy_category_id]['subtotal_in_kgce_actual'] = Decimal(0.0) |
|
| 367 | base[energy_category_id]['subtotal_in_kgce_saving'] = Decimal(0.0) |
|
| 368 | base[energy_category_id]['subtotal_in_kgco2e_baseline'] = Decimal(0.0) |
|
| 369 | base[energy_category_id]['subtotal_in_kgco2e_actual'] = Decimal(0.0) |
|
| 370 | base[energy_category_id]['subtotal_in_kgco2e_saving'] = Decimal(0.0) |
|
| 371 | # query base period's energy baseline |
|
| 372 | cursor_energy_baseline.execute(" SELECT start_datetime_utc, actual_value " |
|
| 373 | " FROM tbl_tenant_input_category_hourly " |
|
| 374 | " WHERE tenant_id = %s " |
|
| 375 | " AND energy_category_id = %s " |
|
| 376 | " AND start_datetime_utc >= %s " |
|
| 377 | " AND start_datetime_utc < %s " |
|
| 378 | " ORDER BY start_datetime_utc ", |
|
| 379 | (tenant['id'], |
|
| 380 | energy_category_id, |
|
| 381 | base_start_datetime_utc, |
|
| 382 | base_end_datetime_utc)) |
|
| 383 | rows_tenant_hourly = cursor_energy_baseline.fetchall() |
|
| 384 | ||
| 385 | rows_tenant_periodically = utilities.aggregate_hourly_data_by_period(rows_tenant_hourly, |
|
| 386 | base_start_datetime_utc, |
|
| 387 | base_end_datetime_utc, |
|
| 388 | period_type) |
|
| 389 | for row_tenant_periodically in rows_tenant_periodically: |
|
| 390 | current_datetime_local = row_tenant_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
| 391 | timedelta(minutes=timezone_offset) |
|
| 392 | if period_type == 'hourly': |
|
| 393 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 394 | elif period_type == 'daily': |
|
| 395 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 396 | elif period_type == 'weekly': |
|
| 397 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 398 | elif period_type == 'monthly': |
|
| 399 | current_datetime = current_datetime_local.isoformat()[0:7] |
|
| 400 | elif period_type == 'yearly': |
|
| 401 | current_datetime = current_datetime_local.isoformat()[0:4] |
|
| 402 | ||
| 403 | baseline_value = Decimal(0.0) if row_tenant_periodically[1] is None else row_tenant_periodically[1] |
|
| 404 | base[energy_category_id]['timestamps'].append(current_datetime) |
|
| 405 | base[energy_category_id]['values_baseline'].append(baseline_value) |
|
| 406 | base[energy_category_id]['subtotal_baseline'] += baseline_value |
|
| 407 | base[energy_category_id]['subtotal_in_kgce_baseline'] += baseline_value * kgce |
|
| 408 | base[energy_category_id]['subtotal_in_kgco2e_baseline'] += baseline_value * kgco2e |
|
| 409 | ||
| 410 | # query base period's energy actual |
|
| 411 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
|
| 412 | " FROM tbl_tenant_input_category_hourly " |
|
| 413 | " WHERE tenant_id = %s " |
|
| 414 | " AND energy_category_id = %s " |
|
| 415 | " AND start_datetime_utc >= %s " |
|
| 416 | " AND start_datetime_utc < %s " |
|
| 417 | " ORDER BY start_datetime_utc ", |
|
| 418 | (tenant['id'], |
|
| 419 | energy_category_id, |
|
| 420 | base_start_datetime_utc, |
|
| 421 | base_end_datetime_utc)) |
|
| 422 | rows_tenant_hourly = cursor_energy.fetchall() |
|
| 423 | ||
| 424 | rows_tenant_periodically = utilities.aggregate_hourly_data_by_period(rows_tenant_hourly, |
|
| 425 | base_start_datetime_utc, |
|
| 426 | base_end_datetime_utc, |
|
| 427 | period_type) |
|
| 428 | for row_tenant_periodically in rows_tenant_periodically: |
|
| 429 | current_datetime_local = row_tenant_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
| 430 | timedelta(minutes=timezone_offset) |
|
| 431 | if period_type == 'hourly': |
|
| 432 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 433 | elif period_type == 'daily': |
|
| 434 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 435 | elif period_type == 'weekly': |
|
| 436 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 437 | elif period_type == 'monthly': |
|
| 438 | current_datetime = current_datetime_local.isoformat()[0:7] |
|
| 439 | elif period_type == 'yearly': |
|
| 440 | current_datetime = current_datetime_local.isoformat()[0:4] |
|
| 441 | ||
| 442 | actual_value = Decimal(0.0) if row_tenant_periodically[1] is None else row_tenant_periodically[1] |
|
| 443 | base[energy_category_id]['values_actual'].append(actual_value) |
|
| 444 | base[energy_category_id]['subtotal_actual'] += actual_value |
|
| 445 | base[energy_category_id]['subtotal_in_kgce_actual'] += actual_value * kgce |
|
| 446 | base[energy_category_id]['subtotal_in_kgco2e_actual'] += actual_value * kgco2e |
|
| 447 | ||
| 448 | # calculate base period's energy saving |
|
| 449 | for i in range(len(base[energy_category_id]['values_baseline'])): |
|
| 450 | base[energy_category_id]['values_saving'].append( |
|
| 451 | base[energy_category_id]['values_baseline'][i] - |
|
| 452 | base[energy_category_id]['values_actual'][i]) |
|
| 453 | ||
| 454 | base[energy_category_id]['subtotal_saving'] = \ |
|
| 455 | base[energy_category_id]['subtotal_baseline'] - \ |
|
| 456 | base[energy_category_id]['subtotal_actual'] |
|
| 457 | base[energy_category_id]['subtotal_in_kgce_saving'] = \ |
|
| 458 | base[energy_category_id]['subtotal_in_kgce_baseline'] - \ |
|
| 459 | base[energy_category_id]['subtotal_in_kgce_actual'] |
|
| 460 | base[energy_category_id]['subtotal_in_kgco2e_saving'] = \ |
|
| 461 | base[energy_category_id]['subtotal_in_kgco2e_baseline'] - \ |
|
| 462 | base[energy_category_id]['subtotal_in_kgco2e_actual'] |
|
| 463 | ################################################################################################################ |
|
| 464 | # Step 7: query reporting period energy saving |
|
| 465 | ################################################################################################################ |
|
| 466 | reporting = dict() |
|
| 467 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
| 468 | for energy_category_id in energy_category_set: |
|
| 469 | kgce = energy_category_dict[energy_category_id]['kgce'] |
|
| 470 | kgco2e = energy_category_dict[energy_category_id]['kgco2e'] |
|
| 471 | ||
| 472 | reporting[energy_category_id] = dict() |
|
| 473 | reporting[energy_category_id]['timestamps'] = list() |
|
| 474 | reporting[energy_category_id]['values_baseline'] = list() |
|
| 475 | reporting[energy_category_id]['values_actual'] = list() |
|
| 476 | reporting[energy_category_id]['values_saving'] = list() |
|
| 477 | reporting[energy_category_id]['subtotal_baseline'] = Decimal(0.0) |
|
| 478 | reporting[energy_category_id]['subtotal_actual'] = Decimal(0.0) |
|
| 479 | reporting[energy_category_id]['subtotal_saving'] = Decimal(0.0) |
|
| 480 | reporting[energy_category_id]['subtotal_in_kgce_baseline'] = Decimal(0.0) |
|
| 481 | reporting[energy_category_id]['subtotal_in_kgce_actual'] = Decimal(0.0) |
|
| 482 | reporting[energy_category_id]['subtotal_in_kgce_saving'] = Decimal(0.0) |
|
| 483 | reporting[energy_category_id]['subtotal_in_kgco2e_baseline'] = Decimal(0.0) |
|
| 484 | reporting[energy_category_id]['subtotal_in_kgco2e_actual'] = Decimal(0.0) |
|
| 485 | reporting[energy_category_id]['subtotal_in_kgco2e_saving'] = Decimal(0.0) |
|
| 486 | # query reporting period's energy baseline |
|
| 487 | cursor_energy_baseline.execute(" SELECT start_datetime_utc, actual_value " |
|
| 488 | " FROM tbl_tenant_input_category_hourly " |
|
| 489 | " WHERE tenant_id = %s " |
|
| 490 | " AND energy_category_id = %s " |
|
| 491 | " AND start_datetime_utc >= %s " |
|
| 492 | " AND start_datetime_utc < %s " |
|
| 493 | " ORDER BY start_datetime_utc ", |
|
| 494 | (tenant['id'], |
|
| 495 | energy_category_id, |
|
| 496 | reporting_start_datetime_utc, |
|
| 497 | reporting_end_datetime_utc)) |
|
| 498 | rows_tenant_hourly = cursor_energy_baseline.fetchall() |
|
| 499 | ||
| 500 | rows_tenant_periodically = utilities.aggregate_hourly_data_by_period(rows_tenant_hourly, |
|
| 501 | reporting_start_datetime_utc, |
|
| 502 | reporting_end_datetime_utc, |
|
| 503 | period_type) |
|
| 504 | for row_tenant_periodically in rows_tenant_periodically: |
|
| 505 | current_datetime_local = row_tenant_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
| 506 | timedelta(minutes=timezone_offset) |
|
| 507 | if period_type == 'hourly': |
|
| 508 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 509 | elif period_type == 'daily': |
|
| 510 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 511 | elif period_type == 'weekly': |
|
| 512 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 513 | elif period_type == 'monthly': |
|
| 514 | current_datetime = current_datetime_local.isoformat()[0:7] |
|
| 515 | elif period_type == 'yearly': |
|
| 516 | current_datetime = current_datetime_local.isoformat()[0:4] |
|
| 517 | ||
| 518 | baseline_value = Decimal(0.0) if row_tenant_periodically[1] is None else row_tenant_periodically[1] |
|
| 519 | reporting[energy_category_id]['timestamps'].append(current_datetime) |
|
| 520 | reporting[energy_category_id]['values_baseline'].append(baseline_value) |
|
| 521 | reporting[energy_category_id]['subtotal_baseline'] += baseline_value |
|
| 522 | reporting[energy_category_id]['subtotal_in_kgce_baseline'] += baseline_value * kgce |
|
| 523 | reporting[energy_category_id]['subtotal_in_kgco2e_baseline'] += baseline_value * kgco2e |
|
| 524 | ||
| 525 | # query reporting period's energy actual |
|
| 526 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
|
| 527 | " FROM tbl_tenant_input_category_hourly " |
|
| 528 | " WHERE tenant_id = %s " |
|
| 529 | " AND energy_category_id = %s " |
|
| 530 | " AND start_datetime_utc >= %s " |
|
| 531 | " AND start_datetime_utc < %s " |
|
| 532 | " ORDER BY start_datetime_utc ", |
|
| 533 | (tenant['id'], |
|
| 534 | energy_category_id, |
|
| 535 | reporting_start_datetime_utc, |
|
| 536 | reporting_end_datetime_utc)) |
|
| 537 | rows_tenant_hourly = cursor_energy.fetchall() |
|
| 538 | ||
| 539 | rows_tenant_periodically = utilities.aggregate_hourly_data_by_period(rows_tenant_hourly, |
|
| 540 | reporting_start_datetime_utc, |
|
| 541 | reporting_end_datetime_utc, |
|
| 542 | period_type) |
|
| 543 | for row_tenant_periodically in rows_tenant_periodically: |
|
| 544 | current_datetime_local = row_tenant_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
| 545 | timedelta(minutes=timezone_offset) |
|
| 546 | if period_type == 'hourly': |
|
| 547 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 548 | elif period_type == 'daily': |
|
| 549 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 550 | elif period_type == 'weekly': |
|
| 551 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 552 | elif period_type == 'monthly': |
|
| 553 | current_datetime = current_datetime_local.isoformat()[0:7] |
|
| 554 | elif period_type == 'yearly': |
|
| 555 | current_datetime = current_datetime_local.isoformat()[0:4] |
|
| 556 | ||
| 557 | actual_value = Decimal(0.0) if row_tenant_periodically[1] is None else row_tenant_periodically[1] |
|
| 558 | reporting[energy_category_id]['values_actual'].append(actual_value) |
|
| 559 | reporting[energy_category_id]['subtotal_actual'] += actual_value |
|
| 560 | reporting[energy_category_id]['subtotal_in_kgce_actual'] += actual_value * kgce |
|
| 561 | reporting[energy_category_id]['subtotal_in_kgco2e_actual'] += actual_value * kgco2e |
|
| 562 | ||
| 563 | # calculate reporting period's energy savings |
|
| 564 | for i in range(len(reporting[energy_category_id]['values_baseline'])): |
|
| 565 | reporting[energy_category_id]['values_saving'].append( |
|
| 566 | reporting[energy_category_id]['values_baseline'][i] - |
|
| 567 | reporting[energy_category_id]['values_actual'][i]) |
|
| 568 | ||
| 569 | reporting[energy_category_id]['subtotal_saving'] = \ |
|
| 570 | reporting[energy_category_id]['subtotal_baseline'] - \ |
|
| 571 | reporting[energy_category_id]['subtotal_actual'] |
|
| 572 | reporting[energy_category_id]['subtotal_in_kgce_saving'] = \ |
|
| 573 | reporting[energy_category_id]['subtotal_in_kgce_baseline'] - \ |
|
| 574 | reporting[energy_category_id]['subtotal_in_kgce_actual'] |
|
| 575 | reporting[energy_category_id]['subtotal_in_kgco2e_saving'] = \ |
|
| 576 | reporting[energy_category_id]['subtotal_in_kgco2e_baseline'] - \ |
|
| 577 | reporting[energy_category_id]['subtotal_in_kgco2e_actual'] |
|
| 578 | ################################################################################################################ |
|
| 579 | # Step 8: query tariff data |
|
| 580 | ################################################################################################################ |
|
| 581 | parameters_data = dict() |
|
| 582 | parameters_data['names'] = list() |
|
| 583 | parameters_data['timestamps'] = list() |
|
| 584 | parameters_data['values'] = list() |
|
| 585 | if config.is_tariff_appended and energy_category_set is not None and len(energy_category_set) > 0 \ |
|
| 586 | and not is_quick_mode: |
|
| 587 | for energy_category_id in energy_category_set: |
|
| 588 | energy_category_tariff_dict = utilities.get_energy_category_tariffs(tenant['cost_center_id'], |
|
| 589 | energy_category_id, |
|
| 590 | reporting_start_datetime_utc, |
|
| 591 | reporting_end_datetime_utc) |
|
| 592 | tariff_timestamp_list = list() |
|
| 593 | tariff_value_list = list() |
|
| 594 | for k, v in energy_category_tariff_dict.items(): |
|
| 595 | # convert k from utc to local |
|
| 596 | k = k + timedelta(minutes=timezone_offset) |
|
| 597 | tariff_timestamp_list.append(k.isoformat()[0:19]) |
|
| 598 | tariff_value_list.append(v) |
|
| 599 | ||
| 600 | parameters_data['names'].append(_('Tariff') + '-' + energy_category_dict[energy_category_id]['name']) |
|
| 601 | parameters_data['timestamps'].append(tariff_timestamp_list) |
|
| 602 | parameters_data['values'].append(tariff_value_list) |
|
| 603 | ||
| 604 | ################################################################################################################ |
|
| 605 | # Step 9: query associated sensors and points data |
|
| 606 | ################################################################################################################ |
|
| 607 | if not is_quick_mode: |
|
| 608 | for point in point_list: |
|
| 609 | point_values = [] |
|
| 610 | point_timestamps = [] |
|
| 611 | if point['object_type'] == 'ENERGY_VALUE': |
|
| 612 | query = (" SELECT utc_date_time, actual_value " |
|
| 613 | " FROM tbl_energy_value " |
|
| 614 | " WHERE point_id = %s " |
|
| 615 | " AND utc_date_time BETWEEN %s AND %s " |
|
| 616 | " ORDER BY utc_date_time ") |
|
| 617 | cursor_historical.execute(query, (point['id'], |
|
| 618 | reporting_start_datetime_utc, |
|
| 619 | reporting_end_datetime_utc)) |
|
| 620 | rows = cursor_historical.fetchall() |
|
| 621 | ||
| 622 | if rows is not None and len(rows) > 0: |
|
| 623 | for row in rows: |
|
| 624 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
| 625 | timedelta(minutes=timezone_offset) |
|
| 626 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 627 | point_timestamps.append(current_datetime) |
|
| 628 | point_values.append(row[1]) |
|
| 629 | elif point['object_type'] == 'ANALOG_VALUE': |
|
| 630 | query = (" SELECT utc_date_time, actual_value " |
|
| 631 | " FROM tbl_analog_value " |
|
| 632 | " WHERE point_id = %s " |
|
| 633 | " AND utc_date_time BETWEEN %s AND %s " |
|
| 634 | " ORDER BY utc_date_time ") |
|
| 635 | cursor_historical.execute(query, (point['id'], |
|
| 636 | reporting_start_datetime_utc, |
|
| 637 | reporting_end_datetime_utc)) |
|
| 638 | rows = cursor_historical.fetchall() |
|
| 639 | ||
| 640 | if rows is not None and len(rows) > 0: |
|
| 641 | for row in rows: |
|
| 642 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
| 643 | timedelta(minutes=timezone_offset) |
|
| 644 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 645 | point_timestamps.append(current_datetime) |
|
| 646 | point_values.append(row[1]) |
|
| 647 | elif point['object_type'] == 'DIGITAL_VALUE': |
|
| 648 | query = (" SELECT utc_date_time, actual_value " |
|
| 649 | " FROM tbl_digital_value " |
|
| 650 | " WHERE point_id = %s " |
|
| 651 | " AND utc_date_time BETWEEN %s AND %s " |
|
| 652 | " ORDER BY utc_date_time ") |
|
| 653 | cursor_historical.execute(query, (point['id'], |
|
| 654 | reporting_start_datetime_utc, |
|
| 655 | reporting_end_datetime_utc)) |
|
| 656 | rows = cursor_historical.fetchall() |
|
| 657 | ||
| 658 | if rows is not None and len(rows) > 0: |
|
| 659 | for row in rows: |
|
| 660 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
| 661 | timedelta(minutes=timezone_offset) |
|
| 662 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 663 | point_timestamps.append(current_datetime) |
|
| 664 | point_values.append(row[1]) |
|
| 665 | ||
| 666 | parameters_data['names'].append(point['name'] + ' (' + point['units'] + ')') |
|
| 667 | parameters_data['timestamps'].append(point_timestamps) |
|
| 668 | parameters_data['values'].append(point_values) |
|
| 669 | ||
| 670 | ################################################################################################################ |
|
| 671 | # Step 10: construct the report |
|
| 672 | ################################################################################################################ |
|
| 673 | if cursor_system: |
|
| 674 | cursor_system.close() |
|
| 675 | if cnx_system: |
|
| 676 | cnx_system.close() |
|
| 677 | ||
| 678 | if cursor_energy: |
|
| 679 | cursor_energy.close() |
|
| 680 | if cnx_energy: |
|
| 681 | cnx_energy.close() |
|
| 682 | ||
| 683 | if cursor_energy_baseline: |
|
| 684 | cursor_energy_baseline.close() |
|
| 685 | if cnx_energy_baseline: |
|
| 686 | cnx_energy_baseline.close() |
|
| 687 | ||
| 688 | if cursor_historical: |
|
| 689 | cursor_historical.close() |
|
| 690 | if cnx_historical: |
|
| 691 | cnx_historical.close() |
|
| 692 | ||
| 693 | result = dict() |
|
| 694 | ||
| 695 | result['tenant'] = dict() |
|
| 696 | result['tenant']['name'] = tenant['name'] |
|
| 697 | result['tenant']['area'] = tenant['area'] |
|
| 698 | ||
| 699 | result['base_period'] = dict() |
|
| 700 | result['base_period']['names'] = list() |
|
| 701 | result['base_period']['units'] = list() |
|
| 702 | result['base_period']['timestamps'] = list() |
|
| 703 | result['base_period']['values_saving'] = list() |
|
| 704 | result['base_period']['subtotals_saving'] = list() |
|
| 705 | result['base_period']['subtotals_in_kgce_saving'] = list() |
|
| 706 | result['base_period']['subtotals_in_kgco2e_saving'] = list() |
|
| 707 | result['base_period']['total_in_kgce_saving'] = Decimal(0.0) |
|
| 708 | result['base_period']['total_in_kgco2e_saving'] = Decimal(0.0) |
|
| 709 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
| 710 | for energy_category_id in energy_category_set: |
|
| 711 | result['base_period']['names'].append(energy_category_dict[energy_category_id]['name']) |
|
| 712 | result['base_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) |
|
| 713 | result['base_period']['timestamps'].append(base[energy_category_id]['timestamps']) |
|
| 714 | result['base_period']['values_saving'].append(base[energy_category_id]['values_saving']) |
|
| 715 | result['base_period']['subtotals_saving'].append(base[energy_category_id]['subtotal_saving']) |
|
| 716 | result['base_period']['subtotals_in_kgce_saving'].append( |
|
| 717 | base[energy_category_id]['subtotal_in_kgce_saving']) |
|
| 718 | result['base_period']['subtotals_in_kgco2e_saving'].append( |
|
| 719 | base[energy_category_id]['subtotal_in_kgco2e_saving']) |
|
| 720 | result['base_period']['total_in_kgce_saving'] += base[energy_category_id]['subtotal_in_kgce_saving'] |
|
| 721 | result['base_period']['total_in_kgco2e_saving'] += base[energy_category_id]['subtotal_in_kgco2e_saving'] |
|
| 722 | ||
| 723 | result['reporting_period'] = dict() |
|
| 724 | result['reporting_period']['names'] = list() |
|
| 725 | result['reporting_period']['energy_category_ids'] = list() |
|
| 726 | result['reporting_period']['units'] = list() |
|
| 727 | result['reporting_period']['timestamps'] = list() |
|
| 728 | result['reporting_period']['values_saving'] = list() |
|
| 729 | result['reporting_period']['rates_saving'] = list() |
|
| 730 | result['reporting_period']['subtotals_saving'] = list() |
|
| 731 | result['reporting_period']['subtotals_in_kgce_saving'] = list() |
|
| 732 | result['reporting_period']['subtotals_in_kgco2e_saving'] = list() |
|
| 733 | result['reporting_period']['subtotals_per_unit_area_saving'] = list() |
|
| 734 | result['reporting_period']['increment_rates_saving'] = list() |
|
| 735 | result['reporting_period']['total_in_kgce_saving'] = Decimal(0.0) |
|
| 736 | result['reporting_period']['total_in_kgco2e_saving'] = Decimal(0.0) |
|
| 737 | result['reporting_period']['increment_rate_in_kgce_saving'] = Decimal(0.0) |
|
| 738 | result['reporting_period']['increment_rate_in_kgco2e_saving'] = Decimal(0.0) |
|
| 739 | ||
| 740 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
| 741 | for energy_category_id in energy_category_set: |
|
| 742 | result['reporting_period']['names'].append(energy_category_dict[energy_category_id]['name']) |
|
| 743 | result['reporting_period']['energy_category_ids'].append(energy_category_id) |
|
| 744 | result['reporting_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) |
|
| 745 | result['reporting_period']['timestamps'].append(reporting[energy_category_id]['timestamps']) |
|
| 746 | result['reporting_period']['values_saving'].append(reporting[energy_category_id]['values_saving']) |
|
| 747 | result['reporting_period']['subtotals_saving'].append(reporting[energy_category_id]['subtotal_saving']) |
|
| 748 | result['reporting_period']['subtotals_in_kgce_saving'].append( |
|
| 749 | reporting[energy_category_id]['subtotal_in_kgce_saving']) |
|
| 750 | result['reporting_period']['subtotals_in_kgco2e_saving'].append( |
|
| 751 | reporting[energy_category_id]['subtotal_in_kgco2e_saving']) |
|
| 752 | result['reporting_period']['subtotals_per_unit_area_saving'].append( |
|
| 753 | reporting[energy_category_id]['subtotal_saving'] / tenant['area'] |
|
| 754 | if tenant['area'] != Decimal(0.0) else None) |
|
| 755 | result['reporting_period']['increment_rates_saving'].append( |
|
| 756 | (reporting[energy_category_id]['subtotal_saving'] - base[energy_category_id]['subtotal_saving']) / |
|
| 757 | base[energy_category_id]['subtotal_saving'] |
|
| 758 | if base[energy_category_id]['subtotal_saving'] != Decimal(0.0) else None) |
|
| 759 | result['reporting_period']['total_in_kgce_saving'] += \ |
|
| 760 | reporting[energy_category_id]['subtotal_in_kgce_saving'] |
|
| 761 | result['reporting_period']['total_in_kgco2e_saving'] += \ |
|
| 762 | reporting[energy_category_id]['subtotal_in_kgco2e_saving'] |
|
| 763 | ||
| 764 | rate = list() |
|
| 765 | for index, value in enumerate(reporting[energy_category_id]['values_saving']): |
|
| 766 | if index < len(base[energy_category_id]['values_saving']) \ |
|
| 767 | and base[energy_category_id]['values_saving'][index] != Decimal(0.0) \ |
|
| 768 | and value != Decimal(0.0): |
|
| 769 | rate.append((value - base[energy_category_id]['values_saving'][index]) |
|
| 770 | / base[energy_category_id]['values_saving'][index]) |
|
| 771 | else: |
|
| 772 | rate.append(None) |
|
| 773 | result['reporting_period']['rates_saving'].append(rate) |
|
| 774 | ||
| 775 | result['reporting_period']['total_in_kgco2e_per_unit_area_saving'] = \ |
|
| 776 | result['reporting_period']['total_in_kgce_saving'] / tenant['area'] \ |
|
| 777 | if tenant['area'] != Decimal(0.0) else None |
|
| 778 | ||
| 779 | result['reporting_period']['increment_rate_in_kgce_saving'] = \ |
|
| 780 | (result['reporting_period']['total_in_kgce_saving'] - result['base_period']['total_in_kgce_saving']) / \ |
|
| 781 | result['base_period']['total_in_kgce_saving'] \ |
|
| 782 | if result['base_period']['total_in_kgce_saving'] != Decimal(0.0) else None |
|
| 783 | ||
| 784 | result['reporting_period']['total_in_kgce_per_unit_area_saving'] = \ |
|
| 785 | result['reporting_period']['total_in_kgco2e_saving'] / tenant['area'] if tenant['area'] > 0.0 else None |
|
| 786 | ||
| 787 | result['reporting_period']['increment_rate_in_kgco2e_saving'] = \ |
|
| 788 | (result['reporting_period']['total_in_kgco2e_saving'] - result['base_period']['total_in_kgco2e_saving']) / \ |
|
| 789 | result['base_period']['total_in_kgco2e_saving'] \ |
|
| 790 | if result['base_period']['total_in_kgco2e_saving'] != Decimal(0.0) else None |
|
| 791 | ||
| 792 | result['parameters'] = { |
|
| 793 | "names": parameters_data['names'], |
|
| 794 | "timestamps": parameters_data['timestamps'], |
|
| 795 | "values": parameters_data['values'] |
|
| 796 | } |
|
| 797 | ||
| 798 | # export result to Excel file and then encode the file to base64 string |
|
| 799 | if not is_quick_mode: |
|
| 800 | result['excel_bytes_base64'] = excelexporters.tenantsaving.export(result, |
|
| 801 | tenant['name'], |
|
| 802 | base_period_start_datetime_local, |
|
| 803 | base_period_end_datetime_local, |
|
| 804 | reporting_period_start_datetime_local, |
|
| 805 | reporting_period_end_datetime_local, |
|
| 806 | period_type, |
|
| 807 | language) |
|
| 808 | ||
| 809 | resp.text = json.dumps(result) |
|
| 810 | ||
| @@ 45-808 (lines=764) @@ | ||
| 42 | from core.useractivity import access_control, api_key_control |
|
| 43 | ||
| 44 | ||
| 45 | class Reporting: |
|
| 46 | def __init__(self): |
|
| 47 | """"Initializes Reporting""" |
|
| 48 | pass |
|
| 49 | ||
| 50 | @staticmethod |
|
| 51 | def on_options(req, resp): |
|
| 52 | _ = req |
|
| 53 | resp.status = falcon.HTTP_200 |
|
| 54 | ||
| 55 | #################################################################################################################### |
|
| 56 | # PROCEDURES |
|
| 57 | # Step 1: valid parameters |
|
| 58 | # Step 2: query the tenant |
|
| 59 | # Step 3: query energy categories |
|
| 60 | # Step 4: query associated sensors |
|
| 61 | # Step 5: query associated points |
|
| 62 | # Step 6: query base period energy saving |
|
| 63 | # Step 7: query reporting period energy saving |
|
| 64 | # Step 8: query tariff data |
|
| 65 | # Step 9: query associated sensors and points data |
|
| 66 | # Step 10: construct the report |
|
| 67 | #################################################################################################################### |
|
| 68 | @staticmethod |
|
| 69 | def on_get(req, resp): |
|
| 70 | if 'API-KEY' not in req.headers or \ |
|
| 71 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 72 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 73 | access_control(req) |
|
| 74 | else: |
|
| 75 | api_key_control(req) |
|
| 76 | print(req.params) |
|
| 77 | tenant_id = req.params.get('tenantid') |
|
| 78 | tenant_uuid = req.params.get('tenantuuid') |
|
| 79 | period_type = req.params.get('periodtype') |
|
| 80 | base_period_start_datetime_local = req.params.get('baseperiodstartdatetime') |
|
| 81 | base_period_end_datetime_local = req.params.get('baseperiodenddatetime') |
|
| 82 | reporting_period_start_datetime_local = req.params.get('reportingperiodstartdatetime') |
|
| 83 | reporting_period_end_datetime_local = req.params.get('reportingperiodenddatetime') |
|
| 84 | language = req.params.get('language') |
|
| 85 | quick_mode = req.params.get('quickmode') |
|
| 86 | ||
| 87 | ################################################################################################################ |
|
| 88 | # Step 1: valid parameters |
|
| 89 | ################################################################################################################ |
|
| 90 | if tenant_id is None and tenant_uuid is None: |
|
| 91 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 92 | title='API.BAD_REQUEST', |
|
| 93 | description='API.INVALID_TENANT_ID') |
|
| 94 | ||
| 95 | if tenant_id is not None: |
|
| 96 | tenant_id = str.strip(tenant_id) |
|
| 97 | if not tenant_id.isdigit() or int(tenant_id) <= 0: |
|
| 98 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 99 | title='API.BAD_REQUEST', |
|
| 100 | description='API.INVALID_TENANT_ID') |
|
| 101 | ||
| 102 | if tenant_uuid is not None: |
|
| 103 | regex = re.compile(r'^[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) |
|
| 104 | match = regex.match(str.strip(tenant_uuid)) |
|
| 105 | if not bool(match): |
|
| 106 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 107 | title='API.BAD_REQUEST', |
|
| 108 | description='API.INVALID_TENANT_UUID') |
|
| 109 | ||
| 110 | if period_type is None: |
|
| 111 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 112 | description='API.INVALID_PERIOD_TYPE') |
|
| 113 | else: |
|
| 114 | period_type = str.strip(period_type) |
|
| 115 | if period_type not in ['hourly', 'daily', 'weekly', 'monthly', 'yearly']: |
|
| 116 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 117 | description='API.INVALID_PERIOD_TYPE') |
|
| 118 | ||
| 119 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
|
| 120 | if config.utc_offset[0] == '-': |
|
| 121 | timezone_offset = -timezone_offset |
|
| 122 | ||
| 123 | base_start_datetime_utc = None |
|
| 124 | if base_period_start_datetime_local is not None and len(str.strip(base_period_start_datetime_local)) > 0: |
|
| 125 | base_period_start_datetime_local = str.strip(base_period_start_datetime_local) |
|
| 126 | try: |
|
| 127 | base_start_datetime_utc = datetime.strptime(base_period_start_datetime_local, '%Y-%m-%dT%H:%M:%S') |
|
| 128 | except ValueError: |
|
| 129 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 130 | description="API.INVALID_BASE_PERIOD_START_DATETIME") |
|
| 131 | base_start_datetime_utc = \ |
|
| 132 | base_start_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset) |
|
| 133 | # nomalize the start datetime |
|
| 134 | if config.minutes_to_count == 30 and base_start_datetime_utc.minute >= 30: |
|
| 135 | base_start_datetime_utc = base_start_datetime_utc.replace(minute=30, second=0, microsecond=0) |
|
| 136 | else: |
|
| 137 | base_start_datetime_utc = base_start_datetime_utc.replace(minute=0, second=0, microsecond=0) |
|
| 138 | ||
| 139 | base_end_datetime_utc = None |
|
| 140 | if base_period_end_datetime_local is not None and len(str.strip(base_period_end_datetime_local)) > 0: |
|
| 141 | base_period_end_datetime_local = str.strip(base_period_end_datetime_local) |
|
| 142 | try: |
|
| 143 | base_end_datetime_utc = datetime.strptime(base_period_end_datetime_local, '%Y-%m-%dT%H:%M:%S') |
|
| 144 | except ValueError: |
|
| 145 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 146 | description="API.INVALID_BASE_PERIOD_END_DATETIME") |
|
| 147 | base_end_datetime_utc = \ |
|
| 148 | base_end_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset) |
|
| 149 | ||
| 150 | if base_start_datetime_utc is not None and base_end_datetime_utc is not None and \ |
|
| 151 | base_start_datetime_utc >= base_end_datetime_utc: |
|
| 152 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 153 | description='API.INVALID_BASE_PERIOD_END_DATETIME') |
|
| 154 | ||
| 155 | if reporting_period_start_datetime_local is None: |
|
| 156 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 157 | description="API.INVALID_REPORTING_PERIOD_START_DATETIME") |
|
| 158 | else: |
|
| 159 | reporting_period_start_datetime_local = str.strip(reporting_period_start_datetime_local) |
|
| 160 | try: |
|
| 161 | reporting_start_datetime_utc = datetime.strptime(reporting_period_start_datetime_local, |
|
| 162 | '%Y-%m-%dT%H:%M:%S') |
|
| 163 | except ValueError: |
|
| 164 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 165 | description="API.INVALID_REPORTING_PERIOD_START_DATETIME") |
|
| 166 | reporting_start_datetime_utc = \ |
|
| 167 | reporting_start_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset) |
|
| 168 | # nomalize the start datetime |
|
| 169 | if config.minutes_to_count == 30 and reporting_start_datetime_utc.minute >= 30: |
|
| 170 | reporting_start_datetime_utc = reporting_start_datetime_utc.replace(minute=30, second=0, microsecond=0) |
|
| 171 | else: |
|
| 172 | reporting_start_datetime_utc = reporting_start_datetime_utc.replace(minute=0, second=0, microsecond=0) |
|
| 173 | ||
| 174 | if reporting_period_end_datetime_local is None: |
|
| 175 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 176 | description="API.INVALID_REPORTING_PERIOD_END_DATETIME") |
|
| 177 | else: |
|
| 178 | reporting_period_end_datetime_local = str.strip(reporting_period_end_datetime_local) |
|
| 179 | try: |
|
| 180 | reporting_end_datetime_utc = datetime.strptime(reporting_period_end_datetime_local, |
|
| 181 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
|
| 182 | timedelta(minutes=timezone_offset) |
|
| 183 | except ValueError: |
|
| 184 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 185 | description="API.INVALID_REPORTING_PERIOD_END_DATETIME") |
|
| 186 | ||
| 187 | if reporting_start_datetime_utc >= reporting_end_datetime_utc: |
|
| 188 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 189 | description='API.INVALID_REPORTING_PERIOD_END_DATETIME') |
|
| 190 | ||
| 191 | # if turn quick mode on, do not return parameters data and excel file |
|
| 192 | is_quick_mode = False |
|
| 193 | if quick_mode is not None and \ |
|
| 194 | len(str.strip(quick_mode)) > 0 and \ |
|
| 195 | str.lower(str.strip(quick_mode)) in ('true', 't', 'on', 'yes', 'y'): |
|
| 196 | is_quick_mode = True |
|
| 197 | ||
| 198 | trans = utilities.get_translation(language) |
|
| 199 | trans.install() |
|
| 200 | _ = trans.gettext |
|
| 201 | ||
| 202 | ################################################################################################################ |
|
| 203 | # Step 2: query the tenant |
|
| 204 | ################################################################################################################ |
|
| 205 | cnx_system = mysql.connector.connect(**config.myems_system_db) |
|
| 206 | cursor_system = cnx_system.cursor() |
|
| 207 | ||
| 208 | cnx_energy = mysql.connector.connect(**config.myems_energy_db) |
|
| 209 | cursor_energy = cnx_energy.cursor() |
|
| 210 | ||
| 211 | cnx_energy_plan = mysql.connector.connect(**config.myems_energy_plan_db) |
|
| 212 | cursor_energy_plan = cnx_energy_plan.cursor() |
|
| 213 | ||
| 214 | cnx_historical = mysql.connector.connect(**config.myems_historical_db) |
|
| 215 | cursor_historical = cnx_historical.cursor() |
|
| 216 | ||
| 217 | if tenant_id is not None: |
|
| 218 | cursor_system.execute(" SELECT id, name, area, cost_center_id " |
|
| 219 | " FROM tbl_tenants " |
|
| 220 | " WHERE id = %s ", (tenant_id,)) |
|
| 221 | row_tenant = cursor_system.fetchone() |
|
| 222 | elif tenant_uuid is not None: |
|
| 223 | cursor_system.execute(" SELECT id, name, area, cost_center_id " |
|
| 224 | " FROM tbl_tenants " |
|
| 225 | " WHERE uuid = %s ", (tenant_uuid,)) |
|
| 226 | row_tenant = cursor_system.fetchone() |
|
| 227 | ||
| 228 | if row_tenant is None: |
|
| 229 | if cursor_system: |
|
| 230 | cursor_system.close() |
|
| 231 | if cnx_system: |
|
| 232 | cnx_system.close() |
|
| 233 | ||
| 234 | if cursor_energy: |
|
| 235 | cursor_energy.close() |
|
| 236 | if cnx_energy: |
|
| 237 | cnx_energy.close() |
|
| 238 | ||
| 239 | if cursor_energy_plan: |
|
| 240 | cursor_energy_plan.close() |
|
| 241 | if cnx_energy_plan: |
|
| 242 | cnx_energy_plan.close() |
|
| 243 | ||
| 244 | if cursor_historical: |
|
| 245 | cursor_historical.close() |
|
| 246 | if cnx_historical: |
|
| 247 | cnx_historical.close() |
|
| 248 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', description='API.TENANT_NOT_FOUND') |
|
| 249 | ||
| 250 | tenant = dict() |
|
| 251 | tenant['id'] = row_tenant[0] |
|
| 252 | tenant['name'] = row_tenant[1] |
|
| 253 | tenant['area'] = row_tenant[2] |
|
| 254 | tenant['cost_center_id'] = row_tenant[3] |
|
| 255 | ||
| 256 | ################################################################################################################ |
|
| 257 | # Step 3: query energy categories |
|
| 258 | ################################################################################################################ |
|
| 259 | energy_category_set = set() |
|
| 260 | # query energy categories in base period |
|
| 261 | cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
|
| 262 | " FROM tbl_tenant_input_category_hourly " |
|
| 263 | " WHERE tenant_id = %s " |
|
| 264 | " AND start_datetime_utc >= %s " |
|
| 265 | " AND start_datetime_utc < %s ", |
|
| 266 | (tenant['id'], base_start_datetime_utc, base_end_datetime_utc)) |
|
| 267 | rows_energy_categories = cursor_energy.fetchall() |
|
| 268 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
| 269 | for row_energy_category in rows_energy_categories: |
|
| 270 | energy_category_set.add(row_energy_category[0]) |
|
| 271 | ||
| 272 | # query energy categories in reporting period |
|
| 273 | cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
|
| 274 | " FROM tbl_tenant_input_category_hourly " |
|
| 275 | " WHERE tenant_id = %s " |
|
| 276 | " AND start_datetime_utc >= %s " |
|
| 277 | " AND start_datetime_utc < %s ", |
|
| 278 | (tenant['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) |
|
| 279 | rows_energy_categories = cursor_energy.fetchall() |
|
| 280 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
| 281 | for row_energy_category in rows_energy_categories: |
|
| 282 | energy_category_set.add(row_energy_category[0]) |
|
| 283 | ||
| 284 | # query all energy categories in base period and reporting period |
|
| 285 | cursor_system.execute(" SELECT id, name, unit_of_measure, kgce, kgco2e " |
|
| 286 | " FROM tbl_energy_categories " |
|
| 287 | " ORDER BY id ", ) |
|
| 288 | rows_energy_categories = cursor_system.fetchall() |
|
| 289 | if rows_energy_categories is None or len(rows_energy_categories) == 0: |
|
| 290 | if cursor_system: |
|
| 291 | cursor_system.close() |
|
| 292 | if cnx_system: |
|
| 293 | cnx_system.close() |
|
| 294 | ||
| 295 | if cursor_energy: |
|
| 296 | cursor_energy.close() |
|
| 297 | if cnx_energy: |
|
| 298 | cnx_energy.close() |
|
| 299 | ||
| 300 | if cursor_energy_plan: |
|
| 301 | cursor_energy_plan.close() |
|
| 302 | if cnx_energy_plan: |
|
| 303 | cnx_energy_plan.close() |
|
| 304 | ||
| 305 | if cursor_historical: |
|
| 306 | cursor_historical.close() |
|
| 307 | if cnx_historical: |
|
| 308 | cnx_historical.close() |
|
| 309 | raise falcon.HTTPError(status=falcon.HTTP_404, |
|
| 310 | title='API.NOT_FOUND', |
|
| 311 | description='API.ENERGY_CATEGORY_NOT_FOUND') |
|
| 312 | energy_category_dict = dict() |
|
| 313 | for row_energy_category in rows_energy_categories: |
|
| 314 | if row_energy_category[0] in energy_category_set: |
|
| 315 | energy_category_dict[row_energy_category[0]] = {"name": row_energy_category[1], |
|
| 316 | "unit_of_measure": row_energy_category[2], |
|
| 317 | "kgce": row_energy_category[3], |
|
| 318 | "kgco2e": row_energy_category[4]} |
|
| 319 | ||
| 320 | ################################################################################################################ |
|
| 321 | # Step 4: query associated sensors |
|
| 322 | ################################################################################################################ |
|
| 323 | point_list = list() |
|
| 324 | cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type " |
|
| 325 | " FROM tbl_tenants t, tbl_sensors s, tbl_tenants_sensors ts, " |
|
| 326 | " tbl_points p, tbl_sensors_points sp " |
|
| 327 | " WHERE t.id = %s AND t.id = ts.tenant_id AND ts.sensor_id = s.id " |
|
| 328 | " AND s.id = sp.sensor_id AND sp.point_id = p.id " |
|
| 329 | " ORDER BY p.id ", (tenant['id'],)) |
|
| 330 | rows_points = cursor_system.fetchall() |
|
| 331 | if rows_points is not None and len(rows_points) > 0: |
|
| 332 | for row in rows_points: |
|
| 333 | point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]}) |
|
| 334 | ||
| 335 | ################################################################################################################ |
|
| 336 | # Step 5: query associated points |
|
| 337 | ################################################################################################################ |
|
| 338 | cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type " |
|
| 339 | " FROM tbl_tenants t, tbl_tenants_points tp, tbl_points p " |
|
| 340 | " WHERE t.id = %s AND t.id = tp.tenant_id AND tp.point_id = p.id " |
|
| 341 | " ORDER BY p.id ", (tenant['id'],)) |
|
| 342 | rows_points = cursor_system.fetchall() |
|
| 343 | if rows_points is not None and len(rows_points) > 0: |
|
| 344 | for row in rows_points: |
|
| 345 | point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]}) |
|
| 346 | ||
| 347 | ################################################################################################################ |
|
| 348 | # Step 6: query base period energy saving |
|
| 349 | ################################################################################################################ |
|
| 350 | base = dict() |
|
| 351 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
| 352 | for energy_category_id in energy_category_set: |
|
| 353 | kgce = energy_category_dict[energy_category_id]['kgce'] |
|
| 354 | kgco2e = energy_category_dict[energy_category_id]['kgco2e'] |
|
| 355 | ||
| 356 | base[energy_category_id] = dict() |
|
| 357 | base[energy_category_id]['timestamps'] = list() |
|
| 358 | base[energy_category_id]['values_plan'] = list() |
|
| 359 | base[energy_category_id]['values_actual'] = list() |
|
| 360 | base[energy_category_id]['values_saving'] = list() |
|
| 361 | base[energy_category_id]['subtotal_plan'] = Decimal(0.0) |
|
| 362 | base[energy_category_id]['subtotal_actual'] = Decimal(0.0) |
|
| 363 | base[energy_category_id]['subtotal_saving'] = Decimal(0.0) |
|
| 364 | base[energy_category_id]['subtotal_in_kgce_plan'] = Decimal(0.0) |
|
| 365 | base[energy_category_id]['subtotal_in_kgce_actual'] = Decimal(0.0) |
|
| 366 | base[energy_category_id]['subtotal_in_kgce_saving'] = Decimal(0.0) |
|
| 367 | base[energy_category_id]['subtotal_in_kgco2e_plan'] = Decimal(0.0) |
|
| 368 | base[energy_category_id]['subtotal_in_kgco2e_actual'] = Decimal(0.0) |
|
| 369 | base[energy_category_id]['subtotal_in_kgco2e_saving'] = Decimal(0.0) |
|
| 370 | # query base period's energy plan |
|
| 371 | cursor_energy_plan.execute(" SELECT start_datetime_utc, actual_value " |
|
| 372 | " FROM tbl_tenant_input_category_hourly " |
|
| 373 | " WHERE tenant_id = %s " |
|
| 374 | " AND energy_category_id = %s " |
|
| 375 | " AND start_datetime_utc >= %s " |
|
| 376 | " AND start_datetime_utc < %s " |
|
| 377 | " ORDER BY start_datetime_utc ", |
|
| 378 | (tenant['id'], |
|
| 379 | energy_category_id, |
|
| 380 | base_start_datetime_utc, |
|
| 381 | base_end_datetime_utc)) |
|
| 382 | rows_tenant_hourly = cursor_energy_plan.fetchall() |
|
| 383 | ||
| 384 | rows_tenant_periodically = utilities.aggregate_hourly_data_by_period(rows_tenant_hourly, |
|
| 385 | base_start_datetime_utc, |
|
| 386 | base_end_datetime_utc, |
|
| 387 | period_type) |
|
| 388 | for row_tenant_periodically in rows_tenant_periodically: |
|
| 389 | current_datetime_local = row_tenant_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
| 390 | timedelta(minutes=timezone_offset) |
|
| 391 | if period_type == 'hourly': |
|
| 392 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 393 | elif period_type == 'daily': |
|
| 394 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 395 | elif period_type == 'weekly': |
|
| 396 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 397 | elif period_type == 'monthly': |
|
| 398 | current_datetime = current_datetime_local.isoformat()[0:7] |
|
| 399 | elif period_type == 'yearly': |
|
| 400 | current_datetime = current_datetime_local.isoformat()[0:4] |
|
| 401 | ||
| 402 | plan_value = Decimal(0.0) if row_tenant_periodically[1] is None else row_tenant_periodically[1] |
|
| 403 | base[energy_category_id]['timestamps'].append(current_datetime) |
|
| 404 | base[energy_category_id]['values_plan'].append(plan_value) |
|
| 405 | base[energy_category_id]['subtotal_plan'] += plan_value |
|
| 406 | base[energy_category_id]['subtotal_in_kgce_plan'] += plan_value * kgce |
|
| 407 | base[energy_category_id]['subtotal_in_kgco2e_plan'] += plan_value * kgco2e |
|
| 408 | ||
| 409 | # query base period's energy actual |
|
| 410 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
|
| 411 | " FROM tbl_tenant_input_category_hourly " |
|
| 412 | " WHERE tenant_id = %s " |
|
| 413 | " AND energy_category_id = %s " |
|
| 414 | " AND start_datetime_utc >= %s " |
|
| 415 | " AND start_datetime_utc < %s " |
|
| 416 | " ORDER BY start_datetime_utc ", |
|
| 417 | (tenant['id'], |
|
| 418 | energy_category_id, |
|
| 419 | base_start_datetime_utc, |
|
| 420 | base_end_datetime_utc)) |
|
| 421 | rows_tenant_hourly = cursor_energy.fetchall() |
|
| 422 | ||
| 423 | rows_tenant_periodically = utilities.aggregate_hourly_data_by_period(rows_tenant_hourly, |
|
| 424 | base_start_datetime_utc, |
|
| 425 | base_end_datetime_utc, |
|
| 426 | period_type) |
|
| 427 | for row_tenant_periodically in rows_tenant_periodically: |
|
| 428 | current_datetime_local = row_tenant_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
| 429 | timedelta(minutes=timezone_offset) |
|
| 430 | if period_type == 'hourly': |
|
| 431 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 432 | elif period_type == 'daily': |
|
| 433 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 434 | elif period_type == 'weekly': |
|
| 435 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 436 | elif period_type == 'monthly': |
|
| 437 | current_datetime = current_datetime_local.isoformat()[0:7] |
|
| 438 | elif period_type == 'yearly': |
|
| 439 | current_datetime = current_datetime_local.isoformat()[0:4] |
|
| 440 | ||
| 441 | actual_value = Decimal(0.0) if row_tenant_periodically[1] is None else row_tenant_periodically[1] |
|
| 442 | base[energy_category_id]['values_actual'].append(actual_value) |
|
| 443 | base[energy_category_id]['subtotal_actual'] += actual_value |
|
| 444 | base[energy_category_id]['subtotal_in_kgce_actual'] += actual_value * kgce |
|
| 445 | base[energy_category_id]['subtotal_in_kgco2e_actual'] += actual_value * kgco2e |
|
| 446 | ||
| 447 | # calculate base period's energy saving |
|
| 448 | for i in range(len(base[energy_category_id]['values_plan'])): |
|
| 449 | base[energy_category_id]['values_saving'].append( |
|
| 450 | base[energy_category_id]['values_plan'][i] - |
|
| 451 | base[energy_category_id]['values_actual'][i]) |
|
| 452 | ||
| 453 | base[energy_category_id]['subtotal_saving'] = \ |
|
| 454 | base[energy_category_id]['subtotal_plan'] - \ |
|
| 455 | base[energy_category_id]['subtotal_actual'] |
|
| 456 | base[energy_category_id]['subtotal_in_kgce_saving'] = \ |
|
| 457 | base[energy_category_id]['subtotal_in_kgce_plan'] - \ |
|
| 458 | base[energy_category_id]['subtotal_in_kgce_actual'] |
|
| 459 | base[energy_category_id]['subtotal_in_kgco2e_saving'] = \ |
|
| 460 | base[energy_category_id]['subtotal_in_kgco2e_plan'] - \ |
|
| 461 | base[energy_category_id]['subtotal_in_kgco2e_actual'] |
|
| 462 | ################################################################################################################ |
|
| 463 | # Step 7: query reporting period energy saving |
|
| 464 | ################################################################################################################ |
|
| 465 | reporting = dict() |
|
| 466 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
| 467 | for energy_category_id in energy_category_set: |
|
| 468 | kgce = energy_category_dict[energy_category_id]['kgce'] |
|
| 469 | kgco2e = energy_category_dict[energy_category_id]['kgco2e'] |
|
| 470 | ||
| 471 | reporting[energy_category_id] = dict() |
|
| 472 | reporting[energy_category_id]['timestamps'] = list() |
|
| 473 | reporting[energy_category_id]['values_plan'] = list() |
|
| 474 | reporting[energy_category_id]['values_actual'] = list() |
|
| 475 | reporting[energy_category_id]['values_saving'] = list() |
|
| 476 | reporting[energy_category_id]['subtotal_plan'] = Decimal(0.0) |
|
| 477 | reporting[energy_category_id]['subtotal_actual'] = Decimal(0.0) |
|
| 478 | reporting[energy_category_id]['subtotal_saving'] = Decimal(0.0) |
|
| 479 | reporting[energy_category_id]['subtotal_in_kgce_plan'] = Decimal(0.0) |
|
| 480 | reporting[energy_category_id]['subtotal_in_kgce_actual'] = Decimal(0.0) |
|
| 481 | reporting[energy_category_id]['subtotal_in_kgce_saving'] = Decimal(0.0) |
|
| 482 | reporting[energy_category_id]['subtotal_in_kgco2e_plan'] = Decimal(0.0) |
|
| 483 | reporting[energy_category_id]['subtotal_in_kgco2e_actual'] = Decimal(0.0) |
|
| 484 | reporting[energy_category_id]['subtotal_in_kgco2e_saving'] = Decimal(0.0) |
|
| 485 | # query reporting period's energy plan |
|
| 486 | cursor_energy_plan.execute(" SELECT start_datetime_utc, actual_value " |
|
| 487 | " FROM tbl_tenant_input_category_hourly " |
|
| 488 | " WHERE tenant_id = %s " |
|
| 489 | " AND energy_category_id = %s " |
|
| 490 | " AND start_datetime_utc >= %s " |
|
| 491 | " AND start_datetime_utc < %s " |
|
| 492 | " ORDER BY start_datetime_utc ", |
|
| 493 | (tenant['id'], |
|
| 494 | energy_category_id, |
|
| 495 | reporting_start_datetime_utc, |
|
| 496 | reporting_end_datetime_utc)) |
|
| 497 | rows_tenant_hourly = cursor_energy_plan.fetchall() |
|
| 498 | ||
| 499 | rows_tenant_periodically = utilities.aggregate_hourly_data_by_period(rows_tenant_hourly, |
|
| 500 | reporting_start_datetime_utc, |
|
| 501 | reporting_end_datetime_utc, |
|
| 502 | period_type) |
|
| 503 | for row_tenant_periodically in rows_tenant_periodically: |
|
| 504 | current_datetime_local = row_tenant_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
| 505 | timedelta(minutes=timezone_offset) |
|
| 506 | if period_type == 'hourly': |
|
| 507 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 508 | elif period_type == 'daily': |
|
| 509 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 510 | elif period_type == 'weekly': |
|
| 511 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 512 | elif period_type == 'monthly': |
|
| 513 | current_datetime = current_datetime_local.isoformat()[0:7] |
|
| 514 | elif period_type == 'yearly': |
|
| 515 | current_datetime = current_datetime_local.isoformat()[0:4] |
|
| 516 | ||
| 517 | plan_value = Decimal(0.0) if row_tenant_periodically[1] is None else row_tenant_periodically[1] |
|
| 518 | reporting[energy_category_id]['timestamps'].append(current_datetime) |
|
| 519 | reporting[energy_category_id]['values_plan'].append(plan_value) |
|
| 520 | reporting[energy_category_id]['subtotal_plan'] += plan_value |
|
| 521 | reporting[energy_category_id]['subtotal_in_kgce_plan'] += plan_value * kgce |
|
| 522 | reporting[energy_category_id]['subtotal_in_kgco2e_plan'] += plan_value * kgco2e |
|
| 523 | ||
| 524 | # query reporting period's energy actual |
|
| 525 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
|
| 526 | " FROM tbl_tenant_input_category_hourly " |
|
| 527 | " WHERE tenant_id = %s " |
|
| 528 | " AND energy_category_id = %s " |
|
| 529 | " AND start_datetime_utc >= %s " |
|
| 530 | " AND start_datetime_utc < %s " |
|
| 531 | " ORDER BY start_datetime_utc ", |
|
| 532 | (tenant['id'], |
|
| 533 | energy_category_id, |
|
| 534 | reporting_start_datetime_utc, |
|
| 535 | reporting_end_datetime_utc)) |
|
| 536 | rows_tenant_hourly = cursor_energy.fetchall() |
|
| 537 | ||
| 538 | rows_tenant_periodically = utilities.aggregate_hourly_data_by_period(rows_tenant_hourly, |
|
| 539 | reporting_start_datetime_utc, |
|
| 540 | reporting_end_datetime_utc, |
|
| 541 | period_type) |
|
| 542 | for row_tenant_periodically in rows_tenant_periodically: |
|
| 543 | current_datetime_local = row_tenant_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
| 544 | timedelta(minutes=timezone_offset) |
|
| 545 | if period_type == 'hourly': |
|
| 546 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 547 | elif period_type == 'daily': |
|
| 548 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 549 | elif period_type == 'weekly': |
|
| 550 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 551 | elif period_type == 'monthly': |
|
| 552 | current_datetime = current_datetime_local.isoformat()[0:7] |
|
| 553 | elif period_type == 'yearly': |
|
| 554 | current_datetime = current_datetime_local.isoformat()[0:4] |
|
| 555 | ||
| 556 | actual_value = Decimal(0.0) if row_tenant_periodically[1] is None else row_tenant_periodically[1] |
|
| 557 | reporting[energy_category_id]['values_actual'].append(actual_value) |
|
| 558 | reporting[energy_category_id]['subtotal_actual'] += actual_value |
|
| 559 | reporting[energy_category_id]['subtotal_in_kgce_actual'] += actual_value * kgce |
|
| 560 | reporting[energy_category_id]['subtotal_in_kgco2e_actual'] += actual_value * kgco2e |
|
| 561 | ||
| 562 | # calculate reporting period's energy savings |
|
| 563 | for i in range(len(reporting[energy_category_id]['values_plan'])): |
|
| 564 | reporting[energy_category_id]['values_saving'].append( |
|
| 565 | reporting[energy_category_id]['values_plan'][i] - |
|
| 566 | reporting[energy_category_id]['values_actual'][i]) |
|
| 567 | ||
| 568 | reporting[energy_category_id]['subtotal_saving'] = \ |
|
| 569 | reporting[energy_category_id]['subtotal_plan'] - \ |
|
| 570 | reporting[energy_category_id]['subtotal_actual'] |
|
| 571 | reporting[energy_category_id]['subtotal_in_kgce_saving'] = \ |
|
| 572 | reporting[energy_category_id]['subtotal_in_kgce_plan'] - \ |
|
| 573 | reporting[energy_category_id]['subtotal_in_kgce_actual'] |
|
| 574 | reporting[energy_category_id]['subtotal_in_kgco2e_saving'] = \ |
|
| 575 | reporting[energy_category_id]['subtotal_in_kgco2e_plan'] - \ |
|
| 576 | reporting[energy_category_id]['subtotal_in_kgco2e_actual'] |
|
| 577 | ################################################################################################################ |
|
| 578 | # Step 8: query tariff data |
|
| 579 | ################################################################################################################ |
|
| 580 | parameters_data = dict() |
|
| 581 | parameters_data['names'] = list() |
|
| 582 | parameters_data['timestamps'] = list() |
|
| 583 | parameters_data['values'] = list() |
|
| 584 | if config.is_tariff_appended and energy_category_set is not None and len(energy_category_set) > 0 \ |
|
| 585 | and not is_quick_mode: |
|
| 586 | for energy_category_id in energy_category_set: |
|
| 587 | energy_category_tariff_dict = utilities.get_energy_category_tariffs(tenant['cost_center_id'], |
|
| 588 | energy_category_id, |
|
| 589 | reporting_start_datetime_utc, |
|
| 590 | reporting_end_datetime_utc) |
|
| 591 | tariff_timestamp_list = list() |
|
| 592 | tariff_value_list = list() |
|
| 593 | for k, v in energy_category_tariff_dict.items(): |
|
| 594 | # convert k from utc to local |
|
| 595 | k = k + timedelta(minutes=timezone_offset) |
|
| 596 | tariff_timestamp_list.append(k.isoformat()[0:19]) |
|
| 597 | tariff_value_list.append(v) |
|
| 598 | ||
| 599 | parameters_data['names'].append(_('Tariff') + '-' + energy_category_dict[energy_category_id]['name']) |
|
| 600 | parameters_data['timestamps'].append(tariff_timestamp_list) |
|
| 601 | parameters_data['values'].append(tariff_value_list) |
|
| 602 | ||
| 603 | ################################################################################################################ |
|
| 604 | # Step 9: query associated sensors and points data |
|
| 605 | ################################################################################################################ |
|
| 606 | if not is_quick_mode: |
|
| 607 | for point in point_list: |
|
| 608 | point_values = [] |
|
| 609 | point_timestamps = [] |
|
| 610 | if point['object_type'] == 'ENERGY_VALUE': |
|
| 611 | query = (" SELECT utc_date_time, actual_value " |
|
| 612 | " FROM tbl_energy_value " |
|
| 613 | " WHERE point_id = %s " |
|
| 614 | " AND utc_date_time BETWEEN %s AND %s " |
|
| 615 | " ORDER BY utc_date_time ") |
|
| 616 | cursor_historical.execute(query, (point['id'], |
|
| 617 | reporting_start_datetime_utc, |
|
| 618 | reporting_end_datetime_utc)) |
|
| 619 | rows = cursor_historical.fetchall() |
|
| 620 | ||
| 621 | if rows is not None and len(rows) > 0: |
|
| 622 | for row in rows: |
|
| 623 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
| 624 | timedelta(minutes=timezone_offset) |
|
| 625 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 626 | point_timestamps.append(current_datetime) |
|
| 627 | point_values.append(row[1]) |
|
| 628 | elif point['object_type'] == 'ANALOG_VALUE': |
|
| 629 | query = (" SELECT utc_date_time, actual_value " |
|
| 630 | " FROM tbl_analog_value " |
|
| 631 | " WHERE point_id = %s " |
|
| 632 | " AND utc_date_time BETWEEN %s AND %s " |
|
| 633 | " ORDER BY utc_date_time ") |
|
| 634 | cursor_historical.execute(query, (point['id'], |
|
| 635 | reporting_start_datetime_utc, |
|
| 636 | reporting_end_datetime_utc)) |
|
| 637 | rows = cursor_historical.fetchall() |
|
| 638 | ||
| 639 | if rows is not None and len(rows) > 0: |
|
| 640 | for row in rows: |
|
| 641 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
| 642 | timedelta(minutes=timezone_offset) |
|
| 643 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 644 | point_timestamps.append(current_datetime) |
|
| 645 | point_values.append(row[1]) |
|
| 646 | elif point['object_type'] == 'DIGITAL_VALUE': |
|
| 647 | query = (" SELECT utc_date_time, actual_value " |
|
| 648 | " FROM tbl_digital_value " |
|
| 649 | " WHERE point_id = %s " |
|
| 650 | " AND utc_date_time BETWEEN %s AND %s " |
|
| 651 | " ORDER BY utc_date_time ") |
|
| 652 | cursor_historical.execute(query, (point['id'], |
|
| 653 | reporting_start_datetime_utc, |
|
| 654 | reporting_end_datetime_utc)) |
|
| 655 | rows = cursor_historical.fetchall() |
|
| 656 | ||
| 657 | if rows is not None and len(rows) > 0: |
|
| 658 | for row in rows: |
|
| 659 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
| 660 | timedelta(minutes=timezone_offset) |
|
| 661 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 662 | point_timestamps.append(current_datetime) |
|
| 663 | point_values.append(row[1]) |
|
| 664 | ||
| 665 | parameters_data['names'].append(point['name'] + ' (' + point['units'] + ')') |
|
| 666 | parameters_data['timestamps'].append(point_timestamps) |
|
| 667 | parameters_data['values'].append(point_values) |
|
| 668 | ||
| 669 | ################################################################################################################ |
|
| 670 | # Step 10: construct the report |
|
| 671 | ################################################################################################################ |
|
| 672 | if cursor_system: |
|
| 673 | cursor_system.close() |
|
| 674 | if cnx_system: |
|
| 675 | cnx_system.close() |
|
| 676 | ||
| 677 | if cursor_energy: |
|
| 678 | cursor_energy.close() |
|
| 679 | if cnx_energy: |
|
| 680 | cnx_energy.close() |
|
| 681 | ||
| 682 | if cursor_energy_plan: |
|
| 683 | cursor_energy_plan.close() |
|
| 684 | if cnx_energy_plan: |
|
| 685 | cnx_energy_plan.close() |
|
| 686 | ||
| 687 | if cursor_historical: |
|
| 688 | cursor_historical.close() |
|
| 689 | if cnx_historical: |
|
| 690 | cnx_historical.close() |
|
| 691 | ||
| 692 | result = dict() |
|
| 693 | ||
| 694 | result['tenant'] = dict() |
|
| 695 | result['tenant']['name'] = tenant['name'] |
|
| 696 | result['tenant']['area'] = tenant['area'] |
|
| 697 | ||
| 698 | result['base_period'] = dict() |
|
| 699 | result['base_period']['names'] = list() |
|
| 700 | result['base_period']['units'] = list() |
|
| 701 | result['base_period']['timestamps'] = list() |
|
| 702 | result['base_period']['values_saving'] = list() |
|
| 703 | result['base_period']['subtotals_saving'] = list() |
|
| 704 | result['base_period']['subtotals_in_kgce_saving'] = list() |
|
| 705 | result['base_period']['subtotals_in_kgco2e_saving'] = list() |
|
| 706 | result['base_period']['total_in_kgce_saving'] = Decimal(0.0) |
|
| 707 | result['base_period']['total_in_kgco2e_saving'] = Decimal(0.0) |
|
| 708 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
| 709 | for energy_category_id in energy_category_set: |
|
| 710 | result['base_period']['names'].append(energy_category_dict[energy_category_id]['name']) |
|
| 711 | result['base_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) |
|
| 712 | result['base_period']['timestamps'].append(base[energy_category_id]['timestamps']) |
|
| 713 | result['base_period']['values_saving'].append(base[energy_category_id]['values_saving']) |
|
| 714 | result['base_period']['subtotals_saving'].append(base[energy_category_id]['subtotal_saving']) |
|
| 715 | result['base_period']['subtotals_in_kgce_saving'].append( |
|
| 716 | base[energy_category_id]['subtotal_in_kgce_saving']) |
|
| 717 | result['base_period']['subtotals_in_kgco2e_saving'].append( |
|
| 718 | base[energy_category_id]['subtotal_in_kgco2e_saving']) |
|
| 719 | result['base_period']['total_in_kgce_saving'] += base[energy_category_id]['subtotal_in_kgce_saving'] |
|
| 720 | result['base_period']['total_in_kgco2e_saving'] += base[energy_category_id]['subtotal_in_kgco2e_saving'] |
|
| 721 | ||
| 722 | result['reporting_period'] = dict() |
|
| 723 | result['reporting_period']['names'] = list() |
|
| 724 | result['reporting_period']['energy_category_ids'] = list() |
|
| 725 | result['reporting_period']['units'] = list() |
|
| 726 | result['reporting_period']['timestamps'] = list() |
|
| 727 | result['reporting_period']['values_saving'] = list() |
|
| 728 | result['reporting_period']['rates_saving'] = list() |
|
| 729 | result['reporting_period']['subtotals_saving'] = list() |
|
| 730 | result['reporting_period']['subtotals_in_kgce_saving'] = list() |
|
| 731 | result['reporting_period']['subtotals_in_kgco2e_saving'] = list() |
|
| 732 | result['reporting_period']['subtotals_per_unit_area_saving'] = list() |
|
| 733 | result['reporting_period']['increment_rates_saving'] = list() |
|
| 734 | result['reporting_period']['total_in_kgce_saving'] = Decimal(0.0) |
|
| 735 | result['reporting_period']['total_in_kgco2e_saving'] = Decimal(0.0) |
|
| 736 | result['reporting_period']['increment_rate_in_kgce_saving'] = Decimal(0.0) |
|
| 737 | result['reporting_period']['increment_rate_in_kgco2e_saving'] = Decimal(0.0) |
|
| 738 | ||
| 739 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
| 740 | for energy_category_id in energy_category_set: |
|
| 741 | result['reporting_period']['names'].append(energy_category_dict[energy_category_id]['name']) |
|
| 742 | result['reporting_period']['energy_category_ids'].append(energy_category_id) |
|
| 743 | result['reporting_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) |
|
| 744 | result['reporting_period']['timestamps'].append(reporting[energy_category_id]['timestamps']) |
|
| 745 | result['reporting_period']['values_saving'].append(reporting[energy_category_id]['values_saving']) |
|
| 746 | result['reporting_period']['subtotals_saving'].append(reporting[energy_category_id]['subtotal_saving']) |
|
| 747 | result['reporting_period']['subtotals_in_kgce_saving'].append( |
|
| 748 | reporting[energy_category_id]['subtotal_in_kgce_saving']) |
|
| 749 | result['reporting_period']['subtotals_in_kgco2e_saving'].append( |
|
| 750 | reporting[energy_category_id]['subtotal_in_kgco2e_saving']) |
|
| 751 | result['reporting_period']['subtotals_per_unit_area_saving'].append( |
|
| 752 | reporting[energy_category_id]['subtotal_saving'] / tenant['area'] |
|
| 753 | if tenant['area'] != Decimal(0.0) else None) |
|
| 754 | result['reporting_period']['increment_rates_saving'].append( |
|
| 755 | (reporting[energy_category_id]['subtotal_saving'] - base[energy_category_id]['subtotal_saving']) / |
|
| 756 | base[energy_category_id]['subtotal_saving'] |
|
| 757 | if base[energy_category_id]['subtotal_saving'] != Decimal(0.0) else None) |
|
| 758 | result['reporting_period']['total_in_kgce_saving'] += \ |
|
| 759 | reporting[energy_category_id]['subtotal_in_kgce_saving'] |
|
| 760 | result['reporting_period']['total_in_kgco2e_saving'] += \ |
|
| 761 | reporting[energy_category_id]['subtotal_in_kgco2e_saving'] |
|
| 762 | ||
| 763 | rate = list() |
|
| 764 | for index, value in enumerate(reporting[energy_category_id]['values_saving']): |
|
| 765 | if index < len(base[energy_category_id]['values_saving']) \ |
|
| 766 | and base[energy_category_id]['values_saving'][index] != Decimal(0.0) \ |
|
| 767 | and value != Decimal(0.0): |
|
| 768 | rate.append((value - base[energy_category_id]['values_saving'][index]) |
|
| 769 | / base[energy_category_id]['values_saving'][index]) |
|
| 770 | else: |
|
| 771 | rate.append(None) |
|
| 772 | result['reporting_period']['rates_saving'].append(rate) |
|
| 773 | ||
| 774 | result['reporting_period']['total_in_kgco2e_per_unit_area_saving'] = \ |
|
| 775 | result['reporting_period']['total_in_kgce_saving'] / tenant['area'] \ |
|
| 776 | if tenant['area'] != Decimal(0.0) else None |
|
| 777 | ||
| 778 | result['reporting_period']['increment_rate_in_kgce_saving'] = \ |
|
| 779 | (result['reporting_period']['total_in_kgce_saving'] - result['base_period']['total_in_kgce_saving']) / \ |
|
| 780 | result['base_period']['total_in_kgce_saving'] \ |
|
| 781 | if result['base_period']['total_in_kgce_saving'] != Decimal(0.0) else None |
|
| 782 | ||
| 783 | result['reporting_period']['total_in_kgce_per_unit_area_saving'] = \ |
|
| 784 | result['reporting_period']['total_in_kgco2e_saving'] / tenant['area'] if tenant['area'] > 0.0 else None |
|
| 785 | ||
| 786 | result['reporting_period']['increment_rate_in_kgco2e_saving'] = \ |
|
| 787 | (result['reporting_period']['total_in_kgco2e_saving'] - result['base_period']['total_in_kgco2e_saving']) / \ |
|
| 788 | result['base_period']['total_in_kgco2e_saving'] \ |
|
| 789 | if result['base_period']['total_in_kgco2e_saving'] != Decimal(0.0) else None |
|
| 790 | ||
| 791 | result['parameters'] = { |
|
| 792 | "names": parameters_data['names'], |
|
| 793 | "timestamps": parameters_data['timestamps'], |
|
| 794 | "values": parameters_data['values'] |
|
| 795 | } |
|
| 796 | ||
| 797 | # export result to Excel file and then encode the file to base64 string |
|
| 798 | if not is_quick_mode: |
|
| 799 | result['excel_bytes_base64'] = excelexporters.tenantplan.export(result, |
|
| 800 | tenant['name'], |
|
| 801 | base_period_start_datetime_local, |
|
| 802 | base_period_end_datetime_local, |
|
| 803 | reporting_period_start_datetime_local, |
|
| 804 | reporting_period_end_datetime_local, |
|
| 805 | period_type, |
|
| 806 | language) |
|
| 807 | ||
| 808 | resp.text = json.dumps(result) |
|
| 809 | ||
| @@ 45-808 (lines=764) @@ | ||
| 42 | from core.useractivity import access_control, api_key_control |
|
| 43 | ||
| 44 | ||
| 45 | class Reporting: |
|
| 46 | def __init__(self): |
|
| 47 | """ Initializes Reporting""" |
|
| 48 | pass |
|
| 49 | ||
| 50 | @staticmethod |
|
| 51 | def on_options(req, resp): |
|
| 52 | _ = req |
|
| 53 | resp.status = falcon.HTTP_200 |
|
| 54 | ||
| 55 | #################################################################################################################### |
|
| 56 | # PROCEDURES |
|
| 57 | # Step 1: valid parameters |
|
| 58 | # Step 2: query the store |
|
| 59 | # Step 3: query energy categories |
|
| 60 | # Step 4: query associated sensors |
|
| 61 | # Step 5: query associated points |
|
| 62 | # Step 6: query base period energy saving |
|
| 63 | # Step 7: query reporting period energy saving |
|
| 64 | # Step 8: query tariff data |
|
| 65 | # Step 9: query associated sensors and points data |
|
| 66 | # Step 10: construct the report |
|
| 67 | #################################################################################################################### |
|
| 68 | @staticmethod |
|
| 69 | def on_get(req, resp): |
|
| 70 | if 'API-KEY' not in req.headers or \ |
|
| 71 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 72 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 73 | access_control(req) |
|
| 74 | else: |
|
| 75 | api_key_control(req) |
|
| 76 | print(req.params) |
|
| 77 | store_id = req.params.get('storeid') |
|
| 78 | store_uuid = req.params.get('storeuuid') |
|
| 79 | period_type = req.params.get('periodtype') |
|
| 80 | base_period_start_datetime_local = req.params.get('baseperiodstartdatetime') |
|
| 81 | base_period_end_datetime_local = req.params.get('baseperiodenddatetime') |
|
| 82 | reporting_period_start_datetime_local = req.params.get('reportingperiodstartdatetime') |
|
| 83 | reporting_period_end_datetime_local = req.params.get('reportingperiodenddatetime') |
|
| 84 | language = req.params.get('language') |
|
| 85 | quick_mode = req.params.get('quickmode') |
|
| 86 | ||
| 87 | ################################################################################################################ |
|
| 88 | # Step 1: valid parameters |
|
| 89 | ################################################################################################################ |
|
| 90 | if store_id is None and store_uuid is None: |
|
| 91 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 92 | title='API.BAD_REQUEST', |
|
| 93 | description='API.INVALID_STORE_ID') |
|
| 94 | ||
| 95 | if store_id is not None: |
|
| 96 | store_id = str.strip(store_id) |
|
| 97 | if not store_id.isdigit() or int(store_id) <= 0: |
|
| 98 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 99 | title='API.BAD_REQUEST', |
|
| 100 | description='API.INVALID_STORE_ID') |
|
| 101 | ||
| 102 | if store_uuid is not None: |
|
| 103 | regex = re.compile(r'^[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) |
|
| 104 | match = regex.match(str.strip(store_uuid)) |
|
| 105 | if not bool(match): |
|
| 106 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 107 | title='API.BAD_REQUEST', |
|
| 108 | description='API.INVALID_STORE_UUID') |
|
| 109 | ||
| 110 | if period_type is None: |
|
| 111 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 112 | description='API.INVALID_PERIOD_TYPE') |
|
| 113 | else: |
|
| 114 | period_type = str.strip(period_type) |
|
| 115 | if period_type not in ['hourly', 'daily', 'weekly', 'monthly', 'yearly']: |
|
| 116 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 117 | description='API.INVALID_PERIOD_TYPE') |
|
| 118 | ||
| 119 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
|
| 120 | if config.utc_offset[0] == '-': |
|
| 121 | timezone_offset = -timezone_offset |
|
| 122 | ||
| 123 | base_start_datetime_utc = None |
|
| 124 | if base_period_start_datetime_local is not None and len(str.strip(base_period_start_datetime_local)) > 0: |
|
| 125 | base_period_start_datetime_local = str.strip(base_period_start_datetime_local) |
|
| 126 | try: |
|
| 127 | base_start_datetime_utc = datetime.strptime(base_period_start_datetime_local, '%Y-%m-%dT%H:%M:%S') |
|
| 128 | except ValueError: |
|
| 129 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 130 | description="API.INVALID_BASE_PERIOD_START_DATETIME") |
|
| 131 | base_start_datetime_utc = \ |
|
| 132 | base_start_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset) |
|
| 133 | # nomalize the start datetime |
|
| 134 | if config.minutes_to_count == 30 and base_start_datetime_utc.minute >= 30: |
|
| 135 | base_start_datetime_utc = base_start_datetime_utc.replace(minute=30, second=0, microsecond=0) |
|
| 136 | else: |
|
| 137 | base_start_datetime_utc = base_start_datetime_utc.replace(minute=0, second=0, microsecond=0) |
|
| 138 | ||
| 139 | base_end_datetime_utc = None |
|
| 140 | if base_period_end_datetime_local is not None and len(str.strip(base_period_end_datetime_local)) > 0: |
|
| 141 | base_period_end_datetime_local = str.strip(base_period_end_datetime_local) |
|
| 142 | try: |
|
| 143 | base_end_datetime_utc = datetime.strptime(base_period_end_datetime_local, '%Y-%m-%dT%H:%M:%S') |
|
| 144 | except ValueError: |
|
| 145 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 146 | description="API.INVALID_BASE_PERIOD_END_DATETIME") |
|
| 147 | base_end_datetime_utc = \ |
|
| 148 | base_end_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset) |
|
| 149 | ||
| 150 | if base_start_datetime_utc is not None and base_end_datetime_utc is not None and \ |
|
| 151 | base_start_datetime_utc >= base_end_datetime_utc: |
|
| 152 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 153 | description='API.INVALID_BASE_PERIOD_END_DATETIME') |
|
| 154 | ||
| 155 | if reporting_period_start_datetime_local is None: |
|
| 156 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 157 | description="API.INVALID_REPORTING_PERIOD_START_DATETIME") |
|
| 158 | else: |
|
| 159 | reporting_period_start_datetime_local = str.strip(reporting_period_start_datetime_local) |
|
| 160 | try: |
|
| 161 | reporting_start_datetime_utc = datetime.strptime(reporting_period_start_datetime_local, |
|
| 162 | '%Y-%m-%dT%H:%M:%S') |
|
| 163 | except ValueError: |
|
| 164 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 165 | description="API.INVALID_REPORTING_PERIOD_START_DATETIME") |
|
| 166 | reporting_start_datetime_utc = \ |
|
| 167 | reporting_start_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset) |
|
| 168 | # nomalize the start datetime |
|
| 169 | if config.minutes_to_count == 30 and reporting_start_datetime_utc.minute >= 30: |
|
| 170 | reporting_start_datetime_utc = reporting_start_datetime_utc.replace(minute=30, second=0, microsecond=0) |
|
| 171 | else: |
|
| 172 | reporting_start_datetime_utc = reporting_start_datetime_utc.replace(minute=0, second=0, microsecond=0) |
|
| 173 | ||
| 174 | if reporting_period_end_datetime_local is None: |
|
| 175 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 176 | description="API.INVALID_REPORTING_PERIOD_END_DATETIME") |
|
| 177 | else: |
|
| 178 | reporting_period_end_datetime_local = str.strip(reporting_period_end_datetime_local) |
|
| 179 | try: |
|
| 180 | reporting_end_datetime_utc = datetime.strptime(reporting_period_end_datetime_local, |
|
| 181 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
|
| 182 | timedelta(minutes=timezone_offset) |
|
| 183 | except ValueError: |
|
| 184 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 185 | description="API.INVALID_REPORTING_PERIOD_END_DATETIME") |
|
| 186 | ||
| 187 | if reporting_start_datetime_utc >= reporting_end_datetime_utc: |
|
| 188 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 189 | description='API.INVALID_REPORTING_PERIOD_END_DATETIME') |
|
| 190 | ||
| 191 | # if turn quick mode on, do not return parameters data and excel file |
|
| 192 | is_quick_mode = False |
|
| 193 | if quick_mode is not None and \ |
|
| 194 | len(str.strip(quick_mode)) > 0 and \ |
|
| 195 | str.lower(str.strip(quick_mode)) in ('true', 't', 'on', 'yes', 'y'): |
|
| 196 | is_quick_mode = True |
|
| 197 | ||
| 198 | trans = utilities.get_translation(language) |
|
| 199 | trans.install() |
|
| 200 | _ = trans.gettext |
|
| 201 | ||
| 202 | ################################################################################################################ |
|
| 203 | # Step 2: query the store |
|
| 204 | ################################################################################################################ |
|
| 205 | cnx_system = mysql.connector.connect(**config.myems_system_db) |
|
| 206 | cursor_system = cnx_system.cursor() |
|
| 207 | ||
| 208 | cnx_energy = mysql.connector.connect(**config.myems_energy_db) |
|
| 209 | cursor_energy = cnx_energy.cursor() |
|
| 210 | ||
| 211 | cnx_energy_baseline = mysql.connector.connect(**config.myems_energy_baseline_db) |
|
| 212 | cursor_energy_baseline = cnx_energy_baseline.cursor() |
|
| 213 | ||
| 214 | cnx_historical = mysql.connector.connect(**config.myems_historical_db) |
|
| 215 | cursor_historical = cnx_historical.cursor() |
|
| 216 | ||
| 217 | if store_id is not None: |
|
| 218 | cursor_system.execute(" SELECT id, name, area, cost_center_id " |
|
| 219 | " FROM tbl_stores " |
|
| 220 | " WHERE id = %s ", (store_id,)) |
|
| 221 | row_store = cursor_system.fetchone() |
|
| 222 | elif store_uuid is not None: |
|
| 223 | cursor_system.execute(" SELECT id, name, area, cost_center_id " |
|
| 224 | " FROM tbl_stores " |
|
| 225 | " WHERE uuid = %s ", (store_uuid,)) |
|
| 226 | row_store = cursor_system.fetchone() |
|
| 227 | ||
| 228 | if row_store is None: |
|
| 229 | if cursor_system: |
|
| 230 | cursor_system.close() |
|
| 231 | if cnx_system: |
|
| 232 | cnx_system.close() |
|
| 233 | ||
| 234 | if cursor_energy: |
|
| 235 | cursor_energy.close() |
|
| 236 | if cnx_energy: |
|
| 237 | cnx_energy.close() |
|
| 238 | ||
| 239 | if cursor_energy_baseline: |
|
| 240 | cursor_energy_baseline.close() |
|
| 241 | if cnx_energy_baseline: |
|
| 242 | cnx_energy_baseline.close() |
|
| 243 | ||
| 244 | if cursor_historical: |
|
| 245 | cursor_historical.close() |
|
| 246 | if cnx_historical: |
|
| 247 | cnx_historical.close() |
|
| 248 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', description='API.STORE_NOT_FOUND') |
|
| 249 | ||
| 250 | store = dict() |
|
| 251 | store['id'] = row_store[0] |
|
| 252 | store['name'] = row_store[1] |
|
| 253 | store['area'] = row_store[2] |
|
| 254 | store['cost_center_id'] = row_store[3] |
|
| 255 | ||
| 256 | ################################################################################################################ |
|
| 257 | # Step 3: query energy categories |
|
| 258 | ################################################################################################################ |
|
| 259 | energy_category_set = set() |
|
| 260 | # query energy categories in base period |
|
| 261 | cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
|
| 262 | " FROM tbl_store_input_category_hourly " |
|
| 263 | " WHERE store_id = %s " |
|
| 264 | " AND start_datetime_utc >= %s " |
|
| 265 | " AND start_datetime_utc < %s ", |
|
| 266 | (store['id'], base_start_datetime_utc, base_end_datetime_utc)) |
|
| 267 | rows_energy_categories = cursor_energy.fetchall() |
|
| 268 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
| 269 | for row_energy_category in rows_energy_categories: |
|
| 270 | energy_category_set.add(row_energy_category[0]) |
|
| 271 | ||
| 272 | # query energy categories in reporting period |
|
| 273 | cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
|
| 274 | " FROM tbl_store_input_category_hourly " |
|
| 275 | " WHERE store_id = %s " |
|
| 276 | " AND start_datetime_utc >= %s " |
|
| 277 | " AND start_datetime_utc < %s ", |
|
| 278 | (store['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) |
|
| 279 | rows_energy_categories = cursor_energy.fetchall() |
|
| 280 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
| 281 | for row_energy_category in rows_energy_categories: |
|
| 282 | energy_category_set.add(row_energy_category[0]) |
|
| 283 | ||
| 284 | # query all energy categories in base period and reporting period |
|
| 285 | cursor_system.execute(" SELECT id, name, unit_of_measure, kgce, kgco2e " |
|
| 286 | " FROM tbl_energy_categories " |
|
| 287 | " ORDER BY id ", ) |
|
| 288 | rows_energy_categories = cursor_system.fetchall() |
|
| 289 | if rows_energy_categories is None or len(rows_energy_categories) == 0: |
|
| 290 | if cursor_system: |
|
| 291 | cursor_system.close() |
|
| 292 | if cnx_system: |
|
| 293 | cnx_system.close() |
|
| 294 | ||
| 295 | if cursor_energy: |
|
| 296 | cursor_energy.close() |
|
| 297 | if cnx_energy: |
|
| 298 | cnx_energy.close() |
|
| 299 | ||
| 300 | if cursor_energy_baseline: |
|
| 301 | cursor_energy_baseline.close() |
|
| 302 | if cnx_energy_baseline: |
|
| 303 | cnx_energy_baseline.close() |
|
| 304 | ||
| 305 | if cursor_historical: |
|
| 306 | cursor_historical.close() |
|
| 307 | if cnx_historical: |
|
| 308 | cnx_historical.close() |
|
| 309 | raise falcon.HTTPError(status=falcon.HTTP_404, |
|
| 310 | title='API.NOT_FOUND', |
|
| 311 | description='API.ENERGY_CATEGORY_NOT_FOUND') |
|
| 312 | energy_category_dict = dict() |
|
| 313 | for row_energy_category in rows_energy_categories: |
|
| 314 | if row_energy_category[0] in energy_category_set: |
|
| 315 | energy_category_dict[row_energy_category[0]] = {"name": row_energy_category[1], |
|
| 316 | "unit_of_measure": row_energy_category[2], |
|
| 317 | "kgce": row_energy_category[3], |
|
| 318 | "kgco2e": row_energy_category[4]} |
|
| 319 | ||
| 320 | ################################################################################################################ |
|
| 321 | # Step 4: query associated sensors |
|
| 322 | ################################################################################################################ |
|
| 323 | point_list = list() |
|
| 324 | cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type " |
|
| 325 | " FROM tbl_stores st, tbl_sensors se, tbl_stores_sensors ss, " |
|
| 326 | " tbl_points p, tbl_sensors_points sp " |
|
| 327 | " WHERE st.id = %s AND st.id = ss.store_id AND ss.sensor_id = se.id " |
|
| 328 | " AND se.id = sp.sensor_id AND sp.point_id = p.id " |
|
| 329 | " ORDER BY p.id ", (store['id'],)) |
|
| 330 | rows_points = cursor_system.fetchall() |
|
| 331 | if rows_points is not None and len(rows_points) > 0: |
|
| 332 | for row in rows_points: |
|
| 333 | point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]}) |
|
| 334 | ||
| 335 | ################################################################################################################ |
|
| 336 | # Step 5: query associated points |
|
| 337 | ################################################################################################################ |
|
| 338 | cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type " |
|
| 339 | " FROM tbl_stores s, tbl_stores_points sp, tbl_points p " |
|
| 340 | " WHERE s.id = %s AND s.id = sp.store_id AND sp.point_id = p.id " |
|
| 341 | " ORDER BY p.id ", (store['id'],)) |
|
| 342 | rows_points = cursor_system.fetchall() |
|
| 343 | if rows_points is not None and len(rows_points) > 0: |
|
| 344 | for row in rows_points: |
|
| 345 | point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]}) |
|
| 346 | ||
| 347 | ################################################################################################################ |
|
| 348 | # Step 6: query base period energy saving |
|
| 349 | ################################################################################################################ |
|
| 350 | base = dict() |
|
| 351 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
| 352 | for energy_category_id in energy_category_set: |
|
| 353 | kgce = energy_category_dict[energy_category_id]['kgce'] |
|
| 354 | kgco2e = energy_category_dict[energy_category_id]['kgco2e'] |
|
| 355 | ||
| 356 | base[energy_category_id] = dict() |
|
| 357 | base[energy_category_id]['timestamps'] = list() |
|
| 358 | base[energy_category_id]['values_baseline'] = list() |
|
| 359 | base[energy_category_id]['values_actual'] = list() |
|
| 360 | base[energy_category_id]['values_saving'] = list() |
|
| 361 | base[energy_category_id]['subtotal_baseline'] = Decimal(0.0) |
|
| 362 | base[energy_category_id]['subtotal_actual'] = Decimal(0.0) |
|
| 363 | base[energy_category_id]['subtotal_saving'] = Decimal(0.0) |
|
| 364 | base[energy_category_id]['subtotal_in_kgce_baseline'] = Decimal(0.0) |
|
| 365 | base[energy_category_id]['subtotal_in_kgce_actual'] = Decimal(0.0) |
|
| 366 | base[energy_category_id]['subtotal_in_kgce_saving'] = Decimal(0.0) |
|
| 367 | base[energy_category_id]['subtotal_in_kgco2e_baseline'] = Decimal(0.0) |
|
| 368 | base[energy_category_id]['subtotal_in_kgco2e_actual'] = Decimal(0.0) |
|
| 369 | base[energy_category_id]['subtotal_in_kgco2e_saving'] = Decimal(0.0) |
|
| 370 | # query base period's energy baseline |
|
| 371 | cursor_energy_baseline.execute(" SELECT start_datetime_utc, actual_value " |
|
| 372 | " FROM tbl_store_input_category_hourly " |
|
| 373 | " WHERE store_id = %s " |
|
| 374 | " AND energy_category_id = %s " |
|
| 375 | " AND start_datetime_utc >= %s " |
|
| 376 | " AND start_datetime_utc < %s " |
|
| 377 | " ORDER BY start_datetime_utc ", |
|
| 378 | (store['id'], |
|
| 379 | energy_category_id, |
|
| 380 | base_start_datetime_utc, |
|
| 381 | base_end_datetime_utc)) |
|
| 382 | rows_store_hourly = cursor_energy_baseline.fetchall() |
|
| 383 | ||
| 384 | rows_store_periodically = utilities.aggregate_hourly_data_by_period(rows_store_hourly, |
|
| 385 | base_start_datetime_utc, |
|
| 386 | base_end_datetime_utc, |
|
| 387 | period_type) |
|
| 388 | for row_store_periodically in rows_store_periodically: |
|
| 389 | current_datetime_local = row_store_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
| 390 | timedelta(minutes=timezone_offset) |
|
| 391 | if period_type == 'hourly': |
|
| 392 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 393 | elif period_type == 'daily': |
|
| 394 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 395 | elif period_type == 'weekly': |
|
| 396 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 397 | elif period_type == 'monthly': |
|
| 398 | current_datetime = current_datetime_local.isoformat()[0:7] |
|
| 399 | elif period_type == 'yearly': |
|
| 400 | current_datetime = current_datetime_local.isoformat()[0:4] |
|
| 401 | ||
| 402 | baseline_value = Decimal(0.0) if row_store_periodically[1] is None else row_store_periodically[1] |
|
| 403 | base[energy_category_id]['timestamps'].append(current_datetime) |
|
| 404 | base[energy_category_id]['values_baseline'].append(baseline_value) |
|
| 405 | base[energy_category_id]['subtotal_baseline'] += baseline_value |
|
| 406 | base[energy_category_id]['subtotal_in_kgce_baseline'] += baseline_value * kgce |
|
| 407 | base[energy_category_id]['subtotal_in_kgco2e_baseline'] += baseline_value * kgco2e |
|
| 408 | ||
| 409 | # query base period's energy actual |
|
| 410 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
|
| 411 | " FROM tbl_store_input_category_hourly " |
|
| 412 | " WHERE store_id = %s " |
|
| 413 | " AND energy_category_id = %s " |
|
| 414 | " AND start_datetime_utc >= %s " |
|
| 415 | " AND start_datetime_utc < %s " |
|
| 416 | " ORDER BY start_datetime_utc ", |
|
| 417 | (store['id'], |
|
| 418 | energy_category_id, |
|
| 419 | base_start_datetime_utc, |
|
| 420 | base_end_datetime_utc)) |
|
| 421 | rows_store_hourly = cursor_energy.fetchall() |
|
| 422 | ||
| 423 | rows_store_periodically = utilities.aggregate_hourly_data_by_period(rows_store_hourly, |
|
| 424 | base_start_datetime_utc, |
|
| 425 | base_end_datetime_utc, |
|
| 426 | period_type) |
|
| 427 | for row_store_periodically in rows_store_periodically: |
|
| 428 | current_datetime_local = row_store_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
| 429 | timedelta(minutes=timezone_offset) |
|
| 430 | if period_type == 'hourly': |
|
| 431 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 432 | elif period_type == 'daily': |
|
| 433 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 434 | elif period_type == 'weekly': |
|
| 435 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 436 | elif period_type == 'monthly': |
|
| 437 | current_datetime = current_datetime_local.isoformat()[0:7] |
|
| 438 | elif period_type == 'yearly': |
|
| 439 | current_datetime = current_datetime_local.isoformat()[0:4] |
|
| 440 | ||
| 441 | actual_value = Decimal(0.0) if row_store_periodically[1] is None else row_store_periodically[1] |
|
| 442 | base[energy_category_id]['values_actual'].append(actual_value) |
|
| 443 | base[energy_category_id]['subtotal_actual'] += actual_value |
|
| 444 | base[energy_category_id]['subtotal_in_kgce_actual'] += actual_value * kgce |
|
| 445 | base[energy_category_id]['subtotal_in_kgco2e_actual'] += actual_value * kgco2e |
|
| 446 | ||
| 447 | # calculate base period's energy savings |
|
| 448 | for i in range(len(base[energy_category_id]['values_baseline'])): |
|
| 449 | base[energy_category_id]['values_saving'].append( |
|
| 450 | base[energy_category_id]['values_baseline'][i] - |
|
| 451 | base[energy_category_id]['values_actual'][i]) |
|
| 452 | ||
| 453 | base[energy_category_id]['subtotal_saving'] = \ |
|
| 454 | base[energy_category_id]['subtotal_baseline'] - \ |
|
| 455 | base[energy_category_id]['subtotal_actual'] |
|
| 456 | base[energy_category_id]['subtotal_in_kgce_saving'] = \ |
|
| 457 | base[energy_category_id]['subtotal_in_kgce_baseline'] - \ |
|
| 458 | base[energy_category_id]['subtotal_in_kgce_actual'] |
|
| 459 | base[energy_category_id]['subtotal_in_kgco2e_saving'] = \ |
|
| 460 | base[energy_category_id]['subtotal_in_kgco2e_baseline'] - \ |
|
| 461 | base[energy_category_id]['subtotal_in_kgco2e_actual'] |
|
| 462 | ################################################################################################################ |
|
| 463 | # Step 7: query reporting period energy saving |
|
| 464 | ################################################################################################################ |
|
| 465 | reporting = dict() |
|
| 466 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
| 467 | for energy_category_id in energy_category_set: |
|
| 468 | kgce = energy_category_dict[energy_category_id]['kgce'] |
|
| 469 | kgco2e = energy_category_dict[energy_category_id]['kgco2e'] |
|
| 470 | ||
| 471 | reporting[energy_category_id] = dict() |
|
| 472 | reporting[energy_category_id]['timestamps'] = list() |
|
| 473 | reporting[energy_category_id]['values_baseline'] = list() |
|
| 474 | reporting[energy_category_id]['values_actual'] = list() |
|
| 475 | reporting[energy_category_id]['values_saving'] = list() |
|
| 476 | reporting[energy_category_id]['subtotal_baseline'] = Decimal(0.0) |
|
| 477 | reporting[energy_category_id]['subtotal_actual'] = Decimal(0.0) |
|
| 478 | reporting[energy_category_id]['subtotal_saving'] = Decimal(0.0) |
|
| 479 | reporting[energy_category_id]['subtotal_in_kgce_baseline'] = Decimal(0.0) |
|
| 480 | reporting[energy_category_id]['subtotal_in_kgce_actual'] = Decimal(0.0) |
|
| 481 | reporting[energy_category_id]['subtotal_in_kgce_saving'] = Decimal(0.0) |
|
| 482 | reporting[energy_category_id]['subtotal_in_kgco2e_baseline'] = Decimal(0.0) |
|
| 483 | reporting[energy_category_id]['subtotal_in_kgco2e_actual'] = Decimal(0.0) |
|
| 484 | reporting[energy_category_id]['subtotal_in_kgco2e_saving'] = Decimal(0.0) |
|
| 485 | # query reporting period's energy baseline |
|
| 486 | cursor_energy_baseline.execute(" SELECT start_datetime_utc, actual_value " |
|
| 487 | " FROM tbl_store_input_category_hourly " |
|
| 488 | " WHERE store_id = %s " |
|
| 489 | " AND energy_category_id = %s " |
|
| 490 | " AND start_datetime_utc >= %s " |
|
| 491 | " AND start_datetime_utc < %s " |
|
| 492 | " ORDER BY start_datetime_utc ", |
|
| 493 | (store['id'], |
|
| 494 | energy_category_id, |
|
| 495 | reporting_start_datetime_utc, |
|
| 496 | reporting_end_datetime_utc)) |
|
| 497 | rows_store_hourly = cursor_energy_baseline.fetchall() |
|
| 498 | ||
| 499 | rows_store_periodically = utilities.aggregate_hourly_data_by_period(rows_store_hourly, |
|
| 500 | reporting_start_datetime_utc, |
|
| 501 | reporting_end_datetime_utc, |
|
| 502 | period_type) |
|
| 503 | for row_store_periodically in rows_store_periodically: |
|
| 504 | current_datetime_local = row_store_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
| 505 | timedelta(minutes=timezone_offset) |
|
| 506 | if period_type == 'hourly': |
|
| 507 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 508 | elif period_type == 'daily': |
|
| 509 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 510 | elif period_type == 'weekly': |
|
| 511 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 512 | elif period_type == 'monthly': |
|
| 513 | current_datetime = current_datetime_local.isoformat()[0:7] |
|
| 514 | elif period_type == 'yearly': |
|
| 515 | current_datetime = current_datetime_local.isoformat()[0:4] |
|
| 516 | ||
| 517 | baseline_value = Decimal(0.0) if row_store_periodically[1] is None else row_store_periodically[1] |
|
| 518 | reporting[energy_category_id]['timestamps'].append(current_datetime) |
|
| 519 | reporting[energy_category_id]['values_baseline'].append(baseline_value) |
|
| 520 | reporting[energy_category_id]['subtotal_baseline'] += baseline_value |
|
| 521 | reporting[energy_category_id]['subtotal_in_kgce_baseline'] += baseline_value * kgce |
|
| 522 | reporting[energy_category_id]['subtotal_in_kgco2e_baseline'] += baseline_value * kgco2e |
|
| 523 | ||
| 524 | # query reporting period's energy actual |
|
| 525 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
|
| 526 | " FROM tbl_store_input_category_hourly " |
|
| 527 | " WHERE store_id = %s " |
|
| 528 | " AND energy_category_id = %s " |
|
| 529 | " AND start_datetime_utc >= %s " |
|
| 530 | " AND start_datetime_utc < %s " |
|
| 531 | " ORDER BY start_datetime_utc ", |
|
| 532 | (store['id'], |
|
| 533 | energy_category_id, |
|
| 534 | reporting_start_datetime_utc, |
|
| 535 | reporting_end_datetime_utc)) |
|
| 536 | rows_store_hourly = cursor_energy.fetchall() |
|
| 537 | ||
| 538 | rows_store_periodically = utilities.aggregate_hourly_data_by_period(rows_store_hourly, |
|
| 539 | reporting_start_datetime_utc, |
|
| 540 | reporting_end_datetime_utc, |
|
| 541 | period_type) |
|
| 542 | for row_store_periodically in rows_store_periodically: |
|
| 543 | current_datetime_local = row_store_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
| 544 | timedelta(minutes=timezone_offset) |
|
| 545 | if period_type == 'hourly': |
|
| 546 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 547 | elif period_type == 'daily': |
|
| 548 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 549 | elif period_type == 'weekly': |
|
| 550 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 551 | elif period_type == 'monthly': |
|
| 552 | current_datetime = current_datetime_local.isoformat()[0:7] |
|
| 553 | elif period_type == 'yearly': |
|
| 554 | current_datetime = current_datetime_local.isoformat()[0:4] |
|
| 555 | ||
| 556 | actual_value = Decimal(0.0) if row_store_periodically[1] is None else row_store_periodically[1] |
|
| 557 | reporting[energy_category_id]['values_actual'].append(actual_value) |
|
| 558 | reporting[energy_category_id]['subtotal_actual'] += actual_value |
|
| 559 | reporting[energy_category_id]['subtotal_in_kgce_actual'] += actual_value * kgce |
|
| 560 | reporting[energy_category_id]['subtotal_in_kgco2e_actual'] += actual_value * kgco2e |
|
| 561 | ||
| 562 | # calculate reporting period's energy savings |
|
| 563 | for i in range(len(reporting[energy_category_id]['values_baseline'])): |
|
| 564 | reporting[energy_category_id]['values_saving'].append( |
|
| 565 | reporting[energy_category_id]['values_baseline'][i] - |
|
| 566 | reporting[energy_category_id]['values_actual'][i]) |
|
| 567 | ||
| 568 | reporting[energy_category_id]['subtotal_saving'] = \ |
|
| 569 | reporting[energy_category_id]['subtotal_baseline'] - \ |
|
| 570 | reporting[energy_category_id]['subtotal_actual'] |
|
| 571 | reporting[energy_category_id]['subtotal_in_kgce_saving'] = \ |
|
| 572 | reporting[energy_category_id]['subtotal_in_kgce_baseline'] - \ |
|
| 573 | reporting[energy_category_id]['subtotal_in_kgce_actual'] |
|
| 574 | reporting[energy_category_id]['subtotal_in_kgco2e_saving'] = \ |
|
| 575 | reporting[energy_category_id]['subtotal_in_kgco2e_baseline'] - \ |
|
| 576 | reporting[energy_category_id]['subtotal_in_kgco2e_actual'] |
|
| 577 | ################################################################################################################ |
|
| 578 | # Step 8: query tariff data |
|
| 579 | ################################################################################################################ |
|
| 580 | parameters_data = dict() |
|
| 581 | parameters_data['names'] = list() |
|
| 582 | parameters_data['timestamps'] = list() |
|
| 583 | parameters_data['values'] = list() |
|
| 584 | if config.is_tariff_appended and energy_category_set is not None and len(energy_category_set) > 0 \ |
|
| 585 | and not is_quick_mode: |
|
| 586 | for energy_category_id in energy_category_set: |
|
| 587 | energy_category_tariff_dict = utilities.get_energy_category_tariffs(store['cost_center_id'], |
|
| 588 | energy_category_id, |
|
| 589 | reporting_start_datetime_utc, |
|
| 590 | reporting_end_datetime_utc) |
|
| 591 | tariff_timestamp_list = list() |
|
| 592 | tariff_value_list = list() |
|
| 593 | for k, v in energy_category_tariff_dict.items(): |
|
| 594 | # convert k from utc to local |
|
| 595 | k = k + timedelta(minutes=timezone_offset) |
|
| 596 | tariff_timestamp_list.append(k.isoformat()[0:19]) |
|
| 597 | tariff_value_list.append(v) |
|
| 598 | ||
| 599 | parameters_data['names'].append(_('Tariff') + '-' + energy_category_dict[energy_category_id]['name']) |
|
| 600 | parameters_data['timestamps'].append(tariff_timestamp_list) |
|
| 601 | parameters_data['values'].append(tariff_value_list) |
|
| 602 | ||
| 603 | ################################################################################################################ |
|
| 604 | # Step 9: query associated sensors and points data |
|
| 605 | ################################################################################################################ |
|
| 606 | if not is_quick_mode: |
|
| 607 | for point in point_list: |
|
| 608 | point_values = [] |
|
| 609 | point_timestamps = [] |
|
| 610 | if point['object_type'] == 'ENERGY_VALUE': |
|
| 611 | query = (" SELECT utc_date_time, actual_value " |
|
| 612 | " FROM tbl_energy_value " |
|
| 613 | " WHERE point_id = %s " |
|
| 614 | " AND utc_date_time BETWEEN %s AND %s " |
|
| 615 | " ORDER BY utc_date_time ") |
|
| 616 | cursor_historical.execute(query, (point['id'], |
|
| 617 | reporting_start_datetime_utc, |
|
| 618 | reporting_end_datetime_utc)) |
|
| 619 | rows = cursor_historical.fetchall() |
|
| 620 | ||
| 621 | if rows is not None and len(rows) > 0: |
|
| 622 | for row in rows: |
|
| 623 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
| 624 | timedelta(minutes=timezone_offset) |
|
| 625 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 626 | point_timestamps.append(current_datetime) |
|
| 627 | point_values.append(row[1]) |
|
| 628 | elif point['object_type'] == 'ANALOG_VALUE': |
|
| 629 | query = (" SELECT utc_date_time, actual_value " |
|
| 630 | " FROM tbl_analog_value " |
|
| 631 | " WHERE point_id = %s " |
|
| 632 | " AND utc_date_time BETWEEN %s AND %s " |
|
| 633 | " ORDER BY utc_date_time ") |
|
| 634 | cursor_historical.execute(query, (point['id'], |
|
| 635 | reporting_start_datetime_utc, |
|
| 636 | reporting_end_datetime_utc)) |
|
| 637 | rows = cursor_historical.fetchall() |
|
| 638 | ||
| 639 | if rows is not None and len(rows) > 0: |
|
| 640 | for row in rows: |
|
| 641 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
| 642 | timedelta(minutes=timezone_offset) |
|
| 643 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 644 | point_timestamps.append(current_datetime) |
|
| 645 | point_values.append(row[1]) |
|
| 646 | elif point['object_type'] == 'DIGITAL_VALUE': |
|
| 647 | query = (" SELECT utc_date_time, actual_value " |
|
| 648 | " FROM tbl_digital_value " |
|
| 649 | " WHERE point_id = %s " |
|
| 650 | " AND utc_date_time BETWEEN %s AND %s " |
|
| 651 | " ORDER BY utc_date_time ") |
|
| 652 | cursor_historical.execute(query, (point['id'], |
|
| 653 | reporting_start_datetime_utc, |
|
| 654 | reporting_end_datetime_utc)) |
|
| 655 | rows = cursor_historical.fetchall() |
|
| 656 | ||
| 657 | if rows is not None and len(rows) > 0: |
|
| 658 | for row in rows: |
|
| 659 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
| 660 | timedelta(minutes=timezone_offset) |
|
| 661 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 662 | point_timestamps.append(current_datetime) |
|
| 663 | point_values.append(row[1]) |
|
| 664 | ||
| 665 | parameters_data['names'].append(point['name'] + ' (' + point['units'] + ')') |
|
| 666 | parameters_data['timestamps'].append(point_timestamps) |
|
| 667 | parameters_data['values'].append(point_values) |
|
| 668 | ||
| 669 | ################################################################################################################ |
|
| 670 | # Step 10: construct the report |
|
| 671 | ################################################################################################################ |
|
| 672 | if cursor_system: |
|
| 673 | cursor_system.close() |
|
| 674 | if cnx_system: |
|
| 675 | cnx_system.close() |
|
| 676 | ||
| 677 | if cursor_energy: |
|
| 678 | cursor_energy.close() |
|
| 679 | if cnx_energy: |
|
| 680 | cnx_energy.close() |
|
| 681 | ||
| 682 | if cursor_energy_baseline: |
|
| 683 | cursor_energy_baseline.close() |
|
| 684 | if cnx_energy_baseline: |
|
| 685 | cnx_energy_baseline.close() |
|
| 686 | ||
| 687 | if cursor_historical: |
|
| 688 | cursor_historical.close() |
|
| 689 | if cnx_historical: |
|
| 690 | cnx_historical.close() |
|
| 691 | ||
| 692 | result = dict() |
|
| 693 | ||
| 694 | result['store'] = dict() |
|
| 695 | result['store']['name'] = store['name'] |
|
| 696 | result['store']['area'] = store['area'] |
|
| 697 | ||
| 698 | result['base_period'] = dict() |
|
| 699 | result['base_period']['names'] = list() |
|
| 700 | result['base_period']['units'] = list() |
|
| 701 | result['base_period']['timestamps'] = list() |
|
| 702 | result['base_period']['values_saving'] = list() |
|
| 703 | result['base_period']['subtotals_saving'] = list() |
|
| 704 | result['base_period']['subtotals_in_kgce_saving'] = list() |
|
| 705 | result['base_period']['subtotals_in_kgco2e_saving'] = list() |
|
| 706 | result['base_period']['total_in_kgce_saving'] = Decimal(0.0) |
|
| 707 | result['base_period']['total_in_kgco2e_saving'] = Decimal(0.0) |
|
| 708 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
| 709 | for energy_category_id in energy_category_set: |
|
| 710 | result['base_period']['names'].append(energy_category_dict[energy_category_id]['name']) |
|
| 711 | result['base_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) |
|
| 712 | result['base_period']['timestamps'].append(base[energy_category_id]['timestamps']) |
|
| 713 | result['base_period']['values_saving'].append(base[energy_category_id]['values_saving']) |
|
| 714 | result['base_period']['subtotals_saving'].append(base[energy_category_id]['subtotal_saving']) |
|
| 715 | result['base_period']['subtotals_in_kgce_saving'].append( |
|
| 716 | base[energy_category_id]['subtotal_in_kgce_saving']) |
|
| 717 | result['base_period']['subtotals_in_kgco2e_saving'].append( |
|
| 718 | base[energy_category_id]['subtotal_in_kgco2e_saving']) |
|
| 719 | result['base_period']['total_in_kgce_saving'] += base[energy_category_id]['subtotal_in_kgce_saving'] |
|
| 720 | result['base_period']['total_in_kgco2e_saving'] += base[energy_category_id]['subtotal_in_kgco2e_saving'] |
|
| 721 | ||
| 722 | result['reporting_period'] = dict() |
|
| 723 | result['reporting_period']['names'] = list() |
|
| 724 | result['reporting_period']['energy_category_ids'] = list() |
|
| 725 | result['reporting_period']['units'] = list() |
|
| 726 | result['reporting_period']['timestamps'] = list() |
|
| 727 | result['reporting_period']['values_saving'] = list() |
|
| 728 | result['reporting_period']['rates_saving'] = list() |
|
| 729 | result['reporting_period']['subtotals_saving'] = list() |
|
| 730 | result['reporting_period']['subtotals_in_kgce_saving'] = list() |
|
| 731 | result['reporting_period']['subtotals_in_kgco2e_saving'] = list() |
|
| 732 | result['reporting_period']['subtotals_per_unit_area_saving'] = list() |
|
| 733 | result['reporting_period']['increment_rates_saving'] = list() |
|
| 734 | result['reporting_period']['total_in_kgce_saving'] = Decimal(0.0) |
|
| 735 | result['reporting_period']['total_in_kgco2e_saving'] = Decimal(0.0) |
|
| 736 | result['reporting_period']['increment_rate_in_kgce_saving'] = Decimal(0.0) |
|
| 737 | result['reporting_period']['increment_rate_in_kgco2e_saving'] = Decimal(0.0) |
|
| 738 | ||
| 739 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
| 740 | for energy_category_id in energy_category_set: |
|
| 741 | result['reporting_period']['names'].append(energy_category_dict[energy_category_id]['name']) |
|
| 742 | result['reporting_period']['energy_category_ids'].append(energy_category_id) |
|
| 743 | result['reporting_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) |
|
| 744 | result['reporting_period']['timestamps'].append(reporting[energy_category_id]['timestamps']) |
|
| 745 | result['reporting_period']['values_saving'].append(reporting[energy_category_id]['values_saving']) |
|
| 746 | result['reporting_period']['subtotals_saving'].append(reporting[energy_category_id]['subtotal_saving']) |
|
| 747 | result['reporting_period']['subtotals_in_kgce_saving'].append( |
|
| 748 | reporting[energy_category_id]['subtotal_in_kgce_saving']) |
|
| 749 | result['reporting_period']['subtotals_in_kgco2e_saving'].append( |
|
| 750 | reporting[energy_category_id]['subtotal_in_kgco2e_saving']) |
|
| 751 | result['reporting_period']['subtotals_per_unit_area_saving'].append( |
|
| 752 | reporting[energy_category_id]['subtotal_saving'] / store['area'] |
|
| 753 | if store['area'] > Decimal(0.0) else None) |
|
| 754 | result['reporting_period']['increment_rates_saving'].append( |
|
| 755 | (reporting[energy_category_id]['subtotal_saving'] - base[energy_category_id]['subtotal_saving']) / |
|
| 756 | base[energy_category_id]['subtotal_saving'] |
|
| 757 | if base[energy_category_id]['subtotal_saving'] != Decimal(0.0) else None) |
|
| 758 | result['reporting_period']['total_in_kgce_saving'] += \ |
|
| 759 | reporting[energy_category_id]['subtotal_in_kgce_saving'] |
|
| 760 | result['reporting_period']['total_in_kgco2e_saving'] += \ |
|
| 761 | reporting[energy_category_id]['subtotal_in_kgco2e_saving'] |
|
| 762 | ||
| 763 | rate = list() |
|
| 764 | for index, value in enumerate(reporting[energy_category_id]['values_saving']): |
|
| 765 | if index < len(base[energy_category_id]['values_saving']) \ |
|
| 766 | and base[energy_category_id]['values_saving'][index] != 0 and value != 0: |
|
| 767 | rate.append((value - base[energy_category_id]['values_saving'][index]) |
|
| 768 | / base[energy_category_id]['values_saving'][index]) |
|
| 769 | else: |
|
| 770 | rate.append(None) |
|
| 771 | result['reporting_period']['rates_saving'].append(rate) |
|
| 772 | ||
| 773 | result['reporting_period']['total_in_kgco2e_per_unit_area_saving'] = \ |
|
| 774 | result['reporting_period']['total_in_kgce_saving'] / store['area'] \ |
|
| 775 | if store['area'] > Decimal(0.0) else None |
|
| 776 | ||
| 777 | result['reporting_period']['increment_rate_in_kgce_saving'] = \ |
|
| 778 | (result['reporting_period']['total_in_kgce_saving'] - result['base_period']['total_in_kgce_saving']) / \ |
|
| 779 | result['base_period']['total_in_kgce_saving'] \ |
|
| 780 | if result['base_period']['total_in_kgce_saving'] != Decimal(0.0) else None |
|
| 781 | ||
| 782 | result['reporting_period']['total_in_kgce_per_unit_area_saving'] = \ |
|
| 783 | result['reporting_period']['total_in_kgco2e_saving'] / store['area'] \ |
|
| 784 | if store['area'] > Decimal(0.0) else None |
|
| 785 | ||
| 786 | result['reporting_period']['increment_rate_in_kgco2e_saving'] = \ |
|
| 787 | (result['reporting_period']['total_in_kgco2e_saving'] - result['base_period']['total_in_kgco2e_saving']) / \ |
|
| 788 | result['base_period']['total_in_kgco2e_saving'] \ |
|
| 789 | if result['base_period']['total_in_kgco2e_saving'] != Decimal(0.0) else None |
|
| 790 | ||
| 791 | result['parameters'] = { |
|
| 792 | "names": parameters_data['names'], |
|
| 793 | "timestamps": parameters_data['timestamps'], |
|
| 794 | "values": parameters_data['values'] |
|
| 795 | } |
|
| 796 | ||
| 797 | # export result to Excel file and then encode the file to base64 string |
|
| 798 | if not is_quick_mode: |
|
| 799 | result['excel_bytes_base64'] = excelexporters.storesaving.export(result, |
|
| 800 | store['name'], |
|
| 801 | base_period_start_datetime_local, |
|
| 802 | base_period_end_datetime_local, |
|
| 803 | reporting_period_start_datetime_local, |
|
| 804 | reporting_period_end_datetime_local, |
|
| 805 | period_type, |
|
| 806 | language) |
|
| 807 | ||
| 808 | resp.text = json.dumps(result) |
|
| 809 | ||
| @@ 45-808 (lines=764) @@ | ||
| 42 | from core.useractivity import access_control, api_key_control |
|
| 43 | ||
| 44 | ||
| 45 | class Reporting: |
|
| 46 | def __init__(self): |
|
| 47 | """ Initializes Reporting""" |
|
| 48 | pass |
|
| 49 | ||
| 50 | @staticmethod |
|
| 51 | def on_options(req, resp): |
|
| 52 | _ = req |
|
| 53 | resp.status = falcon.HTTP_200 |
|
| 54 | ||
| 55 | #################################################################################################################### |
|
| 56 | # PROCEDURES |
|
| 57 | # Step 1: valid parameters |
|
| 58 | # Step 2: query the store |
|
| 59 | # Step 3: query energy categories |
|
| 60 | # Step 4: query associated sensors |
|
| 61 | # Step 5: query associated points |
|
| 62 | # Step 6: query base period energy saving |
|
| 63 | # Step 7: query reporting period energy saving |
|
| 64 | # Step 8: query tariff data |
|
| 65 | # Step 9: query associated sensors and points data |
|
| 66 | # Step 10: construct the report |
|
| 67 | #################################################################################################################### |
|
| 68 | @staticmethod |
|
| 69 | def on_get(req, resp): |
|
| 70 | if 'API-KEY' not in req.headers or \ |
|
| 71 | not isinstance(req.headers['API-KEY'], str) or \ |
|
| 72 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
| 73 | access_control(req) |
|
| 74 | else: |
|
| 75 | api_key_control(req) |
|
| 76 | print(req.params) |
|
| 77 | store_id = req.params.get('storeid') |
|
| 78 | store_uuid = req.params.get('storeuuid') |
|
| 79 | period_type = req.params.get('periodtype') |
|
| 80 | base_period_start_datetime_local = req.params.get('baseperiodstartdatetime') |
|
| 81 | base_period_end_datetime_local = req.params.get('baseperiodenddatetime') |
|
| 82 | reporting_period_start_datetime_local = req.params.get('reportingperiodstartdatetime') |
|
| 83 | reporting_period_end_datetime_local = req.params.get('reportingperiodenddatetime') |
|
| 84 | language = req.params.get('language') |
|
| 85 | quick_mode = req.params.get('quickmode') |
|
| 86 | ||
| 87 | ################################################################################################################ |
|
| 88 | # Step 1: valid parameters |
|
| 89 | ################################################################################################################ |
|
| 90 | if store_id is None and store_uuid is None: |
|
| 91 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 92 | title='API.BAD_REQUEST', |
|
| 93 | description='API.INVALID_STORE_ID') |
|
| 94 | ||
| 95 | if store_id is not None: |
|
| 96 | store_id = str.strip(store_id) |
|
| 97 | if not store_id.isdigit() or int(store_id) <= 0: |
|
| 98 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 99 | title='API.BAD_REQUEST', |
|
| 100 | description='API.INVALID_STORE_ID') |
|
| 101 | ||
| 102 | if store_uuid is not None: |
|
| 103 | regex = re.compile(r'^[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) |
|
| 104 | match = regex.match(str.strip(store_uuid)) |
|
| 105 | if not bool(match): |
|
| 106 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 107 | title='API.BAD_REQUEST', |
|
| 108 | description='API.INVALID_STORE_UUID') |
|
| 109 | ||
| 110 | if period_type is None: |
|
| 111 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 112 | description='API.INVALID_PERIOD_TYPE') |
|
| 113 | else: |
|
| 114 | period_type = str.strip(period_type) |
|
| 115 | if period_type not in ['hourly', 'daily', 'weekly', 'monthly', 'yearly']: |
|
| 116 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 117 | description='API.INVALID_PERIOD_TYPE') |
|
| 118 | ||
| 119 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
|
| 120 | if config.utc_offset[0] == '-': |
|
| 121 | timezone_offset = -timezone_offset |
|
| 122 | ||
| 123 | base_start_datetime_utc = None |
|
| 124 | if base_period_start_datetime_local is not None and len(str.strip(base_period_start_datetime_local)) > 0: |
|
| 125 | base_period_start_datetime_local = str.strip(base_period_start_datetime_local) |
|
| 126 | try: |
|
| 127 | base_start_datetime_utc = datetime.strptime(base_period_start_datetime_local, '%Y-%m-%dT%H:%M:%S') |
|
| 128 | except ValueError: |
|
| 129 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 130 | description="API.INVALID_BASE_PERIOD_START_DATETIME") |
|
| 131 | base_start_datetime_utc = \ |
|
| 132 | base_start_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset) |
|
| 133 | # nomalize the start datetime |
|
| 134 | if config.minutes_to_count == 30 and base_start_datetime_utc.minute >= 30: |
|
| 135 | base_start_datetime_utc = base_start_datetime_utc.replace(minute=30, second=0, microsecond=0) |
|
| 136 | else: |
|
| 137 | base_start_datetime_utc = base_start_datetime_utc.replace(minute=0, second=0, microsecond=0) |
|
| 138 | ||
| 139 | base_end_datetime_utc = None |
|
| 140 | if base_period_end_datetime_local is not None and len(str.strip(base_period_end_datetime_local)) > 0: |
|
| 141 | base_period_end_datetime_local = str.strip(base_period_end_datetime_local) |
|
| 142 | try: |
|
| 143 | base_end_datetime_utc = datetime.strptime(base_period_end_datetime_local, '%Y-%m-%dT%H:%M:%S') |
|
| 144 | except ValueError: |
|
| 145 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 146 | description="API.INVALID_BASE_PERIOD_END_DATETIME") |
|
| 147 | base_end_datetime_utc = \ |
|
| 148 | base_end_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset) |
|
| 149 | ||
| 150 | if base_start_datetime_utc is not None and base_end_datetime_utc is not None and \ |
|
| 151 | base_start_datetime_utc >= base_end_datetime_utc: |
|
| 152 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 153 | description='API.INVALID_BASE_PERIOD_END_DATETIME') |
|
| 154 | ||
| 155 | if reporting_period_start_datetime_local is None: |
|
| 156 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 157 | description="API.INVALID_REPORTING_PERIOD_START_DATETIME") |
|
| 158 | else: |
|
| 159 | reporting_period_start_datetime_local = str.strip(reporting_period_start_datetime_local) |
|
| 160 | try: |
|
| 161 | reporting_start_datetime_utc = datetime.strptime(reporting_period_start_datetime_local, |
|
| 162 | '%Y-%m-%dT%H:%M:%S') |
|
| 163 | except ValueError: |
|
| 164 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 165 | description="API.INVALID_REPORTING_PERIOD_START_DATETIME") |
|
| 166 | reporting_start_datetime_utc = \ |
|
| 167 | reporting_start_datetime_utc.replace(tzinfo=timezone.utc) - timedelta(minutes=timezone_offset) |
|
| 168 | # nomalize the start datetime |
|
| 169 | if config.minutes_to_count == 30 and reporting_start_datetime_utc.minute >= 30: |
|
| 170 | reporting_start_datetime_utc = reporting_start_datetime_utc.replace(minute=30, second=0, microsecond=0) |
|
| 171 | else: |
|
| 172 | reporting_start_datetime_utc = reporting_start_datetime_utc.replace(minute=0, second=0, microsecond=0) |
|
| 173 | ||
| 174 | if reporting_period_end_datetime_local is None: |
|
| 175 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 176 | description="API.INVALID_REPORTING_PERIOD_END_DATETIME") |
|
| 177 | else: |
|
| 178 | reporting_period_end_datetime_local = str.strip(reporting_period_end_datetime_local) |
|
| 179 | try: |
|
| 180 | reporting_end_datetime_utc = datetime.strptime(reporting_period_end_datetime_local, |
|
| 181 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
|
| 182 | timedelta(minutes=timezone_offset) |
|
| 183 | except ValueError: |
|
| 184 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 185 | description="API.INVALID_REPORTING_PERIOD_END_DATETIME") |
|
| 186 | ||
| 187 | if reporting_start_datetime_utc >= reporting_end_datetime_utc: |
|
| 188 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 189 | description='API.INVALID_REPORTING_PERIOD_END_DATETIME') |
|
| 190 | ||
| 191 | # if turn quick mode on, do not return parameters data and excel file |
|
| 192 | is_quick_mode = False |
|
| 193 | if quick_mode is not None and \ |
|
| 194 | len(str.strip(quick_mode)) > 0 and \ |
|
| 195 | str.lower(str.strip(quick_mode)) in ('true', 't', 'on', 'yes', 'y'): |
|
| 196 | is_quick_mode = True |
|
| 197 | ||
| 198 | trans = utilities.get_translation(language) |
|
| 199 | trans.install() |
|
| 200 | _ = trans.gettext |
|
| 201 | ||
| 202 | ################################################################################################################ |
|
| 203 | # Step 2: query the store |
|
| 204 | ################################################################################################################ |
|
| 205 | cnx_system = mysql.connector.connect(**config.myems_system_db) |
|
| 206 | cursor_system = cnx_system.cursor() |
|
| 207 | ||
| 208 | cnx_energy = mysql.connector.connect(**config.myems_energy_db) |
|
| 209 | cursor_energy = cnx_energy.cursor() |
|
| 210 | ||
| 211 | cnx_energy_plan = mysql.connector.connect(**config.myems_energy_plan_db) |
|
| 212 | cursor_energy_plan = cnx_energy_plan.cursor() |
|
| 213 | ||
| 214 | cnx_historical = mysql.connector.connect(**config.myems_historical_db) |
|
| 215 | cursor_historical = cnx_historical.cursor() |
|
| 216 | ||
| 217 | if store_id is not None: |
|
| 218 | cursor_system.execute(" SELECT id, name, area, cost_center_id " |
|
| 219 | " FROM tbl_stores " |
|
| 220 | " WHERE id = %s ", (store_id,)) |
|
| 221 | row_store = cursor_system.fetchone() |
|
| 222 | elif store_uuid is not None: |
|
| 223 | cursor_system.execute(" SELECT id, name, area, cost_center_id " |
|
| 224 | " FROM tbl_stores " |
|
| 225 | " WHERE uuid = %s ", (store_uuid,)) |
|
| 226 | row_store = cursor_system.fetchone() |
|
| 227 | ||
| 228 | if row_store is None: |
|
| 229 | if cursor_system: |
|
| 230 | cursor_system.close() |
|
| 231 | if cnx_system: |
|
| 232 | cnx_system.close() |
|
| 233 | ||
| 234 | if cursor_energy: |
|
| 235 | cursor_energy.close() |
|
| 236 | if cnx_energy: |
|
| 237 | cnx_energy.close() |
|
| 238 | ||
| 239 | if cursor_energy_plan: |
|
| 240 | cursor_energy_plan.close() |
|
| 241 | if cnx_energy_plan: |
|
| 242 | cnx_energy_plan.close() |
|
| 243 | ||
| 244 | if cursor_historical: |
|
| 245 | cursor_historical.close() |
|
| 246 | if cnx_historical: |
|
| 247 | cnx_historical.close() |
|
| 248 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', description='API.STORE_NOT_FOUND') |
|
| 249 | ||
| 250 | store = dict() |
|
| 251 | store['id'] = row_store[0] |
|
| 252 | store['name'] = row_store[1] |
|
| 253 | store['area'] = row_store[2] |
|
| 254 | store['cost_center_id'] = row_store[3] |
|
| 255 | ||
| 256 | ################################################################################################################ |
|
| 257 | # Step 3: query energy categories |
|
| 258 | ################################################################################################################ |
|
| 259 | energy_category_set = set() |
|
| 260 | # query energy categories in base period |
|
| 261 | cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
|
| 262 | " FROM tbl_store_input_category_hourly " |
|
| 263 | " WHERE store_id = %s " |
|
| 264 | " AND start_datetime_utc >= %s " |
|
| 265 | " AND start_datetime_utc < %s ", |
|
| 266 | (store['id'], base_start_datetime_utc, base_end_datetime_utc)) |
|
| 267 | rows_energy_categories = cursor_energy.fetchall() |
|
| 268 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
| 269 | for row_energy_category in rows_energy_categories: |
|
| 270 | energy_category_set.add(row_energy_category[0]) |
|
| 271 | ||
| 272 | # query energy categories in reporting period |
|
| 273 | cursor_energy.execute(" SELECT DISTINCT(energy_category_id) " |
|
| 274 | " FROM tbl_store_input_category_hourly " |
|
| 275 | " WHERE store_id = %s " |
|
| 276 | " AND start_datetime_utc >= %s " |
|
| 277 | " AND start_datetime_utc < %s ", |
|
| 278 | (store['id'], reporting_start_datetime_utc, reporting_end_datetime_utc)) |
|
| 279 | rows_energy_categories = cursor_energy.fetchall() |
|
| 280 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
|
| 281 | for row_energy_category in rows_energy_categories: |
|
| 282 | energy_category_set.add(row_energy_category[0]) |
|
| 283 | ||
| 284 | # query all energy categories in base period and reporting period |
|
| 285 | cursor_system.execute(" SELECT id, name, unit_of_measure, kgce, kgco2e " |
|
| 286 | " FROM tbl_energy_categories " |
|
| 287 | " ORDER BY id ", ) |
|
| 288 | rows_energy_categories = cursor_system.fetchall() |
|
| 289 | if rows_energy_categories is None or len(rows_energy_categories) == 0: |
|
| 290 | if cursor_system: |
|
| 291 | cursor_system.close() |
|
| 292 | if cnx_system: |
|
| 293 | cnx_system.close() |
|
| 294 | ||
| 295 | if cursor_energy: |
|
| 296 | cursor_energy.close() |
|
| 297 | if cnx_energy: |
|
| 298 | cnx_energy.close() |
|
| 299 | ||
| 300 | if cursor_energy_plan: |
|
| 301 | cursor_energy_plan.close() |
|
| 302 | if cnx_energy_plan: |
|
| 303 | cnx_energy_plan.close() |
|
| 304 | ||
| 305 | if cursor_historical: |
|
| 306 | cursor_historical.close() |
|
| 307 | if cnx_historical: |
|
| 308 | cnx_historical.close() |
|
| 309 | raise falcon.HTTPError(status=falcon.HTTP_404, |
|
| 310 | title='API.NOT_FOUND', |
|
| 311 | description='API.ENERGY_CATEGORY_NOT_FOUND') |
|
| 312 | energy_category_dict = dict() |
|
| 313 | for row_energy_category in rows_energy_categories: |
|
| 314 | if row_energy_category[0] in energy_category_set: |
|
| 315 | energy_category_dict[row_energy_category[0]] = {"name": row_energy_category[1], |
|
| 316 | "unit_of_measure": row_energy_category[2], |
|
| 317 | "kgce": row_energy_category[3], |
|
| 318 | "kgco2e": row_energy_category[4]} |
|
| 319 | ||
| 320 | ################################################################################################################ |
|
| 321 | # Step 4: query associated sensors |
|
| 322 | ################################################################################################################ |
|
| 323 | point_list = list() |
|
| 324 | cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type " |
|
| 325 | " FROM tbl_stores st, tbl_sensors se, tbl_stores_sensors ss, " |
|
| 326 | " tbl_points p, tbl_sensors_points sp " |
|
| 327 | " WHERE st.id = %s AND st.id = ss.store_id AND ss.sensor_id = se.id " |
|
| 328 | " AND se.id = sp.sensor_id AND sp.point_id = p.id " |
|
| 329 | " ORDER BY p.id ", (store['id'],)) |
|
| 330 | rows_points = cursor_system.fetchall() |
|
| 331 | if rows_points is not None and len(rows_points) > 0: |
|
| 332 | for row in rows_points: |
|
| 333 | point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]}) |
|
| 334 | ||
| 335 | ################################################################################################################ |
|
| 336 | # Step 5: query associated points |
|
| 337 | ################################################################################################################ |
|
| 338 | cursor_system.execute(" SELECT p.id, p.name, p.units, p.object_type " |
|
| 339 | " FROM tbl_stores s, tbl_stores_points sp, tbl_points p " |
|
| 340 | " WHERE s.id = %s AND s.id = sp.store_id AND sp.point_id = p.id " |
|
| 341 | " ORDER BY p.id ", (store['id'],)) |
|
| 342 | rows_points = cursor_system.fetchall() |
|
| 343 | if rows_points is not None and len(rows_points) > 0: |
|
| 344 | for row in rows_points: |
|
| 345 | point_list.append({"id": row[0], "name": row[1], "units": row[2], "object_type": row[3]}) |
|
| 346 | ||
| 347 | ################################################################################################################ |
|
| 348 | # Step 6: query base period energy saving |
|
| 349 | ################################################################################################################ |
|
| 350 | base = dict() |
|
| 351 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
| 352 | for energy_category_id in energy_category_set: |
|
| 353 | kgce = energy_category_dict[energy_category_id]['kgce'] |
|
| 354 | kgco2e = energy_category_dict[energy_category_id]['kgco2e'] |
|
| 355 | ||
| 356 | base[energy_category_id] = dict() |
|
| 357 | base[energy_category_id]['timestamps'] = list() |
|
| 358 | base[energy_category_id]['values_plan'] = list() |
|
| 359 | base[energy_category_id]['values_actual'] = list() |
|
| 360 | base[energy_category_id]['values_saving'] = list() |
|
| 361 | base[energy_category_id]['subtotal_plan'] = Decimal(0.0) |
|
| 362 | base[energy_category_id]['subtotal_actual'] = Decimal(0.0) |
|
| 363 | base[energy_category_id]['subtotal_saving'] = Decimal(0.0) |
|
| 364 | base[energy_category_id]['subtotal_in_kgce_plan'] = Decimal(0.0) |
|
| 365 | base[energy_category_id]['subtotal_in_kgce_actual'] = Decimal(0.0) |
|
| 366 | base[energy_category_id]['subtotal_in_kgce_saving'] = Decimal(0.0) |
|
| 367 | base[energy_category_id]['subtotal_in_kgco2e_plan'] = Decimal(0.0) |
|
| 368 | base[energy_category_id]['subtotal_in_kgco2e_actual'] = Decimal(0.0) |
|
| 369 | base[energy_category_id]['subtotal_in_kgco2e_saving'] = Decimal(0.0) |
|
| 370 | # query base period's energy plan |
|
| 371 | cursor_energy_plan.execute(" SELECT start_datetime_utc, actual_value " |
|
| 372 | " FROM tbl_store_input_category_hourly " |
|
| 373 | " WHERE store_id = %s " |
|
| 374 | " AND energy_category_id = %s " |
|
| 375 | " AND start_datetime_utc >= %s " |
|
| 376 | " AND start_datetime_utc < %s " |
|
| 377 | " ORDER BY start_datetime_utc ", |
|
| 378 | (store['id'], |
|
| 379 | energy_category_id, |
|
| 380 | base_start_datetime_utc, |
|
| 381 | base_end_datetime_utc)) |
|
| 382 | rows_store_hourly = cursor_energy_plan.fetchall() |
|
| 383 | ||
| 384 | rows_store_periodically = utilities.aggregate_hourly_data_by_period(rows_store_hourly, |
|
| 385 | base_start_datetime_utc, |
|
| 386 | base_end_datetime_utc, |
|
| 387 | period_type) |
|
| 388 | for row_store_periodically in rows_store_periodically: |
|
| 389 | current_datetime_local = row_store_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
| 390 | timedelta(minutes=timezone_offset) |
|
| 391 | if period_type == 'hourly': |
|
| 392 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 393 | elif period_type == 'daily': |
|
| 394 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 395 | elif period_type == 'weekly': |
|
| 396 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 397 | elif period_type == 'monthly': |
|
| 398 | current_datetime = current_datetime_local.isoformat()[0:7] |
|
| 399 | elif period_type == 'yearly': |
|
| 400 | current_datetime = current_datetime_local.isoformat()[0:4] |
|
| 401 | ||
| 402 | plan_value = Decimal(0.0) if row_store_periodically[1] is None else row_store_periodically[1] |
|
| 403 | base[energy_category_id]['timestamps'].append(current_datetime) |
|
| 404 | base[energy_category_id]['values_plan'].append(plan_value) |
|
| 405 | base[energy_category_id]['subtotal_plan'] += plan_value |
|
| 406 | base[energy_category_id]['subtotal_in_kgce_plan'] += plan_value * kgce |
|
| 407 | base[energy_category_id]['subtotal_in_kgco2e_plan'] += plan_value * kgco2e |
|
| 408 | ||
| 409 | # query base period's energy actual |
|
| 410 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
|
| 411 | " FROM tbl_store_input_category_hourly " |
|
| 412 | " WHERE store_id = %s " |
|
| 413 | " AND energy_category_id = %s " |
|
| 414 | " AND start_datetime_utc >= %s " |
|
| 415 | " AND start_datetime_utc < %s " |
|
| 416 | " ORDER BY start_datetime_utc ", |
|
| 417 | (store['id'], |
|
| 418 | energy_category_id, |
|
| 419 | base_start_datetime_utc, |
|
| 420 | base_end_datetime_utc)) |
|
| 421 | rows_store_hourly = cursor_energy.fetchall() |
|
| 422 | ||
| 423 | rows_store_periodically = utilities.aggregate_hourly_data_by_period(rows_store_hourly, |
|
| 424 | base_start_datetime_utc, |
|
| 425 | base_end_datetime_utc, |
|
| 426 | period_type) |
|
| 427 | for row_store_periodically in rows_store_periodically: |
|
| 428 | current_datetime_local = row_store_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
| 429 | timedelta(minutes=timezone_offset) |
|
| 430 | if period_type == 'hourly': |
|
| 431 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 432 | elif period_type == 'daily': |
|
| 433 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 434 | elif period_type == 'weekly': |
|
| 435 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 436 | elif period_type == 'monthly': |
|
| 437 | current_datetime = current_datetime_local.isoformat()[0:7] |
|
| 438 | elif period_type == 'yearly': |
|
| 439 | current_datetime = current_datetime_local.isoformat()[0:4] |
|
| 440 | ||
| 441 | actual_value = Decimal(0.0) if row_store_periodically[1] is None else row_store_periodically[1] |
|
| 442 | base[energy_category_id]['values_actual'].append(actual_value) |
|
| 443 | base[energy_category_id]['subtotal_actual'] += actual_value |
|
| 444 | base[energy_category_id]['subtotal_in_kgce_actual'] += actual_value * kgce |
|
| 445 | base[energy_category_id]['subtotal_in_kgco2e_actual'] += actual_value * kgco2e |
|
| 446 | ||
| 447 | # calculate base period's energy savings |
|
| 448 | for i in range(len(base[energy_category_id]['values_plan'])): |
|
| 449 | base[energy_category_id]['values_saving'].append( |
|
| 450 | base[energy_category_id]['values_plan'][i] - |
|
| 451 | base[energy_category_id]['values_actual'][i]) |
|
| 452 | ||
| 453 | base[energy_category_id]['subtotal_saving'] = \ |
|
| 454 | base[energy_category_id]['subtotal_plan'] - \ |
|
| 455 | base[energy_category_id]['subtotal_actual'] |
|
| 456 | base[energy_category_id]['subtotal_in_kgce_saving'] = \ |
|
| 457 | base[energy_category_id]['subtotal_in_kgce_plan'] - \ |
|
| 458 | base[energy_category_id]['subtotal_in_kgce_actual'] |
|
| 459 | base[energy_category_id]['subtotal_in_kgco2e_saving'] = \ |
|
| 460 | base[energy_category_id]['subtotal_in_kgco2e_plan'] - \ |
|
| 461 | base[energy_category_id]['subtotal_in_kgco2e_actual'] |
|
| 462 | ################################################################################################################ |
|
| 463 | # Step 7: query reporting period energy saving |
|
| 464 | ################################################################################################################ |
|
| 465 | reporting = dict() |
|
| 466 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
| 467 | for energy_category_id in energy_category_set: |
|
| 468 | kgce = energy_category_dict[energy_category_id]['kgce'] |
|
| 469 | kgco2e = energy_category_dict[energy_category_id]['kgco2e'] |
|
| 470 | ||
| 471 | reporting[energy_category_id] = dict() |
|
| 472 | reporting[energy_category_id]['timestamps'] = list() |
|
| 473 | reporting[energy_category_id]['values_plan'] = list() |
|
| 474 | reporting[energy_category_id]['values_actual'] = list() |
|
| 475 | reporting[energy_category_id]['values_saving'] = list() |
|
| 476 | reporting[energy_category_id]['subtotal_plan'] = Decimal(0.0) |
|
| 477 | reporting[energy_category_id]['subtotal_actual'] = Decimal(0.0) |
|
| 478 | reporting[energy_category_id]['subtotal_saving'] = Decimal(0.0) |
|
| 479 | reporting[energy_category_id]['subtotal_in_kgce_plan'] = Decimal(0.0) |
|
| 480 | reporting[energy_category_id]['subtotal_in_kgce_actual'] = Decimal(0.0) |
|
| 481 | reporting[energy_category_id]['subtotal_in_kgce_saving'] = Decimal(0.0) |
|
| 482 | reporting[energy_category_id]['subtotal_in_kgco2e_plan'] = Decimal(0.0) |
|
| 483 | reporting[energy_category_id]['subtotal_in_kgco2e_actual'] = Decimal(0.0) |
|
| 484 | reporting[energy_category_id]['subtotal_in_kgco2e_saving'] = Decimal(0.0) |
|
| 485 | # query reporting period's energy plan |
|
| 486 | cursor_energy_plan.execute(" SELECT start_datetime_utc, actual_value " |
|
| 487 | " FROM tbl_store_input_category_hourly " |
|
| 488 | " WHERE store_id = %s " |
|
| 489 | " AND energy_category_id = %s " |
|
| 490 | " AND start_datetime_utc >= %s " |
|
| 491 | " AND start_datetime_utc < %s " |
|
| 492 | " ORDER BY start_datetime_utc ", |
|
| 493 | (store['id'], |
|
| 494 | energy_category_id, |
|
| 495 | reporting_start_datetime_utc, |
|
| 496 | reporting_end_datetime_utc)) |
|
| 497 | rows_store_hourly = cursor_energy_plan.fetchall() |
|
| 498 | ||
| 499 | rows_store_periodically = utilities.aggregate_hourly_data_by_period(rows_store_hourly, |
|
| 500 | reporting_start_datetime_utc, |
|
| 501 | reporting_end_datetime_utc, |
|
| 502 | period_type) |
|
| 503 | for row_store_periodically in rows_store_periodically: |
|
| 504 | current_datetime_local = row_store_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
| 505 | timedelta(minutes=timezone_offset) |
|
| 506 | if period_type == 'hourly': |
|
| 507 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 508 | elif period_type == 'daily': |
|
| 509 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 510 | elif period_type == 'weekly': |
|
| 511 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 512 | elif period_type == 'monthly': |
|
| 513 | current_datetime = current_datetime_local.isoformat()[0:7] |
|
| 514 | elif period_type == 'yearly': |
|
| 515 | current_datetime = current_datetime_local.isoformat()[0:4] |
|
| 516 | ||
| 517 | plan_value = Decimal(0.0) if row_store_periodically[1] is None else row_store_periodically[1] |
|
| 518 | reporting[energy_category_id]['timestamps'].append(current_datetime) |
|
| 519 | reporting[energy_category_id]['values_plan'].append(plan_value) |
|
| 520 | reporting[energy_category_id]['subtotal_plan'] += plan_value |
|
| 521 | reporting[energy_category_id]['subtotal_in_kgce_plan'] += plan_value * kgce |
|
| 522 | reporting[energy_category_id]['subtotal_in_kgco2e_plan'] += plan_value * kgco2e |
|
| 523 | ||
| 524 | # query reporting period's energy actual |
|
| 525 | cursor_energy.execute(" SELECT start_datetime_utc, actual_value " |
|
| 526 | " FROM tbl_store_input_category_hourly " |
|
| 527 | " WHERE store_id = %s " |
|
| 528 | " AND energy_category_id = %s " |
|
| 529 | " AND start_datetime_utc >= %s " |
|
| 530 | " AND start_datetime_utc < %s " |
|
| 531 | " ORDER BY start_datetime_utc ", |
|
| 532 | (store['id'], |
|
| 533 | energy_category_id, |
|
| 534 | reporting_start_datetime_utc, |
|
| 535 | reporting_end_datetime_utc)) |
|
| 536 | rows_store_hourly = cursor_energy.fetchall() |
|
| 537 | ||
| 538 | rows_store_periodically = utilities.aggregate_hourly_data_by_period(rows_store_hourly, |
|
| 539 | reporting_start_datetime_utc, |
|
| 540 | reporting_end_datetime_utc, |
|
| 541 | period_type) |
|
| 542 | for row_store_periodically in rows_store_periodically: |
|
| 543 | current_datetime_local = row_store_periodically[0].replace(tzinfo=timezone.utc) + \ |
|
| 544 | timedelta(minutes=timezone_offset) |
|
| 545 | if period_type == 'hourly': |
|
| 546 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 547 | elif period_type == 'daily': |
|
| 548 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 549 | elif period_type == 'weekly': |
|
| 550 | current_datetime = current_datetime_local.isoformat()[0:10] |
|
| 551 | elif period_type == 'monthly': |
|
| 552 | current_datetime = current_datetime_local.isoformat()[0:7] |
|
| 553 | elif period_type == 'yearly': |
|
| 554 | current_datetime = current_datetime_local.isoformat()[0:4] |
|
| 555 | ||
| 556 | actual_value = Decimal(0.0) if row_store_periodically[1] is None else row_store_periodically[1] |
|
| 557 | reporting[energy_category_id]['values_actual'].append(actual_value) |
|
| 558 | reporting[energy_category_id]['subtotal_actual'] += actual_value |
|
| 559 | reporting[energy_category_id]['subtotal_in_kgce_actual'] += actual_value * kgce |
|
| 560 | reporting[energy_category_id]['subtotal_in_kgco2e_actual'] += actual_value * kgco2e |
|
| 561 | ||
| 562 | # calculate reporting period's energy savings |
|
| 563 | for i in range(len(reporting[energy_category_id]['values_plan'])): |
|
| 564 | reporting[energy_category_id]['values_saving'].append( |
|
| 565 | reporting[energy_category_id]['values_plan'][i] - |
|
| 566 | reporting[energy_category_id]['values_actual'][i]) |
|
| 567 | ||
| 568 | reporting[energy_category_id]['subtotal_saving'] = \ |
|
| 569 | reporting[energy_category_id]['subtotal_plan'] - \ |
|
| 570 | reporting[energy_category_id]['subtotal_actual'] |
|
| 571 | reporting[energy_category_id]['subtotal_in_kgce_saving'] = \ |
|
| 572 | reporting[energy_category_id]['subtotal_in_kgce_plan'] - \ |
|
| 573 | reporting[energy_category_id]['subtotal_in_kgce_actual'] |
|
| 574 | reporting[energy_category_id]['subtotal_in_kgco2e_saving'] = \ |
|
| 575 | reporting[energy_category_id]['subtotal_in_kgco2e_plan'] - \ |
|
| 576 | reporting[energy_category_id]['subtotal_in_kgco2e_actual'] |
|
| 577 | ################################################################################################################ |
|
| 578 | # Step 8: query tariff data |
|
| 579 | ################################################################################################################ |
|
| 580 | parameters_data = dict() |
|
| 581 | parameters_data['names'] = list() |
|
| 582 | parameters_data['timestamps'] = list() |
|
| 583 | parameters_data['values'] = list() |
|
| 584 | if config.is_tariff_appended and energy_category_set is not None and len(energy_category_set) > 0 \ |
|
| 585 | and not is_quick_mode: |
|
| 586 | for energy_category_id in energy_category_set: |
|
| 587 | energy_category_tariff_dict = utilities.get_energy_category_tariffs(store['cost_center_id'], |
|
| 588 | energy_category_id, |
|
| 589 | reporting_start_datetime_utc, |
|
| 590 | reporting_end_datetime_utc) |
|
| 591 | tariff_timestamp_list = list() |
|
| 592 | tariff_value_list = list() |
|
| 593 | for k, v in energy_category_tariff_dict.items(): |
|
| 594 | # convert k from utc to local |
|
| 595 | k = k + timedelta(minutes=timezone_offset) |
|
| 596 | tariff_timestamp_list.append(k.isoformat()[0:19]) |
|
| 597 | tariff_value_list.append(v) |
|
| 598 | ||
| 599 | parameters_data['names'].append(_('Tariff') + '-' + energy_category_dict[energy_category_id]['name']) |
|
| 600 | parameters_data['timestamps'].append(tariff_timestamp_list) |
|
| 601 | parameters_data['values'].append(tariff_value_list) |
|
| 602 | ||
| 603 | ################################################################################################################ |
|
| 604 | # Step 9: query associated sensors and points data |
|
| 605 | ################################################################################################################ |
|
| 606 | if not is_quick_mode: |
|
| 607 | for point in point_list: |
|
| 608 | point_values = [] |
|
| 609 | point_timestamps = [] |
|
| 610 | if point['object_type'] == 'ENERGY_VALUE': |
|
| 611 | query = (" SELECT utc_date_time, actual_value " |
|
| 612 | " FROM tbl_energy_value " |
|
| 613 | " WHERE point_id = %s " |
|
| 614 | " AND utc_date_time BETWEEN %s AND %s " |
|
| 615 | " ORDER BY utc_date_time ") |
|
| 616 | cursor_historical.execute(query, (point['id'], |
|
| 617 | reporting_start_datetime_utc, |
|
| 618 | reporting_end_datetime_utc)) |
|
| 619 | rows = cursor_historical.fetchall() |
|
| 620 | ||
| 621 | if rows is not None and len(rows) > 0: |
|
| 622 | for row in rows: |
|
| 623 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
| 624 | timedelta(minutes=timezone_offset) |
|
| 625 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 626 | point_timestamps.append(current_datetime) |
|
| 627 | point_values.append(row[1]) |
|
| 628 | elif point['object_type'] == 'ANALOG_VALUE': |
|
| 629 | query = (" SELECT utc_date_time, actual_value " |
|
| 630 | " FROM tbl_analog_value " |
|
| 631 | " WHERE point_id = %s " |
|
| 632 | " AND utc_date_time BETWEEN %s AND %s " |
|
| 633 | " ORDER BY utc_date_time ") |
|
| 634 | cursor_historical.execute(query, (point['id'], |
|
| 635 | reporting_start_datetime_utc, |
|
| 636 | reporting_end_datetime_utc)) |
|
| 637 | rows = cursor_historical.fetchall() |
|
| 638 | ||
| 639 | if rows is not None and len(rows) > 0: |
|
| 640 | for row in rows: |
|
| 641 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
| 642 | timedelta(minutes=timezone_offset) |
|
| 643 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 644 | point_timestamps.append(current_datetime) |
|
| 645 | point_values.append(row[1]) |
|
| 646 | elif point['object_type'] == 'DIGITAL_VALUE': |
|
| 647 | query = (" SELECT utc_date_time, actual_value " |
|
| 648 | " FROM tbl_digital_value " |
|
| 649 | " WHERE point_id = %s " |
|
| 650 | " AND utc_date_time BETWEEN %s AND %s " |
|
| 651 | " ORDER BY utc_date_time ") |
|
| 652 | cursor_historical.execute(query, (point['id'], |
|
| 653 | reporting_start_datetime_utc, |
|
| 654 | reporting_end_datetime_utc)) |
|
| 655 | rows = cursor_historical.fetchall() |
|
| 656 | ||
| 657 | if rows is not None and len(rows) > 0: |
|
| 658 | for row in rows: |
|
| 659 | current_datetime_local = row[0].replace(tzinfo=timezone.utc) + \ |
|
| 660 | timedelta(minutes=timezone_offset) |
|
| 661 | current_datetime = current_datetime_local.isoformat()[0:19] |
|
| 662 | point_timestamps.append(current_datetime) |
|
| 663 | point_values.append(row[1]) |
|
| 664 | ||
| 665 | parameters_data['names'].append(point['name'] + ' (' + point['units'] + ')') |
|
| 666 | parameters_data['timestamps'].append(point_timestamps) |
|
| 667 | parameters_data['values'].append(point_values) |
|
| 668 | ||
| 669 | ################################################################################################################ |
|
| 670 | # Step 10: construct the report |
|
| 671 | ################################################################################################################ |
|
| 672 | if cursor_system: |
|
| 673 | cursor_system.close() |
|
| 674 | if cnx_system: |
|
| 675 | cnx_system.close() |
|
| 676 | ||
| 677 | if cursor_energy: |
|
| 678 | cursor_energy.close() |
|
| 679 | if cnx_energy: |
|
| 680 | cnx_energy.close() |
|
| 681 | ||
| 682 | if cursor_energy_plan: |
|
| 683 | cursor_energy_plan.close() |
|
| 684 | if cnx_energy_plan: |
|
| 685 | cnx_energy_plan.close() |
|
| 686 | ||
| 687 | if cursor_historical: |
|
| 688 | cursor_historical.close() |
|
| 689 | if cnx_historical: |
|
| 690 | cnx_historical.close() |
|
| 691 | ||
| 692 | result = dict() |
|
| 693 | ||
| 694 | result['store'] = dict() |
|
| 695 | result['store']['name'] = store['name'] |
|
| 696 | result['store']['area'] = store['area'] |
|
| 697 | ||
| 698 | result['base_period'] = dict() |
|
| 699 | result['base_period']['names'] = list() |
|
| 700 | result['base_period']['units'] = list() |
|
| 701 | result['base_period']['timestamps'] = list() |
|
| 702 | result['base_period']['values_saving'] = list() |
|
| 703 | result['base_period']['subtotals_saving'] = list() |
|
| 704 | result['base_period']['subtotals_in_kgce_saving'] = list() |
|
| 705 | result['base_period']['subtotals_in_kgco2e_saving'] = list() |
|
| 706 | result['base_period']['total_in_kgce_saving'] = Decimal(0.0) |
|
| 707 | result['base_period']['total_in_kgco2e_saving'] = Decimal(0.0) |
|
| 708 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
| 709 | for energy_category_id in energy_category_set: |
|
| 710 | result['base_period']['names'].append(energy_category_dict[energy_category_id]['name']) |
|
| 711 | result['base_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) |
|
| 712 | result['base_period']['timestamps'].append(base[energy_category_id]['timestamps']) |
|
| 713 | result['base_period']['values_saving'].append(base[energy_category_id]['values_saving']) |
|
| 714 | result['base_period']['subtotals_saving'].append(base[energy_category_id]['subtotal_saving']) |
|
| 715 | result['base_period']['subtotals_in_kgce_saving'].append( |
|
| 716 | base[energy_category_id]['subtotal_in_kgce_saving']) |
|
| 717 | result['base_period']['subtotals_in_kgco2e_saving'].append( |
|
| 718 | base[energy_category_id]['subtotal_in_kgco2e_saving']) |
|
| 719 | result['base_period']['total_in_kgce_saving'] += base[energy_category_id]['subtotal_in_kgce_saving'] |
|
| 720 | result['base_period']['total_in_kgco2e_saving'] += base[energy_category_id]['subtotal_in_kgco2e_saving'] |
|
| 721 | ||
| 722 | result['reporting_period'] = dict() |
|
| 723 | result['reporting_period']['names'] = list() |
|
| 724 | result['reporting_period']['energy_category_ids'] = list() |
|
| 725 | result['reporting_period']['units'] = list() |
|
| 726 | result['reporting_period']['timestamps'] = list() |
|
| 727 | result['reporting_period']['values_saving'] = list() |
|
| 728 | result['reporting_period']['rates_saving'] = list() |
|
| 729 | result['reporting_period']['subtotals_saving'] = list() |
|
| 730 | result['reporting_period']['subtotals_in_kgce_saving'] = list() |
|
| 731 | result['reporting_period']['subtotals_in_kgco2e_saving'] = list() |
|
| 732 | result['reporting_period']['subtotals_per_unit_area_saving'] = list() |
|
| 733 | result['reporting_period']['increment_rates_saving'] = list() |
|
| 734 | result['reporting_period']['total_in_kgce_saving'] = Decimal(0.0) |
|
| 735 | result['reporting_period']['total_in_kgco2e_saving'] = Decimal(0.0) |
|
| 736 | result['reporting_period']['increment_rate_in_kgce_saving'] = Decimal(0.0) |
|
| 737 | result['reporting_period']['increment_rate_in_kgco2e_saving'] = Decimal(0.0) |
|
| 738 | ||
| 739 | if energy_category_set is not None and len(energy_category_set) > 0: |
|
| 740 | for energy_category_id in energy_category_set: |
|
| 741 | result['reporting_period']['names'].append(energy_category_dict[energy_category_id]['name']) |
|
| 742 | result['reporting_period']['energy_category_ids'].append(energy_category_id) |
|
| 743 | result['reporting_period']['units'].append(energy_category_dict[energy_category_id]['unit_of_measure']) |
|
| 744 | result['reporting_period']['timestamps'].append(reporting[energy_category_id]['timestamps']) |
|
| 745 | result['reporting_period']['values_saving'].append(reporting[energy_category_id]['values_saving']) |
|
| 746 | result['reporting_period']['subtotals_saving'].append(reporting[energy_category_id]['subtotal_saving']) |
|
| 747 | result['reporting_period']['subtotals_in_kgce_saving'].append( |
|
| 748 | reporting[energy_category_id]['subtotal_in_kgce_saving']) |
|
| 749 | result['reporting_period']['subtotals_in_kgco2e_saving'].append( |
|
| 750 | reporting[energy_category_id]['subtotal_in_kgco2e_saving']) |
|
| 751 | result['reporting_period']['subtotals_per_unit_area_saving'].append( |
|
| 752 | reporting[energy_category_id]['subtotal_saving'] / store['area'] |
|
| 753 | if store['area'] > Decimal(0.0) else None) |
|
| 754 | result['reporting_period']['increment_rates_saving'].append( |
|
| 755 | (reporting[energy_category_id]['subtotal_saving'] - base[energy_category_id]['subtotal_saving']) / |
|
| 756 | base[energy_category_id]['subtotal_saving'] |
|
| 757 | if base[energy_category_id]['subtotal_saving'] != Decimal(0.0) else None) |
|
| 758 | result['reporting_period']['total_in_kgce_saving'] += \ |
|
| 759 | reporting[energy_category_id]['subtotal_in_kgce_saving'] |
|
| 760 | result['reporting_period']['total_in_kgco2e_saving'] += \ |
|
| 761 | reporting[energy_category_id]['subtotal_in_kgco2e_saving'] |
|
| 762 | ||
| 763 | rate = list() |
|
| 764 | for index, value in enumerate(reporting[energy_category_id]['values_saving']): |
|
| 765 | if index < len(base[energy_category_id]['values_saving']) \ |
|
| 766 | and base[energy_category_id]['values_saving'][index] != 0 and value != 0: |
|
| 767 | rate.append((value - base[energy_category_id]['values_saving'][index]) |
|
| 768 | / base[energy_category_id]['values_saving'][index]) |
|
| 769 | else: |
|
| 770 | rate.append(None) |
|
| 771 | result['reporting_period']['rates_saving'].append(rate) |
|
| 772 | ||
| 773 | result['reporting_period']['total_in_kgco2e_per_unit_area_saving'] = \ |
|
| 774 | result['reporting_period']['total_in_kgce_saving'] / store['area'] \ |
|
| 775 | if store['area'] > Decimal(0.0) else None |
|
| 776 | ||
| 777 | result['reporting_period']['increment_rate_in_kgce_saving'] = \ |
|
| 778 | (result['reporting_period']['total_in_kgce_saving'] - result['base_period']['total_in_kgce_saving']) / \ |
|
| 779 | result['base_period']['total_in_kgce_saving'] \ |
|
| 780 | if result['base_period']['total_in_kgce_saving'] != Decimal(0.0) else None |
|
| 781 | ||
| 782 | result['reporting_period']['total_in_kgce_per_unit_area_saving'] = \ |
|
| 783 | result['reporting_period']['total_in_kgco2e_saving'] / store['area'] \ |
|
| 784 | if store['area'] > Decimal(0.0) else None |
|
| 785 | ||
| 786 | result['reporting_period']['increment_rate_in_kgco2e_saving'] = \ |
|
| 787 | (result['reporting_period']['total_in_kgco2e_saving'] - result['base_period']['total_in_kgco2e_saving']) / \ |
|
| 788 | result['base_period']['total_in_kgco2e_saving'] \ |
|
| 789 | if result['base_period']['total_in_kgco2e_saving'] != Decimal(0.0) else None |
|
| 790 | ||
| 791 | result['parameters'] = { |
|
| 792 | "names": parameters_data['names'], |
|
| 793 | "timestamps": parameters_data['timestamps'], |
|
| 794 | "values": parameters_data['values'] |
|
| 795 | } |
|
| 796 | ||
| 797 | # export result to Excel file and then encode the file to base64 string |
|
| 798 | if not is_quick_mode: |
|
| 799 | result['excel_bytes_base64'] = excelexporters.storeplan.export(result, |
|
| 800 | store['name'], |
|
| 801 | base_period_start_datetime_local, |
|
| 802 | base_period_end_datetime_local, |
|
| 803 | reporting_period_start_datetime_local, |
|
| 804 | reporting_period_end_datetime_local, |
|
| 805 | period_type, |
|
| 806 | language) |
|
| 807 | ||
| 808 | resp.text = json.dumps(result) |
|
| 809 | ||