diff --git a/api/app/models/alert_dto.py b/api/app/models/alert_dto.py index 29ecfee1..237f6d3c 100644 --- a/api/app/models/alert_dto.py +++ b/api/app/models/alert_dto.py @@ -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 @@ -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] @@ -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) diff --git a/api/app/services/model_service.py b/api/app/services/model_service.py index 5793e5c5..ea8c225d 100644 --- a/api/app/services/model_service.py +++ b/api/app/services/model_service.py @@ -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 diff --git a/api/tests/routes/model_route_test.py b/api/tests/routes/model_route_test.py index 40941452..ae8df3ed 100644 --- a/api/tests/routes/model_route_test.py +++ b/api/tests/routes/model_route_test.py @@ -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'], ), ]