gauravlochab commited on
Commit
5f1743b
·
1 Parent(s): 8cfb88e

fix: update graph apr and roi

Browse files
Files changed (1) hide show
  1. app.py +36 -58
app.py CHANGED
@@ -195,6 +195,10 @@ def extract_apr_value(attr: Dict[str, Any]) -> Dict[str, Any]:
195
  if "calculation_metrics" in json_data and json_data["calculation_metrics"] is not None:
196
  roi = json_data["calculation_metrics"].get("f_i_ratio")
197
 
 
 
 
 
198
  logger.debug(f"Agent {agent_id}: Raw APR value: {apr}, adjusted APR value: {adjusted_apr}, ROI value: {roi}, timestamp: {timestamp}")
199
 
200
  # Convert timestamp to datetime if it exists
@@ -302,22 +306,18 @@ def fetch_apr_data_from_db():
302
 
303
  # Process ROI data
304
  if data["roi"] is not None:
305
- # Include all ROI values except extreme outliers
306
- if data["roi"] > -10 and data["roi"] < 10: # Filter extreme outliers
307
- roi_entry = {
308
- "roi": data["roi"],
309
- "timestamp": data["timestamp"],
310
- "agent_id": data["agent_id"],
311
- "agent_name": agent_name,
312
- "is_dummy": False,
313
- "metric_type": "ROI"
314
- }
315
- logger.debug(f"Agent {agent_name} ({attr['agent_id']}): ROI value: {data['roi']}")
316
- # Add to the ROI data list
317
- roi_data_list.append(roi_entry)
318
- else:
319
- # Log that we're skipping extreme outlier values
320
- logger.debug(f"Skipping ROI value for agent {agent_name} ({attr['agent_id']}): {data['roi']} (extreme outlier)")
321
 
322
  logger.info(f"Extracted {len(apr_data_list)} valid APR data points and {len(roi_data_list)} valid ROI data points")
323
 
@@ -1213,28 +1213,17 @@ def create_combined_roi_time_series_graph(df):
1213
  x0=min_time, x1=max_time
1214
  )
1215
 
1216
- # Filter out extreme outliers for better visualization (ROI values above 100% or below -100%)
1217
- outlier_data = df[(df['roi'] > 200) | (df['roi'] < -200)].copy()
1218
- df_filtered = df[(df['roi'] <= 200) & (df['roi'] >= -200)].copy()
1219
-
1220
- # Log the outliers for better debugging
1221
- if len(outlier_data) > 0:
1222
- excluded_count = len(outlier_data)
1223
- logger.info(f"Excluded {excluded_count} data points with outlier ROI values (>200% or <-200%)")
1224
-
1225
- # Group outliers by agent for detailed logging
1226
- outlier_agents = outlier_data.groupby('agent_name')
1227
- for agent_name, agent_outliers in outlier_agents:
1228
- logger.info(f"Agent '{agent_name}' has {len(agent_outliers)} outlier values:")
1229
- for idx, row in agent_outliers.iterrows():
1230
- logger.info(f" - ROI: {row['roi']}, timestamp: {row['timestamp']}")
1231
 
1232
- # Use the filtered data for all subsequent operations
1233
- df = df_filtered
1234
 
1235
- # IMPORTANT: Filter data by hardcoded date range (June 6 to June 19, 2025)
1236
  min_date = datetime(2025, 6, 6)
1237
- max_date = datetime(2025, 6, 19, 23, 59, 59) # Include all of June 19th
1238
  logger.info(f"Filtering ROI data to date range: {min_date} to {max_date}")
1239
 
1240
  # Count data points before filtering
@@ -1420,9 +1409,9 @@ def create_combined_roi_time_series_graph(df):
1420
  title=None # Remove the built-in axis title since we're using annotations
1421
  )
1422
 
1423
- # Update x-axis with better formatting and hardcoded date range (June 6 to June 19)
1424
  min_date = datetime(2025, 6, 6) # Hardcoded start date: June 6, 2025
1425
- max_date = datetime(2025, 6, 19) # Hardcoded end date: June 19, 2025
1426
  logger.info(f"ROI Graph - Hardcoded date range: min_date = {min_date}, max_date = {max_date}")
1427
  fig.update_xaxes(
1428
  showgrid=True,
@@ -1816,28 +1805,17 @@ def create_combined_time_series_graph(df):
1816
  # Filter for APR data only
1817
  apr_data = df[df['metric_type'] == 'APR'].copy()
1818
 
1819
- # Filter out outliers (APR values above 200 or below -200)
1820
- outlier_data = apr_data[(apr_data['apr'] > 200) | (apr_data['apr'] < -200)].copy()
1821
- apr_data_filtered = apr_data[(apr_data['apr'] <= 200) & (apr_data['apr'] >= -200)].copy()
1822
-
1823
- # Log the outliers for better debugging
1824
- if len(outlier_data) > 0:
1825
- excluded_count = len(outlier_data)
1826
- logger.info(f"Excluded {excluded_count} data points with outlier APR values (>200 or <-200)")
1827
-
1828
- # Group outliers by agent for detailed logging
1829
- outlier_agents = outlier_data.groupby('agent_name')
1830
- for agent_name, agent_outliers in outlier_agents:
1831
- logger.info(f"Agent '{agent_name}' has {len(agent_outliers)} outlier values:")
1832
- for idx, row in agent_outliers.iterrows():
1833
- logger.info(f" - APR: {row['apr']}, timestamp: {row['timestamp']}")
1834
 
1835
- # Use the filtered data for all subsequent operations
1836
- apr_data = apr_data_filtered
1837
 
1838
- # IMPORTANT: Filter data by hardcoded date range (June 6 to June 19, 2025)
1839
  min_date = datetime(2025, 6, 6)
1840
- max_date = datetime(2025, 6, 19, 23, 59, 59) # Include all of June 19th
1841
  logger.info(f"Filtering APR data to date range: {min_date} to {max_date}")
1842
 
1843
  # Count data points before filtering
@@ -2145,9 +2123,9 @@ def create_combined_time_series_graph(df):
2145
  title=None # Remove the built-in axis title since we're using annotations
2146
  )
2147
 
2148
- # Update x-axis with better formatting and hardcoded date range (June 6 to June 19)
2149
  min_date = datetime(2025, 6, 6) # Hardcoded start date: June 6, 2025
2150
- max_date = datetime(2025, 6, 19) # Hardcoded end date: June 19, 2025
2151
  logger.info(f"APR Graph - Hardcoded date range: min_date = {min_date}, max_date = {max_date}")
2152
  fig.update_xaxes(
2153
  showgrid=True,
 
195
  if "calculation_metrics" in json_data and json_data["calculation_metrics"] is not None:
196
  roi = json_data["calculation_metrics"].get("f_i_ratio")
197
 
198
+ # Filter ROI values to -10 to 10 range
199
+ if roi is not None and (roi < -10 or roi > 10):
200
+ roi = None # Exclude ROI values outside the range
201
+
202
  logger.debug(f"Agent {agent_id}: Raw APR value: {apr}, adjusted APR value: {adjusted_apr}, ROI value: {roi}, timestamp: {timestamp}")
203
 
204
  # Convert timestamp to datetime if it exists
 
306
 
307
  # Process ROI data
308
  if data["roi"] is not None:
309
+ # Include all ROI values
310
+ roi_entry = {
311
+ "roi": data["roi"],
312
+ "timestamp": data["timestamp"],
313
+ "agent_id": data["agent_id"],
314
+ "agent_name": agent_name,
315
+ "is_dummy": False,
316
+ "metric_type": "ROI"
317
+ }
318
+ logger.debug(f"Agent {agent_name} ({attr['agent_id']}): ROI value: {data['roi']}")
319
+ # Add to the ROI data list
320
+ roi_data_list.append(roi_entry)
 
 
 
 
321
 
322
  logger.info(f"Extracted {len(apr_data_list)} valid APR data points and {len(roi_data_list)} valid ROI data points")
323
 
 
1213
  x0=min_time, x1=max_time
1214
  )
1215
 
1216
+ # Filter ROI outliers for better visualization (±200% range)
1217
+ before_outlier_filter = len(df)
1218
+ df = df[(df['roi'] <= 200) & (df['roi'] >= -200)]
1219
+ after_outlier_filter = len(df)
1220
+ excluded_by_outlier = before_outlier_filter - after_outlier_filter
 
 
 
 
 
 
 
 
 
 
1221
 
1222
+ logger.info(f"ROI outlier filtering: {before_outlier_filter} -> {after_outlier_filter} data points ({excluded_by_outlier} excluded)")
 
1223
 
1224
+ # IMPORTANT: Filter data by hardcoded date range (June 6 to June 22, 2025)
1225
  min_date = datetime(2025, 6, 6)
1226
+ max_date = datetime(2025, 6, 22, 23, 59, 59) # Include all of June 22nd
1227
  logger.info(f"Filtering ROI data to date range: {min_date} to {max_date}")
1228
 
1229
  # Count data points before filtering
 
1409
  title=None # Remove the built-in axis title since we're using annotations
1410
  )
1411
 
1412
+ # Update x-axis with better formatting and hardcoded date range (June 6 to June 22)
1413
  min_date = datetime(2025, 6, 6) # Hardcoded start date: June 6, 2025
1414
+ max_date = datetime(2025, 6, 22) # Hardcoded end date: June 22, 2025
1415
  logger.info(f"ROI Graph - Hardcoded date range: min_date = {min_date}, max_date = {max_date}")
1416
  fig.update_xaxes(
1417
  showgrid=True,
 
1805
  # Filter for APR data only
1806
  apr_data = df[df['metric_type'] == 'APR'].copy()
1807
 
1808
+ # Filter APR outliers (±200% range)
1809
+ before_outlier_filter = len(apr_data)
1810
+ apr_data = apr_data[(apr_data['apr'] <= 200) & (apr_data['apr'] >= -200)]
1811
+ after_outlier_filter = len(apr_data)
1812
+ excluded_by_outlier = before_outlier_filter - after_outlier_filter
 
 
 
 
 
 
 
 
 
 
1813
 
1814
+ logger.info(f"APR outlier filtering: {before_outlier_filter} -> {after_outlier_filter} data points ({excluded_by_outlier} excluded)")
 
1815
 
1816
+ # IMPORTANT: Filter data by hardcoded date range (June 6 to June 22, 2025)
1817
  min_date = datetime(2025, 6, 6)
1818
+ max_date = datetime(2025, 6, 22, 23, 59, 59) # Include all of June 22nd
1819
  logger.info(f"Filtering APR data to date range: {min_date} to {max_date}")
1820
 
1821
  # Count data points before filtering
 
2123
  title=None # Remove the built-in axis title since we're using annotations
2124
  )
2125
 
2126
+ # Update x-axis with better formatting and hardcoded date range (June 6 to June 22)
2127
  min_date = datetime(2025, 6, 6) # Hardcoded start date: June 6, 2025
2128
+ max_date = datetime(2025, 6, 22) # Hardcoded end date: June 22, 2025
2129
  logger.info(f"APR Graph - Hardcoded date range: min_date = {min_date}, max_date = {max_date}")
2130
  fig.update_xaxes(
2131
  showgrid=True,