def create_slice(self, name, ds_id=None, id=None, db_name='main', table_name='wb_health_population'): params = { 'num_period_compare': '10', 'remote_id': id, 'datasource_name': table_name, 'database_name': db_name, 'schema': '', } if table_name and not ds_id: table = self.get_table_by_name(table_name) if table: ds_id = table.id return models.Slice(slice_name=name, datasource_type='table', viz_type='bubble', params=json.dumps(params), datasource_id=ds_id, id=id)
def save(self, args, slc): """Saves (inserts or overwrite a slice) """ session = db.session() slice_name = args.get('slice_name') action = args.get('action') # TODO use form processing form wtforms d = args.to_dict(flat=False) del d['action'] del d['previous_viz_type'] as_list = ('metrics', 'groupby', 'columns') for k in d: v = d.get(k) if k in as_list and not isinstance(v, list): d[k] = [v] if v else [] if k not in as_list and isinstance(v, list): d[k] = v[0] table_id = druid_datasource_id = None datasource_type = args.get('datasource_type') if datasource_type in ('datasource', 'druid'): druid_datasource_id = args.get('datasource_id') elif datasource_type == 'table': table_id = args.get('datasource_id') if action == "save": slc = models.Slice() msg = "Slice [{}] has been saved".format(slice_name) elif action == "overwrite": msg = "Slice [{}] has been overwritten".format(slice_name) slc.params = json.dumps(d, indent=4, sort_keys=True) slc.datasource_name = args.get('datasource_name') slc.viz_type = args.get('viz_type') slc.druid_datasource_id = druid_datasource_id slc.table_id = table_id slc.datasource_type = datasource_type slc.slice_name = slice_name if action == "save": session.add(slc) elif action == "overwrite": session.merge(slc) session.commit() flash(msg, "info") return redirect(slc.slice_url)
def save_or_overwrite_slice(self, args, slc, slice_add_perm, slice_edit_perm): """save or overwrite a slice""" slice_name = args.get('slice_name') action = args.get('action') # TODO use form processing form wtforms d = args.to_dict(flat=False) del d['action'] del d['previous_viz_type'] as_list = ('metrics', 'groupby', 'columns', 'all_columns', 'mapbox_label') for k in d: v = d.get(k) if k in as_list and not isinstance(v, list): d[k] = [v] if v else [] if k not in as_list and isinstance(v, list): d[k] = v[0] table_id = druid_datasource_id = None datasource_type = args.get('datasource_type') if datasource_type in ('datasource', 'druid'): druid_datasource_id = args.get('datasource_id') elif datasource_type == 'table': table_id = args.get('datasource_id') if action == "save": slc = models.Slice() slc.params = json.dumps(d, indent=4, sort_keys=True) slc.datasource_name = args.get('datasource_name') slc.viz_type = args.get('viz_type') slc.druid_datasource_id = druid_datasource_id slc.table_id = table_id slc.datasource_type = datasource_type slc.slice_name = slice_name if action == 'save' and slice_add_perm: self.save_slice(slc) elif action == 'overwrite' and slice_edit_perm: self.overwrite_slice(slc) return redirect(slc.slice_url)
def save_or_overwrite_slice(self, args, slc, slice_add_perm, slice_edit_perm): """Save or overwrite a slice""" slice_name = args.get('slice_name') action = args.get('action') # TODO use form processing form wtforms d = args.to_dict(flat=False) del d['action'] del d['previous_viz_type'] as_list = ('metrics', 'groupby', 'columns', 'all_columns', 'mapbox_label') for k in d: v = d.get(k) if k in as_list and not isinstance(v, list): d[k] = [v] if v else [] if k not in as_list and isinstance(v, list): d[k] = v[0] table_id = druid_datasource_id = None datasource_type = args.get('datasource_type') if datasource_type in ('datasource', 'druid'): druid_datasource_id = args.get('datasource_id') elif datasource_type == 'table': table_id = args.get('datasource_id') if action in ('saveas'): slc = models.Slice(owners=[g.user] if g.user else []) slc.params = json.dumps(d, indent=4, sort_keys=True) slc.datasource_name = args.get('datasource_name') slc.viz_type = args.get('viz_type') slc.druid_datasource_id = druid_datasource_id slc.table_id = table_id slc.datasource_type = datasource_type slc.slice_name = slice_name if action in ('saveas') and slice_add_perm: self.save_slice(slc) elif action == 'overwrite' and slice_edit_perm: self.overwrite_slice(slc) # Adding slice to a dashboard if requested dash = None if request.args.get('add_to_dash') == 'existing': dash = (db.session.query(models.Dashboard).filter_by( id=int(request.args.get('save_to_dashboard_id'))).one()) flash( "Slice [{}] was added to dashboard [{}]".format( slc.slice_name, dash.dashboard_title), "info") elif request.args.get('add_to_dash') == 'new': dash = models.Dashboard( dashboard_title=request.args.get('new_dashboard_name'), owners=[g.user] if g.user else []) flash( "Dashboard [{}] just got created and slice [{}] was added " "to it".format(dash.dashboard_title, slc.slice_name), "info") if dash and slc not in dash.slices: dash.slices.append(slc) db.session.commit() if request.args.get('goto_dash') == 'true': return redirect(dash.url) else: return redirect(slc.slice_url)