Stage 04.3 - repositories, balance snapshots and environment mode fix

This commit is contained in:
2026-04-16 19:54:04 +03:00
parent 2c49bb70c0
commit 76fc122955
9 changed files with 262 additions and 8 deletions

View File

@@ -0,0 +1,56 @@
from __future__ import annotations
from src.integrations.exchange.models import BalanceSummary
from src.integrations.exchange.service import ExchangeService
from src.storage.repositories.balance_snapshots import BalanceSnapshotRepository
from src.trading.journal.service import JournalService
class AccountsService:
def __init__(self) -> None:
self.exchange_service = ExchangeService()
self.snapshot_repository = BalanceSnapshotRepository()
self.journal = JournalService()
def get_live_balance_summary(self) -> list[BalanceSummary]:
balances = self.exchange_service.get_balance_summary()
self._save_snapshot(balances)
return balances
def _save_snapshot(self, balances: list[BalanceSummary]) -> None:
payload = {
"assets": [
{
"currency": item.currency,
"available": item.available,
"locked": item.locked,
"source": item.source,
}
for item in balances
]
}
try:
self.snapshot_repository.add_snapshot(
source="portfolio_screen",
payload=payload,
)
except Exception as exc:
try:
self.journal.log_warning(
"balance_snapshot_error",
f"Не удалось сохранить snapshot баланса: {exc}",
{"assets_count": len(balances)},
)
except Exception:
pass
return
try:
self.journal.log_info(
"balance_snapshot_saved",
f"Snapshot баланса сохранён. Активов: {len(balances)}",
{"assets_count": len(balances)},
)
except Exception:
pass