Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify Price Prediction Logic #33

Merged
merged 3 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions src/scripts/price_prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from dataclasses import dataclass
import datetime as dt
from decimal import Decimal
from json import JSONDecodeError
import time
from typing import Annotated, Any

Expand All @@ -23,7 +24,8 @@
from src.config.services import connect_to_mongodb
from src.models import document_models
from src.models.poe import Item
from src.schemas.poe import Currency, ItemPrice
from src.schemas.poe import Currency, ItemPrice, PriceDatedData


# TODO: handle extreme decimal case scenarios for items like Mirror of Kalandra

Expand Down Expand Up @@ -109,20 +111,29 @@ async def prepare_api_data(
item_id = item.poe_ninja_id
category = item.category

api_call_succeeded = True

if category in ("Currency", "Fragment"):
url = f"currencyhistory?league={LEAGUE}&type={category}&currencyId={item_id}"
else:
url = f"itemhistory?league={LEAGUE}&type={category}&itemId={item_id}"

try:
response = await client.get(url)
# response.raise_for_status()
response.raise_for_status()
price_history_api_data: list[dict] | dict[str, list[dict]] = response.json()
except HTTPError as exc:
logger.error(
f"error getting price history data for item_id {item_id} belonging to '{category}' category: {exc}"
)
price_history_api_data = []
api_call_succeeded = False
except JSONDecodeError:
logger.error(f"invalid price history API response for item_id {item_id} belonging to '{category}' category")
api_call_succeeded = False

if not api_call_succeeded:
price_history_map[item] = []
return

try:
if isinstance(price_history_api_data, dict):
Expand Down Expand Up @@ -303,8 +314,8 @@ def prepare_item_price_data(

now = dt.datetime.now(dt.UTC)

price_prediction_mapping = {}
price_history_mapping = {}
price_prediction = []
price_history = []

if len(price_history_data) < 1:
return
Expand All @@ -313,25 +324,26 @@ def prepare_item_price_data(
rounded_value = round(Decimal(value), 2)
future_date = now + dt.timedelta(index + 1)

price_prediction_mapping[future_date] = rounded_value
price_prediction_record = PriceDatedData(timestamp=future_date, price=rounded_value)
price_prediction.append(price_prediction_record)

last_week_price_data = price_history_data[-7:]
todays_price_data = price_history_data[-1]
todays_price_data = price_history_data[-1]

for entity in last_week_price_data:
previous_date = entity.convert_days_ago_to_date()
price_history_mapping[previous_date] = entity.value
for entity in price_history_data:
previous_date = entity.convert_days_ago_to_date()
price_history_record = PriceDatedData(timestamp=previous_date, price=entity.value)
price_history.append(price_history_record)

# TODO: check for low confidence
price_info = item.price_info
if price_info is None:
price_info = item.price_info = ItemPrice()
price_info.chaos_price = todays_price_data.value

price_info.price_history = price_history_mapping
price_info.price_history_currency = Currency.chaos
price_info.price_prediction = price_prediction_mapping
price_info.price_prediction = price_prediction
price_info.price_prediction_currency = Currency.chaos
price_info.price_history = price_history
price_info.price_history_currency = Currency.chaos


async def main():
Expand Down
5 changes: 5 additions & 0 deletions src/services/poe.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,9 @@ async def get_items(
logger.error(f"error getting items from database; filter_sort: {filter_sort_input}: {exc}")
raise

# limit price history data to past last 7 days' data
for item in items:
if item.price_info is not None and item.price_info.price_history:
item.price_info.price_history = item.price_info.price_history[-7:]

return items, items_count
Loading