country_map.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. import datetime
  18. import pandas as pd
  19. from sqlalchemy import BigInteger, Date, String
  20. from sqlalchemy.sql import column
  21. from superset import db
  22. from superset.connectors.sqla.models import SqlMetric
  23. from superset.models.slice import Slice
  24. from superset.utils import core as utils
  25. from .helpers import (
  26. get_example_data,
  27. get_slice_json,
  28. merge_slice,
  29. misc_dash_slices,
  30. TBL,
  31. )
  32. def load_country_map_data(only_metadata: bool = False, force: bool = False) -> None:
  33. """Loading data for map with country map"""
  34. tbl_name = "birth_france_by_region"
  35. database = utils.get_example_database()
  36. table_exists = database.has_table_by_name(tbl_name)
  37. if not only_metadata and (not table_exists or force):
  38. csv_bytes = get_example_data(
  39. "birth_france_data_for_country_map.csv", is_gzip=False, make_bytes=True
  40. )
  41. data = pd.read_csv(csv_bytes, encoding="utf-8")
  42. data["dttm"] = datetime.datetime.now().date()
  43. data.to_sql( # pylint: disable=no-member
  44. tbl_name,
  45. database.get_sqla_engine(),
  46. if_exists="replace",
  47. chunksize=500,
  48. dtype={
  49. "DEPT_ID": String(10),
  50. "2003": BigInteger,
  51. "2004": BigInteger,
  52. "2005": BigInteger,
  53. "2006": BigInteger,
  54. "2007": BigInteger,
  55. "2008": BigInteger,
  56. "2009": BigInteger,
  57. "2010": BigInteger,
  58. "2011": BigInteger,
  59. "2012": BigInteger,
  60. "2013": BigInteger,
  61. "2014": BigInteger,
  62. "dttm": Date(),
  63. },
  64. index=False,
  65. )
  66. print("Done loading table!")
  67. print("-" * 80)
  68. print("Creating table reference")
  69. obj = db.session.query(TBL).filter_by(table_name=tbl_name).first()
  70. if not obj:
  71. obj = TBL(table_name=tbl_name)
  72. obj.main_dttm_col = "dttm"
  73. obj.database = database
  74. if not any(col.metric_name == "avg__2004" for col in obj.metrics):
  75. col = str(column("2004").compile(db.engine))
  76. obj.metrics.append(SqlMetric(metric_name="avg__2004", expression=f"AVG({col})"))
  77. db.session.merge(obj)
  78. db.session.commit()
  79. obj.fetch_metadata()
  80. tbl = obj
  81. slice_data = {
  82. "granularity_sqla": "",
  83. "since": "",
  84. "until": "",
  85. "viz_type": "country_map",
  86. "entity": "DEPT_ID",
  87. "metric": {
  88. "expressionType": "SIMPLE",
  89. "column": {"type": "INT", "column_name": "2004"},
  90. "aggregate": "AVG",
  91. "label": "Boys",
  92. "optionName": "metric_112342",
  93. },
  94. "row_limit": 500000,
  95. }
  96. print("Creating a slice")
  97. slc = Slice(
  98. slice_name="Birth in France by department in 2016",
  99. viz_type="country_map",
  100. datasource_type="table",
  101. datasource_id=tbl.id,
  102. params=get_slice_json(slice_data),
  103. )
  104. misc_dash_slices.add(slc.slice_name)
  105. merge_slice(slc)