unicode_test_data.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 json
  19. import random
  20. import pandas as pd
  21. from sqlalchemy import Date, Float, String
  22. from superset import db
  23. from superset.models.dashboard import Dashboard
  24. from superset.models.slice import Slice
  25. from superset.utils import core as utils
  26. from .helpers import (
  27. config,
  28. get_example_data,
  29. get_slice_json,
  30. merge_slice,
  31. TBL,
  32. update_slice_ids,
  33. )
  34. def load_unicode_test_data(only_metadata=False, force=False):
  35. """Loading unicode test dataset from a csv file in the repo"""
  36. tbl_name = "unicode_test"
  37. database = utils.get_example_database()
  38. table_exists = database.has_table_by_name(tbl_name)
  39. if not only_metadata and (not table_exists or force):
  40. data = get_example_data(
  41. "unicode_utf8_unixnl_test.csv", is_gzip=False, make_bytes=True
  42. )
  43. df = pd.read_csv(data, encoding="utf-8")
  44. # generate date/numeric data
  45. df["dttm"] = datetime.datetime.now().date()
  46. df["value"] = [random.randint(1, 100) for _ in range(len(df))]
  47. df.to_sql( # pylint: disable=no-member
  48. tbl_name,
  49. database.get_sqla_engine(),
  50. if_exists="replace",
  51. chunksize=500,
  52. dtype={
  53. "phrase": String(500),
  54. "short_phrase": String(10),
  55. "with_missing": String(100),
  56. "dttm": Date(),
  57. "value": Float(),
  58. },
  59. index=False,
  60. )
  61. print("Done loading table!")
  62. print("-" * 80)
  63. print("Creating table [unicode_test] reference")
  64. obj = db.session.query(TBL).filter_by(table_name=tbl_name).first()
  65. if not obj:
  66. obj = TBL(table_name=tbl_name)
  67. obj.main_dttm_col = "dttm"
  68. obj.database = database
  69. db.session.merge(obj)
  70. db.session.commit()
  71. obj.fetch_metadata()
  72. tbl = obj
  73. slice_data = {
  74. "granularity_sqla": "dttm",
  75. "groupby": [],
  76. "metric": {
  77. "aggregate": "SUM",
  78. "column": {"column_name": "value"},
  79. "expressionType": "SIMPLE",
  80. "label": "Value",
  81. },
  82. "row_limit": config["ROW_LIMIT"],
  83. "since": "100 years ago",
  84. "until": "now",
  85. "viz_type": "word_cloud",
  86. "size_from": "10",
  87. "series": "short_phrase",
  88. "size_to": "70",
  89. "rotation": "square",
  90. "limit": "100",
  91. }
  92. print("Creating a slice")
  93. slc = Slice(
  94. slice_name="Unicode Cloud",
  95. viz_type="word_cloud",
  96. datasource_type="table",
  97. datasource_id=tbl.id,
  98. params=get_slice_json(slice_data),
  99. )
  100. merge_slice(slc)
  101. print("Creating a dashboard")
  102. dash = db.session.query(Dashboard).filter_by(slug="unicode-test").first()
  103. if not dash:
  104. dash = Dashboard()
  105. js = """\
  106. {
  107. "CHART-Hkx6154FEm": {
  108. "children": [],
  109. "id": "CHART-Hkx6154FEm",
  110. "meta": {
  111. "chartId": 2225,
  112. "height": 30,
  113. "sliceName": "slice 1",
  114. "width": 4
  115. },
  116. "type": "CHART"
  117. },
  118. "GRID_ID": {
  119. "children": [
  120. "ROW-SyT19EFEQ"
  121. ],
  122. "id": "GRID_ID",
  123. "type": "GRID"
  124. },
  125. "ROOT_ID": {
  126. "children": [
  127. "GRID_ID"
  128. ],
  129. "id": "ROOT_ID",
  130. "type": "ROOT"
  131. },
  132. "ROW-SyT19EFEQ": {
  133. "children": [
  134. "CHART-Hkx6154FEm"
  135. ],
  136. "id": "ROW-SyT19EFEQ",
  137. "meta": {
  138. "background": "BACKGROUND_TRANSPARENT"
  139. },
  140. "type": "ROW"
  141. },
  142. "DASHBOARD_VERSION_KEY": "v2"
  143. }
  144. """
  145. dash.dashboard_title = "Unicode Test"
  146. pos = json.loads(js)
  147. update_slice_ids(pos, [slc])
  148. dash.position_json = json.dumps(pos, indent=4)
  149. dash.slug = "unicode-test"
  150. dash.slices = [slc]
  151. db.session.merge(dash)
  152. db.session.commit()