Skip to content

Commit 54ab6d0

Browse files
committed
Update insert error fixes
1 parent 97acd32 commit 54ab6d0

File tree

1 file changed

+113
-76
lines changed

1 file changed

+113
-76
lines changed

scripts/insert_errors_to_db.py

Lines changed: 113 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,107 @@ def get_db_connection(mainnet=False):
2424
port=5432
2525
)
2626

27-
def extract_ka_number(error_key):
28-
"""Extract KA number from error key or generate a default"""
27+
def extract_ka_number(error_key, node_name):
28+
"""Extract KA number from error key or generate based on node and context"""
2929
# Try to find KA #X pattern in the error message
3030
ka_match = re.search(r'KA\s*#(\d+)', error_key, re.IGNORECASE)
3131
if ka_match:
3232
return f"KA #{ka_match.group(1)}"
3333

34-
# If no KA number found, return a generic label
34+
# If no KA number found, try to extract from the error message
35+
# Look for patterns like "publishing KA #1" or "KA #1"
36+
ka_pattern = re.search(r'KA\s*#(\d+)', error_key, re.IGNORECASE)
37+
if ka_pattern:
38+
return f"KA #{ka_pattern.group(1)}"
39+
40+
# For old format files, try to determine KA number from context
41+
# Extract node number and use it to generate a KA number
42+
node_match = re.search(r'Node\s*(\d+)', node_name, re.IGNORECASE)
43+
if node_match:
44+
node_num = node_match.group(1)
45+
# Use node number as base for KA number (simple mapping)
46+
ka_num = int(node_num) % 10 + 1 # Simple mapping: Node 25 -> KA #6, Node 14 -> KA #5
47+
return f"KA #{ka_num}"
48+
49+
# If still no match, return a generic label
3550
return "Unknown KA"
3651

52+
def determine_blockchain_from_context(file_path, node_name):
53+
"""Determine blockchain from file context and node name"""
54+
# Check if we're in mainnet or testnet context based on node numbers
55+
mainnet_nodes = ["Node 25", "Node 26", "Node 27", "Node 28", "Node 29", "Node 30"]
56+
testnet_nodes = ["Node 01", "Node 04", "Node 05", "Node 06", "Node 07", "Node 08",
57+
"Node 09", "Node 10", "Node 13", "Node 14", "Node 21", "Node 23", "Node 37"]
58+
59+
if node_name in mainnet_nodes:
60+
# Determine which mainnet blockchain based on file path or context
61+
if 'base' in file_path.lower():
62+
return 'base:8453'
63+
elif 'gnosis' in file_path.lower():
64+
return 'gnosis:100'
65+
elif 'neuroweb' in file_path.lower():
66+
return 'neuroweb:2043'
67+
else:
68+
# Default to base mainnet if can't determine
69+
return 'base:8453'
70+
elif node_name in testnet_nodes:
71+
# Determine which testnet blockchain based on file path or context
72+
if 'base' in file_path.lower():
73+
return 'base:84531'
74+
elif 'gnosis' in file_path.lower():
75+
return 'gnosis:10200'
76+
elif 'neuroweb' in file_path.lower():
77+
return 'neuroweb:20432'
78+
else:
79+
# Default to base testnet if can't determine
80+
return 'base:84531'
81+
else:
82+
# Default to base mainnet if node not recognized
83+
return 'base:8453'
84+
85+
def group_errors_by_ka_attempt(errors, node_name):
86+
"""Group errors by KA attempt number to create one row per attempt"""
87+
ka_attempts = {}
88+
89+
for error_key, error_data in errors.items():
90+
# Extract KA number from error key
91+
ka_label = extract_ka_number(error_key, node_name)
92+
93+
# Extract attempt number from KA label
94+
ka_match = re.search(r'KA\s*#(\d+)', ka_label, re.IGNORECASE)
95+
if ka_match:
96+
attempt_num = int(ka_match.group(1))
97+
else:
98+
# If no KA number found, use a default
99+
attempt_num = 1
100+
101+
# Initialize attempt if not exists
102+
if attempt_num not in ka_attempts:
103+
ka_attempts[attempt_num] = {
104+
'ka_label': ka_label,
105+
'attempt': attempt_num,
106+
'publish_error': None,
107+
'query_error': None,
108+
'publisher_get_error': None,
109+
'non_publisher_get_error': None,
110+
'time_stamp': datetime.utcnow().isoformat()
111+
}
112+
113+
# Determine error type and store in appropriate field
114+
error_message = error_key if isinstance(error_data, int) else str(error_data)
115+
label = error_key.lower()
116+
117+
if 'publish' in label:
118+
ka_attempts[attempt_num]['publish_error'] = error_message
119+
elif 'query' in label:
120+
ka_attempts[attempt_num]['query_error'] = error_message
121+
elif 'local get' in label:
122+
ka_attempts[attempt_num]['publisher_get_error'] = error_message
123+
elif 'get' in label:
124+
ka_attempts[attempt_num]['non_publisher_get_error'] = error_message
125+
126+
return ka_attempts
127+
37128
files = sys.argv[1:]
38129

39130
for file in files:
@@ -60,7 +151,9 @@ def extract_ka_number(error_key):
60151
else:
61152
# Fallback: use filename without extension as node name
62153
node_name = os.path.basename(file).replace('.json', '').strip()
63-
blockchain_name = '' # Will be determined by network config
154+
155+
# Determine blockchain from context
156+
blockchain_name = determine_blockchain_from_context(file, node_name)
64157

65158
mainnet = is_mainnet(blockchain_name)
66159
table_name = 'error_messages_mainnet_py' if mainnet else 'error_messages_testnet_py'
@@ -73,82 +166,26 @@ def extract_ka_number(error_key):
73166
print(f"❌ Failed to connect to DB: {e}")
74167
continue
75168

76-
for attempt_key, attempt_data in errors.items():
77-
# Handle both old and new error formats
78-
if isinstance(attempt_data, dict) and 'ka_label' in attempt_data:
79-
# New format with structured error data per attempt
80-
ka_label = attempt_data.get('ka_label', 'Unknown KA')
81-
attempt_number = attempt_data.get('attempt', 1)
82-
publish_error = attempt_data.get('publish_error')
83-
query_error = attempt_data.get('query_error')
84-
publisher_get_error = attempt_data.get('publisher_get_error')
85-
non_publisher_get_error = attempt_data.get('non_publisher_get_error')
86-
time_stamp = attempt_data.get('time_stamp')
87-
88-
# Only insert if there's at least one error
89-
if any([publish_error, query_error, publisher_get_error, non_publisher_get_error]):
90-
row = {
91-
'node_name': node_name,
92-
'blockchain_id': blockchain_name,
93-
'ka_label': ka_label,
94-
'publish_error': publish_error,
95-
'query_error': query_error,
96-
'publisher_get_error': publisher_get_error,
97-
'non_publisher_get_error': non_publisher_get_error,
98-
'time_stamp': time_stamp,
99-
}
100-
101-
insert_query = sql.SQL(f"""
102-
INSERT INTO {sql.Identifier(table_name).string} (
103-
node_name, blockchain_id, ka_label,
104-
publish_error, query_error,
105-
publisher_get_error, non_publisher_get_error,
106-
time_stamp
107-
) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
108-
""")
109-
110-
try:
111-
cursor.execute(insert_query, (
112-
row['node_name'], row['blockchain_id'], row['ka_label'],
113-
row['publish_error'], row['query_error'],
114-
row['publisher_get_error'], row['non_publisher_get_error'],
115-
row['time_stamp']
116-
))
117-
print(f"✅ Inserted {ka_label} (attempt {attempt_number}) for {node_name}")
118-
except Exception as e:
119-
print(f"❌ Failed to insert {ka_label} (attempt {attempt_number}): {e}")
120-
else:
121-
# Old format - simple count (backward compatibility)
122-
error_message = attempt_data if isinstance(attempt_data, str) else str(attempt_data)
123-
124-
# Extract proper KA label from error key
125-
ka_label = extract_ka_number(attempt_key)
126-
127-
# Generate current timestamp for old format files
128-
current_timestamp = datetime.utcnow().isoformat()
169+
# Group errors by KA attempt
170+
ka_attempts = group_errors_by_ka_attempt(errors, node_name)
171+
172+
# Insert one row per KA attempt
173+
for attempt_num, attempt_data in ka_attempts.items():
174+
# Only insert if there's at least one error
175+
if any([attempt_data['publish_error'], attempt_data['query_error'],
176+
attempt_data['publisher_get_error'], attempt_data['non_publisher_get_error']]):
129177

130178
row = {
131179
'node_name': node_name,
132180
'blockchain_id': blockchain_name,
133-
'ka_label': ka_label,
134-
'publish_error': None,
135-
'query_error': None,
136-
'publisher_get_error': None,
137-
'non_publisher_get_error': None,
138-
'time_stamp': current_timestamp,
181+
'ka_label': attempt_data['ka_label'],
182+
'publish_error': attempt_data['publish_error'],
183+
'query_error': attempt_data['query_error'],
184+
'publisher_get_error': attempt_data['publisher_get_error'],
185+
'non_publisher_get_error': attempt_data['non_publisher_get_error'],
186+
'time_stamp': attempt_data['time_stamp'],
139187
}
140188

141-
# Try to determine error type from the key or message
142-
label = attempt_key.lower()
143-
if 'publish' in label:
144-
row['publish_error'] = error_message
145-
elif 'query' in label:
146-
row['query_error'] = error_message
147-
elif 'local get' in label:
148-
row['publisher_get_error'] = error_message
149-
elif 'get' in label:
150-
row['non_publisher_get_error'] = error_message
151-
152189
insert_query = sql.SQL(f"""
153190
INSERT INTO {sql.Identifier(table_name).string} (
154191
node_name, blockchain_id, ka_label,
@@ -165,9 +202,9 @@ def extract_ka_number(error_key):
165202
row['publisher_get_error'], row['non_publisher_get_error'],
166203
row['time_stamp']
167204
))
168-
print(f"✅ Inserted {ka_label} for {node_name} (old format)")
205+
print(f"✅ Inserted {attempt_data['ka_label']} (attempt {attempt_num}) for {node_name}")
169206
except Exception as e:
170-
print(f"❌ Failed to insert {ka_label}: {e}")
207+
print(f"❌ Failed to insert {attempt_data['ka_label']} (attempt {attempt_num}): {e}")
171208

172209
try:
173210
conn.commit()

0 commit comments

Comments
 (0)