Skip to content

Commit

Permalink
refactor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
iakov-aws committed Aug 30, 2023
1 parent 4a77022 commit d369274
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions cid/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,20 +723,27 @@ def delete_view(self, view_name):

@command
def cleanup(self, **kwargs):
"""Delete unused resources (QuickSight datasets, Athena views)"""
"""Delete unused resources (QuickSight datasets not used in Dashboards)"""

self.qs.discover_dashboards()
self.qs.discover_datasets()
used_datasets = [x for v in self.qs.dashboards.values() for x in v.datasets.values() ]
for v in list(self.qs._datasets.values()):
if v.id not in used_datasets and click.confirm(f'Delete unused dataset {v.name}?'):
logger.info(f'Deleting dataset {v.name} ({v.arn})')
self.qs.delete_dataset(v.id)
logger.info(f'Deleted dataset {v.name} ({v.arn})')
print(f'Deleted dataset {v.name} ({v.arn})')
else:
print(f'Dataset {v.name} ({v.arn}) is in use')

references = {}
for dashboard in self.qs.dashboards.values():
for dataset_id in dashboard.datasets.values():
if dataset_id not in references:
references[dataset_id] = []
references[dataset_id].append(dashboard.id)
for dataset in self.qs._datasets.values():
if dataset.id in references:
cid_print(f'Dataset {dataset.name} ({dataset.id}) is in use ({", ".join(references[dataset.id])})')
continue
if get_yesno_parameter(f'confirm-delete-dataset-{dataset.id}',
message=f'Delete dataset "{dataset.name}" (not used in dashboards, but can be used in analysis)?',
default='no',
):
logger.info(f'Deleting dataset {dataset.name} ({dataset.id})')
self.qs.delete_dataset(dataset.id)
cid_print(f'Deleted dataset {dataset.name} ({dataset.id})')

@command
def share(self, dashboard_id, **kwargs):
Expand Down

0 comments on commit d369274

Please sign in to comment.