energy.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. """Loads datasets, dashboards and slices in a new superset instance"""
  18. import textwrap
  19. import pandas as pd
  20. from sqlalchemy import Float, String
  21. from sqlalchemy.sql import column
  22. from superset import db
  23. from superset.connectors.sqla.models import SqlMetric
  24. from superset.models.slice import Slice
  25. from superset.utils import core as utils
  26. from .helpers import get_example_data, merge_slice, misc_dash_slices, TBL
  27. def load_energy(only_metadata=False, force=False):
  28. """Loads an energy related dataset to use with sankey and graphs"""
  29. tbl_name = "energy_usage"
  30. database = utils.get_example_database()
  31. table_exists = database.has_table_by_name(tbl_name)
  32. if not only_metadata and (not table_exists or force):
  33. data = get_example_data("energy.json.gz")
  34. pdf = pd.read_json(data)
  35. pdf.to_sql(
  36. tbl_name,
  37. database.get_sqla_engine(),
  38. if_exists="replace",
  39. chunksize=500,
  40. dtype={"source": String(255), "target": String(255), "value": Float()},
  41. index=False,
  42. )
  43. print("Creating table [wb_health_population] reference")
  44. tbl = db.session.query(TBL).filter_by(table_name=tbl_name).first()
  45. if not tbl:
  46. tbl = TBL(table_name=tbl_name)
  47. tbl.description = "Energy consumption"
  48. tbl.database = database
  49. if not any(col.metric_name == "sum__value" for col in tbl.metrics):
  50. col = str(column("value").compile(db.engine))
  51. tbl.metrics.append(
  52. SqlMetric(metric_name="sum__value", expression=f"SUM({col})")
  53. )
  54. db.session.merge(tbl)
  55. db.session.commit()
  56. tbl.fetch_metadata()
  57. slc = Slice(
  58. slice_name="Energy Sankey",
  59. viz_type="sankey",
  60. datasource_type="table",
  61. datasource_id=tbl.id,
  62. params=textwrap.dedent(
  63. """\
  64. {
  65. "collapsed_fieldsets": "",
  66. "groupby": [
  67. "source",
  68. "target"
  69. ],
  70. "metric": "sum__value",
  71. "row_limit": "5000",
  72. "slice_name": "Energy Sankey",
  73. "viz_type": "sankey"
  74. }
  75. """
  76. ),
  77. )
  78. misc_dash_slices.add(slc.slice_name)
  79. merge_slice(slc)
  80. slc = Slice(
  81. slice_name="Energy Force Layout",
  82. viz_type="directed_force",
  83. datasource_type="table",
  84. datasource_id=tbl.id,
  85. params=textwrap.dedent(
  86. """\
  87. {
  88. "charge": "-500",
  89. "collapsed_fieldsets": "",
  90. "groupby": [
  91. "source",
  92. "target"
  93. ],
  94. "link_length": "200",
  95. "metric": "sum__value",
  96. "row_limit": "5000",
  97. "slice_name": "Force",
  98. "viz_type": "directed_force"
  99. }
  100. """
  101. ),
  102. )
  103. misc_dash_slices.add(slc.slice_name)
  104. merge_slice(slc)
  105. slc = Slice(
  106. slice_name="Heatmap",
  107. viz_type="heatmap",
  108. datasource_type="table",
  109. datasource_id=tbl.id,
  110. params=textwrap.dedent(
  111. """\
  112. {
  113. "all_columns_x": "source",
  114. "all_columns_y": "target",
  115. "canvas_image_rendering": "pixelated",
  116. "collapsed_fieldsets": "",
  117. "linear_color_scheme": "blue_white_yellow",
  118. "metric": "sum__value",
  119. "normalize_across": "heatmap",
  120. "slice_name": "Heatmap",
  121. "viz_type": "heatmap",
  122. "xscale_interval": "1",
  123. "yscale_interval": "1"
  124. }
  125. """
  126. ),
  127. )
  128. misc_dash_slices.add(slc.slice_name)
  129. merge_slice(slc)