multiformat_time_series.py 3.9 KB

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