|
| 1 | +import logging |
| 2 | +import os |
| 3 | +import warnings |
| 4 | +from datetime import datetime, timedelta |
| 5 | +from typing import Any, Dict, List, Set, Tuple |
| 6 | + |
| 7 | +import pandas as pd |
| 8 | +from dateutil import parser, tz |
| 9 | +from fastapi import APIRouter, HTTPException, Query |
| 10 | +from pyathena import connect |
| 11 | + |
| 12 | +warnings.filterwarnings("ignore", category=UserWarning) |
| 13 | + |
| 14 | +logging.basicConfig(level=logging.INFO) |
| 15 | +logger = logging.getLogger(__name__) |
| 16 | +router = APIRouter() |
| 17 | + |
| 18 | +DATABASE = os.environ.get("ATHENA_DATABASE", "mainnet-beta-archive") |
| 19 | +REGION = os.environ.get("AWS_REGION", "eu-west-1") |
| 20 | +S3_OUTPUT = os.environ.get( |
| 21 | + "ATHENA_S3_OUTPUT", "s3://mainnet-beta-data-ingestion-bucket/athena/" |
| 22 | +) |
| 23 | + |
| 24 | +QUOTE_ASSET_SCALE_FACTOR = 1e6 |
| 25 | +UTC = tz.tzutc() |
| 26 | + |
| 27 | + |
| 28 | +def dt_from_ms(ms: int) -> datetime: |
| 29 | + return datetime.fromtimestamp(ms / 1_000, tz=UTC) |
| 30 | + |
| 31 | + |
| 32 | +def partition_tuples(start: datetime, days: int) -> Set[Tuple[str, str, str]]: |
| 33 | + return { |
| 34 | + (d.strftime("%Y"), d.strftime("%m"), d.strftime("%d")) |
| 35 | + for d in (start + timedelta(n) for n in range(days)) |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | +def partition_pred(parts: Set[Tuple[str, str, str]]) -> str: |
| 40 | + lines = [f"(year='{y}' AND month='{m}' AND day='{d}')" for y, m, d in sorted(parts)] |
| 41 | + return " OR ".join(lines) |
| 42 | + |
| 43 | + |
| 44 | +def sql_get_all_traders_since_date(start_date: datetime) -> str: |
| 45 | + """Get all unique traders who have traded since the start date.""" |
| 46 | + now = datetime.now(tz=UTC) |
| 47 | + parts = partition_pred(partition_tuples(start_date, (now - start_date).days + 1)) |
| 48 | + start_ts = int(start_date.timestamp()) |
| 49 | + |
| 50 | + full_query = f""" |
| 51 | + SELECT DISTINCT "user" |
| 52 | + FROM eventtype_orderrecord |
| 53 | + WHERE ({parts}) AND CAST(ts AS BIGINT) >= {start_ts} |
| 54 | + """ |
| 55 | + |
| 56 | + print(f"Sql get all traders since date:\n {full_query}") |
| 57 | + return full_query |
| 58 | + |
| 59 | + |
| 60 | +def sql_get_user_first_trade_date(users: List[str]) -> str: |
| 61 | + """Get the first trade date for each user.""" |
| 62 | + if not users: |
| 63 | + return "" |
| 64 | + user_list = "', '".join(users) |
| 65 | + |
| 66 | + full_query = f""" |
| 67 | + SELECT |
| 68 | + "user", |
| 69 | + MIN(CAST(ts AS BIGINT)) as first_trade_ts |
| 70 | + FROM eventtype_orderrecord |
| 71 | + WHERE "user" IN ('{user_list}') |
| 72 | + GROUP BY "user" |
| 73 | + """ |
| 74 | + |
| 75 | + print(f"Sql get user first trade date:\n {full_query}") |
| 76 | + return full_query |
| 77 | + |
| 78 | + |
| 79 | +def sql_get_user_last_trade_before_date(users: List[str], before_date: datetime) -> str: |
| 80 | + """Get the last trade date before a given date for each user.""" |
| 81 | + if not users: |
| 82 | + return "" |
| 83 | + |
| 84 | + user_list = "', '".join(users) |
| 85 | + before_ts = int(before_date.timestamp()) |
| 86 | + |
| 87 | + start_lookup = before_date - timedelta(days=15) |
| 88 | + days_lookup = (before_date - start_lookup).days |
| 89 | + parts = partition_pred(partition_tuples(start_lookup, days_lookup)) |
| 90 | + |
| 91 | + full_query = f""" |
| 92 | + SELECT |
| 93 | + "user", |
| 94 | + MAX(CAST(ts AS BIGINT)) as last_trade_ts |
| 95 | + FROM eventtype_orderrecord |
| 96 | + WHERE ({parts}) AND CAST(ts AS BIGINT) < {before_ts} |
| 97 | + AND "user" IN ('{user_list}') |
| 98 | + GROUP BY "user" |
| 99 | + """ |
| 100 | + |
| 101 | + print(f"Sql get user last trade before date:\n {full_query}") |
| 102 | + return full_query |
| 103 | + |
| 104 | + |
| 105 | +def sql_get_user_volume_since_date(users: List[str], start_date: datetime) -> str: |
| 106 | + """Get trading volume for users since a given date.""" |
| 107 | + if not users: |
| 108 | + return "" |
| 109 | + |
| 110 | + user_list = "', '".join(users) |
| 111 | + start_ts = int(start_date.timestamp()) |
| 112 | + now = datetime.now(tz=UTC) |
| 113 | + days_since = (now - start_date).days + 1 |
| 114 | + parts = partition_pred(partition_tuples(start_date, days_since)) |
| 115 | + |
| 116 | + full_query = f""" |
| 117 | + WITH user_trades AS ( |
| 118 | + SELECT |
| 119 | + taker AS user, |
| 120 | + CAST(quoteassetamountfilled AS DOUBLE) / {QUOTE_ASSET_SCALE_FACTOR} AS quote_volume |
| 121 | + FROM eventtype_traderecord |
| 122 | + WHERE ({parts}) AND CAST(ts AS BIGINT) >= {start_ts} |
| 123 | + AND taker IN ('{user_list}') |
| 124 | + UNION ALL |
| 125 | + SELECT |
| 126 | + maker AS user, |
| 127 | + CAST(quoteassetamountfilled AS DOUBLE) / {QUOTE_ASSET_SCALE_FACTOR} AS quote_volume |
| 128 | + FROM eventtype_traderecord |
| 129 | + WHERE ({parts}) AND CAST(ts AS BIGINT) >= {start_ts} |
| 130 | + AND maker IN ('{user_list}') |
| 131 | + ) |
| 132 | + SELECT |
| 133 | + user, |
| 134 | + SUM(quote_volume) AS total_volume |
| 135 | + FROM user_trades |
| 136 | + GROUP BY user |
| 137 | + """ |
| 138 | + |
| 139 | + print(f"Sql get user volume since date:\n {full_query}") |
| 140 | + return full_query |
| 141 | + |
| 142 | + |
| 143 | +async def calculate_wallet_activity(since_date_str: str) -> Dict[str, Any]: |
| 144 | + conn = None |
| 145 | + try: |
| 146 | + since_date = parser.parse(since_date_str).replace(tzinfo=UTC) |
| 147 | + logger.info( |
| 148 | + f"Connecting to Athena. S3 staging: {S3_OUTPUT}, Region: {REGION}, DB: {DATABASE}" |
| 149 | + ) |
| 150 | + conn = connect( |
| 151 | + s3_staging_dir=S3_OUTPUT, region_name=REGION, schema_name=DATABASE |
| 152 | + ) |
| 153 | + logger.info("Successfully connected to Athena.") |
| 154 | + logger.info(f"Getting all traders since {since_date_str}...") |
| 155 | + q_all_traders = sql_get_all_traders_since_date(since_date) |
| 156 | + all_traders_df = pd.read_sql(q_all_traders, conn) |
| 157 | + all_traders = all_traders_df["user"].tolist() |
| 158 | + |
| 159 | + if not all_traders: |
| 160 | + return { |
| 161 | + "analysis_date": since_date_str, |
| 162 | + "new_wallets_count": 0, |
| 163 | + "new_wallets_volume": 0.0, |
| 164 | + "reactivated_wallets_count": 0, |
| 165 | + "reactivated_wallets_volume": 0.0, |
| 166 | + "active_wallets_count": 0, |
| 167 | + "active_wallets_volume": 0.0, |
| 168 | + "total_wallets_count": 0, |
| 169 | + "total_volume": 0.0, |
| 170 | + "wallet_data": [], |
| 171 | + } |
| 172 | + |
| 173 | + logger.info("Getting first trade dates for all traders...") |
| 174 | + q_first_trades = sql_get_user_first_trade_date(all_traders) |
| 175 | + first_trades_df = pd.read_sql(q_first_trades, conn) |
| 176 | + logger.info("Getting last trade dates before analysis date...") |
| 177 | + q_last_trades = sql_get_user_last_trade_before_date(all_traders, since_date) |
| 178 | + last_trades_df = pd.read_sql(q_last_trades, conn) |
| 179 | + logger.info("Calculating volume since analysis date...") |
| 180 | + q_volume = sql_get_user_volume_since_date(all_traders, since_date) |
| 181 | + volume_df = pd.read_sql(q_volume, conn) |
| 182 | + |
| 183 | + result = { |
| 184 | + "analysis_date": since_date_str, |
| 185 | + "volume_df": volume_df.to_dict(orient="records"), |
| 186 | + "first_trades_df": first_trades_df.to_dict(orient="records"), |
| 187 | + "last_trades_df": last_trades_df.to_dict(orient="records"), |
| 188 | + } |
| 189 | + |
| 190 | + logger.info(f"Successfully calculated wallet activity for {since_date_str}.") |
| 191 | + return result |
| 192 | + |
| 193 | + except Exception as e: |
| 194 | + logger.error(f"Error in calculate_wallet_activity: {e}", exc_info=True) |
| 195 | + raise HTTPException( |
| 196 | + status_code=500, detail=f"Failed to process wallet activity data: {str(e)}" |
| 197 | + ) |
| 198 | + finally: |
| 199 | + if conn: |
| 200 | + conn.close() |
| 201 | + logger.info("Athena connection closed.") |
| 202 | + |
| 203 | + |
| 204 | +@router.get("/calculate", response_model=Dict[str, Any]) |
| 205 | +async def get_wallet_activity( |
| 206 | + since_date: str = Query( |
| 207 | + ..., description="The date to analyze wallet activity from (YYYY-MM-DD)." |
| 208 | + ), |
| 209 | +): |
| 210 | + """ |
| 211 | + Analyzes wallet activity since a given date, categorizing wallets into: |
| 212 | + - New Wallets: Wallets that connected for the first time after the given date |
| 213 | + - Reactivated Wallets: Wallets that had no trading activity for more than 15 days prior to the given date, but traded again after |
| 214 | + - Active Wallets: Wallets that did trade within 15 days before the given date, and continued trading after |
| 215 | + """ |
| 216 | + try: |
| 217 | + logger.info(f"Received request for /calculate: since_date='{since_date}'") |
| 218 | + result_data = await calculate_wallet_activity(since_date) |
| 219 | + return result_data |
| 220 | + except HTTPException as http_exc: |
| 221 | + raise http_exc |
| 222 | + except Exception as e: |
| 223 | + logger.error(f"Unhandled error in /calculate endpoint: {e}", exc_info=True) |
| 224 | + raise HTTPException( |
| 225 | + status_code=500, |
| 226 | + detail="An internal server error occurred during calculation.", |
| 227 | + ) |
0 commit comments