-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: update main cron and add legacy prices api
- Loading branch information
Showing
9 changed files
with
120 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,64 @@ | ||
from fastapi import APIRouter, Depends, Response | ||
from fastapi import APIRouter, Depends, HTTPException, status | ||
from sqlalchemy.ext.asyncio import AsyncSession | ||
from sqlalchemy.future import select | ||
from sqlmodel import select | ||
from pydantic import BaseModel | ||
from datetime import datetime | ||
|
||
from icon_stats.db import get_session | ||
from icon_stats.models.exchanges_legacy import ExchangesLegacy | ||
from icon_stats.models.cmc_cryptocurrency_quotes_latest import CmcListingsLatestQuote | ||
|
||
router = APIRouter() | ||
|
||
|
||
class ExchangesLegacyResponseData(BaseModel): | ||
marketName: str | ||
tradeName: str | ||
createDate: datetime | ||
price: float | ||
prePrice: float | ||
dailyRate: float | None | ||
volume: float | ||
marketCap: float | ||
|
||
|
||
class ExchangesLegacyResponse(BaseModel): | ||
result: int = 200 | ||
description: str = "" | ||
totalData: int | None = None | ||
data: dict | ||
data: ExchangesLegacyResponseData | ||
|
||
|
||
@router.get("/stats/exchanges/legacy") | ||
@router.get("/stats/exchanges/legacy", response_model=ExchangesLegacyResponse) | ||
async def get_exchange_prices( | ||
response: Response, | ||
session: AsyncSession = Depends(get_session), | ||
) -> list[ExchangesLegacyResponse]: | ||
) -> ExchangesLegacyResponse: | ||
"""Return list of delegations.""" | ||
query = ( | ||
select(ExchangesLegacy) | ||
select(CmcListingsLatestQuote) | ||
.where(CmcListingsLatestQuote.base == "ICX") # noqa | ||
.order_by(CmcListingsLatestQuote.last_updated.desc()) | ||
.limit(1) | ||
) | ||
|
||
result = await session.execute(query) | ||
data = result.scalars().all() | ||
data = result.scalars().first() | ||
|
||
if not data: | ||
raise HTTPException(status_code=status.HTTP_204_NO_CONTENT) # Raise an HTTPException for the 204 status | ||
|
||
exchanges_legacy_response_data = ExchangesLegacyResponseData( | ||
marketName="coinmarketcap", | ||
tradeName="icxusd", | ||
createDate=data.last_updated, | ||
price=data.price, | ||
prePrice=data.price + (data.price * data.percent_change_24h / 100), | ||
dailyRate=None, | ||
volume=data.volume_24h, | ||
marketCap=data.market_cap, | ||
) | ||
exchanges_legacy = ExchangesLegacyResponse( | ||
data=data, | ||
data=exchanges_legacy_response_data, | ||
) | ||
|
||
return exchanges_legacy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
from fastapi import APIRouter | ||
|
||
from icon_stats.api.v1.endpoints import token_stats | ||
from icon_stats.api.v1.endpoints import exchanges_legacy | ||
|
||
|
||
api_router = APIRouter() | ||
api_router.include_router(token_stats.router) | ||
# api_router.include_router(token_stats.router) | ||
api_router.include_router(exchanges_legacy.router) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from datetime import datetime, timezone | ||
|
||
def convert_str_date(date_str: str) -> datetime: | ||
# Check if the date string ends with 'Z' (which means it's in UTC) | ||
if date_str.endswith("Z"): | ||
# If it does, we remove the 'Z' and parse the datetime as UTC. | ||
# The 'Z' is not supported by the fromisoformat method, so we have to handle it | ||
# manually. | ||
date_str = date_str.rstrip("Z") | ||
last_updated = datetime.fromisoformat(date_str) | ||
last_updated = last_updated.replace(tzinfo=timezone.utc) | ||
else: | ||
# If the string doesn't end with 'Z', we assume it's in local time (this may | ||
# not be a correct assumption depending on your data) | ||
# Here, you might want to handle other formats or timezones if necessary. | ||
try: | ||
last_updated = datetime.fromisoformat(date_str) | ||
except ValueError: | ||
# If parsing fails, we fall back to strptime with a defined format. Adjust | ||
# the format as necessary. | ||
last_updated = datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S') | ||
# If your time is in a specific timezone, you can adjust it here. | ||
# Assuming UTC for this example. | ||
last_updated = last_updated.replace(tzinfo=timezone.utc) | ||
|
||
return last_updated.replace(tzinfo=None) |