def create_dashboard(title=None, slices=None, tags=None): """Create a new test dashboard (and slice and table if needed)""" if tags is None: tags = ['test'] if title is None: title = "dashboard-%s" % uuid.uuid4() if slices is None: slices = [new_slice(tags=tags)] dashboard = Dashboard( dashboard_title=title, slug=title.lower(), slices=slices, position_json=json.dumps(dict(size_y=2, size_x=2)), json_metadata=json.dumps(dict(tags=tags)), ) # Return imported obj dashboard = superset.db.session.query(Dashboard).filter_by( id=Dashboard.import_obj(dashboard)).first() dashboard.published = True superset.db.session.merge(dashboard) superset.db.session.commit() return dashboard
def import_dashboards(session, data_stream): """Imports dashboards from a stream to databases""" data = json.loads(data_stream.read(), object_hook=decode_dashboards) # TODO: import DRUID datasources for table in data["datasources"]: type(table).import_obj(table) session.commit() for dashboard in data["dashboards"]: Dashboard.import_obj(dashboard) session.commit()
def import_dashboards(session, data_stream, import_time=None): """Imports dashboards from a stream to databases""" current_tt = int(time.time()) import_time = current_tt if import_time is None else import_time data = json.loads(data_stream.read(), object_hook=decode_dashboards) # TODO: import DRUID datasources for table in data['datasources']: type(table).import_obj(table, import_time=import_time) session.commit() for dashboard in data['dashboards']: Dashboard.import_obj(dashboard, import_time=import_time) session.commit()
def import_dashboards(session, data_stream, import_time=None): """Imports dashboards from a stream to databases""" current_tt = int(time.time()) import_time = current_tt if import_time is None else import_time data = json.loads(data_stream.read(), object_hook=decode_dashboards) # TODO: import DRUID datasources for table in data['datasources']: type(table).import_obj(table, import_time=import_time) session.commit() for dashboard in data['dashboards']: Dashboard.import_obj( dashboard, import_time=import_time) session.commit()
def import_dashboard_json(session, data, import_time=None): current_tt = int(time.time()) import_time = current_tt if import_time is None else import_time dashboard = data['dashboards'][0] update_slice_options(dashboard.slices) dash_id = Dashboard.import_obj(dashboard, import_time=import_time) session.commit() return dash_id
def export_dashboards(session): """Returns all dashboards metadata as a json dump""" logging.info('Starting export') dashboards = session.query(Dashboard) dashboard_ids = [] for dashboard in dashboards: dashboard_ids.append(dashboard.id) data = Dashboard.export_dashboards(dashboard_ids) return data
def export_dashboards(session, id_=None): """Returns all dashboards metadata as a json dump""" logging.info("Starting export") dashboard_ids = [] if id is None: dashboards = session.query(Dashboard) for dashboard in dashboards: dashboard_ids.append(dashboard.id) else: dashboard_ids = [id_] data = Dashboard.export_dashboards(dashboard_ids) return data
def decode_dashboards(o): """ Function to be passed into json.loads obj_hook parameter Recreates the dashboard object from a json representation. """ import superset.models.core as models if "__Dashboard__" in o: return Dashboard(**o["__Dashboard__"]) elif "__Slice__" in o: return Slice(**o["__Slice__"]) elif "__TableColumn__" in o: return TableColumn(**o["__TableColumn__"]) elif "__SqlaTable__" in o: return SqlaTable(**o["__SqlaTable__"]) elif "__SqlMetric__" in o: return SqlMetric(**o["__SqlMetric__"]) elif "__datetime__" in o: return datetime.strptime(o["__datetime__"], "%Y-%m-%dT%H:%M:%S") else: return o
def create_dashboard(type, gamecode): dashboard_name = dashboard_name_template.format(type=type, gamecode=gamecode) dashboard = db.session.query(Dashboard).filter_by(dashboard_title=dashboard_name).first() if not dashboard: dashboard = Dashboard(dashboard_title=dashboard_name) db.session.merge(dashboard) dashboard = db.session.query(Dashboard).filter_by(dashboard_title=dashboard_name).first() TBL = ConnectorRegistry.sources['table'] tables = db.session.query(TBL).filter(TBL.table_name.like(dashboard_name+'%')).all() position_json = '{' tname_list = [] for table in tables : id = table.id table_name = table.table_name column_idx = len(dashboard_name)+1 column = table_name[column_idx:] slice = db.session.query(Slice).filter_by(datasource_id=id).first() slice_json = ''' "CHART-{tname}":{{"id":"CHART-{tname}", "meta":{{"sliceName":"{tname}", "chartId":{id}, "height":52, "width":12}}, "type":"CHART"}}, "ROW-{tname}":{{"id":"ROW-{tname}", "meta":{{"background":"BACKGROUND_TRANSPARENT"}}, "children":["CHART-{tname}"], "type":"ROW"}}, "TAB-{tname}":{{"id":"TAB-{tname}", "meta":{{"text":"{colname}"}}, "children":["ROW-{tname}"], "type":"TAB"}}, '''.format(tname=table_name, id=slice.id, colname=column) position_json += slice_json tname_list.append(table_name) dashboard_slices_row = db.session.query(DashboardSlices).filter_by(dashboard_id=dashboard.id, slice_id=slice.id).first() if not dashboard_slices_row: dashboard_slices_row = DashboardSlices(dashboard_id=dashboard.id, slice_id=slice.id) db.session.merge(dashboard_slices_row) count = 0 unit = 10 tabs_map = {} for tname in tname_list : tabs_num = int(count/unit) if count % unit == 0 : tabs_map[tabs_num] = [] tabs_map[tabs_num].append('"TAB-{tname}"'.format(tname=tname)) count += 1 tabs_id_list = [] for tabs_num in tabs_map : tabs_id = '"TABS-{tabs_num}"'.format(tabs_num=str(tabs_num)) tabs_json = '{tabs_id}:{{"id":{tabs_id}, "children":[{tab_id_csv}], "type":"TABS"}},\n'.format(tabs_id=tabs_id, tab_id_csv=','.join(tabs_map[tabs_num])) position_json += tabs_json tabs_id_list.append(tabs_id) grid_json = '"GRID_ID": {{"id":"GRID_ID", "children":[{tabs_id_csv}], "type":"GRID"}},\n'.format(tabs_id_csv=','.join(tabs_id_list)) position_json += grid_json position_json += ''' "ROOT_ID": {{ "id":"ROOT_ID", "children":["GRID_ID"], "type":"ROOT"}}, "HEADER_ID": {{"id":"HEADER_ID", "meta":{{ "text":"{dashboard_name}"}}, "type":"HEADER"}}, "DASHBOARD_VERSION_KEY":"v2" }} '''.format(dashboard_name=dashboard_name) dashboard.position_json = position_json db.session.merge(dashboard) db.session.commit()