Skip to content

Commit 3b14b01

Browse files
committed
fix: apply ruff formatting to pass CI linting checks
1 parent 00df701 commit 3b14b01

3 files changed

Lines changed: 71 additions & 41 deletions

File tree

classifier/app.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,35 +56,36 @@
5656
# Metrics tracking
5757
class ServiceMetrics:
5858
"""In-memory metrics tracking for observability"""
59+
5960
def __init__(self):
6061
self.start_time = datetime.now()
6162
self.issues_processed = 0
6263
self.openai_api_errors = 0
6364
self.classification_times_ms = []
64-
65+
6566
def record_issue_processed(self):
6667
self.issues_processed += 1
67-
68+
6869
def record_openai_error(self):
6970
self.openai_api_errors += 1
70-
71+
7172
def record_classification_time(self, duration_ms: float):
7273
self.classification_times_ms.append(duration_ms)
7374
# Keep only last 1000 measurements
7475
if len(self.classification_times_ms) > 1000:
7576
self.classification_times_ms = self.classification_times_ms[-1000:]
76-
77+
7778
def get_percentile(self, percentile: int) -> float:
7879
"""Calculate percentile from classification times"""
7980
if not self.classification_times_ms:
8081
return 0.0
8182
sorted_times = sorted(self.classification_times_ms)
8283
index = int(len(sorted_times) * percentile / 100)
8384
return round(sorted_times[min(index, len(sorted_times) - 1)], 2)
84-
85+
8586
def get_uptime_seconds(self) -> int:
8687
return int((datetime.now() - self.start_time).total_seconds())
87-
88+
8889
def to_dict(self) -> Dict[str, Any]:
8990
return {
9091
"service": "classifier",
@@ -98,6 +99,7 @@ def to_dict(self) -> Dict[str, Any]:
9899
"uptime_seconds": self.get_uptime_seconds(),
99100
}
100101

102+
101103
metrics = ServiceMetrics()
102104

103105

@@ -309,7 +311,7 @@ async def classify_issue(issue: IssueData):
309311
confidence=result.confidence,
310312
processing_time_ms=result.processing_time_ms,
311313
)
312-
314+
313315
# Record metrics
314316
metrics.record_issue_processed()
315317
metrics.record_classification_time(result.processing_time_ms)
@@ -673,9 +675,11 @@ async def process_kafka_message(message):
673675
if key == "correlation_id":
674676
correlation_id = value.decode("utf-8")
675677
break
676-
677-
bound_logger = logger.bind(correlation_id=correlation_id) if correlation_id else logger
678-
678+
679+
bound_logger = (
680+
logger.bind(correlation_id=correlation_id) if correlation_id else logger
681+
)
682+
679683
# Parse the message
680684
issue_data = json.loads(message.value.decode("utf-8"))
681685

@@ -714,9 +718,12 @@ async def process_kafka_message(message):
714718
)
715719

716720
except Exception as e:
717-
error_correlation_id = correlation_id if 'correlation_id' in locals() else None
721+
error_correlation_id = correlation_id if "correlation_id" in locals() else None
718722
logger.error(
719-
"Failed to process Kafka message", error=str(e), message=message.value, correlation_id=error_correlation_id
723+
"Failed to process Kafka message",
724+
error=str(e),
725+
message=message.value,
726+
correlation_id=error_correlation_id,
720727
)
721728

722729

gateway/app.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,35 +96,36 @@
9696
# Metrics tracking
9797
class ServiceMetrics:
9898
"""In-memory metrics tracking for observability"""
99+
99100
def __init__(self):
100101
self.start_time = datetime.now()
101102
self.messages_broadcast = 0
102103
self.api_requests = 0
103104
self.api_latencies_ms = []
104-
105+
105106
def record_message_broadcast(self):
106107
self.messages_broadcast += 1
107-
108+
108109
def record_api_request(self):
109110
self.api_requests += 1
110-
111+
111112
def record_api_latency(self, latency_ms: float):
112113
self.api_latencies_ms.append(latency_ms)
113114
# Keep only last 1000 measurements
114115
if len(self.api_latencies_ms) > 1000:
115116
self.api_latencies_ms = self.api_latencies_ms[-1000:]
116-
117+
117118
def get_percentile(self, percentile: int) -> float:
118119
"""Calculate percentile from API latencies"""
119120
if not self.api_latencies_ms:
120121
return 0.0
121122
sorted_times = sorted(self.api_latencies_ms)
122123
index = int(len(sorted_times) * percentile / 100)
123124
return round(sorted_times[min(index, len(sorted_times) - 1)], 2)
124-
125+
125126
def get_uptime_seconds(self) -> int:
126127
return int((datetime.now() - self.start_time).total_seconds())
127-
128+
128129
def to_dict(self, active_connections: int) -> Dict[str, Any]:
129130
return {
130131
"service": "gateway",
@@ -139,6 +140,7 @@ def to_dict(self, active_connections: int) -> Dict[str, Any]:
139140
"uptime_seconds": self.get_uptime_seconds(),
140141
}
141142

143+
142144
metrics = ServiceMetrics()
143145

144146

@@ -499,12 +501,12 @@ async def get_issues(
499501
try:
500502
user_id = current_user["sub"]
501503
is_dev_mode = current_user.get("dev_mode", False)
502-
504+
503505
conn = psycopg2.connect(DATABASE_URL)
504506
with conn.cursor(cursor_factory=RealDictCursor) as cur:
505507
where_conditions = []
506508
params = []
507-
509+
508510
if not is_dev_mode:
509511
where_conditions.append(
510512
"""(
@@ -834,7 +836,7 @@ async def get_stats(current_user: dict = Depends(get_current_user_required)):
834836
try:
835837
user_id = current_user["sub"]
836838
is_dev_mode = current_user.get("dev_mode", False)
837-
839+
838840
conn = psycopg2.connect(DATABASE_URL)
839841
with conn.cursor(cursor_factory=RealDictCursor) as cur:
840842
if is_dev_mode:
@@ -1772,8 +1774,10 @@ async def _process_message(self, message):
17721774
if key == "correlation_id":
17731775
correlation_id = value.decode("utf-8")
17741776
break
1775-
1776-
print(f"DEBUG: Processing message from {message.topic}, correlation_id={correlation_id}")
1777+
1778+
print(
1779+
f"DEBUG: Processing message from {message.topic}, correlation_id={correlation_id}"
1780+
)
17771781

17781782
event_data = json.loads(message.value.decode("utf-8"))
17791783

@@ -1831,7 +1835,7 @@ def user_can_see_issue(user: Optional[Dict[str, Any]]) -> bool:
18311835
await self.manager.broadcast(
18321836
json.dumps(websocket_message), user_filter=user_can_see_issue
18331837
)
1834-
1838+
18351839
# Record metrics
18361840
metrics.record_message_broadcast()
18371841

@@ -1849,7 +1853,9 @@ def user_can_see_issue(user: Optional[Dict[str, Any]]) -> bool:
18491853
)
18501854

18511855
except Exception as e:
1852-
error_correlation_id = correlation_id if 'correlation_id' in locals() else None
1856+
error_correlation_id = (
1857+
correlation_id if "correlation_id" in locals() else None
1858+
)
18531859
logger.error(
18541860
"Error processing Kafka message",
18551861
error=str(e),

ingress/app.py

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -138,43 +138,44 @@ def _get_client_ip(self, request: Request) -> str:
138138
# Metrics tracking
139139
class ServiceMetrics:
140140
"""In-memory metrics tracking for observability"""
141+
141142
def __init__(self):
142143
self.start_time = time.time()
143144
self.webhooks_received = 0
144145
self.webhooks_accepted = 0
145146
self.webhooks_rejected = 0
146147
self.kafka_publish_errors = 0
147148
self.processing_times_ms = []
148-
149+
149150
def record_webhook_received(self):
150151
self.webhooks_received += 1
151-
152+
152153
def record_webhook_accepted(self):
153154
self.webhooks_accepted += 1
154-
155+
155156
def record_webhook_rejected(self):
156157
self.webhooks_rejected += 1
157-
158+
158159
def record_kafka_error(self):
159160
self.kafka_publish_errors += 1
160-
161+
161162
def record_processing_time(self, duration_ms: float):
162163
self.processing_times_ms.append(duration_ms)
163164
# Keep only last 1000 measurements to prevent memory issues
164165
if len(self.processing_times_ms) > 1000:
165166
self.processing_times_ms = self.processing_times_ms[-1000:]
166-
167+
167168
def get_percentile(self, percentile: int) -> float:
168169
"""Calculate percentile from processing times"""
169170
if not self.processing_times_ms:
170171
return 0.0
171172
sorted_times = sorted(self.processing_times_ms)
172173
index = int(len(sorted_times) * percentile / 100)
173174
return round(sorted_times[min(index, len(sorted_times) - 1)], 2)
174-
175+
175176
def get_uptime_seconds(self) -> int:
176177
return int(time.time() - self.start_time)
177-
178+
178179
def to_dict(self) -> Dict[str, Any]:
179180
return {
180181
"service": "ingress",
@@ -190,6 +191,7 @@ def to_dict(self) -> Dict[str, Any]:
190191
"uptime_seconds": self.get_uptime_seconds(),
191192
}
192193

194+
193195
metrics = ServiceMetrics()
194196

195197

@@ -360,7 +362,9 @@ def verify_github_signature(payload_body: bytes, signature_header: str) -> bool:
360362
return False
361363

362364

363-
async def publish_to_kafka(topic: str, key: str, message: dict, correlation_id: Optional[str] = None):
365+
async def publish_to_kafka(
366+
topic: str, key: str, message: dict, correlation_id: Optional[str] = None
367+
):
364368
"""
365369
Publish message to Kafka/Redpanda topic with optional correlation ID in headers
366370
"""
@@ -369,7 +373,7 @@ async def publish_to_kafka(topic: str, key: str, message: dict, correlation_id:
369373
headers = []
370374
if correlation_id:
371375
headers.append(("correlation_id", correlation_id.encode("utf-8")))
372-
376+
373377
future = producer.send(topic, key=key, value=message, headers=headers)
374378
record_metadata = future.get(timeout=10)
375379

@@ -383,7 +387,13 @@ async def publish_to_kafka(topic: str, key: str, message: dict, correlation_id:
383387
)
384388

385389
except Exception as e:
386-
logger.error("Failed to publish to Kafka", topic=topic, key=key, correlation_id=correlation_id, error=str(e))
390+
logger.error(
391+
"Failed to publish to Kafka",
392+
topic=topic,
393+
key=key,
394+
correlation_id=correlation_id,
395+
error=str(e),
396+
)
387397
metrics.record_kafka_error()
388398
raise
389399

@@ -412,10 +422,10 @@ async def github_webhook(
412422
"""
413423
start_time = time.time()
414424
metrics.record_webhook_received()
415-
425+
416426
try:
417427
correlation_id = str(uuid.uuid4())
418-
428+
419429
# Get raw body for signature verification
420430
body = await request.body()
421431

@@ -428,7 +438,9 @@ async def github_webhook(
428438
try:
429439
payload = json.loads(body.decode("utf-8"))
430440
except json.JSONDecodeError as e:
431-
logger.error("Invalid JSON payload", error=str(e), correlation_id=correlation_id)
441+
logger.error(
442+
"Invalid JSON payload", error=str(e), correlation_id=correlation_id
443+
)
432444
raise HTTPException(status_code=400, detail="Invalid JSON payload")
433445

434446
# Get event type and validate
@@ -561,7 +573,12 @@ async def github_webhook(
561573
message_key = f"{event_data['repository']['full_name']}:{event_data['event_type']}:{event_data.get('issue', event_data.get('pull_request', {})).get('number', 'unknown')}"
562574
print(f"Publishing to Kafka: {message_key}")
563575

564-
await publish_to_kafka(KAFKA_TOPIC_RAW_ISSUES, message_key, event_data, correlation_id=correlation_id)
576+
await publish_to_kafka(
577+
KAFKA_TOPIC_RAW_ISSUES,
578+
message_key,
579+
event_data,
580+
correlation_id=correlation_id,
581+
)
565582

566583
logger.info(
567584
"Webhook processed successfully",
@@ -571,7 +588,7 @@ async def github_webhook(
571588
message_key=message_key,
572589
correlation_id=correlation_id,
573590
)
574-
591+
575592
# Record successful processing
576593
metrics.record_webhook_accepted()
577594
processing_time_ms = (time.time() - start_time) * 1000

0 commit comments

Comments
 (0)