-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
157 lines (142 loc) · 4.76 KB
/
main.py
File metadata and controls
157 lines (142 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import os
import traceback
from pathlib import Path
from fastapi import FastAPI
from fastapi.responses import FileResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
import google.generativeai as genai
from pydantic import BaseModel
from typing import List, Optional, Any
import json
# Define request/response models
class GenerateRequest(BaseModel):
alert_type: str
service: str
quantity: int
class AnalyzeRequest(BaseModel):
alerts: List[Any]
app = FastAPI(title="SENTINEL AIOps API", version="1.0.0")
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
# Mount static files
app.mount("/static", StaticFiles(directory="."), name="static")
@app.get("/")
async def serve_frontend():
return FileResponse(Path.cwd() / "index.html")
# ============= SIMPLIFIED ENDPOINTS =============
@app.get("/health")
async def health_check():
return {
"status": "healthy",
"gemini_configured": False,
"stored_alerts": 0
}
@app.post("/generate")
async def generate_alerts(request: GenerateRequest):
try:
alerts = []
for i in range(request.quantity):
alerts.append({
"timestamp": "2024-01-01T12:00:00Z",
"service": request.service or f"service-{i}",
"type": request.alert_type or "error",
"message": f"Alert {i+1}",
"severity": ["CRITICAL", "HIGH", "MEDIUM"][i % 3]
})
return {"alerts": alerts}
except Exception as e:
return JSONResponse(status_code=500, content={"error": str(e)})
@app.post("/simulate-incident")
async def simulate_incident():
try:
return {
"alerts": [
{
"timestamp": "2024-01-01T12:00:00Z",
"service": "database",
"type": "connection_failure",
"message": "Database connection failed",
"severity": "CRITICAL"
}
],
"chain_summary": ["DB_FAILURE", "API_TIMEOUT"],
"count": 1
}
except Exception as e:
return JSONResponse(status_code=500, content={"error": str(e)})
@app.post("/analyze")
async def analyze_alerts(request: AnalyzeRequest):
try:
return {
"noise_removed": 0,
"filtered_alerts": len(request.alerts),
"total_alerts": len(request.alerts),
"ai_summary": "Analysis complete.",
"security_threats": [],
"future_prediction": {
"prediction": "NOMINAL",
"confidence": "HIGH",
"message": "System stable",
"eta": "24h",
"risk_factors": {"critical_alerts": 0, "high_alerts": 0, "affected_services": 0}
},
"root_cause": {"service": "unknown", "confidence": "LOW", "affected": []},
"cascade_chain": [],
"top_alerts": [],
"clusters": [],
"priority_ranking": [],
"recommendations": [],
"severity_distribution": {"CRITICAL": 0, "HIGH": 0, "MEDIUM": 0},
"type_counts": {}
}
except Exception as e:
return JSONResponse(status_code=500, content={"error": str(e)})
@app.get("/stats")
async def get_stats():
try:
return {
"reduction_percent": 0,
"total_raw_alerts": 0,
"total_noise_removed": 0,
"total_clean_alerts": 0,
"by_severity": {"CRITICAL": 0, "HIGH": 0, "MEDIUM": 0},
"top_alert_types": {},
"by_service": {}
}
except Exception as e:
return JSONResponse(status_code=500, content={"error": str(e)})
@app.get("/history")
async def get_history(limit: int = 20):
try:
return {"analyses": []}
except Exception as e:
return JSONResponse(status_code=500, content={"error": str(e)})
@app.get("/history/{analysis_id}")
async def get_analysis(analysis_id: int):
try:
return {
"id": analysis_id,
"noise_removed": 0,
"filtered_alerts": 0,
"total_alerts": 0,
"ai_summary": f"Analysis #{analysis_id}",
"security_threats": [],
"future_prediction": {},
"root_cause": {},
"cascade_chain": [],
"severity_distribution": {}
}
except Exception as e:
return JSONResponse(status_code=500, content={"error": str(e)})
@app.post("/clear-all")
async def clear_all():
try:
return {"status": "success", "message": "All history cleared"}
except Exception as e:
return JSONResponse(status_code=500, content={"error": str(e)})