Skip to content

Commit

Permalink
tota_runs_by_hour_one_direction: writer
Browse files Browse the repository at this point in the history
  • Loading branch information
why-not-try-calmer committed Jan 22, 2024
1 parent 2b1bd1c commit 09a114d
Showing 1 changed file with 55 additions and 43 deletions.
98 changes: 55 additions & 43 deletions comptages/report/yearly_report_bike.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def tjms_by_weekday_hour(self) -> "ValuesQuerySet[CountDetail, dict[str, Any]]":

def total_runs_by_hour_and_direction(
self, directions=(1, 2), weekdays=(0, 1, 2, 3, 4, 5, 6)
) -> dict[str, Any]:
) -> dict[int, Any]:
# Get all the count details for section and the year
qs = CountDetail.objects.filter(
id_lane__id_section__id=self.section_id,
Expand All @@ -92,21 +92,21 @@ def total_runs_by_hour_and_direction(
)

def partition(acc: dict, val: dict) -> dict:
hour = str(val["hour"])
direction = str(val["direction"])
hour = val["hour"]
direction = val["direction"]

if hour not in acc:
acc[hour] = {}

if direction not in acc[hour]:
acc[hour][direction] = {}

acc[hour][direction][direction] = val
acc[hour][direction] = val
return acc

return reduce(partition, results, {})

def total_runs_by_hour_one_direction(self, direction: int) -> dict[str, Any]:
def total_runs_by_hour_one_direction(self, direction: int) -> dict[int, Any]:
# Get all the count details for hours and the specific direction
qs = CountDetail.objects.filter(
id_lane__id_section__id=self.section_id,
Expand All @@ -119,12 +119,13 @@ def total_runs_by_hour_one_direction(self, direction: int) -> dict[str, Any]:
.values("hour")
.annotate(runs=Sum("times"))
.values("runs", "hour")
.order_by("hour")
.annotate(day=ExtractIsoWeekDay("timestamp"))
.order_by("day")
)

def reducer(acc: dict, val: dict) -> dict:
day = str(val["day"])
hour = str(val["hour"])
day = val["day"]
hour = val["hour"]

if not day in acc:
acc[day] = {}
Expand Down Expand Up @@ -248,8 +249,8 @@ def total_runs_by_class(self) -> dict[str, Any]:
)

def reducer(acc: dict, i: dict):
code = str(i["code"])
day = str(i["day"])
code = i["code"]
day = i["day"]
runs = i["runs"]

if code not in acc:
Expand Down Expand Up @@ -434,51 +435,62 @@ def render_section_dist(value: Union[str, Decimal, None]) -> str:
ws = workbook["Data_hour"]

# Data hour > Whole weeks
window = ws["C5:D28"]
print_area = ws["C5:D28"]
data = self.total_runs_by_hour_and_direction(directions=(1, 2))
for row_idx, row in enumerate(window, 1):
hour = str(row_idx)
for column_idx, cell in enumerate(row, 1):
direction = str(column_idx)
cell.value = data[hour][direction]["runs"]
for hour, row in enumerate(print_area, 1):
if hour == 24:
hour = 0
for direction, cell in enumerate(row, 1):
if not hour in data or not direction in data[hour]:
cell.value = 0
else:
cell.value = data[hour][direction]["runs"]

# Data hour > Weekends only
window = ws["C37:D60"]
data = self.total_runs_by_hour_and_direction(directions=(1,), weekdays=(5, 6))
for row_idx, row in enumerate(window, 1):
hour = str(row_idx)
for column_idx, cell in enumerate(row, 1):
direction = str(column_idx)
cell.value = data[hour][direction]["runs"]
print_area = ws["C37:D60"]
data = self.total_runs_by_hour_and_direction(directions=(1, 2), weekdays=(5, 6))
for hour, row in enumerate(print_area, 1):
if hour == 24:
hour = 0
for direction, cell in enumerate(row, 1):
if not hour in data or not direction in data[hour]:
cell.value = 0
else:
cell.value = data[hour][direction]["runs"]

# Data hour > Only dir 1
window = ws["B69:H92"]
print_area = ws["B69:H92"]
data = self.total_runs_by_hour_one_direction(1)
for row_idx, row in enumerate(window, 1):
hour = str(row_idx)
for column_idx, cell in enumerate(row, 0):
day = str(column_idx)
cell.value = data[day][hour]
for hour, row in enumerate(print_area, 1):
if hour == 24:
hour = 0
for day, cell in enumerate(row, 1):
if not day in data or not hour in data[day]:
cell.value = 0
else:
cell.value = data[day][hour]

# Data hour > Only dir 2
window = ws["B101:H124"]
print_area = ws["B101:H124"]
data = self.total_runs_by_hour_one_direction(2)
for row_idx, row in enumerate(window, 1):
hour = str(row_idx)
for column_idx, cell in enumerate(row, 0):
day = str(column_idx)
cell.value = data[day][hour]
for hour, row in enumerate(print_area, 1):
if hour == 24:
hour = 0
for day, cell in enumerate(row, 1):
if not day in data or not hour in data[day]:
cell.value = 0
else:
cell.value = data[day][hour]

data = self.total_runs_by_class()
ws = workbook["Data_class"]
window = ws["B4:H18"]
for idx_row, row in enumerate(window, 0):
code = str(idx_row)
for idx_column, cell in enumerate(row, 1):
day = str(idx_column)
cell.value = (
data[code][day] if code in data and day in data[code] else ""
)
print_area = ws["B4:H18"]
for code, row in enumerate(print_area, 0):
for day, cell in enumerate(row, 1):
if not code in data or not day in data[code]:
cell.value = 0
else:
cell.value = data[code][day]

ws = workbook["AN_GR"]
ws.print_area = "A1:Z62"
Expand Down

0 comments on commit 09a114d

Please sign in to comment.