Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactoring: speed up working with dashboard #1081

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 22 additions & 30 deletions cid/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import urllib
import logging
import functools
import webbrowser
from string import Template
from typing import Dict
from pkg_resources import resource_string
from importlib.metadata import entry_points
from functools import cached_property

import yaml
import click
import requests
from botocore.exceptions import ClientError, NoCredentialsError, CredentialRetrievalError

Expand Down Expand Up @@ -433,9 +433,7 @@ def _deploy(self, dashboard_id: str=None, recursive=True, update=False, **kwargs

self.ensure_subscription()

# In case if we cannot discover datasets, we need to discover dashboards
# TODO: check if datasets returns explicit permission denied and only then discover dashboards as a workaround
self.qs.dashboards
self.qs.pre_discover()

dashboard_id = dashboard_id or get_parameters().get('dashboard-id')
category_filter = [cat for cat in get_parameters().get('category', '').upper().split(',') if cat]
Expand Down Expand Up @@ -478,7 +476,7 @@ def _deploy(self, dashboard_id: str=None, recursive=True, update=False, **kwargs
dashboard_definition = self.get_definition("dashboard", id=dashboard_id)
dashboard = None
try:
dashboard = self.qs.discover_dashboard(dashboardId=dashboard_id)
dashboard = self.qs.discover_dashboard(dashboard_id)
except CidCritical:
pass

Expand Down Expand Up @@ -653,19 +651,17 @@ def open(self, dashboard_id, **kwargs):
if not dashboard_id:
dashboard_id = self.qs.select_dashboard(force=True)

dashboard = self.qs.discover_dashboard(dashboardId=dashboard_id)

click.echo('Getting dashboard status...', nl=False)
if dashboard is not None:
if dashboard.version.get('Status') not in ['CREATION_SUCCESSFUL']:
print(json.dumps(dashboard.version.get('Errors'),
indent=4, sort_keys=True, default=str))
click.echo(
f'\nDashboard is unhealthy, please check errors above.')
click.echo('healthy, opening...')
click.launch(self.qs_url.format(dashboard_id=dashboard_id, **self.qs_url_params))
else:
click.echo('not deployed.')
dashboard = self.qs.discover_dashboard(dashboard_id)

logger.info('Getting dashboard status...')
if not dashboard:
logger.error(f'{dashboard_id} is not deployed.')
return None
if dashboard.version.get('Status') not in ['CREATION_SUCCESSFUL', 'UPDATE_IN_PROGRESS', 'UPDATE_SUCCESSFUL']:
cid_print(json.dumps(dashboard.version.get('Errors'), indent=4, sort_keys=True, default=str))
cid_print(f'Dashboard {dashboard_id} is unhealthy, please check errors above.')
logger.info('healthy, opening...')
webbrowser.open(self.qs_url.format(dashboard_id=dashboard_id, **self.qs_url_params))

return dashboard_id

Expand All @@ -682,7 +678,7 @@ def status(self, dashboard_id, **kwargs):
if not dashboard_id:
print('No dashboard selected')
return
dashboard = self.qs.discover_dashboard(dashboardId=dashboard_id)
dashboard = self.qs.discover_dashboard(dashboard_id)

if dashboard is not None:
dashboard.display_status()
Expand Down Expand Up @@ -724,11 +720,7 @@ def status(self, dashboard_id, **kwargs):
logger.info(f'Updating dashboard: {dashboard.id} with Recursive = {recursive}')
self._deploy(dashboard_id, recursive=recursive, update=True)
logger.info('Rediscover dashboards after update')

refresh_overrides = [
dashboard.id
]
self.qs.discover_dashboards(refresh_overrides = refresh_overrides)
self.qs.discover_dashboards(refresh_overrides=[dashboard.id])
self.qs.clear_dashboard_selection()
dashboard_id = None
else:
Expand All @@ -747,7 +739,7 @@ def delete(self, dashboard_id, **kwargs):
return

if self.qs.dashboards and dashboard_id in self.qs.dashboards:
datasets = self.qs.discover_dashboard(dashboardId=dashboard_id).datasets # save for later
datasets = self.qs.discover_dashboard(dashboard_id).datasets # save for later
else:
dashboard_definition = self.get_definition("dashboard", id=dashboard_id)
datasets = {d: None for d in (dashboard_definition or {}).get('dependsOn', {}).get('datasets', [])}
Expand Down Expand Up @@ -854,7 +846,7 @@ def delete_view(self, view_name):
def cleanup(self, **kwargs):
"""Delete unused resources (QuickSight datasets not used in Dashboards)"""

self.qs.discover_dashboards()
self.qs.pre_discover()
self.qs.discover_datasets()
references = {}
for dashboard in self.qs.dashboards.values():
Expand Down Expand Up @@ -892,9 +884,9 @@ def _share(self, dashboard_id, **kwargs):
return
else:
# Describe dashboard by the ID given, no discovery
self.qs.discover_dashboard(dashboardId=dashboard_id)
self.qs.discover_dashboard(dashboard_id)

dashboard = self.qs.discover_dashboard(dashboardId=dashboard_id)
dashboard = self.qs.discover_dashboard(dashboard_id)

if dashboard is None:
print('not deployed.')
Expand Down Expand Up @@ -1065,7 +1057,7 @@ def update(self, dashboard_id, recursive=False, force=False, **kwargs):
def check_dashboard_version_compatibility(self, dashboard_id):
""" Returns True | False | None if could not check """
try:
dashboard = self.qs.discover_dashboard(dashboardId=dashboard_id)
dashboard = self.qs.discover_dashboard(dashboard_id)
except CidCritical:
print(f'Dashboard "{dashboard_id}" is not deployed')
return None
Expand All @@ -1085,7 +1077,7 @@ def check_dashboard_version_compatibility(self, dashboard_id):

def update_dashboard(self, dashboard_id, dashboard_definition):

dashboard = self.qs.discover_dashboard(dashboardId=dashboard_id)
dashboard = self.qs.discover_dashboard(dashboard_id)
if not dashboard:
print(f'Dashboard "{dashboard_id}" is not deployed')
return
Expand Down
Loading
Loading