Code Duplication    Length = 47-47 lines in 3 locations

reports/dashboard.py 1 location

@@ 310-356 (lines=47) @@
307
        # Step 7: query base period energy input
308
        ################################################################################################################
309
        base_input = dict()
310
        if energy_category_set is not None and len(energy_category_set) > 0:
311
            for energy_category_id in energy_category_set:
312
                kgce = energy_category_dict[energy_category_id]['kgce']
313
                kgco2e = energy_category_dict[energy_category_id]['kgco2e']
314
315
                base_input[energy_category_id] = dict()
316
                base_input[energy_category_id]['timestamps'] = list()
317
                base_input[energy_category_id]['values'] = list()
318
                base_input[energy_category_id]['subtotal'] = Decimal(0.0)
319
                base_input[energy_category_id]['subtotal_in_kgce'] = Decimal(0.0)
320
                base_input[energy_category_id]['subtotal_in_kgco2e'] = Decimal(0.0)
321
322
                cursor_energy.execute(" SELECT start_datetime_utc, actual_value "
323
                                      " FROM tbl_space_input_category_hourly "
324
                                      " WHERE space_id = %s "
325
                                      "     AND energy_category_id = %s "
326
                                      "     AND start_datetime_utc >= %s "
327
                                      "     AND start_datetime_utc < %s "
328
                                      " ORDER BY start_datetime_utc ",
329
                                      (space['id'],
330
                                       energy_category_id,
331
                                       base_start_datetime_utc,
332
                                       base_end_datetime_utc))
333
                rows_space_hourly = cursor_energy.fetchall()
334
335
                rows_space_periodically = utilities.aggregate_hourly_data_by_period(rows_space_hourly,
336
                                                                                    base_start_datetime_utc,
337
                                                                                    base_end_datetime_utc,
338
                                                                                    period_type)
339
                for row_space_periodically in rows_space_periodically:
340
                    current_datetime_local = row_space_periodically[0].replace(tzinfo=timezone.utc) + \
341
                                             timedelta(minutes=timezone_offset)
342
                    if period_type == 'hourly':
343
                        current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
344
                    elif period_type == 'daily':
345
                        current_datetime = current_datetime_local.strftime('%Y-%m-%d')
346
                    elif period_type == 'monthly':
347
                        current_datetime = current_datetime_local.strftime('%Y-%m')
348
                    elif period_type == 'yearly':
349
                        current_datetime = current_datetime_local.strftime('%Y')
350
351
                    actual_value = Decimal(0.0) if row_space_periodically[1] is None else row_space_periodically[1]
352
                    base_input[energy_category_id]['timestamps'].append(current_datetime)
353
                    base_input[energy_category_id]['values'].append(actual_value)
354
                    base_input[energy_category_id]['subtotal'] += actual_value
355
                    base_input[energy_category_id]['subtotal_in_kgce'] += actual_value * kgce
356
                    base_input[energy_category_id]['subtotal_in_kgco2e'] += actual_value * kgco2e
357
358
        ################################################################################################################
359
        # Step 8: query base period energy cost

reports/spaceenergycategory.py 1 location

@@ 264-310 (lines=47) @@
261
        # Step 7: query base period energy input
262
        ################################################################################################################
263
        base = dict()
264
        if energy_category_set is not None and len(energy_category_set) > 0:
265
            for energy_category_id in energy_category_set:
266
                kgce = energy_category_dict[energy_category_id]['kgce']
267
                kgco2e = energy_category_dict[energy_category_id]['kgco2e']
268
269
                base[energy_category_id] = dict()
270
                base[energy_category_id]['timestamps'] = list()
271
                base[energy_category_id]['values'] = list()
272
                base[energy_category_id]['subtotal'] = Decimal(0.0)
273
                base[energy_category_id]['subtotal_in_kgce'] = Decimal(0.0)
274
                base[energy_category_id]['subtotal_in_kgco2e'] = Decimal(0.0)
275
276
                cursor_energy.execute(" SELECT start_datetime_utc, actual_value "
277
                                      " FROM tbl_space_input_category_hourly "
278
                                      " WHERE space_id = %s "
279
                                      "     AND energy_category_id = %s "
280
                                      "     AND start_datetime_utc >= %s "
281
                                      "     AND start_datetime_utc < %s "
282
                                      " ORDER BY start_datetime_utc ",
283
                                      (space['id'],
284
                                       energy_category_id,
285
                                       base_start_datetime_utc,
286
                                       base_end_datetime_utc))
287
                rows_space_hourly = cursor_energy.fetchall()
288
289
                rows_space_periodically = utilities.aggregate_hourly_data_by_period(rows_space_hourly,
290
                                                                                    base_start_datetime_utc,
291
                                                                                    base_end_datetime_utc,
292
                                                                                    period_type)
293
                for row_space_periodically in rows_space_periodically:
294
                    current_datetime_local = row_space_periodically[0].replace(tzinfo=timezone.utc) + \
295
                                             timedelta(minutes=timezone_offset)
296
                    if period_type == 'hourly':
297
                        current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
298
                    elif period_type == 'daily':
299
                        current_datetime = current_datetime_local.strftime('%Y-%m-%d')
300
                    elif period_type == 'monthly':
301
                        current_datetime = current_datetime_local.strftime('%Y-%m')
302
                    elif period_type == 'yearly':
303
                        current_datetime = current_datetime_local.strftime('%Y')
304
305
                    actual_value = Decimal(0.0) if row_space_periodically[1] is None else row_space_periodically[1]
306
                    base[energy_category_id]['timestamps'].append(current_datetime)
307
                    base[energy_category_id]['values'].append(actual_value)
308
                    base[energy_category_id]['subtotal'] += actual_value
309
                    base[energy_category_id]['subtotal_in_kgce'] += actual_value * kgce
310
                    base[energy_category_id]['subtotal_in_kgco2e'] += actual_value * kgco2e
311
312
        ################################################################################################################
313
        # Step 8: query reporting period energy input

reports/tenantenergycategory.py 1 location

@@ 249-295 (lines=47) @@
246
        # Step 6: query base period energy input
247
        ################################################################################################################
248
        base = dict()
249
        if energy_category_set is not None and len(energy_category_set) > 0:
250
            for energy_category_id in energy_category_set:
251
                kgce = energy_category_dict[energy_category_id]['kgce']
252
                kgco2e = energy_category_dict[energy_category_id]['kgco2e']
253
254
                base[energy_category_id] = dict()
255
                base[energy_category_id]['timestamps'] = list()
256
                base[energy_category_id]['values'] = list()
257
                base[energy_category_id]['subtotal'] = Decimal(0.0)
258
                base[energy_category_id]['subtotal_in_kgce'] = Decimal(0.0)
259
                base[energy_category_id]['subtotal_in_kgco2e'] = Decimal(0.0)
260
261
                cursor_energy.execute(" SELECT start_datetime_utc, actual_value "
262
                                      " FROM tbl_tenant_input_category_hourly "
263
                                      " WHERE tenant_id = %s "
264
                                      "     AND energy_category_id = %s "
265
                                      "     AND start_datetime_utc >= %s "
266
                                      "     AND start_datetime_utc < %s "
267
                                      " ORDER BY start_datetime_utc ",
268
                                      (tenant['id'],
269
                                       energy_category_id,
270
                                       base_start_datetime_utc,
271
                                       base_end_datetime_utc))
272
                rows_tenant_hourly = cursor_energy.fetchall()
273
274
                rows_tenant_periodically = utilities.aggregate_hourly_data_by_period(rows_tenant_hourly,
275
                                                                                     base_start_datetime_utc,
276
                                                                                     base_end_datetime_utc,
277
                                                                                     period_type)
278
                for row_tenant_periodically in rows_tenant_periodically:
279
                    current_datetime_local = row_tenant_periodically[0].replace(tzinfo=timezone.utc) + \
280
                                             timedelta(minutes=timezone_offset)
281
                    if period_type == 'hourly':
282
                        current_datetime = current_datetime_local.strftime('%Y-%m-%dT%H:%M:%S')
283
                    elif period_type == 'daily':
284
                        current_datetime = current_datetime_local.strftime('%Y-%m-%d')
285
                    elif period_type == 'monthly':
286
                        current_datetime = current_datetime_local.strftime('%Y-%m')
287
                    elif period_type == 'yearly':
288
                        current_datetime = current_datetime_local.strftime('%Y')
289
290
                    actual_value = Decimal(0.0) if row_tenant_periodically[1] is None else row_tenant_periodically[1]
291
                    base[energy_category_id]['timestamps'].append(current_datetime)
292
                    base[energy_category_id]['values'].append(actual_value)
293
                    base[energy_category_id]['subtotal'] += actual_value
294
                    base[energy_category_id]['subtotal_in_kgce'] += actual_value * kgce
295
                    base[energy_category_id]['subtotal_in_kgco2e'] += actual_value * kgco2e
296
297
        ################################################################################################################
298
        # Step 8: query reporting period energy input