Show events before and after gaps
This commit is contained in:
parent
45e4a04fb9
commit
7c54fdfe09
|
@ -2,6 +2,7 @@
|
|||
|
||||
import asyncio
|
||||
import collections
|
||||
import itertools
|
||||
import os
|
||||
import typing
|
||||
from datetime import date, datetime, timedelta
|
||||
|
@ -37,6 +38,8 @@ from . import (
|
|||
)
|
||||
from .types import Event, Holiday
|
||||
|
||||
StrDict = dict[str, typing.Any]
|
||||
|
||||
here = dateutil.tz.tzlocal()
|
||||
|
||||
# deadline to file tax return
|
||||
|
@ -272,13 +275,23 @@ def find_markets_during_stay(
|
|||
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."""
|
||||
# Sort events by start date
|
||||
|
||||
gaps: list[tuple[date, date]] = []
|
||||
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:
|
||||
# Use start date for current event
|
||||
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)
|
||||
|
||||
# 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:
|
||||
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:
|
||||
|
@ -455,7 +476,9 @@ async def get_data(
|
|||
|
||||
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
|
||||
# at the top of the list for today. This is achieved by sorting first by
|
||||
|
|
|
@ -50,12 +50,15 @@ class Event:
|
|||
@property
|
||||
def end_as_date(self) -> datetime.date:
|
||||
"""Date of event."""
|
||||
assert self.end_date
|
||||
return (
|
||||
(
|
||||
self.end_date.date()
|
||||
if isinstance(self.end_date, datetime.datetime)
|
||||
else self.end_date
|
||||
)
|
||||
if self.end_date
|
||||
else self.as_date
|
||||
)
|
||||
|
||||
@property
|
||||
def display_time(self) -> str | None:
|
||||
|
|
|
@ -17,13 +17,26 @@
|
|||
|
||||
|
||||
<table class="table table-hover w-auto">
|
||||
{% for start, end in gaps %}
|
||||
<thead>
|
||||
<tr>
|
||||
<td class="text-end">{{ start.strftime("%A, %-d %b %Y") }}</td>
|
||||
<td class="text-end">{{ end.strftime("%A, %-d %b %Y") }}</td>
|
||||
<td>{{ (end - start).days }} days</td>
|
||||
<th>before</th>
|
||||
<th class="text-end">start</th>
|
||||
<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>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
|
|
Loading…
Reference in a new issue