misc_dashboard-backup.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 json
  18. import textwrap
  19. from superset import db
  20. from superset.models.dashboard import Dashboard
  21. from superset.models.slice import Slice
  22. from .helpers import misc_dash_slices, update_slice_ids
  23. DASH_SLUG = "misc_charts"
  24. def load_misc_dashboard() -> None:
  25. """Loading a dashboard featuring misc charts"""
  26. print("Creating the dashboard")
  27. db.session.expunge_all()
  28. dash = db.session.query(Dashboard).filter_by(slug=DASH_SLUG).first()
  29. if not dash:
  30. dash = Dashboard()
  31. js = textwrap.dedent(
  32. """\
  33. {
  34. "CHART-BkeVbh8ANQ": {
  35. "children": [],
  36. "id": "CHART-BkeVbh8ANQ",
  37. "meta": {
  38. "chartId": 4004,
  39. "height": 34,
  40. "sliceName": "Multi Line",
  41. "width": 8
  42. },
  43. "type": "CHART"
  44. },
  45. "CHART-H1HYNzEANX": {
  46. "children": [],
  47. "id": "CHART-H1HYNzEANX",
  48. "meta": {
  49. "chartId": 3940,
  50. "height": 50,
  51. "sliceName": "Energy Sankey",
  52. "width": 6
  53. },
  54. "type": "CHART"
  55. },
  56. "CHART-HJOYVMV0E7": {
  57. "children": [],
  58. "id": "CHART-HJOYVMV0E7",
  59. "meta": {
  60. "chartId": 3969,
  61. "height": 63,
  62. "sliceName": "Mapbox Long/Lat",
  63. "width": 6
  64. },
  65. "type": "CHART"
  66. },
  67. "CHART-S1WYNz4AVX": {
  68. "children": [],
  69. "id": "CHART-S1WYNz4AVX",
  70. "meta": {
  71. "chartId": 3989,
  72. "height": 25,
  73. "sliceName": "Parallel Coordinates",
  74. "width": 4
  75. },
  76. "type": "CHART"
  77. },
  78. "CHART-r19KVMNCE7": {
  79. "children": [],
  80. "id": "CHART-r19KVMNCE7",
  81. "meta": {
  82. "chartId": 3971,
  83. "height": 34,
  84. "sliceName": "Calendar Heatmap multiformat 0",
  85. "width": 4
  86. },
  87. "type": "CHART"
  88. },
  89. "CHART-rJ4K4GV04Q": {
  90. "children": [],
  91. "id": "CHART-rJ4K4GV04Q",
  92. "meta": {
  93. "chartId": 3941,
  94. "height": 63,
  95. "sliceName": "Energy Force Layout",
  96. "width": 6
  97. },
  98. "type": "CHART"
  99. },
  100. "CHART-rkgF4G4A4X": {
  101. "children": [],
  102. "id": "CHART-rkgF4G4A4X",
  103. "meta": {
  104. "chartId": 3970,
  105. "height": 25,
  106. "sliceName": "Birth in France by department in 2016",
  107. "width": 8
  108. },
  109. "type": "CHART"
  110. },
  111. "CHART-rywK4GVR4X": {
  112. "children": [],
  113. "id": "CHART-rywK4GVR4X",
  114. "meta": {
  115. "chartId": 3942,
  116. "height": 50,
  117. "sliceName": "Heatmap",
  118. "width": 6
  119. },
  120. "type": "CHART"
  121. },
  122. "COLUMN-ByUFVf40EQ": {
  123. "children": [
  124. "CHART-rywK4GVR4X",
  125. "CHART-HJOYVMV0E7"
  126. ],
  127. "id": "COLUMN-ByUFVf40EQ",
  128. "meta": {
  129. "background": "BACKGROUND_TRANSPARENT",
  130. "width": 6
  131. },
  132. "type": "COLUMN"
  133. },
  134. "COLUMN-rkmYVGN04Q": {
  135. "children": [
  136. "CHART-rJ4K4GV04Q",
  137. "CHART-H1HYNzEANX"
  138. ],
  139. "id": "COLUMN-rkmYVGN04Q",
  140. "meta": {
  141. "background": "BACKGROUND_TRANSPARENT",
  142. "width": 6
  143. },
  144. "type": "COLUMN"
  145. },
  146. "GRID_ID": {
  147. "children": [
  148. "ROW-SytNzNA4X",
  149. "ROW-S1MK4M4A4X",
  150. "ROW-HkFFEzVRVm"
  151. ],
  152. "id": "GRID_ID",
  153. "type": "GRID"
  154. },
  155. "HEADER_ID": {
  156. "id": "HEADER_ID",
  157. "meta": {
  158. "text": "Misc Charts"
  159. },
  160. "type": "HEADER"
  161. },
  162. "ROOT_ID": {
  163. "children": [
  164. "GRID_ID"
  165. ],
  166. "id": "ROOT_ID",
  167. "type": "ROOT"
  168. },
  169. "ROW-HkFFEzVRVm": {
  170. "children": [
  171. "CHART-r19KVMNCE7",
  172. "CHART-BkeVbh8ANQ"
  173. ],
  174. "id": "ROW-HkFFEzVRVm",
  175. "meta": {
  176. "background": "BACKGROUND_TRANSPARENT"
  177. },
  178. "type": "ROW"
  179. },
  180. "ROW-S1MK4M4A4X": {
  181. "children": [
  182. "COLUMN-rkmYVGN04Q",
  183. "COLUMN-ByUFVf40EQ"
  184. ],
  185. "id": "ROW-S1MK4M4A4X",
  186. "meta": {
  187. "background": "BACKGROUND_TRANSPARENT"
  188. },
  189. "type": "ROW"
  190. },
  191. "ROW-SytNzNA4X": {
  192. "children": [
  193. "CHART-rkgF4G4A4X",
  194. "CHART-S1WYNz4AVX"
  195. ],
  196. "id": "ROW-SytNzNA4X",
  197. "meta": {
  198. "background": "BACKGROUND_TRANSPARENT"
  199. },
  200. "type": "ROW"
  201. },
  202. "DASHBOARD_VERSION_KEY": "v2"
  203. }
  204. """
  205. )
  206. pos = json.loads(js)
  207. slices = (
  208. db.session.query(Slice).filter(Slice.slice_name.in_(misc_dash_slices)).all()
  209. )
  210. slices = sorted(slices, key= x.id)
  211. update_slice_ids(pos, slices)
  212. dash.dashboard_title = "Misc Charts"
  213. dash.position_json = json.dumps(pos, indent=4)
  214. dash.slug = DASH_SLUG
  215. dash.slices = slices
  216. db.session.merge(dash)
  217. db.session.commit()