2024-11-30 11:27:32 +00:00
|
|
|
import datetime
|
2024-12-02 12:58:16 +00:00
|
|
|
from typing import List, Dict, Tuple
|
2024-11-30 11:27:32 +00:00
|
|
|
|
|
|
|
from token_bot.history_manager.history import History
|
|
|
|
from token_bot.persistant_database import Alert
|
2024-12-02 12:58:16 +00:00
|
|
|
from token_bot.token_database import database as tdb
|
2024-11-30 11:27:32 +00:00
|
|
|
from token_bot.token_database.flavor import Flavor
|
|
|
|
from token_bot.token_database.region import Region
|
|
|
|
|
|
|
|
|
|
|
|
class HistoryManager:
|
2024-12-09 01:07:26 +00:00
|
|
|
HIGH_FIDELITY_PERIOD = "72h"
|
|
|
|
|
2024-11-30 11:27:32 +00:00
|
|
|
def __init__(self, token_db: tdb.Database):
|
2024-12-09 01:07:26 +00:00
|
|
|
self._history: Dict[Flavor, Dict[Region, History]] = {}
|
|
|
|
self._tdb: tdb.Database = token_db
|
2024-11-30 11:27:32 +00:00
|
|
|
for flavor in Flavor:
|
|
|
|
self._history[flavor] = {}
|
|
|
|
for region in Region:
|
|
|
|
self._history[flavor][Region(region)] = History(flavor, Region(region))
|
|
|
|
|
2024-12-09 01:07:26 +00:00
|
|
|
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)
|
2024-11-30 11:27:32 +00:00
|
|
|
all_history = await self._tdb.history(flavor, region)
|
2024-12-09 01:07:26 +00:00
|
|
|
high_fidelity_history = await self._tdb.history(
|
|
|
|
flavor, region, self.HIGH_FIDELITY_PERIOD
|
|
|
|
)
|
2024-11-30 11:27:32 +00:00
|
|
|
final_response = []
|
2024-12-01 07:42:35 +00:00
|
|
|
|
2024-12-09 01:07:26 +00:00
|
|
|
def _convert_to_datetime(
|
|
|
|
data: Tuple[str, int]
|
|
|
|
) -> Tuple[datetime.datetime, int]:
|
2024-12-01 07:42:35 +00:00
|
|
|
return datetime.datetime.fromisoformat(data[0]), data[1]
|
|
|
|
|
2024-11-30 11:27:32 +00:00
|
|
|
for data_point in all_history:
|
2024-12-01 07:42:35 +00:00
|
|
|
datetime_tuple = _convert_to_datetime(data_point)
|
|
|
|
if datetime_tuple[0] < high_fidelity_time:
|
|
|
|
final_response.append(datetime_tuple)
|
|
|
|
|
2024-12-09 01:07:26 +00:00
|
|
|
final_response.extend(
|
|
|
|
_convert_to_datetime(data_point) for data_point in high_fidelity_history
|
|
|
|
)
|
2024-12-01 07:42:35 +00:00
|
|
|
|
2024-11-30 11:27:32 +00:00
|
|
|
return final_response
|
|
|
|
|
|
|
|
async def load_data(self):
|
|
|
|
for flavor in Flavor:
|
|
|
|
for r in Region:
|
|
|
|
region = Region(r)
|
2024-12-01 07:42:35 +00:00
|
|
|
history = History(flavor, region)
|
2024-11-30 11:27:32 +00:00
|
|
|
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]),
|
2024-12-09 01:07:26 +00:00
|
|
|
current_region_data[1],
|
2024-11-30 11:27:32 +00:00
|
|
|
)
|
|
|
|
if datum != history.last_price_datum:
|
|
|
|
return await history.add_price(datum)
|
|
|
|
return []
|
|
|
|
|
2024-12-08 00:21:52 +00:00
|
|
|
def get_history(self, flavor, region) -> History:
|
2024-11-30 11:27:32 +00:00
|
|
|
return self._history[flavor][region]
|