Show events before and after gaps

This commit is contained in:
Edward Betts 2023-12-28 20:32:55 +00:00
parent 45e4a04fb9
commit 7c54fdfe09
3 changed files with 54 additions and 15 deletions

View file

@ -2,6 +2,7 @@
import asyncio import asyncio
import collections import collections
import itertools
import os import os
import typing import typing
from datetime import date, datetime, timedelta from datetime import date, datetime, timedelta
@ -37,6 +38,8 @@ from . import (
) )
from .types import Event, Holiday from .types import Event, Holiday
StrDict = dict[str, typing.Any]
here = dateutil.tz.tzlocal() here = dateutil.tz.tzlocal()
# deadline to file tax return # deadline to file tax return
@ -272,13 +275,23 @@ def find_markets_during_stay(
return overlapping_markets return overlapping_markets
def find_gaps(events: list[Event], min_gap_days: int = 3) -> list[tuple[date, date]]: def find_gaps(events: list[Event], min_gap_days: int = 3) -> list[StrDict]:
"""Gaps of at least `min_gap_days` between events in a list of events.""" """Gaps of at least `min_gap_days` between events in a list of events."""
# Sort events by start date # Sort events by start date
gaps: list[tuple[date, date]] = [] gaps: list[tuple[date, date]] = []
previous_event_end = None previous_event_end = None
by_start_date = {
d: list(on_day)
for d, on_day in itertools.groupby(events, key=lambda e: e.as_date)
}
by_end_date = {
d: list(on_day)
for d, on_day in itertools.groupby(events, key=lambda e: e.end_as_date)
}
for event in events: for event in events:
# Use start date for current event # Use start date for current event
start_date = event.as_date start_date = event.as_date
@ -294,11 +307,19 @@ def find_gaps(events: list[Event], min_gap_days: int = 3) -> list[tuple[date, da
gaps.append(start_end) gaps.append(start_end)
# Update previous event end date # Update previous event end date
end = event.end_as_date if event.end_date else start_date end = event.end_as_date
if not previous_event_end or end > previous_event_end: if not previous_event_end or end > previous_event_end:
previous_event_end = end previous_event_end = end
return gaps return [
{
"start": gap_start,
"end": gap_end,
"after": by_start_date[gap_end + timedelta(days=1)],
"before": by_end_date[gap_start - timedelta(days=1)],
}
for gap_start, gap_end in gaps
]
def busy_event(e: Event) -> bool: def busy_event(e: Event) -> bool:
@ -455,7 +476,9 @@ async def get_data(
gaps = find_gaps(busy_events) gaps = find_gaps(busy_events)
events += [Event(name="gap", date=start, end_date=end) for start, end in gaps] events += [
Event(name="gap", date=gap["start"], end_date=gap["end"]) for gap in gaps
]
# Sort events by their datetime; the "today" event is prioritised # Sort events by their datetime; the "today" event is prioritised
# at the top of the list for today. This is achieved by sorting first by # at the top of the list for today. This is achieved by sorting first by

View file

@ -50,12 +50,15 @@ class Event:
@property @property
def end_as_date(self) -> datetime.date: def end_as_date(self) -> datetime.date:
"""Date of event.""" """Date of event."""
assert self.end_date
return ( return (
(
self.end_date.date() self.end_date.date()
if isinstance(self.end_date, datetime.datetime) if isinstance(self.end_date, datetime.datetime)
else self.end_date else self.end_date
) )
if self.end_date
else self.as_date
)
@property @property
def display_time(self) -> str | None: def display_time(self) -> str | None:

View file

@ -17,13 +17,26 @@
<table class="table table-hover w-auto"> <table class="table table-hover w-auto">
{% for start, end in gaps %} <thead>
<tr> <tr>
<td class="text-end">{{ start.strftime("%A, %-d %b %Y") }}</td> <th>before</th>
<td class="text-end">{{ end.strftime("%A, %-d %b %Y") }}</td> <th class="text-end">start</th>
<td>{{ (end - start).days }} days</td> <th class="text-end">end</th>
<th class="text-end">days</th>
<th>after</th>
</tr>
</thead>
<tbody>
{% for gap in gaps %}
<tr>
<td>{% for event in gap.before %}{% if not loop.first %}, {% endif %}{{ event.title or event.name }}{% endfor %}</td>
<td class="text-end">{{ gap.start.strftime("%A, %-d %b %Y") }}</td>
<td class="text-end">{{ gap.end.strftime("%A, %-d %b %Y") }}</td>
<td class="text-end">{{ (gap.end - gap.start).days }} days</td>
<td>{% for event in gap.after %}{% if not loop.first %}, {% endif %}{{ event.title or event.name }}{% endfor %}</td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody>
</table> </table>
</div> </div>