multiformat_time_series.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 pandas as pd
  18. from sqlalchemy import BigInteger, Date, DateTime, String
  19. from superset import db
  20. from superset.models.slice import Slice
  21. from superset.utils.core import get_example_database
  22. from .helpers import (
  23. config,
  24. get_example_data,
  25. get_slice_json,
  26. merge_slice,
  27. misc_dash_slices,
  28. TBL,
  29. )
  30. def load_multiformat_time_series(only_metadata=False, force=False):
  31. """Loading time series data from a zip file in the repo"""
  32. tbl_name = "multiformat_time_series"
  33. database = get_example_database()
  34. table_exists = database.has_table_by_name(tbl_name)
  35. if not only_metadata and (not table_exists or force):
  36. data = get_example_data("multiformat_time_series.json.gz")
  37. pdf = pd.read_json(data)
  38. pdf.ds = pd.to_datetime(pdf.ds, unit="s")
  39. pdf.ds2 = pd.to_datetime(pdf.ds2, unit="s")
  40. pdf.to_sql(
  41. tbl_name,
  42. database.get_sqla_engine(),
  43. if_exists="replace",
  44. chunksize=500,
  45. dtype={
  46. "ds": Date,
  47. "ds2": DateTime,
  48. "epoch_s": BigInteger,
  49. "epoch_ms": BigInteger,
  50. "string0": String(100),
  51. "string1": String(100),
  52. "string2": String(100),
  53. "string3": String(100),
  54. },
  55. index=False,
  56. )
  57. print("Done loading table!")
  58. print("-" * 80)
  59. print(f"Creating table [{tbl_name}] reference")
  60. obj = db.session.query(TBL).filter_by(table_name=tbl_name).first()
  61. if not obj:
  62. obj = TBL(table_name=tbl_name)
  63. obj.main_dttm_col = "ds"
  64. obj.database = database
  65. dttm_and_expr_dict = {
  66. "ds": [None, None],
  67. "ds2": [None, None],
  68. "epoch_s": ["epoch_s", None],
  69. "epoch_ms": ["epoch_ms", None],
  70. "string2": ["%Y%m%d-%H%M%S", None],
  71. "string1": ["%Y-%m-%d^%H:%M:%S", None],
  72. "string0": ["%Y-%m-%d %H:%M:%S.%f", None],
  73. "string3": ["%Y/%m/%d%H:%M:%S.%f", None],
  74. }
  75. for col in obj.columns:
  76. dttm_and_expr = dttm_and_expr_dict[col.column_name]
  77. col.python_date_format = dttm_and_expr[0]
  78. col.dbatabase_expr = dttm_and_expr[1]
  79. col.is_dttm = True
  80. db.session.merge(obj)
  81. db.session.commit()
  82. obj.fetch_metadata()
  83. tbl = obj
  84. print("Creating Heatmap charts")
  85. for i, col in enumerate(tbl.columns):
  86. slice_data = {
  87. "metrics": ["count"],
  88. "granularity_sqla": col.column_name,
  89. "row_limit": config["ROW_LIMIT"],
  90. "since": "2015",
  91. "until": "2016",
  92. "viz_type": "cal_heatmap",
  93. "domain_granularity": "month",
  94. "subdomain_granularity": "day",
  95. }
  96. slc = Slice(
  97. slice_name=f"Calendar Heatmap multiformat {i}",
  98. viz_type="cal_heatmap",
  99. datasource_type="table",
  100. datasource_id=tbl.id,
  101. params=get_slice_json(slice_data),
  102. )
  103. merge_slice(slc)
  104. misc_dash_slices.add("Calendar Heatmap multiformat 0")