Spaces:
Running
Running
File size: 51,141 Bytes
322b74c 07d589f 322b74c e737d2f 322b74c bffbc7a b8d3277 bffbc7a e737d2f bffbc7a e737d2f 3663584 e737d2f 3663584 e737d2f 3663584 cba6d8a e737d2f 3663584 e737d2f 3663584 e737d2f 3663584 e737d2f 3663584 e737d2f 3663584 e737d2f 3663584 e737d2f 3663584 e737d2f 3663584 e737d2f 3663584 e737d2f 3663584 e737d2f 3663584 e737d2f 3663584 e737d2f 3663584 cba6d8a e737d2f cba6d8a 3663584 e737d2f 3663584 e737d2f 3663584 cba6d8a e737d2f 3663584 e737d2f 3663584 e737d2f 3663584 e737d2f 3663584 e737d2f 3663584 e737d2f 3663584 e737d2f 3663584 e737d2f 3663584 e737d2f 3663584 e737d2f 3663584 e737d2f 3663584 e737d2f cba6d8a 3663584 cba6d8a e737d2f 3663584 e737d2f 31508e9 e737d2f 31508e9 e737d2f 31508e9 e737d2f 31508e9 e737d2f 31508e9 e737d2f 9408331 3663584 cba6d8a 9408331 3663584 cba6d8a 3663584 cba6d8a 044e2c9 e737d2f 9408331 e737d2f 044e2c9 9408331 e737d2f 9408331 e737d2f 9408331 e737d2f 044e2c9 e737d2f 9408331 044e2c9 e737d2f 044e2c9 e737d2f 3663584 e737d2f 3663584 e737d2f 044e2c9 9408331 044e2c9 9408331 044e2c9 e737d2f 044e2c9 9408331 044e2c9 9408331 044e2c9 e737d2f bffbc7a b8d3277 39b4e26 b8d3277 322b74c b8d3277 322b74c b8d3277 322b74c b8d3277 bffbc7a 322b74c bffbc7a 322b74c bffbc7a 322b74c bffbc7a 322b74c bffbc7a b8d3277 322b74c bffbc7a 322b74c b8d3277 322b74c c744bf3 322b74c 39b4e26 b8d3277 322b74c b8d3277 322b74c b8d3277 322b74c b8d3277 322b74c 27431d4 322b74c b8d3277 27431d4 b8d3277 322b74c b8d3277 322b74c 27431d4 322b74c b8d3277 27431d4 b8d3277 322b74c b8d3277 322b74c b8d3277 13dff28 322b74c 13dff28 b8d3277 13dff28 322b74c 5b3ed4c b8d3277 322b74c 9a26d72 5b3ed4c 13dff28 322b74c 5b3ed4c 322b74c 9a26d72 b8d3277 27431d4 322b74c 9a26d72 322b74c c744bf3 bffbc7a 322b74c 3663584 322b74c bffbc7a 322b74c bffbc7a 31508e9 bffbc7a 31508e9 3663584 bffbc7a 3663584 bffbc7a 31508e9 bffbc7a 31508e9 bffbc7a 3663584 b8d3277 322b74c e737d2f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 |
import requests
import pandas as pd
import gradio as gr
import plotly.graph_objects as go
import plotly.express as px
from plotly.subplots import make_subplots
from datetime import datetime, timedelta
import json
# Commenting out blockchain-related imports that cause loading issues
# from web3 import Web3
import os
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import random
import logging
from typing import List, Dict, Any
# Comment out the import for now and replace with dummy functions
# from app_trans_new import create_transcation_visualizations,create_active_agents_visualizations
# APR visualization functions integrated directly
# Set up more detailed logging
logging.basicConfig(
level=logging.DEBUG, # Change to DEBUG for more detailed logs
format="%(asctime)s - %(levelname)s - %(message)s",
handlers=[
logging.FileHandler("app_debug.log"), # Log to file for persistence
logging.StreamHandler() # Also log to console
]
)
logger = logging.getLogger(__name__)
# Log the startup information
logger.info("============= APPLICATION STARTING =============")
logger.info(f"Running from directory: {os.getcwd()}")
# Global variable to store the data for reuse
global_df = None
# Configuration
API_BASE_URL = "https://afmdb.autonolas.tech"
logger.info(f"Using API endpoint: {API_BASE_URL}")
def get_agent_type_by_name(type_name: str) -> Dict[str, Any]:
"""Get agent type by name"""
url = f"{API_BASE_URL}/api/agent-types/name/{type_name}"
logger.debug(f"Calling API: {url}")
try:
response = requests.get(url)
logger.debug(f"Response status: {response.status_code}")
if response.status_code == 404:
logger.error(f"Agent type '{type_name}' not found")
return None
response.raise_for_status()
result = response.json()
logger.debug(f"Agent type response: {result}")
return result
except Exception as e:
logger.error(f"Error in get_agent_type_by_name: {e}")
return None
def get_attribute_definition_by_name(attr_name: str) -> Dict[str, Any]:
"""Get attribute definition by name"""
url = f"{API_BASE_URL}/api/attributes/name/{attr_name}"
logger.debug(f"Calling API: {url}")
try:
response = requests.get(url)
logger.debug(f"Response status: {response.status_code}")
if response.status_code == 404:
logger.error(f"Attribute definition '{attr_name}' not found")
return None
response.raise_for_status()
result = response.json()
logger.debug(f"Attribute definition response: {result}")
return result
except Exception as e:
logger.error(f"Error in get_attribute_definition_by_name: {e}")
return None
def get_agents_by_type(type_id: int) -> List[Dict[str, Any]]:
"""Get all agents of a specific type"""
url = f"{API_BASE_URL}/api/agent-types/{type_id}/agents/"
logger.debug(f"Calling API: {url}")
try:
response = requests.get(url)
logger.debug(f"Response status: {response.status_code}")
if response.status_code == 404:
logger.error(f"No agents found for type ID {type_id}")
return []
response.raise_for_status()
result = response.json()
logger.debug(f"Agents count: {len(result)}")
logger.debug(f"First few agents: {result[:2] if result else []}")
return result
except Exception as e:
logger.error(f"Error in get_agents_by_type: {e}")
return []
def get_attribute_values_by_type_and_attr(agents: List[Dict[str, Any]], attr_def_id: int) -> List[Dict[str, Any]]:
"""Get all attribute values for a specific attribute definition across all agents of a given list"""
all_attributes = []
logger.debug(f"Getting attributes for {len(agents)} agents with attr_def_id: {attr_def_id}")
# For each agent, get their attributes and filter for the one we want
for agent in agents:
agent_id = agent["agent_id"]
# Call the /api/agents/{agent_id}/attributes/ endpoint
url = f"{API_BASE_URL}/api/agents/{agent_id}/attributes/"
logger.debug(f"Calling API for agent {agent_id}: {url}")
try:
response = requests.get(url, params={"limit": 1000})
if response.status_code == 404:
logger.error(f"No attributes found for agent ID {agent_id}")
continue
response.raise_for_status()
agent_attrs = response.json()
logger.debug(f"Agent {agent_id} has {len(agent_attrs)} attributes")
# Filter for the specific attribute definition ID
filtered_attrs = [attr for attr in agent_attrs if attr.get("attr_def_id") == attr_def_id]
logger.debug(f"Agent {agent_id} has {len(filtered_attrs)} APR attributes")
if filtered_attrs:
logger.debug(f"Sample attribute for agent {agent_id}: {filtered_attrs[0]}")
all_attributes.extend(filtered_attrs)
except requests.exceptions.RequestException as e:
logger.error(f"Error fetching attributes for agent ID {agent_id}: {e}")
logger.info(f"Total APR attributes found across all agents: {len(all_attributes)}")
return all_attributes
def get_agent_name(agent_id: int, agents: List[Dict[str, Any]]) -> str:
"""Get agent name from agent ID"""
for agent in agents:
if agent["agent_id"] == agent_id:
return agent["agent_name"]
return "Unknown"
def extract_apr_value(attr: Dict[str, Any]) -> Dict[str, Any]:
"""Extract APR value and timestamp from JSON value"""
try:
agent_id = attr.get("agent_id", "unknown")
logger.debug(f"Extracting APR value for agent {agent_id}")
# The APR value is stored in the json_value field
if attr["json_value"] is None:
logger.debug(f"Agent {agent_id}: json_value is None")
return {"apr": None, "timestamp": None, "agent_id": agent_id, "is_dummy": False}
# If json_value is a string, parse it
if isinstance(attr["json_value"], str):
logger.debug(f"Agent {agent_id}: json_value is string, parsing")
json_data = json.loads(attr["json_value"])
else:
json_data = attr["json_value"]
apr = json_data.get("apr")
timestamp = json_data.get("timestamp")
logger.debug(f"Agent {agent_id}: Raw APR value: {apr}, timestamp: {timestamp}")
# Convert timestamp to datetime if it exists
timestamp_dt = None
if timestamp:
timestamp_dt = datetime.fromtimestamp(timestamp)
result = {"apr": apr, "timestamp": timestamp_dt, "agent_id": agent_id, "is_dummy": False}
logger.debug(f"Agent {agent_id}: Extracted result: {result}")
return result
except (json.JSONDecodeError, KeyError, TypeError) as e:
logger.error(f"Error parsing JSON value: {e} for agent_id: {attr.get('agent_id')}")
logger.error(f"Problematic json_value: {attr.get('json_value')}")
return {"apr": None, "timestamp": None, "agent_id": attr.get('agent_id'), "is_dummy": False}
def fetch_apr_data_from_db():
"""
Fetch APR data from database using the API.
"""
global global_df
logger.info("==== Starting APR data fetch ====")
try:
# Step 1: Find the Modius agent type
logger.info("Finding Modius agent type")
modius_type = get_agent_type_by_name("Modius")
if not modius_type:
logger.error("Modius agent type not found, using placeholder data")
global_df = pd.DataFrame([])
return global_df
type_id = modius_type["type_id"]
logger.info(f"Found Modius agent type with ID: {type_id}")
# Step 2: Find the APR attribute definition
logger.info("Finding APR attribute definition")
apr_attr_def = get_attribute_definition_by_name("APR")
if not apr_attr_def:
logger.error("APR attribute definition not found, using placeholder data")
global_df = pd.DataFrame([])
return global_df
attr_def_id = apr_attr_def["attr_def_id"]
logger.info(f"Found APR attribute definition with ID: {attr_def_id}")
# Step 3: Get all agents of type Modius
logger.info(f"Getting all agents of type Modius (type_id: {type_id})")
modius_agents = get_agents_by_type(type_id)
if not modius_agents:
logger.error("No agents of type 'Modius' found")
global_df = pd.DataFrame([])
return global_df
logger.info(f"Found {len(modius_agents)} Modius agents")
logger.debug(f"Modius agents: {[{'agent_id': a['agent_id'], 'agent_name': a['agent_name']} for a in modius_agents]}")
# Step 4: Fetch all APR values for Modius agents
logger.info(f"Fetching APR values for all Modius agents (attr_def_id: {attr_def_id})")
apr_attributes = get_attribute_values_by_type_and_attr(modius_agents, attr_def_id)
if not apr_attributes:
logger.error("No APR values found for 'Modius' agents")
global_df = pd.DataFrame([])
return global_df
logger.info(f"Found {len(apr_attributes)} APR attributes total")
# Step 5: Extract APR data
logger.info("Extracting APR data from attributes")
apr_data_list = []
for attr in apr_attributes:
apr_data = extract_apr_value(attr)
if apr_data["apr"] is not None and apr_data["timestamp"] is not None:
# Get agent name
agent_name = get_agent_name(attr["agent_id"], modius_agents)
# Add agent name to the data
apr_data["agent_name"] = agent_name
# Add is_dummy flag (all real data)
apr_data["is_dummy"] = False
# Mark negative values as "Performance" metrics
if apr_data["apr"] < 0:
apr_data["metric_type"] = "Performance"
logger.debug(f"Agent {agent_name} ({attr['agent_id']}): Performance value: {apr_data['apr']}")
else:
apr_data["metric_type"] = "APR"
logger.debug(f"Agent {agent_name} ({attr['agent_id']}): APR value: {apr_data['apr']}")
apr_data_list.append(apr_data)
# Convert list of dictionaries to DataFrame
if not apr_data_list:
logger.error("No valid APR data extracted")
global_df = pd.DataFrame([])
return global_df
global_df = pd.DataFrame(apr_data_list)
# Log the resulting dataframe
logger.info(f"Created DataFrame with {len(global_df)} rows")
logger.info(f"DataFrame columns: {global_df.columns.tolist()}")
logger.info(f"APR statistics: min={global_df['apr'].min()}, max={global_df['apr'].max()}, mean={global_df['apr'].mean()}")
logger.info(f"Metric types count: {global_df['metric_type'].value_counts().to_dict()}")
logger.info(f"Agents count: {global_df['agent_name'].value_counts().to_dict()}")
# Log the entire dataframe for debugging
logger.debug("Final DataFrame contents:")
for idx, row in global_df.iterrows():
logger.debug(f"Row {idx}: {row.to_dict()}")
return global_df
except requests.exceptions.RequestException as e:
logger.error(f"API request error: {e}")
global_df = pd.DataFrame([])
return global_df
except Exception as e:
logger.error(f"Error fetching APR data: {e}")
logger.exception("Exception details:")
global_df = pd.DataFrame([])
return global_df
def generate_apr_visualizations():
"""Generate APR visualizations with real data only (no dummy data)"""
global global_df
# Fetch data from database
df = fetch_apr_data_from_db()
# If we got no data at all, return placeholder figures
if df.empty:
logger.info("No APR data available. Using fallback visualization.")
# Create empty visualizations with a message using Plotly
fig = go.Figure()
fig.add_annotation(
x=0.5, y=0.5,
text="No APR data available",
font=dict(size=20),
showarrow=False
)
fig.update_layout(
xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
yaxis=dict(showgrid=False, zeroline=False, showticklabels=False)
)
# Save as static file for reference
fig.write_html("modius_apr_combined_graph.html")
fig.write_image("modius_apr_combined_graph.png")
csv_file = None
return fig, csv_file
# No longer generating dummy data
# Set global_df for access by other functions
global_df = df
# Save to CSV before creating visualizations
csv_file = save_to_csv(df)
# Only create combined time series graph
combined_fig = create_combined_time_series_graph(df)
return combined_fig, csv_file
def create_time_series_graph_per_agent(df):
"""Create a time series graph for each agent using Plotly"""
# Get unique agents
unique_agents = df['agent_id'].unique()
if len(unique_agents) == 0:
logger.error("No agent data to plot")
fig = go.Figure()
fig.add_annotation(
text="No agent data available",
x=0.5, y=0.5,
showarrow=False, font=dict(size=20)
)
return fig
# Create a subplot figure for each agent
fig = make_subplots(rows=len(unique_agents), cols=1,
subplot_titles=[f"Agent: {df[df['agent_id'] == agent_id]['agent_name'].iloc[0]}"
for agent_id in unique_agents],
vertical_spacing=0.1)
# Plot data for each agent
for i, agent_id in enumerate(unique_agents):
agent_data = df[df['agent_id'] == agent_id].copy()
agent_name = agent_data['agent_name'].iloc[0]
row = i + 1
# Add zero line to separate APR and Performance
fig.add_shape(
type="line", line=dict(dash="solid", width=1.5, color="black"),
y0=0, y1=0, x0=agent_data['timestamp'].min(), x1=agent_data['timestamp'].max(),
row=row, col=1
)
# Add background colors
fig.add_shape(
type="rect", fillcolor="rgba(230, 243, 255, 0.3)", line=dict(width=0),
y0=0, y1=1000, x0=agent_data['timestamp'].min(), x1=agent_data['timestamp'].max(),
row=row, col=1, layer="below"
)
fig.add_shape(
type="rect", fillcolor="rgba(255, 230, 230, 0.3)", line=dict(width=0),
y0=-1000, y1=0, x0=agent_data['timestamp'].min(), x1=agent_data['timestamp'].max(),
row=row, col=1, layer="below"
)
# Create separate dataframes for different data types
apr_data = agent_data[agent_data['metric_type'] == 'APR']
perf_data = agent_data[agent_data['metric_type'] == 'Performance']
# Sort all data by timestamp for the line plots
combined_agent_data = agent_data.sort_values('timestamp')
# Add main line connecting all points
fig.add_trace(
go.Scatter(
x=combined_agent_data['timestamp'],
y=combined_agent_data['apr'],
mode='lines',
line=dict(color='purple', width=2),
name=f'{agent_name}',
legendgroup=agent_name,
showlegend=(i == 0), # Only show in legend once
hovertemplate='Time: %{x}<br>Value: %{y:.2f}<extra></extra>'
),
row=row, col=1
)
# Add scatter points for APR values
if not apr_data.empty:
fig.add_trace(
go.Scatter(
x=apr_data['timestamp'],
y=apr_data['apr'],
mode='markers',
marker=dict(color='blue', size=10, symbol='circle'),
name='APR',
legendgroup='APR',
showlegend=(i == 0),
hovertemplate='Time: %{x}<br>APR: %{y:.2f}<extra></extra>'
),
row=row, col=1
)
# Add scatter points for Performance values
if not perf_data.empty:
fig.add_trace(
go.Scatter(
x=perf_data['timestamp'],
y=perf_data['apr'],
mode='markers',
marker=dict(color='red', size=10, symbol='square'),
name='Performance',
legendgroup='Performance',
showlegend=(i == 0),
hovertemplate='Time: %{x}<br>Performance: %{y:.2f}<extra></extra>'
),
row=row, col=1
)
# Update axes
fig.update_xaxes(title_text="Time", row=row, col=1)
fig.update_yaxes(title_text="Value", row=row, col=1, gridcolor='rgba(0,0,0,0.1)')
# Update layout
fig.update_layout(
height=400 * len(unique_agents),
width=1000,
title_text="APR and Performance Values per Agent",
template="plotly_white",
legend=dict(
orientation="h",
yanchor="bottom",
y=1.02,
xanchor="right",
x=1
),
margin=dict(r=20, l=20, t=30, b=20),
hovermode="closest"
)
# Save the figure (still useful for reference)
graph_file = "modius_apr_per_agent_graph.html"
fig.write_html(graph_file, include_plotlyjs='cdn', full_html=False)
# Also save as image for compatibility
img_file = "modius_apr_per_agent_graph.png"
fig.write_image(img_file)
logger.info(f"Per-agent graph saved to {graph_file} and {img_file}")
# Return the figure object for direct use in Gradio
return fig
def create_combined_time_series_graph(df):
"""Create a combined time series graph for all agents using Plotly"""
if len(df) == 0:
logger.error("No data to plot combined graph")
fig = go.Figure()
fig.add_annotation(
text="No data available",
x=0.5, y=0.5,
showarrow=False, font=dict(size=20)
)
return fig
# IMPORTANT: Force data types to ensure consistency
df['apr'] = df['apr'].astype(float) # Ensure APR is float
df['metric_type'] = df['metric_type'].astype(str) # Ensure metric_type is string
# CRITICAL: Log the exact dataframe we're using for plotting to help debug
logger.info(f"Graph data - shape: {df.shape}, columns: {df.columns}")
logger.info(f"Graph data - unique agents: {df['agent_name'].unique().tolist()}")
logger.info(f"Graph data - unique metric types: {df['metric_type'].unique().tolist()}")
logger.info(f"Graph data - min APR: {df['apr'].min()}, max APR: {df['apr'].max()}")
# Export full dataframe to CSV for debugging
debug_csv = "debug_graph_data.csv"
df.to_csv(debug_csv)
logger.info(f"Exported graph data to {debug_csv} for debugging")
# Write detailed data report
with open("debug_graph_data_report.txt", "w") as f:
f.write("==== GRAPH DATA REPORT ====\n\n")
f.write(f"Total data points: {len(df)}\n")
f.write(f"Timestamp range: {df['timestamp'].min()} to {df['timestamp'].max()}\n\n")
# Output per-agent details
unique_agents = df['agent_id'].unique()
f.write(f"Number of agents: {len(unique_agents)}\n\n")
for agent_id in unique_agents:
agent_data = df[df['agent_id'] == agent_id]
agent_name = agent_data['agent_name'].iloc[0]
f.write(f"== Agent: {agent_name} (ID: {agent_id}) ==\n")
f.write(f" Total data points: {len(agent_data)}\n")
apr_data = agent_data[agent_data['metric_type'] == 'APR']
perf_data = agent_data[agent_data['metric_type'] == 'Performance']
f.write(f" APR data points: {len(apr_data)}\n")
f.write(f" Performance data points: {len(perf_data)}\n")
if not apr_data.empty:
f.write(f" APR values: {apr_data['apr'].tolist()}\n")
f.write(f" APR timestamps: {[ts.strftime('%Y-%m-%d %H:%M:%S') if ts is not None else 'None' for ts in apr_data['timestamp']]}\n")
if not perf_data.empty:
f.write(f" Performance values: {perf_data['apr'].tolist()}\n")
f.write(f" Performance timestamps: {[ts.strftime('%Y-%m-%d %H:%M:%S') if ts is not None else 'None' for ts in perf_data['timestamp']]}\n")
f.write("\n")
logger.info("Generated detailed graph data report")
# ENSURE THERE ARE NO CONFLICTING AXES OR TRACES
# Create Plotly figure in a clean state
fig = go.Figure()
# Get unique agents
unique_agents = df['agent_id'].unique()
colors = px.colors.qualitative.Plotly[:len(unique_agents)]
# IMPORTANT: Fixed y-axis range that always includes -100
min_apr = -110 # Fixed minimum to ensure -100 is visible
max_apr = 110 # Fixed maximum to ensure data is visible
# Add background shapes for APR and Performance regions
min_time = df['timestamp'].min()
max_time = df['timestamp'].max()
# Add shape for APR region (above zero)
fig.add_shape(
type="rect",
fillcolor="rgba(230, 243, 255, 0.3)",
line=dict(width=0),
y0=0, y1=max_apr,
x0=min_time, x1=max_time,
layer="below"
)
# Add shape for Performance region (below zero)
fig.add_shape(
type="rect",
fillcolor="rgba(255, 230, 230, 0.3)",
line=dict(width=0),
y0=min_apr, y1=0,
x0=min_time, x1=max_time,
layer="below"
)
# Add zero line
fig.add_shape(
type="line",
line=dict(dash="solid", width=1.5, color="black"),
y0=0, y1=0,
x0=min_time, x1=max_time
)
# MODIFIED: Changed order of trace addition - add markers first, then lines
# Add data for each agent
for i, agent_id in enumerate(unique_agents):
agent_data = df[df['agent_id'] == agent_id].copy()
agent_name = agent_data['agent_name'].iloc[0]
color = colors[i % len(colors)]
# Sort the data by timestamp
agent_data = agent_data.sort_values('timestamp')
# Log actual points being plotted for this agent
logger.info(f"Plotting agent: {agent_name} (ID: {agent_id}) with {len(agent_data)} points")
for idx, row in agent_data.iterrows():
logger.info(f" Point {idx}: timestamp={row['timestamp']}, apr={row['apr']}, type={row['metric_type']}")
# First add scatter points for Performance values
perf_data = agent_data[agent_data['metric_type'] == 'Performance']
if not perf_data.empty:
logger.info(f" Adding {len(perf_data)} Performance markers for {agent_name}")
for idx, row in perf_data.iterrows():
logger.info(f" Performance marker: timestamp={row['timestamp']}, apr={row['apr']}")
# Use explicit Python boolean for showlegend
is_first_point = bool(idx == perf_data.index[0])
fig.add_trace(
go.Scatter(
x=[row['timestamp']],
y=[row['apr']],
mode='markers',
marker=dict(
color='red', # Force consistent color
symbol='square',
size=16, # Make markers larger
line=dict(
width=2,
color='black'
)
),
name=f'{agent_name} Perf',
legendgroup=agent_name,
showlegend=is_first_point, # Use native Python boolean
hovertemplate='Time: %{x}<br>Performance: %{y:.2f}<br>Agent: ' + agent_name + '<extra></extra>'
)
)
# Now add scatter points for APR values
apr_data = agent_data[agent_data['metric_type'] == 'APR']
if not apr_data.empty:
logger.info(f" Adding {len(apr_data)} APR markers for {agent_name}")
for idx, row in apr_data.iterrows():
logger.info(f" APR marker: timestamp={row['timestamp']}, apr={row['apr']}")
# Use explicit Python boolean for showlegend
is_first_point = bool(idx == apr_data.index[0])
fig.add_trace(
go.Scatter(
x=[row['timestamp']],
y=[row['apr']],
mode='markers',
marker=dict(
color='blue', # Force consistent color
symbol='circle',
size=14, # Make markers larger
line=dict(
width=2,
color='black'
)
),
name=f'{agent_name} APR',
legendgroup=agent_name,
showlegend=is_first_point, # Use native Python boolean
hovertemplate='Time: %{x}<br>APR: %{y:.2f}<br>Agent: ' + agent_name + '<extra></extra>'
)
)
# Add the combined line AFTER markers for both APR and Performance
fig.add_trace(
go.Scatter(
x=agent_data['timestamp'],
y=agent_data['apr'],
mode='lines',
line=dict(color=color, width=2),
name=f'{agent_name}',
legendgroup=agent_name,
hovertemplate='Time: %{x}<br>Value: %{y:.2f}<br>Agent: ' + agent_name + '<extra></extra>'
)
)
# Update layout - use simple boolean values everywhere
fig.update_layout(
title="APR and Performance Values for All Agents",
xaxis_title="Time",
yaxis_title="Value",
template="plotly_white",
height=600,
width=1000,
legend=dict(
orientation="h",
yanchor="bottom",
y=1.02,
xanchor="right",
x=1,
groupclick="toggleitem"
),
margin=dict(r=20, l=20, t=30, b=20),
hovermode="closest"
)
# FORCE FIXED Y-AXIS RANGE
fig.update_yaxes(
showgrid=True,
gridwidth=1,
gridcolor='rgba(0,0,0,0.1)',
range=[min_apr, max_apr], # Fixed range
tickmode='linear',
tick0=-100,
dtick=50
)
# Update x-axis
fig.update_xaxes(
showgrid=True,
gridwidth=1,
gridcolor='rgba(0,0,0,0.1)'
)
# SIMPLIFIED APPROACH: Do a direct plot without markers for comparison
# This creates a simple, reliable fallback plot if the advanced one fails
try:
# Save the figure (still useful for reference)
graph_file = "modius_apr_combined_graph.html"
fig.write_html(graph_file, include_plotlyjs='cdn', full_html=False)
# Also save as image for compatibility
img_file = "modius_apr_combined_graph.png"
try:
fig.write_image(img_file)
logger.info(f"Combined graph saved to {graph_file} and {img_file}")
except Exception as e:
logger.error(f"Error saving image: {e}")
logger.info(f"Combined graph saved to {graph_file} only")
# Return the figure object for direct use in Gradio
return fig
except Exception as e:
# If the complex graph approach fails, create a simpler one
logger.error(f"Error creating advanced graph: {e}")
logger.info("Falling back to simpler graph")
# Create a simpler graph as fallback
simple_fig = go.Figure()
# Add zero line
simple_fig.add_shape(
type="line",
line=dict(dash="solid", width=1.5, color="black"),
y0=0, y1=0,
x0=min_time, x1=max_time
)
# Simply plot each agent's data as a line with markers
for i, agent_id in enumerate(unique_agents):
agent_data = df[df['agent_id'] == agent_id].copy()
agent_name = agent_data['agent_name'].iloc[0]
color = colors[i % len(colors)]
# Sort by timestamp
agent_data = agent_data.sort_values('timestamp')
# Add a single trace with markers+lines
simple_fig.add_trace(
go.Scatter(
x=agent_data['timestamp'],
y=agent_data['apr'],
mode='lines+markers',
name=agent_name,
marker=dict(size=10),
line=dict(width=2)
)
)
# Simplified layout
simple_fig.update_layout(
title="APR and Performance Values (Simplified View)",
xaxis_title="Time",
yaxis_title="Value",
yaxis=dict(range=[-110, 110]),
height=600,
width=1000
)
# Return the simple figure
return simple_fig
def save_to_csv(df):
"""Save the APR data DataFrame to a CSV file and return the file path"""
if df.empty:
logger.error("No APR data to save to CSV")
return None
# Define the CSV file path
csv_file = "modius_apr_values.csv"
# Save to CSV
df.to_csv(csv_file, index=False)
logger.info(f"APR data saved to {csv_file}")
# Also generate a statistics CSV file
stats_df = generate_statistics_from_data(df)
stats_csv = "modius_apr_statistics.csv"
stats_df.to_csv(stats_csv, index=False)
logger.info(f"Statistics saved to {stats_csv}")
return csv_file
def generate_statistics_from_data(df):
"""Generate statistics from the APR data"""
if df.empty:
return pd.DataFrame()
# Get unique agents
unique_agents = df['agent_id'].unique()
stats_list = []
# Generate per-agent statistics
for agent_id in unique_agents:
agent_data = df[df['agent_id'] == agent_id]
agent_name = agent_data['agent_name'].iloc[0]
# APR statistics
apr_data = agent_data[agent_data['metric_type'] == 'APR']
real_apr = apr_data[apr_data['is_dummy'] == False]
# Performance statistics
perf_data = agent_data[agent_data['metric_type'] == 'Performance']
real_perf = perf_data[perf_data['is_dummy'] == False]
stats = {
'agent_id': agent_id,
'agent_name': agent_name,
'total_points': len(agent_data),
'apr_points': len(apr_data),
'performance_points': len(perf_data),
'real_apr_points': len(real_apr),
'real_performance_points': len(real_perf),
'avg_apr': apr_data['apr'].mean() if not apr_data.empty else None,
'avg_performance': perf_data['apr'].mean() if not perf_data.empty else None,
'max_apr': apr_data['apr'].max() if not apr_data.empty else None,
'min_apr': apr_data['apr'].min() if not apr_data.empty else None,
'latest_timestamp': agent_data['timestamp'].max().strftime('%Y-%m-%d %H:%M:%S') if not agent_data.empty else None
}
stats_list.append(stats)
# Generate overall statistics
apr_only = df[df['metric_type'] == 'APR']
perf_only = df[df['metric_type'] == 'Performance']
overall_stats = {
'agent_id': 'ALL',
'agent_name': 'All Agents',
'total_points': len(df),
'apr_points': len(apr_only),
'performance_points': len(perf_only),
'real_apr_points': len(apr_only[apr_only['is_dummy'] == False]),
'real_performance_points': len(perf_only[perf_only['is_dummy'] == False]),
'avg_apr': apr_only['apr'].mean() if not apr_only.empty else None,
'avg_performance': perf_only['apr'].mean() if not perf_only.empty else None,
'max_apr': apr_only['apr'].max() if not apr_only.empty else None,
'min_apr': apr_only['apr'].min() if not apr_only.empty else None,
'latest_timestamp': df['timestamp'].max().strftime('%Y-%m-%d %H:%M:%S') if not df.empty else None
}
stats_list.append(overall_stats)
return pd.DataFrame(stats_list)
# Create dummy functions for the commented out imports
def create_transcation_visualizations():
"""Dummy implementation that returns a placeholder graph"""
fig = go.Figure()
fig.add_annotation(
text="Blockchain data loading disabled - placeholder visualization",
x=0.5, y=0.5, xref="paper", yref="paper",
showarrow=False, font=dict(size=20)
)
return fig
def create_active_agents_visualizations():
"""Dummy implementation that returns a placeholder graph"""
fig = go.Figure()
fig.add_annotation(
text="Blockchain data loading disabled - placeholder visualization",
x=0.5, y=0.5, xref="paper", yref="paper",
showarrow=False, font=dict(size=20)
)
return fig
# Comment out the blockchain connection code
"""
# Load environment variables from .env file
# RPC URLs
OPTIMISM_RPC_URL = os.getenv('OPTIMISM_RPC_URL')
MODE_RPC_URL = os.getenv('MODE_RPC_URL')
# Initialize Web3 instances
web3_instances = {
'optimism': Web3(Web3.HTTPProvider(OPTIMISM_RPC_URL)),
'mode': Web3(Web3.HTTPProvider(MODE_RPC_URL))
}
# Contract addresses for service registries
contract_addresses = {
'optimism': '0x3d77596beb0f130a4415df3D2D8232B3d3D31e44',
'mode': '0x3C1fF68f5aa342D296d4DEe4Bb1cACCA912D95fE'
}
# Load the ABI from the provided JSON file
with open('./contracts/service_registry_abi.json', 'r') as abi_file:
contract_abi = json.load(abi_file)
# Create the contract instances
service_registries = {
chain_name: web3.eth.contract(address=contract_addresses[chain_name], abi=contract_abi)
for chain_name, web3 in web3_instances.items()
}
# Check if connections are successful
for chain_name, web3_instance in web3_instances.items():
if not web3_instance.is_connected():
raise Exception(f"Failed to connect to the {chain_name.capitalize()} network.")
else:
print(f"Successfully connected to the {chain_name.capitalize()} network.")
"""
# Dummy blockchain functions to replace the commented ones
def get_transfers(integrator: str, wallet: str) -> str:
"""Dummy function that returns an empty result"""
return {"transfers": []}
def fetch_and_aggregate_transactions():
"""Dummy function that returns empty data"""
return [], {}
# Function to parse the transaction data and prepare it for visualization
def process_transactions_and_agents(data):
"""Dummy function that returns empty dataframes"""
df_transactions = pd.DataFrame()
df_agents = pd.DataFrame(columns=['date', 'agent_count'])
df_agents_weekly = pd.DataFrame()
return df_transactions, df_agents, df_agents_weekly
# Function to create visualizations based on the metrics
def create_visualizations():
"""
# Commenting out the original visualization code temporarily for debugging
transactions_data = fetch_and_aggregate_transactions()
df_transactions, df_agents, df_agents_weekly = process_transactions_and_agents(transactions_data)
# Fetch daily value locked data
df_tvl = pd.read_csv('daily_value_locked.csv')
# Calculate total value locked per chain per day
df_tvl["total_value_locked_usd"] = df_tvl["amount0_usd"] + df_tvl["amount1_usd"]
df_tvl_daily = df_tvl.groupby(["date", "chain_name"])["total_value_locked_usd"].sum().reset_index()
df_tvl_daily['date'] = pd.to_datetime(df_tvl_daily['date'])
# Filter out dates with zero total value locked
df_tvl_daily = df_tvl_daily[df_tvl_daily["total_value_locked_usd"] > 0]
chain_name_map = {
"mode": "Mode",
"base": "Base",
"ethereum": "Ethereum",
"optimism": "Optimism"
}
df_tvl_daily["chain_name"] = df_tvl_daily["chain_name"].map(chain_name_map)
# Plot total value locked
fig_tvl = px.bar(
df_tvl_daily,
x="date",
y="total_value_locked_usd",
color="chain_name",
opacity=0.7,
title="Total Volume Invested in Pools in Different Chains Daily",
labels={"date": "Date","chain_name": "Transaction Chain", "total_value_locked_usd": "Total Volume Invested (USD)"},
barmode='stack',
color_discrete_map={
"Mode": "orange",
"Base": "purple",
"Ethereum": "darkgreen",
"Optimism": "blue"
}
)
fig_tvl.update_layout(
xaxis_title="Date",
yaxis=dict(tickmode='linear', tick0=0, dtick=4),
xaxis=dict(
tickmode='array',
tickvals=df_tvl_daily['date'],
ticktext=df_tvl_daily['date'].dt.strftime('%b %d'),
tickangle=-45,
),
bargap=0.6, # Increase gap between bar groups (0-1)
bargroupgap=0.1, # Decrease gap between bars in a group (0-1)
height=600,
width=1200, # Specify width to prevent bars from being too wide
showlegend=True,
template='plotly_white'
)
fig_tvl.update_xaxes(tickformat="%b %d")
chain_name_map = {
10: "Optimism",
8453: "Base",
1: "Ethereum",
34443: "Mode"
}
df_transactions["sending_chain"] = df_transactions["sending_chain"].map(chain_name_map)
df_transactions["receiving_chain"] = df_transactions["receiving_chain"].map(chain_name_map)
df_transactions["sending_chain"] = df_transactions["sending_chain"].astype(str)
df_transactions["receiving_chain"] = df_transactions["receiving_chain"].astype(str)
df_transactions['date'] = pd.to_datetime(df_transactions['date'])
df_transactions["is_swap"] = df_transactions.apply(lambda x: x["sending_chain"] == x["receiving_chain"], axis=1)
swaps_per_chain = df_transactions[df_transactions["is_swap"]].groupby(["date", "sending_chain"]).size().reset_index(name="swap_count")
fig_swaps_chain = px.bar(
swaps_per_chain,
x="date",
y="swap_count",
color="sending_chain",
title="Chain Daily Activity: Swaps",
labels={"sending_chain": "Transaction Chain", "swap_count": "Daily Swap Nr"},
barmode="stack",
opacity=0.7,
color_discrete_map={
"Optimism": "blue",
"Ethereum": "darkgreen",
"Base": "purple",
"Mode": "orange"
}
)
fig_swaps_chain.update_layout(
xaxis_title="Date",
yaxis_title="Daily Swap Count",
yaxis=dict(tickmode='linear', tick0=0, dtick=1),
xaxis=dict(
tickmode='array',
tickvals=[d for d in swaps_per_chain['date']],
ticktext=[d.strftime('%m-%d') for d in swaps_per_chain['date']],
tickangle=-45,
),
bargap=0.6,
bargroupgap=0.1,
height=600,
width=1200,
margin=dict(l=50, r=50, t=50, b=50),
showlegend=True,
legend=dict(
yanchor="top",
y=0.99,
xanchor="right",
x=0.99
),
template='plotly_white'
)
fig_swaps_chain.update_xaxes(tickformat="%m-%d")
df_transactions["is_bridge"] = df_transactions.apply(lambda x: x["sending_chain"] != x["receiving_chain"], axis=1)
bridges_per_chain = df_transactions[df_transactions["is_bridge"]].groupby(["date", "sending_chain"]).size().reset_index(name="bridge_count")
fig_bridges_chain = px.bar(
bridges_per_chain,
x="date",
y="bridge_count",
color="sending_chain",
title="Chain Daily Activity: Bridges",
labels={"sending_chain": "Transaction Chain", "bridge_count": "Daily Bridge Nr"},
barmode="stack",
opacity=0.7,
color_discrete_map={
"Optimism": "blue",
"Ethereum": "darkgreen",
"Base": "purple",
"Mode": "orange"
}
)
fig_bridges_chain.update_layout(
xaxis_title="Date",
yaxis_title="Daily Bridge Count",
yaxis=dict(tickmode='linear', tick0=0, dtick=1),
xaxis=dict(
tickmode='array',
tickvals=[d for d in bridges_per_chain['date']],
ticktext=[d.strftime('%m-%d') for d in bridges_per_chain['date']],
tickangle=-45,
),
bargap=0.6,
bargroupgap=0.1,
height=600,
width=1200,
margin=dict(l=50, r=50, t=50, b=50),
showlegend=True,
legend=dict(
yanchor="top",
y=0.99,
xanchor="right",
x=0.99
),
template='plotly_white'
)
fig_bridges_chain.update_xaxes(tickformat="%m-%d")
df_agents['date'] = pd.to_datetime(df_agents['date'])
daily_agents_df = df_agents.groupby('date').agg({'agent_count': 'sum'}).reset_index()
daily_agents_df.rename(columns={'agent_count': 'daily_agent_count'}, inplace=True)
# Sort by date to ensure proper running total calculation
daily_agents_df = daily_agents_df.sort_values('date')
# Create week column
daily_agents_df['week'] = daily_agents_df['date'].dt.to_period('W').apply(lambda r: r.start_time)
# Calculate running total within each week
daily_agents_df['running_weekly_total'] = daily_agents_df.groupby('week')['daily_agent_count'].cumsum()
# Create final merged dataframe
weekly_merged_df = daily_agents_df.copy()
adjustment_date = pd.to_datetime('2024-11-15')
weekly_merged_df.loc[weekly_merged_df['date'] == adjustment_date, 'daily_agent_count'] -= 1
weekly_merged_df.loc[weekly_merged_df['date'] == adjustment_date, 'running_weekly_total'] -= 1
fig_agents_registered = go.Figure(data=[
go.Bar(
name='Daily nr of Registered Agents',
x=weekly_merged_df['date'].dt.strftime("%b %d"),
y=weekly_merged_df['daily_agent_count'],
opacity=0.7,
marker_color='blue'
),
go.Bar(
name='Weekly Nr of Registered Agents',
x=weekly_merged_df['date'].dt.strftime("%b %d"),
y=weekly_merged_df['running_weekly_total'],
opacity=0.7,
marker_color='purple'
)
])
fig_agents_registered.update_layout(
xaxis_title='Date',
yaxis_title='Number of Agents',
title="Nr of Agents Registered",
barmode='group',
yaxis=dict(tickmode='linear', tick0=0, dtick=1),
xaxis=dict(
categoryorder='array',
categoryarray=weekly_merged_df['date'].dt.strftime("%b %d"),
tickangle=-45
),
bargap=0.3,
height=600,
width=1200,
showlegend=True,
legend=dict(
yanchor="top",
xanchor="right",
),
template='plotly_white',
)
return fig_swaps_chain, fig_bridges_chain, fig_agents_registered,fig_tvl
"""
# Placeholder figures for testing
fig_swaps_chain = go.Figure()
fig_swaps_chain.add_annotation(
text="Blockchain data loading disabled - placeholder visualization",
x=0.5, y=0.5, xref="paper", yref="paper",
showarrow=False, font=dict(size=20)
)
fig_bridges_chain = go.Figure()
fig_bridges_chain.add_annotation(
text="Blockchain data loading disabled - placeholder visualization",
x=0.5, y=0.5, xref="paper", yref="paper",
showarrow=False, font=dict(size=20)
)
fig_agents_registered = go.Figure()
fig_agents_registered.add_annotation(
text="Blockchain data loading disabled - placeholder visualization",
x=0.5, y=0.5, xref="paper", yref="paper",
showarrow=False, font=dict(size=20)
)
fig_tvl = go.Figure()
fig_tvl.add_annotation(
text="Blockchain data loading disabled - placeholder visualization",
x=0.5, y=0.5, xref="paper", yref="paper",
showarrow=False, font=dict(size=20)
)
return fig_swaps_chain, fig_bridges_chain, fig_agents_registered, fig_tvl
# Add new function to the bottom of the file, before the dashboard() function
def add_diagnostic_controls(demo):
"""Add diagnostic UI controls to help debug the difference between local and production"""
with gr.Column():
gr.Markdown("## Diagnostics")
diagnostic_button = gr.Button("Run Data Diagnostics")
diagnostic_output = gr.Textbox(label="Diagnostic Results", lines=10)
def run_diagnostics():
"""Function to diagnose data issues"""
global global_df
if global_df is None or global_df.empty:
return "No data available. Please click 'Refresh APR Data' first."
# Gather diagnostics
result = []
result.append(f"=== DIAGNOSTIC REPORT ===")
result.append(f"Time: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
result.append(f"API Endpoint: {API_BASE_URL}")
result.append(f"Total data points: {len(global_df)}")
unique_agents = global_df['agent_id'].unique()
result.append(f"Number of unique agents: {len(unique_agents)}")
# Per-agent diagnostics
for agent_id in unique_agents:
agent_data = global_df[global_df['agent_id'] == agent_id]
agent_name = agent_data['agent_name'].iloc[0]
result.append(f"\nAgent: {agent_name} (ID: {agent_id})")
result.append(f" Data points: {len(agent_data)}")
# Check APR values
apr_data = agent_data[agent_data['metric_type'] == 'APR']
perf_data = agent_data[agent_data['metric_type'] == 'Performance']
result.append(f" APR points: {len(apr_data)}")
if not apr_data.empty:
result.append(f" APR values: {apr_data['apr'].tolist()}")
result.append(f" Performance points: {len(perf_data)}")
if not perf_data.empty:
result.append(f" Performance values: {perf_data['apr'].tolist()}")
# Write to file as well
with open("latest_diagnostics.txt", "w") as f:
f.write("\n".join(result))
return "\n".join(result)
# Fix for Gradio interface - use event listeners properly
try:
# Different Gradio versions have different APIs
# Try the newer approach first
diagnostic_button.click(
fn=run_diagnostics,
inputs=None,
outputs=diagnostic_output
)
except TypeError:
# Fall back to original approach
diagnostic_button.click(
fn=run_diagnostics,
inputs=[],
outputs=[diagnostic_output]
)
return demo
# Modify dashboard function to include diagnostics
def dashboard():
with gr.Blocks() as demo:
gr.Markdown("# Valory APR Metrics")
# APR Metrics tab - the only tab
with gr.Tab("APR Metrics"):
with gr.Column():
refresh_btn = gr.Button("Refresh APR Data")
# Create container for plotly figure (combined graph only)
combined_graph = gr.Plot(label="APR for All Agents")
# Function to update the graph
def update_apr_graph():
# Generate visualization and get figure object directly
try:
combined_fig, _ = generate_apr_visualizations()
return combined_fig
except Exception as e:
logger.exception("Error generating APR visualization")
# Create error figure
error_fig = go.Figure()
error_fig.add_annotation(
text=f"Error: {str(e)}",
x=0.5, y=0.5,
showarrow=False,
font=dict(size=15, color="red")
)
return error_fig
# Set up the button click event with error handling
try:
# Try newer Gradio API first
refresh_btn.click(
fn=update_apr_graph,
inputs=None,
outputs=combined_graph
)
except TypeError:
# Fall back to original method
refresh_btn.click(
fn=update_apr_graph,
inputs=[],
outputs=[combined_graph]
)
# Initialize the graph on load
# We'll use placeholder figure initially
import plotly.graph_objects as go
placeholder_fig = go.Figure()
placeholder_fig.add_annotation(
text="Click 'Refresh APR Data' to load APR graph",
x=0.5, y=0.5,
showarrow=False,
font=dict(size=15)
)
combined_graph.value = placeholder_fig
# Add diagnostics section
demo = add_diagnostic_controls(demo)
return demo
# Launch the dashboard
if __name__ == "__main__":
dashboard().launch()
|