diff --git a/elementary/monitor/api/alerts/alert_filters.py b/elementary/monitor/api/alerts/alert_filters.py index d2dddbb8f..47a9daef8 100644 --- a/elementary/monitor/api/alerts/alert_filters.py +++ b/elementary/monitor/api/alerts/alert_filters.py @@ -98,7 +98,7 @@ def _filter_alerts_by_owner( filtered_alerts = [] for alert in alerts: - raw_owners = alert.owners + raw_owners = alert.unified_owners alert_owners = ( try_load_json(raw_owners) if isinstance(raw_owners, str) else raw_owners ) diff --git a/elementary/monitor/fetchers/alerts/schema/pending_alerts.py b/elementary/monitor/fetchers/alerts/schema/pending_alerts.py index 7bea37213..4f3a5c936 100644 --- a/elementary/monitor/fetchers/alerts/schema/pending_alerts.py +++ b/elementary/monitor/fetchers/alerts/schema/pending_alerts.py @@ -17,7 +17,7 @@ ALERTS_CONFIG_KEY = "alerts_config" CHANNEL_KEY = "channel" DESCRIPTION_KEY = "description" -OWNERS_KEY = "owners" +OWNER_KEY = "owner" SUBSCRIBERS_KEY = "subscribers" ALERT_FIELDS_KEY = "alert_fields" ALERT_SUPPRESSION_INTERVAL_KEY = "alert_suppression_interval" @@ -32,6 +32,7 @@ class BasePendingAlertSchema(BaseModel): database_name: Optional[str] = None schema_name: str tags: Optional[List[str]] = None + owners: Optional[List[str]] = None model_meta: Optional[Dict] = None suppression_status: str sent_at: Optional[datetime] = None @@ -54,8 +55,11 @@ def group_alerts_by(self) -> Optional[str]: return self.unified_meta.get(GROUP_ALERTS_BY_KEY) @property - def owners(self) -> List[str]: - return self._get_alert_meta_attrs(OWNERS_KEY) + def unified_owners(self) -> List[str]: + # Make sure we return both meta defined owners and config defined owners. + config_owners = self.owners or [] + meta_owners = self._get_alert_meta_attrs(OWNER_KEY) + return list(set(config_owners + meta_owners)) @property def subscribers(self) -> List[str]: @@ -77,6 +81,10 @@ def validate_model_meta(cls, model_meta: Optional[Dict]) -> Dict: def validate_tags(cls, tags: Optional[Union[List[str], str]]): return unpack_and_flatten_and_dedup_list_of_strings(tags) + @validator("owners", pre=True, always=True) + def validate_owners(cls, owners: Optional[Union[List[str], str]]): + return unpack_and_flatten_and_dedup_list_of_strings(owners) + @staticmethod def _flatten_meta(meta: Optional[Dict] = None) -> Dict: return flatten_dict_by_key(meta, ALERTS_CONFIG_KEY) if meta else dict() @@ -199,7 +207,7 @@ def format_alert( detected_at=self.detected_at, database_name=self.database_name, schema_name=self.schema_name, - owners=self.owners, + owners=self.unified_owners, tags=self.tags, subscribers=self.subscribers, status=self.status, @@ -245,7 +253,7 @@ def format_alert( detected_at=self.detected_at, database_name=self.database_name, schema_name=self.schema_name, - owners=self.owners, + owners=self.unified_owners, tags=self.tags, subscribers=self.subscribers, status=self.status, @@ -304,7 +312,7 @@ def format_alert( detected_at=self.detected_at, database_name=self.database_name, schema_name=self.schema_name, - owners=self.owners, + owners=self.unified_owners, tags=self.tags, subscribers=self.subscribers, status=self.status, diff --git a/tests/unit/monitor/api/alerts/test_alert_filters.py b/tests/unit/monitor/api/alerts/test_alert_filters.py index b9805f043..67acbfc4d 100644 --- a/tests/unit/monitor/api/alerts/test_alert_filters.py +++ b/tests/unit/monitor/api/alerts/test_alert_filters.py @@ -29,7 +29,7 @@ def initial_alerts(): test_name="test_1", test_created_at="2022-10-10 10:10:10", tags='["one", "two"]', - model_meta=dict(owners='["jeff", "john"]'), + model_meta=dict(owner='["jeff", "john"]'), status="fail", elementary_unique_id="elementary.model_id_1.test_id_1.9cf2f5f6ad.None.generic", detected_at="2022-10-10 10:00:00", @@ -52,7 +52,7 @@ def initial_alerts(): test_name="test_2", test_created_at="2022-10-10 09:10:10", tags='["three"]', - model_meta=dict(owners='["jeff", "john"]'), + model_meta=dict(owner='["jeff", "john"]'), status="fail", elementary_unique_id="elementary.model_id_1.test_id_2.9cf2f5f6ad.None.generic", detected_at="2022-10-10 10:00:00", @@ -76,7 +76,7 @@ def initial_alerts(): test_created_at="2022-10-10 10:10:10", # invalid tag tags="one", - model_meta=dict(owners='["john"]'), + model_meta=dict(owner='["john"]'), status="fail", elementary_unique_id="elementary.model_id_1.test_id_3.9cf2f5f6ad.None.generic", detected_at="2022-10-10 10:00:00", @@ -99,7 +99,7 @@ def initial_alerts(): test_name="test_4", test_created_at="2022-10-10 09:10:10", tags='["three", "four"]', - model_meta=dict(owners='["jeff"]'), + model_meta=dict(owner='["jeff"]'), status="warn", elementary_unique_id="elementary.model_id_1.test_id_4.9cf2f5f6ad.None.generic", detected_at="2022-10-10 10:00:00", @@ -129,7 +129,7 @@ def initial_alerts(): detected_at="2022-10-10 10:00:00", alert_suppression_interval=0, tags='["one", "two"]', - model_meta=dict(owners='["jeff", "john"]'), + model_meta=dict(owner='["jeff", "john"]'), status="error", database_name="test_db", schema_name="test_schema", @@ -148,7 +148,7 @@ def initial_alerts(): detected_at="2022-10-10 09:00:00", alert_suppression_interval=3, tags='["three"]', - model_meta=dict(owners='["john"]'), + model_meta=dict(owner='["john"]'), status="error", database_name="test_db", schema_name="test_schema", @@ -167,7 +167,7 @@ def initial_alerts(): detected_at="2022-10-10 08:00:00", alert_suppression_interval=1, tags='["three", "four"]', - model_meta=dict(owners='["jeff"]'), + model_meta=dict(owner='["jeff"]'), status="skipped", database_name="test_db", schema_name="test_schema", @@ -189,7 +189,7 @@ def initial_alerts(): detected_at="2022-10-10 10:00:00", alert_suppression_interval=0, tags='["one", "two"]', - model_meta=dict(owners='["jeff", "john"]'), + model_meta=dict(owner='["jeff", "john"]'), status="error", normalized_status="fail", snapshotted_at="2023-08-15T12:26:06.884065+00:00", @@ -219,7 +219,7 @@ def initial_alerts(): detected_at="2022-10-10 10:00:00", alert_suppression_interval=0, tags='["one", "two"]', - model_meta=dict(owners='["jeff", "john"]'), + model_meta=dict(owner='["jeff", "john"]'), status="warn", normalized_status="warn", snapshotted_at="2023-08-15T12:26:06.884065+00:00", @@ -249,7 +249,7 @@ def initial_alerts(): detected_at="2022-10-10 10:00:00", alert_suppression_interval=0, tags='["one", "two"]', - model_meta=dict(owners='["jeff", "john"]'), + model_meta=dict(owner='["jeff", "john"]'), status="runtime error", normalized_status="error", snapshotted_at="2023-08-15T12:26:06.884065+00:00",