Skip to content

Commit

Permalink
Add weekends chart
Browse files Browse the repository at this point in the history
  • Loading branch information
marioba committed Oct 14, 2021
1 parent e598955 commit 1b9f371
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
Binary file modified comptages/report/template_yearly_bike.xlsx
Binary file not shown.
34 changes: 31 additions & 3 deletions comptages/report/yearly_report_bike.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def values_by_day_and_hour(self):
# TODO: don't divide by 51 but actually aggregate first by the
# real days (with sum) and then aggregate by weekday (with average)

# Total by day of the week (0->monday, 7->sunday) and by hour (0->23)
# Total by day of the week (0->monday, 6->sunday) and by hour (0->23)
result = qs.annotate(weekday=ExtractIsoWeekDay('timestamp')) \
.annotate(hour=ExtractHour('timestamp')) \
.values('weekday', 'hour') \
Expand All @@ -57,12 +57,13 @@ def values_by_day_and_hour(self):

return result

def values_by_hour_and_direction(self, direction):
def values_by_hour_and_direction(self, direction, weekdays=[0, 1, 2, 3, 4, 5, 6]):
# Get all the count details for section and the year
qs = CountDetail.objects.filter(
id_lane__id_section__id=self.section_id,
timestamp__year=self.year,
id_lane__direction=direction,
timestamp__iso_week_day__in=weekdays,
)

# TODO: don't divide by 365
Expand All @@ -85,7 +86,7 @@ def values_by_day_and_month(self):
# TODO: don't divide by 12 but actually aggregate first by the
# real days (with sum) and then aggregate by weekday (with average)

# Total by day of the week (0->monday, 7->sunday) and by month (1->12)
# Total by day of the week (0->monday, 6->sunday) and by month (1->12)
result = qs.annotate(weekday=ExtractIsoWeekDay('timestamp')) \
.annotate(month=ExtractMonth('timestamp')) \
.values('weekday', 'month') \
Expand Down Expand Up @@ -328,6 +329,33 @@ def run(self):
)
row += 1

# Weekend days only
row_offset = 37
column_offset = 3

data = self.values_by_hour_and_direction(1, [5, 6])
row = row_offset
for i in data:
ws.cell(
row=row,
column=column_offset,
value=i['tjm']
)
row += 1

row_offset = 37
column_offset = 4

data = self.values_by_hour_and_direction(2, [5, 6])
row = row_offset
for i in data:
ws.cell(
row=row,
column=column_offset,
value=i['tjm']
)
row += 1

# Save the file
output = os.path.join(
self.file_path, '{}_{}_r.xlsx'.format(self.section_id, self.year))
Expand Down

0 comments on commit 1b9f371

Please sign in to comment.