Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gl5 ignore trash, tenir compte timezone #2

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 106 additions & 32 deletions comptages/core/report.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import os
from datetime import date, datetime, timedelta
from typing import Generator
from typing import Generator, Optional
from qgis.core import Qgis, QgsMessageLog

from datetime import timedelta, datetime
from typing import Optional
from openpyxl import load_workbook, Workbook
from qgis.core import Qgis, QgsMessageLog

from comptages.core import statistics
from comptages.datamodel import models
Expand All @@ -24,6 +22,7 @@ def prepare_reports(
callback_progress=simple_print_callback,
sections_days: Optional[dict[str, list[date]]] = None,
):
print(f"{datetime.now()}: _prepare_reports: begin, folder: {file_path}")
current_dir = os.path.dirname(os.path.abspath(__file__))

if template == "default":
Expand All @@ -48,6 +47,7 @@ def prepare_reports(
)
elif template == "yearly_bike":
pass
print(f"{datetime.now()}: _prepare_reports: ended, folder: {file_path}")


def _prepare_default_reports(
Expand All @@ -58,6 +58,7 @@ def _prepare_default_reports(
sections_days: Optional[dict[str, list[date]]] = None,
):
"""Write default reports to disk (1 per section in count, per week)"""
print(f"{datetime.now()}: _prepare_default_reports: begin, count: {count}")
# We do by section and not by count because of special cases.
sections = models.Section.objects.filter(
lane__id_installation__count=count
Expand Down Expand Up @@ -95,6 +96,8 @@ def _prepare_default_reports(

workbook.save(filename=output)

print(f"{datetime.now()}: _prepare_default_reports: ended, count: {count}")


def _prepare_yearly_report(
file_path: str,
Expand All @@ -105,6 +108,9 @@ def _prepare_yearly_report(
sections_days: Optional[dict[str, list[date]]] = None,
):
"""Write default reports to disk (1 per section included in the count)"""
print(
f"{datetime.now()}: _prepare_yearly_report: begin, sections_ids: {sections_ids}"
)
# Get first count to be used as example
count_qs = models.Count.objects.filter(
id_installation__lane__id_section=sections_ids[0],
Expand Down Expand Up @@ -137,6 +143,10 @@ def _prepare_yearly_report(
output = os.path.join(file_path, f"{section.id}_{year}_r.xlsx")
workbook.save(filename=output)

print(
f"{datetime.now()}: _prepare_yearly_report: ended, sections_ids: {sections_ids}"
)


def _mondays_of_count(count: models.Count) -> Generator[date, None, None]:
"""Generator that return the Mondays of the count"""
Expand Down Expand Up @@ -285,6 +295,7 @@ def _data_day(count: models.Count, section: models.Section, monday, workbook: Wo
section,
start=monday + timedelta(days=i),
end=monday + timedelta(days=i + 1),
exclude_trash=True,
)

for row in df.itertuples():
Expand All @@ -300,6 +311,7 @@ def _data_day(count: models.Count, section: models.Section, monday, workbook: Wo
start=monday + timedelta(days=i),
end=monday + timedelta(days=i + 1),
direction=1,
exclude_trash=True,
)

for row in df.itertuples():
Expand All @@ -315,6 +327,7 @@ def _data_day(count: models.Count, section: models.Section, monday, workbook: Wo
start=monday + timedelta(days=i),
end=monday + timedelta(days=i + 1),
direction=1,
exclude_trash=True,
)
ws.cell(row=row_offset, column=col_offset + i, value=light.get(True, 0))
ws.cell(row=row_offset + 1, column=col_offset + i, value=light.get(False, 0))
Expand All @@ -330,6 +343,7 @@ def _data_day(count: models.Count, section: models.Section, monday, workbook: Wo
start=monday + timedelta(days=i),
end=monday + timedelta(days=i + 1),
direction=2,
exclude_trash=True,
)

for row in df.itertuples():
Expand All @@ -345,6 +359,7 @@ def _data_day(count: models.Count, section: models.Section, monday, workbook: Wo
start=monday + timedelta(days=i),
end=monday + timedelta(days=i + 1),
direction=2,
exclude_trash=True,
)
ws.cell(row=row_offset, column=col_offset + i, value=light.get(True, 0))
ws.cell(
Expand All @@ -360,8 +375,11 @@ def _data_day_yearly(
# 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(
Expand All @@ -378,7 +396,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):
Expand All @@ -396,10 +417,17 @@ def _data_day_yearly(
# Direction 1
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):
Expand All @@ -411,7 +439,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):
Expand All @@ -430,8 +462,12 @@ def _data_day_yearly(
if len(section.lane_set.all()) == 2:
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]
Expand All @@ -446,6 +482,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):
Expand All @@ -469,28 +506,42 @@ def _data_month_yearly(
end = datetime(year + 1, 1, 1)

# Section
df = statistics.get_month_data(section, start, end)

row_offset = 14
col_offset = 2
df = statistics.get_month_data(
section,
start,
end,
exclude_trash=True,
)

for col in df.itertuples():
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)

row_offset = 4
col_offset = 2
df = statistics.get_month_data(
section,
start,
end,
direction=1,
exclude_trash=True,
)

for col in df.itertuples():
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)

row_offset = 9
col_offset = 2
df = statistics.get_month_data(
section,
start,
end,
direction=2,
exclude_trash=True,
)

for col in df.itertuples():
ws.cell(row=row_offset, column=col_offset + col.Index, value=col.tm)
Expand Down Expand Up @@ -573,8 +624,8 @@ def _data_speed(
end=monday + timedelta(days=7),
speed_low=range_[0],
speed_high=range_[1],
exclude_trash=True,
)

for row in res:
ws.cell(row=row_offset + row[0], column=col_offset + i, value=row[1])

Expand All @@ -590,6 +641,7 @@ def _data_speed(
start=monday,
end=monday + timedelta(days=7),
v=v,
exclude_trash=True,
)
for row in df.itertuples():
ws.cell(
Expand All @@ -599,9 +651,13 @@ def _data_speed(
# Average speed direction 1
row_offset = 5
col_offset = 19

df = statistics.get_average_speed_by_hour(
count, section, direction=1, start=monday, end=monday + timedelta(days=7)
count,
section,
direction=1,
start=monday,
end=monday + timedelta(days=7),
exclude_trash=True,
)
for row in df.itertuples():
ws.cell(row=row_offset + row.Index, column=col_offset, value=row.speed)
Expand All @@ -619,8 +675,8 @@ def _data_speed(
end=monday + timedelta(days=7),
speed_low=range_[0],
speed_high=range_[1],
exclude_trash=True,
)

for row in res:
ws.cell(row=row_offset + row[0], column=col_offset + i, value=row[1])

Expand All @@ -636,6 +692,7 @@ def _data_speed(
start=monday,
end=monday + timedelta(days=7),
v=v,
exclude_trash=True,
)
for row in df.itertuples():
ws.cell(
Expand All @@ -647,9 +704,13 @@ def _data_speed(
# Average speed direction 2
row_offset = 33
col_offset = 19

df = statistics.get_average_speed_by_hour(
count, section, direction=2, start=monday, end=monday + timedelta(days=7)
count,
section,
direction=2,
start=monday,
end=monday + timedelta(days=7),
exclude_trash=True,
)
for row in df.itertuples():
ws.cell(row=row_offset + row.Index, column=col_offset, value=row.speed)
Expand Down Expand Up @@ -708,8 +769,8 @@ def _data_speed_yearly(
end=end,
speed_low=range_[0],
speed_high=range_[1],
exclude_trash=True,
)

for row in res:
ws.cell(row=row_offset + row[0], column=col_offset + i, value=row[1])

Expand All @@ -719,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(
Expand All @@ -729,13 +796,13 @@ def _data_speed_yearly(
# Average speed direction 1
row_offset = 5
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)
Expand All @@ -746,13 +813,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:
Expand All @@ -764,7 +832,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(
Expand All @@ -776,13 +850,13 @@ def _data_speed_yearly(
# Average speed direction 2
row_offset = 33
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)
Expand Down
Loading
Loading