Code Duplication    Length = 24-27 lines in 3 locations

myems-api/core/utilities.py 3 locations

@@ 527-553 (lines=27) @@
524
        maximum = None
525
        counter = 0
526
        current_datetime_utc = start_datetime_utc.replace(minute=0, second=0, microsecond=0, tzinfo=None)
527
        while current_datetime_utc <= end_datetime_utc:
528
            sub_total = Decimal(0.0)
529
            sub_maximum = None
530
            sub_counter = 0
531
            for row in rows_hourly:
532
                if current_datetime_utc <= row[0] < current_datetime_utc + \
533
                        timedelta(minutes=config.minutes_to_count):
534
                    sub_total += row[1]
535
                    if sub_maximum is None:
536
                        sub_maximum = row[1]
537
                    elif sub_maximum < row[1]:
538
                        sub_maximum = row[1]
539
                    sub_counter += 1
540
541
            sub_average = (sub_total / sub_counter) if sub_counter > 0 else None
542
            result_rows_hourly.append((current_datetime_utc, sub_average, sub_maximum))
543
544
            total += sub_total
545
            counter += sub_counter
546
            if sub_maximum is None:
547
                pass
548
            elif maximum is None:
549
                maximum = sub_maximum
550
            elif maximum < sub_maximum:
551
                maximum = sub_maximum
552
553
            current_datetime_utc += timedelta(minutes=config.minutes_to_count)
554
555
        average = total / counter if counter > 0 else None
556
        return result_rows_hourly, average, maximum
@@ 608-631 (lines=24) @@
605
        weekday = start_datetime_local.weekday()
606
        current_datetime_utc = \
607
            start_datetime_local.replace(hour=0) - timedelta(days=weekday, hours=int(config.utc_offset[1:3]))
608
        while current_datetime_utc <= end_datetime_utc:
609
            sub_total = Decimal(0.0)
610
            sub_maximum = None
611
            sub_counter = 0
612
            for row in rows_hourly:
613
                if current_datetime_utc <= row[0] < current_datetime_utc + timedelta(days=7):
614
                    sub_total += row[1]
615
                    if sub_maximum is None:
616
                        sub_maximum = row[1]
617
                    elif sub_maximum < row[1]:
618
                        sub_maximum = row[1]
619
                    sub_counter += 1
620
621
            sub_average = (sub_total / sub_counter) if sub_counter > 0 else None
622
            result_rows_weekly.append((current_datetime_utc, sub_average, sub_maximum))
623
            total += sub_total
624
            counter += sub_counter
625
            if sub_maximum is None:
626
                pass
627
            elif maximum is None:
628
                maximum = sub_maximum
629
            elif maximum < sub_maximum:
630
                maximum = sub_maximum
631
            current_datetime_utc += timedelta(days=7)
632
633
        average = total / counter if counter > 0 else None
634
        return result_rows_weekly, average, maximum
@@ 568-591 (lines=24) @@
565
        # calculate the start datetime in utc of the first day in local
566
        start_datetime_local = start_datetime_utc + timedelta(hours=int(config.utc_offset[1:3]))
567
        current_datetime_utc = start_datetime_local.replace(hour=0) - timedelta(hours=int(config.utc_offset[1:3]))
568
        while current_datetime_utc <= end_datetime_utc:
569
            sub_total = Decimal(0.0)
570
            sub_maximum = None
571
            sub_counter = 0
572
            for row in rows_hourly:
573
                if current_datetime_utc <= row[0] < current_datetime_utc + timedelta(days=1):
574
                    sub_total += row[1]
575
                    if sub_maximum is None:
576
                        sub_maximum = row[1]
577
                    elif sub_maximum < row[1]:
578
                        sub_maximum = row[1]
579
                    sub_counter += 1
580
581
            sub_average = (sub_total / sub_counter) if sub_counter > 0 else None
582
            result_rows_daily.append((current_datetime_utc, sub_average, sub_maximum))
583
            total += sub_total
584
            counter += sub_counter
585
            if sub_maximum is None:
586
                pass
587
            elif maximum is None:
588
                maximum = sub_maximum
589
            elif maximum < sub_maximum:
590
                maximum = sub_maximum
591
            current_datetime_utc += timedelta(days=1)
592
593
        average = total / counter if counter > 0 else None
594
        return result_rows_daily, average, maximum