Skip to content

Commit

Permalink
feat: updated alerts (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
SteZamboni authored Oct 29, 2024
1 parent b422cb3 commit 2d2a015
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 46 deletions.
13 changes: 3 additions & 10 deletions api/app/models/alert_dto.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from enum import Enum
from typing import Dict, List, Optional
from typing import List, Optional
from uuid import UUID

from pydantic import BaseModel, ConfigDict
Expand All @@ -9,10 +9,11 @@
class AnomalyType(str, Enum):
DATA_QUALITY = 'DATA_QUALITY'
MODEL_QUALITY = 'MODEL_QUALITY'
DRIFT = 'DRIFT'
DRIFT = 'DATA_DRIFT'


class AlertDTO(BaseModel):
model_name: str
model_uuid: UUID
reference_uuid: Optional[UUID]
current_uuid: Optional[UUID]
Expand All @@ -24,11 +25,3 @@ class AlertDTO(BaseModel):
populate_by_name=True,
alias_generator=to_camel,
)

@staticmethod
def from_dict(
alert_data: Optional[Dict],
) -> 'AlertDTO':
"""Create a AlertDTO from a dictionary of data."""

return AlertDTO.model_validate(alert_data)
39 changes: 19 additions & 20 deletions api/app/services/model_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,26 +143,25 @@ def get_last_n_alerts(self, n_alerts) -> List[AlertDTO]:
return res
if 0 <= metrics.percentages[perc]['value'] < 1:
res.append(
AlertDTO.from_dict(
{
'model_uuid': model.uuid,
'reference_uuid': latest_reference_dataset.uuid
if latest_reference_dataset
else None,
'current_uuid': latest_current_dataset.uuid
if latest_current_dataset
else None,
'anomaly_type': AnomalyType[perc.upper()],
'anomaly_features': [
x['feature_name']
for x in sorted(
metrics.percentages[perc]['details'],
key=lambda e: e['score'],
reverse=True,
)
if x['score'] > 0
],
}
AlertDTO(
model_name=model.name,
model_uuid=model.uuid,
reference_uuid=latest_reference_dataset.uuid
if latest_reference_dataset
else None,
current_uuid=latest_current_dataset.uuid
if latest_current_dataset
else None,
anomaly_type=AnomalyType[perc.upper()],
anomaly_features=[
x['feature_name']
for x in sorted(
metrics.percentages[perc]['details'],
key=lambda e: e['score'],
reverse=True,
)
if x['score'] > 0
],
)
)
count_alerts += 1
Expand Down
30 changes: 14 additions & 16 deletions api/tests/routes/model_route_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,23 +170,21 @@ def test_get_last_n_alerts(self):
)

sample_alerts_out = [
AlertDTO.from_dict(
{
'model_uuid': model1.uuid,
'reference_uuid': None,
'current_uuid': current1.uuid,
'anomaly_type': AnomalyType.DRIFT,
'anomaly_features': ['num1', 'num2'],
}
AlertDTO(
model_name=model1.name,
model_uuid=model1.uuid,
reference_uuid=None,
current_uuid=current1.uuid,
anomaly_type=AnomalyType.DRIFT,
anomaly_features=['num1', 'num2'],
),
AlertDTO.from_dict(
{
'model_uuid': model0.uuid,
'reference_uuid': None,
'current_uuid': current0.uuid,
'anomaly_type': AnomalyType.DRIFT,
'anomaly_features': ['num1', 'num2'],
}
AlertDTO(
model_name=model0.name,
model_uuid=model0.uuid,
reference_uuid=None,
current_uuid=current0.uuid,
anomaly_type=AnomalyType.DRIFT,
anomaly_features=['num1', 'num2'],
),
]

Expand Down

0 comments on commit 2d2a015

Please sign in to comment.