def downgrade(): with op.batch_alter_table('dashboards') as batch_op: batch_op.alter_column( 'position_json', existing_type=MediumText(), type_=sa.Text(), existing_nullable=True, )
class Dashboard(Model, AuditMixinNullable, ImportMixin): """The dashboard object!""" __tablename__ = 'dashboards' id = Column(Integer, primary_key=True) dashboard_title = Column(String(500)) position_json = Column(MediumText()) description = Column(Text) css = Column(Text) json_metadata = Column(Text) slug = Column(String(255), unique=True) slices = relationship('Slice', secondary=dashboard_slices, backref='dashboards') owners = relationship(security_manager.user_model, secondary=dashboard_user) export_fields = ('dashboard_title', 'position_json', 'json_metadata', 'description', 'css', 'slug') def __repr__(self): return self.dashboard_title @property def table_names(self): # pylint: disable=no-member return ', '.join( {'{}'.format(s.datasource.full_name) for s in self.slices}) @property def url(self): if self.json_metadata: # add default_filters to the preselect_filters of dashboard json_metadata = json.loads(self.json_metadata) default_filters = json_metadata.get('default_filters') # make sure default_filters is not empty and is valid if default_filters and default_filters != '{}': try: if json.loads(default_filters): filters = parse.quote(default_filters.encode('utf8')) return '/superset/dashboard/{}/?preselect_filters={}'.format( self.slug or self.id, filters) except Exception: pass return '/superset/dashboard/{}/'.format(self.slug or self.id) @property def datasources(self): return {slc.datasource for slc in self.slices} @property def sqla_metadata(self): # pylint: disable=no-member metadata = MetaData(bind=self.get_sqla_engine()) return metadata.reflect() def dashboard_link(self): title = escape(self.dashboard_title) return Markup('<a href="{self.url}">{title}</a>'.format(**locals())) @property def data(self): positions = self.position_json if positions: positions = json.loads(positions) return { 'id': self.id, 'metadata': self.params_dict, 'css': self.css, 'dashboard_title': self.dashboard_title, 'slug': self.slug, 'slices': [slc.data for slc in self.slices], 'position_json': positions, } @property def params(self): return self.json_metadata @params.setter def params(self, value): self.json_metadata = value @property def position(self): if self.position_json: return json.loads(self.position_json) return {} @classmethod def import_obj(cls, dashboard_to_import, import_time=None): """Imports the dashboard from the object to the database. Once dashboard is imported, json_metadata field is extended and stores remote_id and import_time. It helps to decide if the dashboard has to be overridden or just copies over. Slices that belong to this dashboard will be wired to existing tables. This function can be used to import/export dashboards between multiple superset instances. Audit metadata isn't copied over. """ def alter_positions(dashboard, old_to_new_slc_id_dict): """ Updates slice_ids in the position json. Sample position_json data: { "DASHBOARD_VERSION_KEY": "v2", "DASHBOARD_ROOT_ID": { "type": "DASHBOARD_ROOT_TYPE", "id": "DASHBOARD_ROOT_ID", "children": ["DASHBOARD_GRID_ID"] }, "DASHBOARD_GRID_ID": { "type": "DASHBOARD_GRID_TYPE", "id": "DASHBOARD_GRID_ID", "children": ["DASHBOARD_CHART_TYPE-2"] }, "DASHBOARD_CHART_TYPE-2": { "type": "DASHBOARD_CHART_TYPE", "id": "DASHBOARD_CHART_TYPE-2", "children": [], "meta": { "width": 4, "height": 50, "chartId": 118 } }, } """ position_data = json.loads(dashboard.position_json) position_json = position_data.values() for value in position_json: if (isinstance(value, dict) and value.get('meta') and value.get('meta').get('chartId')): old_slice_id = value.get('meta').get('chartId') if old_slice_id in old_to_new_slc_id_dict: value['meta']['chartId'] = ( old_to_new_slc_id_dict[old_slice_id]) dashboard.position_json = json.dumps(position_data) logging.info('Started import of the dashboard: {}'.format( dashboard_to_import.to_json())) session = db.session logging.info('Dashboard has {} slices'.format( len(dashboard_to_import.slices))) # copy slices object as Slice.import_slice will mutate the slice # and will remove the existing dashboard - slice association slices = copy(dashboard_to_import.slices) old_to_new_slc_id_dict = {} new_filter_immune_slices = [] new_timed_refresh_immune_slices = [] new_expanded_slices = {} i_params_dict = dashboard_to_import.params_dict remote_id_slice_map = { slc.params_dict['remote_id']: slc for slc in session.query(Slice).all() if 'remote_id' in slc.params_dict } for slc in slices: logging.info('Importing slice {} from the dashboard: {}'.format( slc.to_json(), dashboard_to_import.dashboard_title)) remote_slc = remote_id_slice_map.get(slc.id) new_slc_id = Slice.import_obj(slc, remote_slc, import_time=import_time) old_to_new_slc_id_dict[slc.id] = new_slc_id # update json metadata that deals with slice ids new_slc_id_str = '{}'.format(new_slc_id) old_slc_id_str = '{}'.format(slc.id) if ('filter_immune_slices' in i_params_dict and old_slc_id_str in i_params_dict['filter_immune_slices']): new_filter_immune_slices.append(new_slc_id_str) if ('timed_refresh_immune_slices' in i_params_dict and old_slc_id_str in i_params_dict['timed_refresh_immune_slices']): new_timed_refresh_immune_slices.append(new_slc_id_str) if ('expanded_slices' in i_params_dict and old_slc_id_str in i_params_dict['expanded_slices']): new_expanded_slices[new_slc_id_str] = ( i_params_dict['expanded_slices'][old_slc_id_str]) # override the dashboard existing_dashboard = None for dash in session.query(Dashboard).all(): if ('remote_id' in dash.params_dict and dash.params_dict['remote_id'] == dashboard_to_import.id): existing_dashboard = dash dashboard_to_import.id = None alter_positions(dashboard_to_import, old_to_new_slc_id_dict) dashboard_to_import.alter_params(import_time=import_time) if new_expanded_slices: dashboard_to_import.alter_params( expanded_slices=new_expanded_slices) if new_filter_immune_slices: dashboard_to_import.alter_params( filter_immune_slices=new_filter_immune_slices) if new_timed_refresh_immune_slices: dashboard_to_import.alter_params( timed_refresh_immune_slices=new_timed_refresh_immune_slices) new_slices = session.query(Slice).filter( Slice.id.in_(old_to_new_slc_id_dict.values())).all() if existing_dashboard: existing_dashboard.override(dashboard_to_import) existing_dashboard.slices = new_slices session.flush() return existing_dashboard.id else: # session.add(dashboard_to_import) causes sqlachemy failures # related to the attached users / slices. Creating new object # allows to avoid conflicts in the sql alchemy state. copied_dash = dashboard_to_import.copy() copied_dash.slices = new_slices session.add(copied_dash) session.flush() return copied_dash.id @classmethod def export_dashboards(cls, dashboard_ids): copied_dashboards = [] datasource_ids = set() for dashboard_id in dashboard_ids: # make sure that dashboard_id is an integer dashboard_id = int(dashboard_id) copied_dashboard = (db.session.query(Dashboard).options( subqueryload( Dashboard.slices)).filter_by(id=dashboard_id).first()) make_transient(copied_dashboard) for slc in copied_dashboard.slices: datasource_ids.add((slc.datasource_id, slc.datasource_type)) # add extra params for the import slc.alter_params( remote_id=slc.id, datasource_name=slc.datasource.name, schema=slc.datasource.name, database_name=slc.datasource.database.name, ) copied_dashboard.alter_params(remote_id=dashboard_id) copied_dashboards.append(copied_dashboard) eager_datasources = [] for dashboard_id, dashboard_type in datasource_ids: eager_datasource = ConnectorRegistry.get_eager_datasource( db.session, dashboard_type, dashboard_id) eager_datasource.alter_params( remote_id=eager_datasource.id, database_name=eager_datasource.database.name, ) make_transient(eager_datasource) eager_datasources.append(eager_datasource) return json.dumps( { 'dashboards': copied_dashboards, 'datasources': eager_datasources, }, cls=utils.DashboardEncoder, indent=4)