Skip to content

Commit baf1771

Browse files
committed
Ruff
1 parent b6db2de commit baf1771

File tree

4 files changed

+152
-227
lines changed

4 files changed

+152
-227
lines changed

scripts/test_slack_notifications.py

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import logging
88
import os
99
import sys
10-
import time
1110
from typing import Optional
1211

1312
# Add project root to path
@@ -22,14 +21,13 @@
2221
)
2322
logger = logging.getLogger("slack-notifier-test")
2423

25-
from src.utils.config_loader import load_config
2624
from src.utils.slack_notifier import SlackNotifier, create_slack_notifier
2725

2826

2927
def load_slack_configuration() -> Optional[str]:
3028
"""
3129
Load Slack webhook URL from configuration.
32-
30+
3331
Returns:
3432
Slack webhook URL if found, None otherwise
3533
"""
@@ -43,42 +41,39 @@ def load_slack_configuration() -> Optional[str]:
4341
def create_and_validate_slack_notifier(webhook_url: str) -> Optional[SlackNotifier]:
4442
"""
4543
Create and validate Slack notifier instance.
46-
44+
4745
Args:
4846
webhook_url: The Slack webhook URL
49-
47+
5048
Returns:
5149
SlackNotifier instance if successful, None otherwise
5250
"""
5351
# Create notifier instance using factory function
5452
notifier = create_slack_notifier(webhook_url)
55-
53+
5654
if not notifier:
5755
logger.error("Failed to create Slack notifier instance")
5856
return None
59-
57+
6058
logger.info("Slack notifier created successfully")
6159
return notifier
6260

6361

6462
def test_info_notification(notifier: SlackNotifier) -> bool:
6563
"""
6664
Test sending an informational notification to Slack.
67-
65+
6866
Args:
6967
notifier: Configured SlackNotifier instance
70-
68+
7169
Returns:
7270
True if test passes, False otherwise
7371
"""
7472
# Send test info notification with sample message
7573
logger.info("Testing info notification...")
76-
77-
success = notifier.send_info_notification(
78-
message="Test send info notification",
79-
title="Test Notification"
80-
)
81-
74+
75+
success = notifier.send_info_notification(message="Test send info notification", title="Test Notification")
76+
8277
if success:
8378
logger.info("Info notification TEST PASSED")
8479
return True
@@ -90,28 +85,26 @@ def test_info_notification(notifier: SlackNotifier) -> bool:
9085
def test_success_notification(notifier: SlackNotifier) -> bool:
9186
"""
9287
Test sending a success notification to Slack.
93-
88+
9489
Args:
9590
notifier: Configured SlackNotifier instance
96-
91+
9792
Returns:
9893
True if test passes, False otherwise
9994
"""
10095
# Send test success notification with sample indexer data
10196
logger.info("Testing success notification...")
102-
97+
10398
test_indexers = [
10499
"0x1234567890abcdef1234567890abcdef12345678",
105100
"0xabcdef1234567890abcdef1234567890abcdef12",
106-
"0x9876543210fedcba9876543210fedcba98765432"
101+
"0x9876543210fedcba9876543210fedcba98765432",
107102
]
108-
103+
109104
success = notifier.send_success_notification(
110-
eligible_indexers=test_indexers,
111-
total_processed=len(test_indexers),
112-
execution_time=1.0
105+
eligible_indexers=test_indexers, total_processed=len(test_indexers), execution_time=1.0
113106
)
114-
107+
115108
if success:
116109
logger.info("Success notification TEST PASSED")
117110
return True
@@ -123,22 +116,22 @@ def test_success_notification(notifier: SlackNotifier) -> bool:
123116
def test_failure_notification(notifier: SlackNotifier) -> bool:
124117
"""
125118
Test sending a failure notification to Slack.
126-
119+
127120
Args:
128121
notifier: Configured SlackNotifier instance
129-
122+
130123
Returns:
131124
True if test passes, False otherwise
132125
"""
133126
# Send test failure notification with sample error
134127
logger.info("Testing failure notification...")
135-
128+
136129
success = notifier.send_failure_notification(
137130
error_message="Test error message to verify failure notifications work correctly.",
138131
stage="Test Stage",
139-
execution_time=1
132+
execution_time=1,
140133
)
141-
134+
142135
if success:
143136
logger.info("Failure notification TEST PASSED")
144137
return True
@@ -155,7 +148,7 @@ def execute_all_slack_tests() -> bool:
155148
- Info notification
156149
- Success notification
157150
- Failure notification
158-
151+
159152
Returns:
160153
True if all tests pass, False otherwise
161154
"""
@@ -164,28 +157,28 @@ def execute_all_slack_tests() -> bool:
164157
if not webhook_url:
165158
logger.error("Failed to load webhook_url")
166159
return False
167-
160+
168161
# Create and validate Slack notifier instance
169162
notifier = create_and_validate_slack_notifier(webhook_url)
170163
if not notifier:
171164
logger.error("Failed to create Slack notifier instance")
172165
return False
173-
166+
174167
# Execute info notification test
175168
if not test_info_notification(notifier):
176169
logger.error("Failed to send info notification")
177170
return False
178-
171+
179172
# Execute success notification test
180173
if not test_success_notification(notifier):
181174
logger.error("Failed to send success notification")
182175
return False
183-
176+
184177
# Execute failure notification test
185178
if not test_failure_notification(notifier):
186179
logger.error("Failed to send failure notification")
187180
return False
188-
181+
189182
# All tests completed successfully
190183
logger.info("All Slack notification tests passed!")
191184
return True
@@ -213,11 +206,11 @@ def main():
213206
"""
214207
# Display test header information
215208
logger.info("Service Quality Oracle - Slack Notification Test")
216-
209+
217210
# Check environment variable configuration
218211
if not check_environment_variable_configuration():
219212
sys.exit(1)
220-
213+
221214
# Execute all tests and handle results
222215
if execute_all_slack_tests():
223216
logger.info("All tests completed successfully!")
@@ -231,4 +224,4 @@ def main():
231224

232225

233226
if __name__ == "__main__":
234-
main()
227+
main()

src/models/issuance_eligibility_oracle_core.py

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def main():
4242
"""
4343
start_time = time.time()
4444
slack_notifier = None
45-
45+
4646
try:
4747
# Load configuration to get Slack webhook and other settings
4848
config = load_config()
@@ -51,11 +51,11 @@ def main():
5151
logger.info("Slack notifications enabled")
5252
else:
5353
logger.info("Slack notifications disabled (no webhook URL configured)")
54-
54+
5555
except Exception as e:
5656
logger.error(f"Failed to load configuration: {str(e)}")
5757
sys.exit(1)
58-
58+
5959
try:
6060
# Attempt to load google bigquery data access credentials
6161
try:
@@ -65,7 +65,7 @@ def main():
6565
# If credentials could not be loaded, set them up in memory via helper function using environment variables
6666
except Exception:
6767
_setup_google_credentials_in_memory_from_env_var()
68-
68+
6969
try:
7070
# Fetch + save indexer eligibility data and return eligible list as 'eligible_indexers' array
7171
eligible_indexers = (
@@ -76,73 +76,64 @@ def main():
7676
max_age_before_deletion=config.get("MAX_AGE_BEFORE_DELETION"),
7777
)
7878
)
79-
79+
8080
logger.info(f"Found {len(eligible_indexers)} eligible indexers.")
81-
81+
8282
# Send eligible indexers to the blockchain contract
8383
try:
8484
batch_allow_indexers_issuance_eligibility_smart_contract(
85-
eligible_indexers,
86-
replace=True,
87-
batch_size=config.get("BATCH_SIZE"),
88-
data_bytes=b""
85+
eligible_indexers, replace=True, batch_size=config.get("BATCH_SIZE"), data_bytes=b""
8986
)
90-
87+
9188
# Calculate execution time and send success notification
9289
execution_time = time.time() - start_time
9390
logger.info(f"Oracle run completed successfully in {execution_time:.2f} seconds")
94-
91+
9592
if slack_notifier:
9693
# TODO: For success notification, we need to get total processed count
9794
# This would ideally come from the data processing functions
9895
total_processed = len(eligible_indexers) # Simplified for now
99-
96+
10097
slack_notifier.send_success_notification(
10198
eligible_indexers=eligible_indexers,
10299
total_processed=total_processed,
103-
execution_time=execution_time
100+
execution_time=execution_time,
104101
)
105-
102+
106103
except Exception as e:
107104
execution_time = time.time() - start_time
108105
error_msg = f"Failed to allow indexers to claim issuance because: {str(e)}"
109106
logger.error(error_msg)
110-
107+
111108
if slack_notifier:
112109
slack_notifier.send_failure_notification(
113-
error_message=str(e),
114-
stage="Blockchain Submission",
115-
execution_time=execution_time
110+
error_message=str(e), stage="Blockchain Submission", execution_time=execution_time
116111
)
117-
112+
118113
sys.exit(1)
119-
114+
120115
except Exception as e:
121116
execution_time = time.time() - start_time
122117
error_msg = f"Failed to process indexer issuance eligibility data because: {str(e)}"
123118
logger.error(error_msg)
124-
119+
125120
if slack_notifier:
126121
slack_notifier.send_failure_notification(
127-
error_message=str(e),
128-
stage="Data Processing",
129-
execution_time=execution_time
122+
error_message=str(e), stage="Data Processing", execution_time=execution_time
130123
)
131-
124+
132125
sys.exit(1)
133-
126+
134127
except Exception as e:
135128
execution_time = time.time() - start_time
136129
error_msg = f"Oracle initialization or authentication failed: {str(e)}"
137130
logger.error(error_msg)
138-
131+
139132
if slack_notifier:
140133
slack_notifier.send_failure_notification(
141-
error_message=str(e),
142-
stage="Initialization",
143-
execution_time=execution_time
134+
error_message=str(e), stage="Initialization", execution_time=execution_time
144135
)
145-
136+
146137
sys.exit(1)
147138

148139

0 commit comments

Comments
 (0)