import logging from typing import List from service.tasks.audience import soft_delete_audience, unshare_fb_audience from service.tasks.delete_collection import _delete_collection, _start_delete_collection from service.utils.aws_connectors import run_query from .basic import Consequence, Trigger from .deletion_triggers import ( PostDeletionGenerateAllianceSegmentsTrigger, PostDeletionNotifyAllianceDeletedTrigger, PostDeletionRefreshMaterializedQueriesTrigger, UpdateCAWMetaTrigger, ) logger = logging.getLogger(__name__) class AudienceUnsharedConsequence(Consequence): description = "These shared Audiences will be unshared" params_to_keep = ["schema", "audience_internal_id", "adaccount_id"] def execute(self, context, schema, audience_internal_id, adaccount_id, **kw): """Implement to make the consequence happen""" rid = f"{schema} {audience_internal_id}" if not self.is_resolved_id(context, rid): logger.info(f"Unshared Audeince {audience_internal_id}") unshare_fb_audience( schema, context["user_id"], audience_internal_id, adaccount_id, context["management_schema"], ) self.add_resolved_id(context, rid) return super().execute(context) class AudienceDeletedConsequence(Consequence): description = "These Audiences will be deleted/deactivated" params_to_keep = ["schema", "audience_internal_id"] def execute(self, context, schema, audience_internal_id, **kw): """Implement to make the consequence happen""" logger.info(f"Deleted Audience {audience_internal_id}") try: soft_delete_audience( schema, context["management_schema"], context["user_id"], audience_internal_id, ) except Exception as e: logger.exception("FAILED TO DELETE AUDIENCE", exc_info=e) return super().execute(context) class CollectionDeletedConsequence(Consequence): description = "These segments will be deleted" params_to_keep = ["schema", "collection_ids", "user_id", "done_by_parent"] def __len__(self): return len(self.params["collection_ids"]) def pre_execute( # type: ignore self, context, schema, collection_ids, user_id, done_by_parent=None, **kw ) -> List[Trigger]: """Implement to do something immediately before execution""" if not done_by_parent: _start_delete_collection(schema, user_id, collection_ids) return [] def execute( # type: ignore self, context, schema, collection_ids, user_id, done_by_parent=None, **kw ) -> List[Trigger]: """Implement to make the consequence happen""" if not done_by_parent: logger.info( f"{self.__class__.__name__}: {self.description} - {collection_ids}" ) try: _delete_collection( schema, context["management_schema"], user_id, collection_ids ) except Exception as e: logger.exception("Collection delete error: ", exc_info=e) return [ UpdateCAWMetaTrigger(context, schema=schema), PostDeletionRefreshMaterializedQueriesTrigger(context, schema=schema), ] return super().execute(context) class WorkspaceSegmentDeletedConsequence(CollectionDeletedConsequence): description = "These Workspace segments will be deleted" class AllianceSegmentDeletedConsequence(CollectionDeletedConsequence): description = "These Alliance segments will be deleted" class FilteringSetDeletedConsequence(CollectionDeletedConsequence): description = "These filtering results will be deleted" visible = False class AllianceSourceDeletedConsequence(CollectionDeletedConsequence): description = "These Alliance datasets will be deleted" def execute(self, context, schema, collection_ids, user_id, **kw): triggers = super().execute(context, schema, collection_ids, user_id, **kw) triggers.append( PostDeletionGenerateAllianceSegmentsTrigger( context, schema=schema, user_id=user_id ) ) return triggers class WorkspaceSourceDeletedConsequence(CollectionDeletedConsequence): description = "These source collections will be deleted" class AudienceCollectionDeletedConsequence(Consequence): description = "Just in-between step, not presented to user" params_to_keep = ["schema", "collection_ids", "user_id", "done_by_parent"] visible = False def __len__(self): return len(self.params["collection_ids"]) def execute(self, context, **kw): """Will be deleted as part of audience AudienceDeletedConsequence, so nothing to do""" return super().execute(context) class SupersetDeletedConsequence(CollectionDeletedConsequence): description = "These Supersets will be deleted" class SupersetSegmentDeletedConsequence(CollectionDeletedConsequence): description = "Just in-between step, not presented to user" visible = False class WorkspaceDeletedConsequence(Consequence): description = "Workspace will be deleted" params_to_keep = ["workspace_schema", "management_schema"] def execute(self, context, workspace_schema, management_schema, **kw): """Will be deleted as part of audience AudienceDeletedConsequence, so nothing to do""" run_query( [ f"DELETE FROM {management_schema}.workspace " f"WHERE id = '{workspace_schema}';", f"DELETE FROM {management_schema}.user_roles " f"WHERE id = '{workspace_schema}' AND company_alliance_workspace = 'workspace';", f"DROP SCHEMA {workspace_schema} CASCADE;", ], fetch=False, ) return [] class AllianceDeletedConsequence(Consequence): description = "Alliance will be deleted" params_to_keep = ["alliance_schema", "management_schema"] def execute(self, context, alliance_schema, management_schema, **kw): """Will be deleted as part of audience AudienceDeletedConsequence, so nothing to do""" run_query( [ f"DELETE FROM {management_schema}.alliance " f"WHERE id = '{alliance_schema}';", f"DELETE FROM {management_schema}.user_roles " f"WHERE id = '{alliance_schema}' AND company_alliance_workspace = 'alliance';", f"DELETE FROM commons.company_alliance " f"WHERE alliance_id = '{alliance_schema}';", f"DROP SCHEMA {alliance_schema} CASCADE;", ], fetch=False, ) self.add_resolved_id(context, alliance_schema) return [ PostDeletionNotifyAllianceDeletedTrigger( context, alliance_schema=alliance_schema ) ]