diff --git a/panoramix/bin/panoramix b/panoramix/bin/panoramix index ec508d491cab5..a498d428c84ee 100755 --- a/panoramix/bin/panoramix +++ b/panoramix/bin/panoramix @@ -55,6 +55,7 @@ def load_examples(sample): print("Loading [World Bank's Health Nutrition and Population Stats]") data.load_world_bank_health_n_pop() + print("Loading [Birth names]") data.load_birth_names() diff --git a/panoramix/data/__init__.py b/panoramix/data/__init__.py index 547854acb8fe3..ca1159e46c9fc 100644 --- a/panoramix/data/__init__.py +++ b/panoramix/data/__init__.py @@ -7,6 +7,12 @@ from panoramix import app, db, models, utils +# Shortcuts +DB = models.Database +Slice = models.Slice +TBL = models.SqlaTable +Dash = models.Dashboard + config = app.config DATA_FOLDER = os.path.join(config.get("BASE_DIR"), 'data') @@ -14,7 +20,6 @@ def get_or_create_db(session): print("Creating database reference") - DB = models.Database dbobj = session.query(DB).filter_by(database_name='main').first() if not dbobj: dbobj = DB(database_name="main") @@ -25,6 +30,20 @@ def get_or_create_db(session): return dbobj +def merge_slice(slc): + o = db.session.query(Slice).filter_by(slice_name=slc.slice_name).first() + if o: + db.session.delete(o) + db.session.add(slc) + db.session.commit() + + +def get_slice_json(defaults, **kwargs): + d = defaults.copy() + d.update(kwargs) + return json.dumps(d, indent=4, sort_keys=True) + + def load_world_bank_health_n_pop(): tbl = 'wb_health_population' with gzip.open(os.path.join(DATA_FOLDER, 'countries.json.gz')) as f: @@ -42,17 +61,220 @@ def load_world_bank_health_n_pop(): 'region': String(255), }, index=False) - print("Creating table reference") - TBL = models.SqlaTable - obj = db.session.query(TBL).filter_by(table_name=tbl).first() - if not obj: - obj = TBL(table_name='wb_health_population') - obj.description = utils.readfile(os.path.join(DATA_FOLDER, 'countries.md')) - obj.main_dttm_col = 'year' - obj.database = get_or_create_db(db.session) - db.session.merge(obj) + + print("Creating table [wb_health_population] reference") + tbl = db.session.query(TBL).filter_by(table_name=tbl).first() + if not tbl: + tbl = TBL(table_name='wb_health_population') + tbl.description = utils.readfile(os.path.join(DATA_FOLDER, 'countries.md')) + tbl.main_dttm_col = 'year' + tbl.database = get_or_create_db(db.session) + db.session.merge(tbl) + db.session.commit() + #tbl.fetch_metadata() + + defaults = { + "compare_lag": "10", + "compare_suffix": "o10Y", + "datasource_id": "1", + "datasource_name": "birth_names", + "datasource_type": "table", + "limit": "25", + "granularity": "year", + "groupby": [], + "metric": 'sum__SP.POP.TOTL', + "metrics": ["sum__SP.POP.TOTL"], + "row_limit": config.get("ROW_LIMIT"), + "since": "2014-01-01", + "until": "2014-01-01", + "where": "", + "markup_type": "markdown", + "country_fieldtype": "cca3", + "secondary_metric": "sum__SP.POP.TOTL", + "entity": "country_code", + "show_bubbles": "y", + } + + print("Creating slices") + slices = [ + Slice( + slice_name="Region Filter", + viz_type='filter_box', + datasource_type='table', + table=tbl, + params=get_slice_json( + defaults, + viz_type='filter_box', + groupby=['region'], + )), + Slice( + slice_name="World's Population", + viz_type='big_number', + datasource_type='table', + table=tbl, + params=get_slice_json( + defaults, + since='2000', + viz_type='big_number', + compare_lag="10", + metric='sum__SP.POP.TOTL', + compare_suffix="over 10Y")), + Slice( + slice_name="Most Populated Countries", + viz_type='table', + datasource_type='table', + table=tbl, + params=get_slice_json( + defaults, + viz_type='table', + metrics=["sum__SP.POP.TOTL"], + groupby=['country_name'])), + Slice( + slice_name="Growth Rate", + viz_type='line', + datasource_type='table', + table=tbl, + params=get_slice_json( + defaults, + viz_type='line', + since="1960-01-01", + metrics=["sum__SP.POP.TOTL"], + num_period_compare="10", + groupby=['country_name'])), + Slice( + slice_name="% Rural", + viz_type='world_map', + datasource_type='table', + table=tbl, + params=get_slice_json( + defaults, + viz_type='world_map', + metric= "sum__SP.RUR.TOTL.ZS", + num_period_compare="10",)), + Slice( + slice_name="Life Expexctancy VS Rural %", + viz_type='bubble', + datasource_type='table', + table=tbl, + params=get_slice_json( + defaults, + viz_type='bubble', + since= "2011-01-01", + until= "2011-01-01", + series="region", + limit="0", + entity="country_name", + x="sum__SP.RUR.TOTL.ZS", + y="sum__SP.DYN.LE00.IN", + size="sum__SP.POP.TOTL", + max_bubble_size="50", + flt_col_1="country_code", + flt_op_1= "not in", + flt_eq_1="TCA,MNP,DMA,MHL,MCO,SXM,CYM,TUV,IMY,KNA,ASM,ADO,AMA,PLW", + num_period_compare="10",)), + Slice( + slice_name="Rural Breakdown", + viz_type='sunburst', + datasource_type='table', + table=tbl, + params=get_slice_json( + defaults, + viz_type='sunburst', + groupby=["region", "country_name"], + secondary_metric="sum__SP.RUR.TOTL", + since= "2011-01-01", + until= "2011-01-01",)), + Slice( + slice_name="World's Pop Growth", + viz_type='area', + datasource_type='table', + table=tbl, + params=get_slice_json( + defaults, + since="1960-01-01", + until="now", + viz_type='area', + groupby=["region"],)), + ] + for slc in slices: + merge_slice(slc) + + print("Creating a World's Health Bank dashboard") + dash_name = "World's Health Bank Dashboard" + dash = db.session.query(Dash).filter_by(dashboard_title=dash_name).first() + + if dash: + db.session.delete(dash) + js = """\ +[ + { + "size_y": 1, + "size_x": 3, + "col": 1, + "slice_id": "269", + "row": 1 + }, + { + "size_y": 3, + "size_x": 3, + "col": 1, + "slice_id": "270", + "row": 2 + }, + { + "size_y": 7, + "size_x": 3, + "col": 10, + "slice_id": "271", + "row": 1 + }, + { + "size_y": 3, + "size_x": 6, + "col": 1, + "slice_id": "272", + "row": 5 + }, + { + "size_y": 4, + "size_x": 6, + "col": 4, + "slice_id": "273", + "row": 1 + }, + { + "size_y": 4, + "size_x": 6, + "col": 7, + "slice_id": "274", + "row": 8 + }, + { + "size_y": 3, + "size_x": 3, + "col": 7, + "slice_id": "275", + "row": 5 + }, + { + "size_y": 4, + "size_x": 6, + "col": 1, + "slice_id": "276", + "row": 8 + } +] + """ + l = json.loads(js) + for i, pos in enumerate(l): + pos['slice_id'] = str(slices[i].id) + dash = Dash( + dashboard_title=dash_name, + position_json=json.dumps(l, indent=4), + ) + for s in slices: + dash.slices.append(s) db.session.commit() - obj.fetch_metadata() def load_birth_names(): @@ -77,154 +299,129 @@ def load_birth_names(): print("-" * 80) print("Creating table reference") - TBL = models.SqlaTable obj = db.session.query(TBL).filter_by(table_name='birth_names').first() if not obj: obj = TBL(table_name = 'birth_names') obj.main_dttm_col = 'ds' obj.database = get_or_create_db(db.session) - models.Table db.session.merge(obj) db.session.commit() obj.fetch_metadata() tbl = obj - print("Creating some slices") - def get_slice_json(**kwargs): - defaults = { - "compare_lag": "10", - "compare_suffix": "o10Y", - "datasource_id": "1", - "datasource_name": "birth_names", - "datasource_type": "table", - "limit": "25", - "flt_col_1": "gender", - "flt_eq_1": "", - "flt_op_1": "in", - "granularity": "ds", - "groupby": [], - "metric": 'sum__num', - "metrics": ["sum__num"], - "row_limit": config.get("ROW_LIMIT"), - "since": "100 years", - "until": "now", - "viz_type": "table", - "where": "", - "markup_type": "markdown", - } - d = defaults.copy() - d.update(kwargs) - return json.dumps(d, indent=4, sort_keys=True) - Slice = models.Slice - slices = [] - - def merge_slice(slc): - o = db.session.query( - Slice).filter_by(slice_name=slc.slice_name).first() - if o: - db.session.delete(slc) - db.session.add(slc) - session.commit() - slices.append(slc) + defaults = { + "compare_lag": "10", + "compare_suffix": "o10Y", + "datasource_id": "1", + "datasource_name": "birth_names", + "datasource_type": "table", + "limit": "25", + "granularity": "ds", + "groupby": [], + "metric": 'sum__num', + "metrics": ["sum__num"], + "row_limit": config.get("ROW_LIMIT"), + "since": "100 years", + "until": "now", + "viz_type": "table", + "where": "", + "markup_type": "markdown", + } - merge_slice( + print("Creating some slices") + slices = [ Slice( slice_name="Girls", viz_type='table', datasource_type='table', table=tbl, params=get_slice_json( - groupby=['name'], flt_eq_1="girl", row_limit=50))) - - merge_slice( + defaults, + groupby=['name'], flt_eq_1="girl", row_limit=50)), Slice( slice_name="Boys", viz_type='table', datasource_type='table', table=tbl, params=get_slice_json( - groupby=['name'], flt_eq_1="boy", row_limit=50))) - - merge_slice( + defaults, + groupby=['name'], flt_eq_1="boy", row_limit=50)), Slice( slice_name="Participants", viz_type='big_number', datasource_type='table', table=tbl, params=get_slice_json( + defaults, viz_type="big_number", granularity="ds", - compare_lag="5", compare_suffix="over 5Y"))) - - merge_slice( + compare_lag="5", compare_suffix="over 5Y")), Slice( slice_name="Genders", viz_type='pie', datasource_type='table', table=tbl, params=get_slice_json( - viz_type="pie", groupby=['gender']))) - - merge_slice( + defaults, + viz_type="pie", groupby=['gender'])), Slice( slice_name="Genders by State", viz_type='dist_bar', datasource_type='table', table=tbl, params=get_slice_json( + defaults, flt_eq_1="other", viz_type="dist_bar", metrics=['sum__sum_girls', 'sum__sum_boys'], - groupby=['state'], flt_op_1='not in', flt_col_1='state'))) - - merge_slice( + groupby=['state'], flt_op_1='not in', flt_col_1='state')), Slice( slice_name="Trends", viz_type='line', datasource_type='table', table=tbl, params=get_slice_json( + defaults, viz_type="line", groupby=['name'], - granularity='ds', rich_tooltip='y', show_legend='y'))) - - code = """ -
The source dataset came from [here]
- -The source dataset came from [here]
+ +