2
2
3
3
import os
4
4
from datetime import datetime , timedelta , timezone
5
- from typing import Dict , List , Tuple , Set , Optional , Any
5
+ from typing import Callable , Dict , List , Tuple , Set , Optional , Any
6
6
7
7
import pandas as pd
8
8
from dateutil import tz , parser
@@ -84,6 +84,10 @@ class RetentionExplorerItem(BaseModel):
84
84
retention_ratio_14d : float
85
85
retention_ratio_28d : float
86
86
user_data : List [UserVolumeDetails ]
87
+ total_initial_selected_market_volume_7d : float
88
+ total_initial_other_market_volume_7d : float
89
+ total_volume_14d : float
90
+ total_volume_28d : float
87
91
88
92
UTC = tz .tzutc ()
89
93
@@ -142,7 +146,7 @@ def sql_users_volume(
142
146
users : List [str ],
143
147
start_dt : datetime ,
144
148
end_dt : datetime ,
145
- partition_func : callable ,
149
+ partition_func : Callable [[ Set [ Tuple [ str , str , str ]]], str ] ,
146
150
market_index : Optional [int ] = None ,
147
151
exclude_market_index : Optional [int ] = None
148
152
) -> str :
@@ -219,11 +223,15 @@ async def calculate_retention_for_market(market_name: str, start_date_str: str)
219
223
return {
220
224
"market" : market_name , "category" : market_config .get ("category" , []), "start_date" : start_date_str ,
221
225
"new_traders_count" : 0 , "retained_14d_count" : 0 , "retained_28d_count" : 0 ,
222
- "retention_ratio_14d" : 0.0 , "retention_ratio_28d" : 0.0 , "user_data" : []
226
+ "retention_ratio_14d" : 0.0 , "retention_ratio_28d" : 0.0 , "user_data" : [],
227
+ "total_initial_selected_market_volume_7d" : 0.0 ,
228
+ "total_initial_other_market_volume_7d" : 0.0 ,
229
+ "total_volume_14d" : 0.0 ,
230
+ "total_volume_28d" : 0.0 ,
223
231
}
224
232
225
233
# --- Volume Calculation ---
226
- traders_df = pd .DataFrame (mkt_traders , columns = [ ' user' ] )
234
+ traders_df = pd .DataFrame ({ " user" : mkt_traders } )
227
235
228
236
# Define time windows
229
237
initial_window_end = start_date + timedelta (days = NEW_TRADER_WINDOW_DAYS )
@@ -240,6 +248,9 @@ def get_and_merge_volume(
240
248
241
249
vol_df = pd .read_sql (vol_sql , conn )
242
250
merged_df = base_df .merge (vol_df , on = 'user' , how = 'left' )
251
+
252
+ # Explicitly convert to numeric to avoid FutureWarning on downcasting
253
+ merged_df ['total_volume' ] = pd .to_numeric (merged_df ['total_volume' ], errors = 'coerce' )
243
254
merged_df ['total_volume' ] = merged_df ['total_volume' ].fillna (0 )
244
255
return merged_df .rename (columns = {'total_volume' : column_name })
245
256
@@ -253,6 +264,12 @@ def get_and_merge_volume(
253
264
254
265
traders_df = traders_df .rename (columns = {'user' : 'user_address' })
255
266
267
+ # --- Summary Volume Metrics ---
268
+ total_initial_selected_market_volume_7d = traders_df ['initial_selected_market_volume' ].sum ()
269
+ total_initial_other_market_volume_7d = traders_df ['initial_other_market_volume' ].sum ()
270
+ total_volume_14d = (traders_df ['selected_market_volume_14d' ] + traders_df ['other_market_volume_14d' ]).sum ()
271
+ total_volume_28d = (traders_df ['selected_market_volume_28d' ] + traders_df ['other_market_volume_28d' ]).sum ()
272
+
256
273
# --- Summary Statistics ---
257
274
retained_14d_count = int ((traders_df ['other_market_volume_14d' ] > 0 ).sum ())
258
275
retained_28d_count = int ((traders_df ['other_market_volume_28d' ] > 0 ).sum ())
@@ -268,7 +285,11 @@ def get_and_merge_volume(
268
285
"retained_28d_count" : retained_28d_count ,
269
286
"retention_ratio_14d" : round (retention_ratio_14d , 4 ),
270
287
"retention_ratio_28d" : round (retention_ratio_28d , 4 ),
271
- "user_data" : traders_df .to_dict ('records' )
288
+ "user_data" : traders_df .to_dict ('records' ),
289
+ "total_initial_selected_market_volume_7d" : float (total_initial_selected_market_volume_7d ),
290
+ "total_initial_other_market_volume_7d" : float (total_initial_other_market_volume_7d ),
291
+ "total_volume_14d" : float (total_volume_14d ),
292
+ "total_volume_28d" : float (total_volume_28d ),
272
293
}
273
294
274
295
logger .info (f"Successfully calculated consolidated retention for { market_name } ." )
0 commit comments