Compare commits

..

No commits in common. "1453c4015c71f2fde3971f4632348cc92048fb86" and "acbad39df7a3c8c60dab2616b7479193eccdb258" have entirely different histories.

2 changed files with 25 additions and 62 deletions

View file

@ -6,7 +6,6 @@ import itertools
import os import os
import typing import typing
from datetime import date, datetime, timedelta from datetime import date, datetime, timedelta
from time import time
import dateutil.rrule import dateutil.rrule
import dateutil.tz import dateutil.tz
@ -262,19 +261,15 @@ def read_events_yaml(data_dir: str, start: date, end: date) -> list[Event]:
return events return events
def find_events_during_stay( def find_markets_during_stay(
accommodation_events: list[Event], markets: list[Event] accommodation_events: list[Event], markets: list[Event]
) -> list[Event]: ) -> list[Event]:
"""Market events that happen during accommodation stays.""" """Market events that happen during accommodation stays."""
overlapping_markets = [] overlapping_markets = []
for market in markets: for market in markets:
market_date = market.as_date
assert isinstance(market_date, date)
for e in accommodation_events: for e in accommodation_events:
start, end = e.as_date, e.end_as_date
assert start and end and all(isinstance(i, date) for i in (start, end))
# Check if the market date is within the accommodation dates. # Check if the market date is within the accommodation dates.
if start <= market_date <= end: if e.as_date <= market.as_date <= e.end_as_date:
overlapping_markets.append(market) overlapping_markets.append(market)
break # Breaks the inner loop if overlap is found. break # Breaks the inner loop if overlap is found.
return overlapping_markets return overlapping_markets
@ -354,19 +349,6 @@ def busy_event(e: Event) -> bool:
return "rebels" not in lc_title and "south west data social" not in lc_title return "rebels" not in lc_title and "south west data social" not in lc_title
async def time_function(
name: str,
func: typing.Callable[..., typing.Coroutine[typing.Any, typing.Any, typing.Any]],
*args,
**kwargs,
) -> tuple[str, typing.Any, float]:
"""Time the execution of an asynchronous function."""
start_time = time()
result = await func(*args, **kwargs)
end_time = time()
return name, result, end_time - start_time
async def get_data( async def get_data(
now: datetime, config: flask.config.Config now: datetime, config: flask.config.Config
) -> typing.Mapping[str, str | object]: ) -> typing.Mapping[str, str | object]:
@ -383,37 +365,28 @@ async def get_data(
minus_365 = now - timedelta(days=365) minus_365 = now - timedelta(days=365)
plus_365 = now + timedelta(days=365) plus_365 = now + timedelta(days=365)
t0 = time() (
result_list = await asyncio.gather( gbpusd,
time_function("gbpusd", fx.get_gbpusd, config), gwr_advance_tickets,
time_function("gwr_advance_tickets", gwr.advance_ticket_date, data_dir), bank_holiday,
time_function( rockets,
"bank_holiday", uk_holiday.bank_holiday_list, last_year, next_year, data_dir backwell_bins,
), bristol_bins,
time_function("rockets", thespacedevs.get_launches, rocket_dir, limit=40), ) = await asyncio.gather(
time_function("backwell_bins", waste_collection_events, data_dir), fx.get_gbpusd(config),
time_function("bristol_bins", bristol_waste_collection_events, data_dir, today), gwr.advance_ticket_date(data_dir),
uk_holiday.bank_holiday_list(last_year, next_year, data_dir),
thespacedevs.get_launches(rocket_dir, limit=40),
waste_collection_events(data_dir),
bristol_waste_collection_events(data_dir, today),
) )
results = {call[0]: call[1] for call in result_list}
gwr_advance_tickets = results["gwr_advance_tickets"]
data_gather_seconds = time() - t0
t0 = time()
stock_market_times = stock_market.open_and_close()
stock_market_times_seconds = time() - t0
reply: dict[str, typing.Any] = { reply: dict[str, typing.Any] = {
"now": now, "now": now,
"gbpusd": results["gbpusd"], "gbpusd": gbpusd,
"stock_markets": stock_market_times, "stock_markets": stock_market.open_and_close(),
"rockets": results["rockets"], "rockets": rockets,
"gwr_advance_tickets": gwr_advance_tickets, "gwr_advance_tickets": gwr_advance_tickets,
"data_gather_seconds": data_gather_seconds,
"stock_market_times_seconds": stock_market_times_seconds,
"timings": [(call[0], call[2]) for call in result_list],
} }
my_data = config["PERSONAL_DATA"] my_data = config["PERSONAL_DATA"]
@ -432,7 +405,7 @@ async def get_data(
us_hols = us_holidays(last_year, next_year) us_hols = us_holidays(last_year, next_year)
holidays: list[Holiday] = results["bank_holiday"] + us_hols holidays: list[Holiday] = bank_holiday + us_hols
for country in ( for country in (
"at", "at",
"be", "be",
@ -464,7 +437,7 @@ async def get_data(
events += accommodation_events events += accommodation_events
events += travel.all_events(my_data) events += travel.all_events(my_data)
events += conference.get_list(os.path.join(my_data, "conferences.yaml")) events += conference.get_list(os.path.join(my_data, "conferences.yaml"))
events += results["backwell_bins"] + results["bristol_bins"] events += backwell_bins + bristol_bins
events += read_events_yaml(my_data, last_year, next_year) events += read_events_yaml(my_data, last_year, next_year)
events += subscription.get_events(os.path.join(my_data, "subscriptions.yaml")) events += subscription.get_events(os.path.join(my_data, "subscriptions.yaml"))
events += economist.publication_dates(last_week, next_year) events += economist.publication_dates(last_week, next_year)
@ -474,20 +447,16 @@ async def get_data(
events += domains.renewal_dates(my_data) events += domains.renewal_dates(my_data)
# hide markets that happen while away # hide markets that happen while away
optional = [ markets = [e for e in events if e.name == "market"]
e
for e in events
if e.name == "market" or (e.title and "LHG Run Club" in e.title)
]
going = [e for e in events if e.going] going = [e for e in events if e.going]
overlapping_markets = find_events_during_stay( overlapping_markets = find_markets_during_stay(
accommodation_events + going, optional accommodation_events + going, markets
) )
for market in overlapping_markets: for market in overlapping_markets:
events.remove(market) events.remove(market)
for launch in results["rockets"]: for launch in rockets:
dt = None dt = None
if launch["net_precision"] == "Day": if launch["net_precision"] == "Day":

View file

@ -125,12 +125,6 @@
</li> </li>
<li>Bristol Sunrise: {{ sunrise.strftime("%H:%M:%S") }} / <li>Bristol Sunrise: {{ sunrise.strftime("%H:%M:%S") }} /
Sunset: {{ sunset.strftime("%H:%M:%S") }}</li> Sunset: {{ sunset.strftime("%H:%M:%S") }}</li>
<li>Data gather took {{ "%.1f" | format(data_gather_seconds) }} seconds</li>
<li>Stock market open/close took
{{ "%.1f" | format(stock_market_times_seconds) }} seconds</li>
{% for name, seconds in timings %}
<li>{{ name }} took {{ "%.1f" | format(seconds) }} seconds</li>
{% endfor %}
</ul> </ul>
<h3>Stock markets</h3> <h3>Stock markets</h3>