From 2505c771925383f2c63bc3b4ff3fd91ff93088a6 Mon Sep 17 00:00:00 2001 From: Stefano Zamboni Date: Mon, 28 Oct 2024 14:41:53 +0100 Subject: [PATCH] fix: fixed alerts when importing --- api/app/services/model_service.py | 55 ++++++++++++++++--------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/api/app/services/model_service.py b/api/app/services/model_service.py index 710fcf4c..5793e5c5 100644 --- a/api/app/services/model_service.py +++ b/api/app/services/model_service.py @@ -137,34 +137,35 @@ def get_last_n_alerts(self, n_alerts) -> List[AlertDTO]: latest_reference_dataset, latest_current_dataset = self.get_latest_datasets( model.uuid ) - for perc in ['data_quality', 'model_quality', 'drift']: - if count_alerts == n_alerts: - 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 - ], - } + if metrics: + for perc in ['data_quality', 'model_quality', 'drift']: + if count_alerts == n_alerts: + 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 + ], + } + ) ) - ) - count_alerts += 1 + count_alerts += 1 return res