Introduce V2 of current and historical functions
This commit is contained in:
50
wow_token/path_handler/math_path_handler.py
Normal file
50
wow_token/path_handler/math_path_handler.py
Normal file
@@ -0,0 +1,50 @@
|
||||
import datetime
|
||||
from typing import List, Tuple
|
||||
|
||||
from wow_token.db.cached_range import CachedRange
|
||||
from wow_token.db.compacted import Compacted
|
||||
from wow_token.db.recent import Recent
|
||||
from wow_token.db.trinity import Trinity
|
||||
from wow_token.flavor import Flavor
|
||||
from wow_token.path_handler.relative_error import InvalidRelativePathError
|
||||
from wow_token.path_handler.relative_path_handler import RelativePathHandler
|
||||
from wow_token.region import Region
|
||||
|
||||
|
||||
class MathPathHandler:
|
||||
_cdb : Compacted
|
||||
_rdb : Recent
|
||||
def __init__(self, cdb: Compacted, rdb: Recent):
|
||||
self._cdb = cdb
|
||||
self._rdb = rdb
|
||||
|
||||
def path_handler(self, uri: str) -> List[Tuple[str, int]]:
|
||||
# This URI takes the form of /v2/math/{math_function}/{flavor}/{region}/{range}
|
||||
split_uri = uri.split('/')
|
||||
math_function = split_uri[-4]
|
||||
data = RelativePathHandler(self._cdb, self._rdb).path_handler(uri)
|
||||
|
||||
match math_function:
|
||||
case 'avg':
|
||||
return self._avg(data)
|
||||
case _:
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
def _avg(self, data: List[Tuple[str, int]]) -> List[Tuple[str, int]]:
|
||||
avg_buckets = []
|
||||
bucket_timestamp = None
|
||||
bucket_price = 0
|
||||
bucket_count = 0
|
||||
for timestamp, price in data:
|
||||
if bucket_timestamp is None:
|
||||
bucket_timestamp = datetime.datetime.fromisoformat(timestamp)
|
||||
elif bucket_timestamp.date() != datetime.datetime.fromisoformat(timestamp).date():
|
||||
bucket_head = datetime.datetime(year=bucket_timestamp.year, month=bucket_timestamp.month, day=bucket_timestamp.day)
|
||||
avg_buckets.append((bucket_head.isoformat(), int(bucket_price/bucket_count)))
|
||||
bucket_price = 0
|
||||
bucket_count = 0
|
||||
bucket_timestamp = datetime.datetime.fromisoformat(timestamp)
|
||||
bucket_price += price
|
||||
bucket_count += 1
|
||||
return avg_buckets
|
||||
Reference in New Issue
Block a user