v1.0
This commit is contained in:
@@ -28,6 +28,8 @@ class BaseMarketplaceUpdater(ABC):
|
||||
pass
|
||||
|
||||
async def update(self, updates: List[StockUpdate]):
|
||||
if not self.marketplace_api:
|
||||
return
|
||||
product_ids = list(set([update.product_id for update in updates]))
|
||||
stock_data_list = await queries.general.get_stocks_data(
|
||||
session=self.session,
|
||||
@@ -41,6 +43,8 @@ class BaseMarketplaceUpdater(ABC):
|
||||
await self.marketplace_api.update_stocks(marketplace_updates)
|
||||
|
||||
async def update_all(self):
|
||||
if not self.marketplace_api:
|
||||
return
|
||||
stock_data_list = await queries.general.get_stocks_data(
|
||||
session=self.session,
|
||||
marketplace=self.marketplace,
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
import asyncio
|
||||
import logging
|
||||
import time
|
||||
from collections import defaultdict
|
||||
from enum import unique, IntEnum
|
||||
from typing import List, Union
|
||||
|
||||
from sqlalchemy import select, or_
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.orm import joinedload, selectinload
|
||||
|
||||
from backend.session import get_session, session_factory
|
||||
from backend.session import session_factory
|
||||
from database import Marketplace, MarketplaceProduct, Warehouse, Company
|
||||
from database.sipro.enums.general import BaseMarketplace
|
||||
from schemas.general import StockUpdate
|
||||
@@ -62,16 +62,23 @@ class StocksUpdater:
|
||||
|
||||
async def full_update_marketplace(self, marketplace_id: int):
|
||||
marketplace = await self.get_marketplace(marketplace_id)
|
||||
start = time.time()
|
||||
updater = UpdaterFactory.get_updater(self.session, marketplace)
|
||||
await updater.update_all()
|
||||
logging.info(
|
||||
f"{marketplace.name} successfully fully updated in {round(time.time() - start, 2)} seconds.")
|
||||
|
||||
async def full_update_all_marketplaces(self, marketplace_ids: Union[List[int], None] = None):
|
||||
marketplaces = await self.get_marketplaces(marketplace_ids)
|
||||
|
||||
async def update_marketplace(marketplace):
|
||||
async with session_factory() as session:
|
||||
start = time.time()
|
||||
|
||||
updater = UpdaterFactory.get_updater(session, marketplace)
|
||||
await updater.update_all()
|
||||
logging.info(
|
||||
f"{marketplace.name} successfully fully updated in {round(time.time() - start, 2)} seconds.")
|
||||
|
||||
tasks = [update_marketplace(marketplace) for marketplace in marketplaces]
|
||||
await asyncio.gather(*tasks)
|
||||
@@ -79,10 +86,13 @@ class StocksUpdater:
|
||||
async def update_marketplace(self, marketplace_id: int, updates: List[StockUpdate]):
|
||||
marketplace = await self.get_marketplace(marketplace_id)
|
||||
async with session_factory() as session:
|
||||
start = time.time()
|
||||
updater = UpdaterFactory.get_updater(session, marketplace)
|
||||
if not updater:
|
||||
return
|
||||
await updater.update(updates)
|
||||
logging.info(
|
||||
f"Successfully uploaded {len(updates)} updates to {marketplace.name} in {round(time.time() - start, 2)} seconds.")
|
||||
|
||||
async def update(self, updates: list[StockUpdate]):
|
||||
updates_dict = defaultdict(list)
|
||||
@@ -108,8 +118,6 @@ class StocksUpdater:
|
||||
updates_dict[marketplace_id].append(update)
|
||||
updates_list = list(updates_dict.items())
|
||||
updates_list = sorted(updates_list, key=lambda x: len(x[1]))
|
||||
print(updates_list)
|
||||
return
|
||||
tasks = []
|
||||
for marketplace_id, marketplace_updates in updates_list:
|
||||
tasks.append(self.update_marketplace(marketplace_id, marketplace_updates))
|
||||
|
||||
Reference in New Issue
Block a user