Skip to content

Commit

Permalink
fix: pass translated uuids directly to method
Browse files Browse the repository at this point in the history
  • Loading branch information
saraburns1 committed Oct 3, 2024
1 parent ecdd0da commit ca60ad7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@
from superset.models.dashboard import Dashboard
from superset.models.slice import Slice
from superset.connectors.sqla.models import SqlaTable
from superset.utils.database import get_or_create_db
from superset.models.embedded_dashboard import EmbeddedDashboard
from pythonpath.create_assets_utils import load_configs_from_directory
from pythonpath.delete_assets import delete_assets
from yaml.representer import Representer

from pythonpath.localization import get_translation
from pythonpath.create_row_level_security import create_rls_filters
Expand Down Expand Up @@ -62,8 +60,6 @@ def main():
def create_assets():
"""Create assets from a yaml file."""
roles = {}

yaml.add_representer(defaultdict, Representer.represent_dict)
translated_asset_uuids = defaultdict(set)

for root, dirs, files in os.walk(ASSETS_PATH):
Expand All @@ -86,25 +82,20 @@ def create_assets():
process_asset(asset, roles, translated_asset_uuids)


for uuid in translated_asset_uuids:
translated_asset_uuids[uuid] = list(translated_asset_uuids[uuid])

# Write parent UUID & translated child UUIDs to yaml file
path = os.path.join(PYTHONPATH,"translated_asset_mapping.yaml")
with open(path, "w") as file:
yaml.dump(translated_asset_uuids, file, default_flow_style=False)
# for uuid in translated_asset_uuids:
# translated_asset_uuids[uuid] = list(translated_asset_uuids[uuid])

import_assets()
update_dashboard_roles(roles)
update_embeddable_uuids()
update_datasets()
create_rls_filters()

# Delete unused UUIDs from yaml list
# Delete unused assets
with open(os.path.join(PYTHONPATH,"aspects_asset_list.yaml"), "r", encoding="utf-8") as file:
assets = yaml.safe_load(file)
unused_aspect_uuids = assets['unused_uuids']
delete_assets(unused_aspect_uuids)
delete_assets(unused_aspect_uuids, translated_asset_uuids)

def process_asset(asset, roles, translated_asset_uuids):
if FILE_NAME_ATTRIBUTE not in asset:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
"""Delete all unused Aspects assets from Superset tables"""
import os

import logging
import yaml
from collections import defaultdict
from flask import g

from superset import security_manager
Expand All @@ -13,11 +9,9 @@
from superset.tags.models import TaggedObject, ObjectType
from superset.commands.chart.delete import DeleteChartCommand
from superset.commands.dataset.delete import DeleteDatasetCommand
from superset.commands.tag.delete import DeleteTagsCommand
from sqlalchemy.exc import NoResultFound
from superset.commands.exceptions import CommandInvalidError


logger = logging.getLogger("delete_assets")
PYTHONPATH = "/app/pythonpath"

Expand All @@ -26,13 +20,8 @@
ASSET_COMMANDS = {'charts': DeleteChartCommand, 'datasets': DeleteDatasetCommand}
OBJECT_TYPES = {'charts': ObjectType.chart, 'datasets': ObjectType.dataset}

def delete_assets(unused_uuids):
def delete_assets(unused_uuids, translated_asset_uuids):
"""Delete unused assets and their translated versions"""
with open(os.path.join(PYTHONPATH,"translated_asset_mapping.yaml"),'r', encoding="utf-8") as file:
mapping = yaml.safe_load_all(file)
for line in mapping:
translated_ids = line

for type in unused_uuids:
id_list = []
asset_list = set()
Expand All @@ -42,8 +31,8 @@ def delete_assets(unused_uuids):
id_list.append(row.id)
asset_list.add(getattr(row,ASSET_NAME_COLUMN[type]))

if uuid in translated_ids:
for child_uuid in translated_ids[uuid]:
if uuid in translated_asset_uuids:
for child_uuid in translated_asset_uuids[uuid]:
row = db.session.query(ASSET_TABLES[type]).filter_by(uuid=child_uuid).one()
id_list.append(row.id)
asset_list.add(getattr(row,ASSET_NAME_COLUMN[type]))
Expand Down

0 comments on commit ca60ad7

Please sign in to comment.