import datetime from typing import Set, List, Dict, LiteralString, Tuple from token_bot.history_manager.history import History from token_bot.persistant_database import Alert from token_bot.token_database.flavor import Flavor from token_bot.token_database.region import Region from token_bot.token_database import database as tdb class HistoryManager: def __init__(self, token_db: tdb.Database): self._history : Dict[Flavor, Dict[Region, History]] = {} self._tdb : tdb.Database = token_db for flavor in Flavor: self._history[flavor] = {} for region in Region: self._history[flavor][Region(region)] = History(flavor, Region(region)) async def _retrieve_data(self, flavor: Flavor, region: Region) -> List[Tuple[datetime.datetime, int]]: high_fidelity_time = datetime.datetime.now(tz=datetime.UTC) - datetime.timedelta(hours=72) all_history = await self._tdb.history(flavor, region) high_fidelity_history = await self._tdb.history(flavor, region, '72h') final_response = [] for data_point in all_history: datum = (datetime.datetime.fromisoformat(data_point[0]), data_point[1]) if datum[0] < high_fidelity_time: final_response.append(datum) for data_point in high_fidelity_history: datum = (datetime.datetime.fromisoformat(data_point[0]), data_point[1]) final_response.append(datum) return final_response async def load_data(self): for flavor in Flavor: for r in Region: region = Region(r) history = History(flavor, Region(region)) history_response = await self._retrieve_data(flavor, region) for item in history_response: await history.add_price(item) self._history[flavor][region] = history async def update_data(self, flavor: Flavor, region: Region) -> List[Alert]: history = self._history[flavor][region] current_price_data = await self._tdb.current(flavor) current_region_data = current_price_data[region.value.lower()] datum = ( datetime.datetime.fromisoformat(current_region_data[0]), current_region_data[1] ) if datum != history.last_price_datum: return await history.add_price(datum) return [] async def get_history(self, flavor, region) -> History: return self._history[flavor][region]