Spaces:
Configuration error
Configuration error
maryangel101 commited on
Commit Β·
62ccdb1
1
Parent(s): 6a12fe7
Add robust anomaly detector with mock fallback
Browse files
.github/scripts/anomaly_detector.py
CHANGED
|
@@ -26,21 +26,25 @@ def download_workflow_logs(run_id, token):
|
|
| 26 |
return None
|
| 27 |
|
| 28 |
def analyze_logs_with_model(log_content, model_url):
|
| 29 |
-
"""Send logs to the model API for analysis"""
|
| 30 |
try:
|
| 31 |
-
#
|
| 32 |
-
if not model_url:
|
| 33 |
-
print("
|
| 34 |
-
return
|
| 35 |
|
| 36 |
# Ensure the URL has a scheme
|
| 37 |
if not model_url.startswith(('http://', 'https://')):
|
| 38 |
print(f"β MODEL_API_URL is missing scheme: {model_url}")
|
| 39 |
-
print("π‘
|
| 40 |
-
return
|
| 41 |
|
| 42 |
# Construct the full prediction endpoint URL
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
print(f"π Calling model API: {prediction_url}")
|
| 45 |
|
| 46 |
response = requests.post(
|
|
@@ -57,20 +61,54 @@ def analyze_logs_with_model(log_content, model_url):
|
|
| 57 |
else:
|
| 58 |
print(f"β Model API error: {response.status_code}")
|
| 59 |
print(f"Response: {response.text}")
|
| 60 |
-
|
|
|
|
| 61 |
|
| 62 |
except requests.exceptions.Timeout:
|
| 63 |
print("β Model API request timed out (30 seconds)")
|
| 64 |
-
|
|
|
|
| 65 |
except requests.exceptions.ConnectionError:
|
| 66 |
print("β Failed to connect to Model API - check the URL")
|
| 67 |
-
|
|
|
|
| 68 |
except requests.exceptions.RequestException as e:
|
| 69 |
print(f"β Failed to call model API: {e}")
|
| 70 |
-
|
|
|
|
| 71 |
except Exception as e:
|
| 72 |
print(f"β Unexpected error: {e}")
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
def main():
|
| 76 |
# Get environment variables
|
|
@@ -83,9 +121,10 @@ def main():
|
|
| 83 |
print(f" WORKFLOW_RUN_ID: {'β
' if workflow_run_id else 'β'} -> {workflow_run_id}")
|
| 84 |
print(f" MODEL_API_URL: {'β
' if model_api_url else 'β'} -> {model_api_url}")
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
| 89 |
|
| 90 |
print(f"π Analyzing workflow run: {workflow_run_id}")
|
| 91 |
|
|
@@ -94,16 +133,15 @@ def main():
|
|
| 94 |
logs = download_workflow_logs(workflow_run_id, github_token)
|
| 95 |
if not logs:
|
| 96 |
print("β Failed to download logs")
|
| 97 |
-
sys.exit(
|
| 98 |
|
| 99 |
print(f"π Logs downloaded ({len(logs)} characters)")
|
| 100 |
|
| 101 |
-
# Analyze with model
|
| 102 |
-
print("π€ Sending logs to model API...")
|
| 103 |
result = analyze_logs_with_model(logs, model_api_url)
|
| 104 |
if not result:
|
| 105 |
-
print("β Failed to analyze logs
|
| 106 |
-
sys.exit(
|
| 107 |
|
| 108 |
print(f"π Analysis result: {json.dumps(result, indent=2)}")
|
| 109 |
|
|
|
|
| 26 |
return None
|
| 27 |
|
| 28 |
def analyze_logs_with_model(log_content, model_url):
|
| 29 |
+
"""Send logs to the model API for analysis with fallback to mock response"""
|
| 30 |
try:
|
| 31 |
+
# If no model URL provided, use mock response
|
| 32 |
+
if not model_url or model_url == "https://httpbin.org":
|
| 33 |
+
print("π€ Using mock response (no valid API URL provided)")
|
| 34 |
+
return generate_mock_response(log_content)
|
| 35 |
|
| 36 |
# Ensure the URL has a scheme
|
| 37 |
if not model_url.startswith(('http://', 'https://')):
|
| 38 |
print(f"β MODEL_API_URL is missing scheme: {model_url}")
|
| 39 |
+
print("π‘ Using mock response instead")
|
| 40 |
+
return generate_mock_response(log_content)
|
| 41 |
|
| 42 |
# Construct the full prediction endpoint URL
|
| 43 |
+
if model_url.endswith('/predict'):
|
| 44 |
+
prediction_url = model_url
|
| 45 |
+
else:
|
| 46 |
+
prediction_url = urljoin(model_url.rstrip('/') + '/', 'predict')
|
| 47 |
+
|
| 48 |
print(f"π Calling model API: {prediction_url}")
|
| 49 |
|
| 50 |
response = requests.post(
|
|
|
|
| 61 |
else:
|
| 62 |
print(f"β Model API error: {response.status_code}")
|
| 63 |
print(f"Response: {response.text}")
|
| 64 |
+
print("π‘ Falling back to mock response")
|
| 65 |
+
return generate_mock_response(log_content)
|
| 66 |
|
| 67 |
except requests.exceptions.Timeout:
|
| 68 |
print("β Model API request timed out (30 seconds)")
|
| 69 |
+
print("π‘ Falling back to mock response")
|
| 70 |
+
return generate_mock_response(log_content)
|
| 71 |
except requests.exceptions.ConnectionError:
|
| 72 |
print("β Failed to connect to Model API - check the URL")
|
| 73 |
+
print("π‘ Falling back to mock response")
|
| 74 |
+
return generate_mock_response(log_content)
|
| 75 |
except requests.exceptions.RequestException as e:
|
| 76 |
print(f"β Failed to call model API: {e}")
|
| 77 |
+
print("π‘ Falling back to mock response")
|
| 78 |
+
return generate_mock_response(log_content)
|
| 79 |
except Exception as e:
|
| 80 |
print(f"β Unexpected error: {e}")
|
| 81 |
+
print("π‘ Falling back to mock response")
|
| 82 |
+
return generate_mock_response(log_content)
|
| 83 |
+
|
| 84 |
+
def generate_mock_response(log_content):
|
| 85 |
+
"""Generate a mock response for testing when API is unavailable"""
|
| 86 |
+
# Simple anomaly detection based on log content
|
| 87 |
+
error_keywords = ['error', 'fail', 'failed', 'exception', 'traceback', 'timeout']
|
| 88 |
+
warning_keywords = ['warning', 'slow', 'retry', 'timeout']
|
| 89 |
+
|
| 90 |
+
error_count = sum(1 for keyword in error_keywords if keyword in log_content.lower())
|
| 91 |
+
warning_count = sum(1 for keyword in warning_keywords if keyword in log_content.lower())
|
| 92 |
+
|
| 93 |
+
# Determine if anomaly based on error count
|
| 94 |
+
is_anomaly = error_count > 2 or (error_count > 0 and warning_count > 3)
|
| 95 |
+
|
| 96 |
+
# Calculate confidence based on keyword matches
|
| 97 |
+
confidence = min(0.95, 0.7 + (error_count * 0.1) + (warning_count * 0.05))
|
| 98 |
+
|
| 99 |
+
return {
|
| 100 |
+
'is_anomaly': is_anomaly,
|
| 101 |
+
'confidence': round(confidence, 2),
|
| 102 |
+
'anomaly_probability': 0.8 if is_anomaly else 0.2,
|
| 103 |
+
'explanation': {
|
| 104 |
+
'detection_method': 'mock_analysis',
|
| 105 |
+
'error_keywords_found': [kw for kw in error_keywords if kw in log_content.lower()],
|
| 106 |
+
'warning_keywords_found': [kw for kw in warning_keywords if kw in log_content.lower()],
|
| 107 |
+
'error_count': error_count,
|
| 108 |
+
'warning_count': warning_count,
|
| 109 |
+
'message': 'This analysis used mock detection. Set up a real model API for accurate results.'
|
| 110 |
+
}
|
| 111 |
+
}
|
| 112 |
|
| 113 |
def main():
|
| 114 |
# Get environment variables
|
|
|
|
| 121 |
print(f" WORKFLOW_RUN_ID: {'β
' if workflow_run_id else 'β'} -> {workflow_run_id}")
|
| 122 |
print(f" MODEL_API_URL: {'β
' if model_api_url else 'β'} -> {model_api_url}")
|
| 123 |
|
| 124 |
+
# Only require GITHUB_TOKEN and WORKFLOW_RUN_ID, MODEL_API_URL is optional
|
| 125 |
+
if not github_token or not workflow_run_id:
|
| 126 |
+
print("β Missing required environment variables: GITHUB_TOKEN or WORKFLOW_RUN_ID")
|
| 127 |
+
sys.exit(0) # Exit gracefully, don't fail the workflow
|
| 128 |
|
| 129 |
print(f"π Analyzing workflow run: {workflow_run_id}")
|
| 130 |
|
|
|
|
| 133 |
logs = download_workflow_logs(workflow_run_id, github_token)
|
| 134 |
if not logs:
|
| 135 |
print("β Failed to download logs")
|
| 136 |
+
sys.exit(0) # Exit gracefully, don't fail the workflow
|
| 137 |
|
| 138 |
print(f"π Logs downloaded ({len(logs)} characters)")
|
| 139 |
|
| 140 |
+
# Analyze with model (or mock)
|
|
|
|
| 141 |
result = analyze_logs_with_model(logs, model_api_url)
|
| 142 |
if not result:
|
| 143 |
+
print("β Failed to analyze logs")
|
| 144 |
+
sys.exit(0) # Exit gracefully, don't fail the workflow
|
| 145 |
|
| 146 |
print(f"π Analysis result: {json.dumps(result, indent=2)}")
|
| 147 |
|