File size: 7,802 Bytes
81e3338
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
"""
Pydantic schemas for the CloudPulse AI service.

These define the request and response contracts that the data engineer's pipeline
will conform to. Every endpoint is fully typed, every field is documented.
"""
from datetime import datetime
from typing import Literal, Optional

from pydantic import BaseModel, Field

# Severity values used across the system
Severity4Class = Literal["low", "medium", "high", "critical"]
Severity3Class = Literal["low", "medium", "high"]
SeverityBinary = Literal["severe", "not_severe"]
OriginalImpact = Literal["none", "minor", "major", "critical"]


# ─────────────────────────────────────────────────────────────────────────────
# /classify
# ─────────────────────────────────────────────────────────────────────────────
class ClassifyRequest(BaseModel):
    """Incident classification request from the data engineer's pipeline."""

    incident_id: str = Field(..., description="Unique incident identifier")
    description: str = Field(..., min_length=1, description="Raw incident description text")
    original_impact: Optional[OriginalImpact] = Field(
        None,
        description="Provider's own severity tag from the upstream API. Used by the "
                    "escalation rule to upgrade high to critical when applicable.",
    )
    provider: Optional[str] = Field(None, description="Cloud or service provider name")
    service: Optional[str]  = Field(None, description="Affected service name")
    affected_services_count: Optional[int] = Field(
        None, ge=0, description="Number of affected services for anomaly detection"
    )
    service_impact: Optional[str] = Field(
        None, description="Type of service impact (compute, ai, platform, storage, network)"
    )


class RemediationBlock(BaseModel):
    """Structured remediation generated by Claude Haiku."""

    summary: str
    root_cause_hypothesis: str
    immediate_mitigation: list[str]
    investigation: list[str]
    prevention: list[str]


class ClassifyResponse(BaseModel):
    """Full classification response sent back to the pipeline."""

    incident_id: str

    ml_severity_3class: Severity3Class = Field(
        ..., description="Raw 3-class prediction from the trained classifier"
    )
    final_severity: Severity4Class = Field(
        ..., description="Final label after escalation rule. high becomes critical "
                         "if original_impact is critical."
    )
    severity_binary: SeverityBinary = Field(
        ..., description="Binary classification (severe vs not_severe)"
    )

    confidence: Literal["high", "medium", "low"] = Field(
        ..., description="Confidence label derived from model probability"
    )
    confidence_score: float = Field(
        ..., ge=0.0, le=1.0, description="Probability of the predicted class"
    )

    escalated: bool = Field(
        ..., description="True if the rule upgraded the model prediction to critical"
    )
    escalation_reason: Optional[str] = Field(
        None, description="Why the escalation fired (or None)"
    )

    explanation: dict = Field(
        ..., description="Top contributing TF-IDF terms with weights"
    )
    anomaly: dict = Field(
        ..., description="Anomaly detection result"
    )
    remediation: Optional[RemediationBlock] = Field(
        None, description="Structured remediation. Returned when ?include_remediation=true"
    )

    processing_ms: int


# ─────────────────────────────────────────────────────────────────────────────
# /similar
# ─────────────────────────────────────────────────────────────────────────────
class SimilarRequest(BaseModel):
    description: str = Field(..., min_length=1)
    top_k: int = Field(3, ge=1, le=10)


class SimilarIncident(BaseModel):
    incident_id: str
    provider: str
    service: Optional[str]
    severity: str
    description: str
    similarity: float = Field(..., ge=0.0, le=1.0)


class SimilarResponse(BaseModel):
    results: list[SimilarIncident]


# ─────────────────────────────────────────────────────────────────────────────
# /metrics
# ─────────────────────────────────────────────────────────────────────────────
class MetricsResponse(BaseModel):
    """Static evaluation results loaded from notebook 09."""
    dataset_size: int
    train_size: int
    val_size: int
    test_size: int
    best_3class_model: str
    best_3class_macro_f1: float
    best_binary_model: str
    best_binary_macro_f1: float
    per_class_f1: dict


# ─────────────────────────────────────────────────────────────────────────────
# /health
# ─────────────────────────────────────────────────────────────────────────────
class HealthResponse(BaseModel):
    status: Literal["ok", "degraded"]
    artefacts_loaded: dict[str, bool]
    timestamp: datetime

# ─────────────────────────────────────────────────────────────────────────────
# /weekly
# ─────────────────────────────────────────────────────────────────────────────
class ClassifiedIncidentInput(BaseModel):
    """Input for an already-classified incident in the weekly batch.

    Mădălina'\''s pipeline classifies incidents via /classify throughout the week,
    then batches them and posts to /weekly for the natural-language report.
    """
    incident_id   : str
    provider      : str
    service       : Optional[str] = None
    final_severity: Severity4Class
    description   : Optional[str] = None


class WeeklyRequest(BaseModel):
    """Weekly intelligence pass request."""
    period_start : str = Field(..., description="ISO date, e.g. 2026-04-27")
    period_end   : str = Field(..., description="ISO date, e.g. 2026-05-04")
    incidents    : list[ClassifiedIncidentInput]
    previous_week: Optional[list[ClassifiedIncidentInput]] = Field(
        None,
        description="Previous week incidents for week-on-week trend deltas. Optional.",
    )


class WeeklyResponse(BaseModel):
    """Weekly intelligence pass response."""
    period_start   : str
    period_end     : str
    total_incidents: int
    summary        : str = Field(..., description="Natural-language report from Claude Haiku")
    aggregation    : Optional[dict] = Field(
        None,
        description="Deterministic aggregation: hotspots, severity counts, trend deltas, notable incidents",
    )