Use cache for space launch data

This commit is contained in:
Edward Betts 2024-01-19 19:53:21 +00:00
parent 98b7c4a89d
commit e475f98dd6
3 changed files with 16 additions and 4 deletions

View file

@ -274,10 +274,10 @@ async def get_data(
result_list = await asyncio.gather( result_list = await asyncio.gather(
time_function("gbpusd", fx.get_gbpusd, config), time_function("gbpusd", fx.get_gbpusd, config),
time_function("gwr_advance_tickets", gwr.advance_ticket_date, data_dir), time_function("gwr_advance_tickets", gwr.advance_ticket_date, data_dir),
time_function("rockets", thespacedevs.get_launches, rocket_dir, limit=40),
time_function("backwell_bins", waste_collection_events, data_dir), time_function("backwell_bins", waste_collection_events, data_dir),
time_function("bristol_bins", bristol_waste_collection_events, data_dir, today), time_function("bristol_bins", bristol_waste_collection_events, data_dir, today),
) )
rockets = thespacedevs.read_cached_launches(rocket_dir)
results = {call[0]: call[1] for call in result_list} results = {call[0]: call[1] for call in result_list}
@ -293,7 +293,7 @@ async def get_data(
"now": now, "now": now,
"gbpusd": results["gbpusd"], "gbpusd": results["gbpusd"],
"stock_markets": stock_market_times, "stock_markets": stock_market_times,
"rockets": results["rockets"], "rockets": rockets,
"gwr_advance_tickets": gwr_advance_tickets, "gwr_advance_tickets": gwr_advance_tickets,
"data_gather_seconds": data_gather_seconds, "data_gather_seconds": data_gather_seconds,
"stock_market_times_seconds": stock_market_times_seconds, "stock_market_times_seconds": stock_market_times_seconds,
@ -350,7 +350,7 @@ async def get_data(
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

@ -139,6 +139,18 @@ def summarize_launch(launch: Launch) -> Summary:
} }
def read_cached_launches(rocket_dir: str) -> list[Summary]:
"""Read the most recent cache of launches."""
existing = [x for x in (filename_timestamp(f) for f in os.listdir(rocket_dir)) if x]
existing.sort(reverse=True)
f = existing[0][1]
filename = os.path.join(rocket_dir, f)
data = json.load(open(filename))
return [summarize_launch(launch) for launch in data["results"]]
async def get_launches( async def get_launches(
rocket_dir: str, limit: int = 200, refresh: bool = False rocket_dir: str, limit: int = 200, refresh: bool = False
) -> list[Summary]: ) -> list[Summary]:

View file

@ -14,7 +14,7 @@ rocket_dir = os.path.join(config.DATA_DIR, "thespacedevs")
async def get_launches() -> list[agenda.thespacedevs.Launch]: async def get_launches() -> list[agenda.thespacedevs.Launch]:
"""Call space launch API and cache results.""" """Call space launch API and cache results."""
return await agenda.thespacedevs.get_launches(rocket_dir, limit=200, refresh=True) return await agenda.thespacedevs.next_launch_api(rocket_dir)
t0 = time() t0 = time()