diff --git a/comptages/core/report.py b/comptages/core/report.py index 76f4415..e80be55 100644 --- a/comptages/core/report.py +++ b/comptages/core/report.py @@ -242,7 +242,12 @@ def _data_count_yearly( ws["B14"] = lanes[1].direction_desc -def _data_day(count: models.Count, section: models.Section, monday, workbook: Workbook): +def _data_day( + count: models.Count, + section: models.Section, + monday, + workbook: Workbook + ): ws = workbook["Data_day"] # Monthly coefficients @@ -353,15 +358,22 @@ def _data_day(count: models.Count, section: models.Section, monday, workbook: Wo def _data_day_yearly( - count: models.Count, section: models.Section, year: int, workbook: Workbook -): + count: models.Count, + section: models.Section, + year: int, + workbook: Workbook + ): ws = workbook["Data_day"] # Total (section) row_offset = 69 col_offset = 2 - df = statistics.get_time_data_yearly(year, section) + df = statistics.get_time_data_yearly( + year, + section, + exclude_trash=True, + ) if df is None: print(f"{datetime.now()}:_data_day_yearly - Pas de données pour cette section {section} et cette année {year} /!\\/!\\/!\\") @@ -376,7 +388,10 @@ def _data_day_yearly( row_offset = 95 col_offset = 2 df = statistics.get_light_numbers_yearly( - section, start=datetime(year, 1, 1), end=datetime(year + 1, 1, 1) + section, + start=datetime(year, 1, 1), + end=datetime(year + 1, 1, 1), + exclude_trash=True, ) for i in range(7): @@ -395,9 +410,15 @@ def _data_day_yearly( row_offset = 5 col_offset = 2 - df = statistics.get_time_data_yearly(year, section, direction=1) + df = statistics.get_time_data_yearly( + year, + section, + direction=1, + exclude_trash=True, + ) if df is None: + print(f"{datetime.now()}:_data_day_yearly - Pas de données pour cette section:{section}, cette direction:{direction} et cette année:{year} /!\\/!\\/!\\") return for i in range(7): @@ -409,7 +430,11 @@ def _data_day_yearly( row_offset = 31 col_offset = 2 df = statistics.get_light_numbers_yearly( - section, start=datetime(year, 1, 1), end=datetime(year + 1, 1, 1), direction=1 + section, + start=datetime(year, 1, 1), + end=datetime(year + 1, 1, 1), + direction=1, + exclude_trash=True, ) for i in range(7): @@ -429,7 +454,12 @@ def _data_day_yearly( row_offset = 37 col_offset = 2 - df = statistics.get_time_data_yearly(year, section, direction=2) + df = statistics.get_time_data_yearly( + year, + section, + direction=2, + exclude_trash=True, + ) for i in range(7): day_df = df[df["date"] == i] @@ -444,6 +474,7 @@ def _data_day_yearly( start=datetime(year, 1, 1), end=datetime(year + 1, 1, 1), direction=2, + exclude_trash=True, ) for i in range(7): @@ -467,7 +498,12 @@ def _data_month_yearly( end = datetime(year + 1, 1, 1) # Section - df = statistics.get_month_data(section, start, end) + df = statistics.get_month_data( + section, + start, + end, + exclude_trash=True, + ) row_offset = 14 col_offset = 2 @@ -476,7 +512,13 @@ def _data_month_yearly( ws.cell(row=row_offset, column=col_offset + col.Index, value=col.tm) # Direction 1 - df = statistics.get_month_data(section, start, end, direction=1) + df = statistics.get_month_data( + section, + start, + end, + direction=1, + exclude_trash=True, + ) row_offset = 4 col_offset = 2 @@ -485,7 +527,13 @@ def _data_month_yearly( ws.cell(row=row_offset, column=col_offset + col.Index, value=col.tm) # Direction 2 - df = statistics.get_month_data(section, start, end, direction=2) + df = statistics.get_month_data( + section, + start, + end, + direction=2, + exclude_trash=True, + ) row_offset = 9 col_offset = 2 @@ -720,6 +768,7 @@ def _data_speed_yearly( end=end, speed_low=range_[0], speed_high=range_[1], + exclude_trash=True, ) for row in res: @@ -731,7 +780,13 @@ def _data_speed_yearly( col_offset = 16 for i, v in enumerate(characteristic_speeds): df = statistics.get_characteristic_speed_by_hour( - None, section, direction=1, start=start, end=end, v=v + None, + section, + direction=1, + start=start, + end=end, + v=v, + exclude_trash=True, ) for row in df.itertuples(): ws.cell( @@ -743,11 +798,12 @@ def _data_speed_yearly( col_offset = 19 df = statistics.get_average_speed_by_hour( - count, + None, section, direction=1, start=start, end=end, + exclude_trash=True, ) for row in df.itertuples(): ws.cell(row=row_offset + row.Index, column=col_offset, value=row.speed) @@ -758,13 +814,14 @@ def _data_speed_yearly( col_offset = 2 for i, range_ in enumerate(speed_ranges): res = statistics.get_speed_data_by_hour( - count, + None, section, direction=2, start=start, end=end, speed_low=range_[0], speed_high=range_[1], + exclude_trash=True, ) for row in res: @@ -776,7 +833,13 @@ def _data_speed_yearly( col_offset = 16 for i, v in enumerate(characteristic_speeds): df = statistics.get_characteristic_speed_by_hour( - count, section, direction=2, start=start, end=end, v=v + None, + section, + direction=2, + start=start, + end=end, + v=v, + exclude_trash=True, ) for row in df.itertuples(): ws.cell( @@ -790,11 +853,12 @@ def _data_speed_yearly( col_offset = 19 df = statistics.get_average_speed_by_hour( - count, + None, section, direction=2, start=start, end=end, + exclude_trash=True, ) for row in df.itertuples(): ws.cell(row=row_offset + row.Index, column=col_offset, value=row.speed) diff --git a/comptages/core/statistics.py b/comptages/core/statistics.py index c4683ba..2898f80 100644 --- a/comptages/core/statistics.py +++ b/comptages/core/statistics.py @@ -13,14 +13,14 @@ def get_time_data( - count, - section, - lane=None, - direction=None, - start=None, - end=None, - exclude_trash=False, -): + count, + section, + lane=None, + direction=None, + start=None, + end=None, + exclude_trash=False, + ): if not start: start = count.start_process_date if not end: @@ -66,8 +66,12 @@ def get_time_data( def get_time_data_yearly( - year, section: models.Section, lane=None, direction=None -) -> DataFrame: + year, + section: models.Section, + lane=None, + direction=None, + exclude_trash=False, + ) -> DataFrame: """Vehicles by hour and day of the week""" start = datetime(year, 1, 1) end = datetime(year + 1, 1, 1) @@ -82,6 +86,9 @@ def get_time_data_yearly( timestamp__lt=end, ) + if exclude_trash: + qs = qs.exclude(id_category__trash=True) + if lane is not None: qs = qs.filter(id_lane=lane) @@ -119,15 +126,15 @@ def get_time_data_yearly( def get_day_data( - count: models.Count, - section=None, - lane=None, - direction=None, - status=None, - exclude_trash=False, - start=None, - end=None, -) -> tuple[DataFrame, int]: + count: models.Count, + section=None, + lane=None, + direction=None, + status=None, + exclude_trash=False, + start=None, + end=None, + ) -> tuple[DataFrame, int]: if not start: start = count.start_process_date if not end: @@ -176,12 +183,12 @@ def get_day_data( def get_category_data( - count: models.Count, - section: models.Section, - status=definitions.IMPORT_STATUS_DEFINITIVE, - start=None, - end=None, -) -> DataFrame: + count: models.Count, + section: models.Section, + status=definitions.IMPORT_STATUS_DEFINITIVE, + start=None, + end=None, + ) -> DataFrame: if not start: start = count.start_process_date if not end: @@ -221,12 +228,12 @@ def get_category_data( def get_speed_data( - count: models.Count, - section: models.Section, - exclude_trash=False, - start=None, - end=None, -) -> DataFrame: + count: models.Count, + section: models.Section, + exclude_trash=False, + start=None, + end=None, + ) -> DataFrame: if not start: start = count.start_process_date if not end: @@ -285,14 +292,14 @@ def get_speed_data( def get_light_numbers( - count: models.Count, - section: models.Section, - lane=None, - direction=None, - start=None, - end=None, - exclude_trash=False, -) -> dict: + count: models.Count, + section: models.Section, + lane=None, + direction=None, + start=None, + end=None, + exclude_trash=False, + ) -> dict: if not start: start = count.start_process_date if not end: @@ -330,8 +337,13 @@ def get_light_numbers( def get_light_numbers_yearly( - section: models.Section, lane=None, direction=None, start=None, end=None -) -> DataFrame: + section: models.Section, + lane=None, + direction=None, + start=None, + end=None, + exclude_trash=False, + ) -> DataFrame: qs = models.CountDetail.objects.filter( id_lane__id_section=section, id_category__isnull=False, @@ -339,6 +351,9 @@ def get_light_numbers_yearly( timestamp__lt=end, ) + if exclude_trash: + qs = qs.exclude(id_category__trash=True) + if lane is not None: qs = qs.filter(id_lane=lane) @@ -356,16 +371,16 @@ def get_light_numbers_yearly( def get_speed_data_by_hour( - count: models.Count, - section: models.Section, - lane=None, - direction=None, - start=None, - end=None, - speed_low=0, - speed_high=15, - exclude_trash=False, -) -> "ValuesQuerySet[models.CountDetail, Any]": + count: models.Count, + section: models.Section, + lane=None, + direction=None, + start=None, + end=None, + speed_low=0, + speed_high=15, + exclude_trash=False, + ) -> "ValuesQuerySet[models.CountDetail, Any]": if not start: start = count.start_process_date if not end: @@ -405,15 +420,15 @@ def get_speed_data_by_hour( def get_characteristic_speed_by_hour( - count: models.Count, - section: models.Section, - lane=None, - direction=None, - start=None, - end=None, - v=0.15, - exclude_trash=False, -) -> DataFrame: + count: models.Count, + section: models.Section, + lane=None, + direction=None, + start=None, + end=None, + v=0.15, + exclude_trash=False, + ) -> DataFrame: if not start: start = count.start_process_date if not end: @@ -454,15 +469,15 @@ def get_characteristic_speed_by_hour( def get_average_speed_by_hour( - count: models.Count, - section: models.Section, - lane=None, - direction=None, - start=None, - end=None, - v=0.15, - exclude_trash=False, -) -> DataFrame: + count: models.Count, + section: models.Section, + lane=None, + direction=None, + start=None, + end=None, + v=0.15, + exclude_trash=False, + ) -> DataFrame: if not start: start = count.start_process_date if not end: @@ -476,6 +491,9 @@ def get_average_speed_by_hour( timestamp__lt=end, ) + if exclude_trash: + qs = qs.exclude(id_category__trash=True) + if count is not None: qs = qs.filter(id_count=count) @@ -501,14 +519,14 @@ def get_average_speed_by_hour( def get_category_data_by_hour( - count: models.Count, - section: models.Section, - category, - lane=None, - direction=None, - start=None, - end=None, -) -> "ValuesQuerySet[models.CountDetail, Any]": + count: models.Count, + section: models.Section, + category, + lane=None, + direction=None, + start=None, + end=None, + ) -> "ValuesQuerySet[models.CountDetail, Any]": if not start: start = count.start_process_date if not end: @@ -553,7 +571,12 @@ def get_special_periods(first_day, last_day) -> QuerySet[models.SpecialPeriod]: return qs -def get_month_data(section: models.Section, start, end, direction=None) -> DataFrame: +def get_month_data( + section: models.Section, + start, end, + direction=None, + exclude_trash=False, + ) -> DataFrame: qs = models.CountDetail.objects.filter( id_lane__id_section=section, timestamp__gte=start, timestamp__lt=end ) @@ -566,6 +589,9 @@ def get_month_data(section: models.Section, start, end, direction=None) -> DataF .values("month", "tm", "import_status") ) + if exclude_trash: + qs = qs.exclude(id_category__trash=True) + if direction is not None: qs = qs.filter(id_lane__direction=direction) diff --git a/comptages/report/template_yearly.xlsx b/comptages/report/template_yearly.xlsx index 4cd05b4..8de2ff9 100644 Binary files a/comptages/report/template_yearly.xlsx and b/comptages/report/template_yearly.xlsx differ