Skip to content

Commit 0c65dcb

Browse files
committed
Update insert error script
1 parent caea92d commit 0c65dcb

File tree

1 file changed

+120
-78
lines changed

1 file changed

+120
-78
lines changed

scripts/insert_errors_to_db.py

Lines changed: 120 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -7,103 +7,145 @@
77

88
load_dotenv()
99

10-
network_config = {
11-
'base:mainnet': {'blockchain_id': 'base:8453', 'table': 'error_messages_mainnet_py', 'host': os.getenv('DB_HOST_PUBLISH_MAINNET')},
12-
'base:testnet': {'blockchain_id': 'base:84531', 'table': 'error_messages_testnet_py', 'host': os.getenv('DB_HOST_PUBLISH_TESTNET')},
13-
'gnosis:mainnet': {'blockchain_id': 'gnosis:100', 'table': 'error_messages_mainnet_py', 'host': os.getenv('DB_HOST_PUBLISH_MAINNET')},
14-
'gnosis:testnet': {'blockchain_id': 'gnosis:10200', 'table': 'error_messages_testnet_py', 'host': os.getenv('DB_HOST_PUBLISH_TESTNET')},
15-
'neuroweb:mainnet': {'blockchain_id': 'neuroweb:2043', 'table': 'error_messages_mainnet_py', 'host': os.getenv('DB_HOST_PUBLISH_MAINNET')},
16-
'neuroweb:testnet': {'blockchain_id': 'neuroweb:20432', 'table': 'error_messages_testnet_py', 'host': os.getenv('DB_HOST_PUBLISH_TESTNET')},
17-
}
10+
MAINNET_PORTS = [':8453', ':100', ':2043']
11+
12+
def is_mainnet(blockchain_name):
13+
return any(blockchain_name.endswith(port) for port in MAINNET_PORTS)
14+
15+
def get_db_connection(mainnet=False):
16+
host = os.getenv('DB_HOST_PUBLISH_MAINNET') if mainnet else os.getenv('DB_HOST_PUBLISH_TESTNET')
17+
return psycopg2.connect(
18+
host=host,
19+
user=os.getenv('DB_USER_PUBLISH'),
20+
password=os.getenv('DB_PASSWORD_PUBLISH'),
21+
dbname=os.getenv('DB_NAME_PUBLISH'),
22+
port=5432
23+
)
1824

1925
files = sys.argv[1:]
2026

2127
for file in files:
2228
print(f"📁 Processing error file: {file}")
2329
try:
2430
with open(file, 'r') as f:
25-
errors = json.load(f)
31+
error_data = json.load(f)
2632
except Exception as e:
2733
print(f"❌ Failed to read or parse {file}: {e}")
2834
continue
2935

30-
match = file.lower().split('errors_')
31-
if len(match) < 2:
32-
print(f"❌ Filename format incorrect for {file}. Expected: errors_Node_XX.json")
33-
continue
34-
35-
node_name = match[1].replace('_', ' ').replace('.json', '').strip()
36-
is_mainnet = 'mainnet' in file.lower()
37-
38-
matched_key = next(
39-
(key for key in network_config if key.startswith(tuple(file.lower().split('_'))) and key.endswith('mainnet') == is_mainnet),
40-
None
41-
)
42-
43-
if not matched_key:
44-
print(f"❌ Could not determine network config for file: {file}")
45-
continue
36+
# Handle both old and new format
37+
if isinstance(error_data, dict) and 'blockchain_name' in error_data:
38+
# New format with blockchain information
39+
blockchain_name = error_data.get('blockchain_name', '')
40+
node_name = error_data.get('node_name', '')
41+
errors = error_data.get('errors', {})
42+
else:
43+
# Old format - try to determine from filename
44+
errors = error_data
45+
node_name = file.split('errors_')[1].replace('_', ' ').replace('.json', '').strip()
46+
blockchain_name = '' # Will be determined by network config
4647

47-
config = network_config[matched_key]
48-
blockchain_id = config['blockchain_id']
49-
table_name = config['table']
50-
db_host = config['host']
48+
mainnet = is_mainnet(blockchain_name)
49+
table_name = 'error_messages_mainnet_py' if mainnet else 'error_messages_testnet_py'
5150

5251
try:
53-
conn = psycopg2.connect(
54-
host=db_host,
55-
user=os.getenv('DB_USER_PUBLISH'),
56-
password=os.getenv('DB_PASSWORD_PUBLISH'),
57-
dbname=os.getenv('DB_NAME_PUBLISH'),
58-
port=5432
59-
)
52+
conn = get_db_connection(mainnet)
6053
cursor = conn.cursor()
61-
print(f"✅ Connected to DB ({table_name})")
54+
print(f"✅ Connected to DB ({'mainnet' if mainnet else 'testnet'})")
6255
except Exception as e:
6356
print(f"❌ Failed to connect to DB: {e}")
6457
continue
6558

66-
for ka_label in errors:
67-
row = {
68-
'node_name': node_name,
69-
'blockchain_id': blockchain_id,
70-
'ka_label': ka_label,
71-
'publish_error': None,
72-
'query_error': None,
73-
'publisher_get_error': None,
74-
'non_publisher_get_error': None,
75-
'time_stamp': errors[ka_label].get('time_stamp') or None,
76-
}
77-
78-
label = ka_label.lower()
79-
if 'publish' in label:
80-
row['publish_error'] = ka_label
81-
elif 'query' in label:
82-
row['query_error'] = ka_label
83-
elif 'local get' in label:
84-
row['publisher_get_error'] = ka_label
85-
elif 'get' in label:
86-
row['non_publisher_get_error'] = ka_label
87-
88-
insert_query = sql.SQL(f"""
89-
INSERT INTO {sql.Identifier(table_name).string} (
90-
node_name, blockchain_id, ka_label,
91-
publish_error, query_error,
92-
publisher_get_error, non_publisher_get_error,
93-
time_stamp
94-
) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
95-
""")
96-
97-
try:
98-
cursor.execute(insert_query, (
99-
row['node_name'], row['blockchain_id'], row['ka_label'],
100-
row['publish_error'], row['query_error'],
101-
row['publisher_get_error'], row['non_publisher_get_error'],
102-
row['time_stamp']
103-
))
104-
print(f"✅ Inserted KA {ka_label} for {node_name}")
105-
except Exception as e:
106-
print(f"❌ Failed to insert KA {ka_label}: {e}")
59+
for attempt_key, attempt_data in errors.items():
60+
# Handle both old and new error formats
61+
if isinstance(attempt_data, dict) and 'ka_label' in attempt_data:
62+
# New format with structured error data per attempt
63+
ka_label = attempt_data.get('ka_label', 'Unknown KA')
64+
attempt_number = attempt_data.get('attempt', 1)
65+
publish_error = attempt_data.get('publish_error')
66+
query_error = attempt_data.get('query_error')
67+
publisher_get_error = attempt_data.get('publisher_get_error')
68+
non_publisher_get_error = attempt_data.get('non_publisher_get_error')
69+
time_stamp = attempt_data.get('time_stamp')
70+
71+
# Only insert if there's at least one error
72+
if any([publish_error, query_error, publisher_get_error, non_publisher_get_error]):
73+
row = {
74+
'node_name': node_name,
75+
'blockchain_id': blockchain_name,
76+
'ka_label': ka_label,
77+
'publish_error': publish_error,
78+
'query_error': query_error,
79+
'publisher_get_error': publisher_get_error,
80+
'non_publisher_get_error': non_publisher_get_error,
81+
'time_stamp': time_stamp,
82+
}
83+
84+
insert_query = sql.SQL(f"""
85+
INSERT INTO {sql.Identifier(table_name).string} (
86+
node_name, blockchain_id, ka_label,
87+
publish_error, query_error,
88+
publisher_get_error, non_publisher_get_error,
89+
time_stamp
90+
) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
91+
""")
92+
93+
try:
94+
cursor.execute(insert_query, (
95+
row['node_name'], row['blockchain_id'], row['ka_label'],
96+
row['publish_error'], row['query_error'],
97+
row['publisher_get_error'], row['non_publisher_get_error'],
98+
row['time_stamp']
99+
))
100+
print(f"✅ Inserted {ka_label} (attempt {attempt_number}) for {node_name}")
101+
except Exception as e:
102+
print(f"❌ Failed to insert {ka_label} (attempt {attempt_number}): {e}")
103+
else:
104+
# Old format - simple count (backward compatibility)
105+
ka_label = attempt_key
106+
error_message = attempt_data if isinstance(attempt_data, str) else str(attempt_data)
107+
108+
row = {
109+
'node_name': node_name,
110+
'blockchain_id': blockchain_name,
111+
'ka_label': ka_label,
112+
'publish_error': None,
113+
'query_error': None,
114+
'publisher_get_error': None,
115+
'non_publisher_get_error': None,
116+
'time_stamp': None,
117+
}
118+
119+
# Try to determine error type from the key or message
120+
label = attempt_key.lower()
121+
if 'publish' in label:
122+
row['publish_error'] = error_message
123+
elif 'query' in label:
124+
row['query_error'] = error_message
125+
elif 'local get' in label:
126+
row['publisher_get_error'] = error_message
127+
elif 'get' in label:
128+
row['non_publisher_get_error'] = error_message
129+
130+
insert_query = sql.SQL(f"""
131+
INSERT INTO {sql.Identifier(table_name).string} (
132+
node_name, blockchain_id, ka_label,
133+
publish_error, query_error,
134+
publisher_get_error, non_publisher_get_error,
135+
time_stamp
136+
) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
137+
""")
138+
139+
try:
140+
cursor.execute(insert_query, (
141+
row['node_name'], row['blockchain_id'], row['ka_label'],
142+
row['publish_error'], row['query_error'],
143+
row['publisher_get_error'], row['non_publisher_get_error'],
144+
row['time_stamp']
145+
))
146+
print(f"✅ Inserted {ka_label} for {node_name} (old format)")
147+
except Exception as e:
148+
print(f"❌ Failed to insert {ka_label}: {e}")
107149

108150
try:
109151
conn.commit()

0 commit comments

Comments
 (0)