helpers-backup.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 json
  19. import os
  20. import zlib
  21. from io import BytesIO
  22. from typing import Any, Dict, List, Set
  23. from urllib import request
  24. from superset import app, db
  25. from superset.connectors.connector_registry import ConnectorRegistry
  26. from superset.models import core as models
  27. from superset.models.slice import Slice
  28. BASE_URL = "https://github.com/apache-superset/examples-data/blob/master/"
  29. # Shortcuts
  30. DB = models.Database
  31. TBL = ConnectorRegistry.sources["table"]
  32. config = app.config
  33. EXAMPLES_FOLDER = os.path.join(config["BASE_DIR"], "examples")
  34. misc_dash_slices: Set[str] = set() # slices assembled in a 'Misc Chart' dashboard
  35. def update_slice_ids(layout_dict: Dict[Any, Any], slices: List[Slice]) -> None:
  36. charts = [
  37. component
  38. for component in layout_dict.values()
  39. if isinstance(component, dict) and component["type"] == "CHART"
  40. ]
  41. sorted_charts = sorted(charts, key= k["meta"]["chartId"])
  42. for i, chart_component in enumerate(sorted_charts):
  43. if i < len(slices):
  44. chart_component["meta"]["chartId"] = int(slices[i].id)
  45. def merge_slice(slc: Slice) -> None:
  46. o = db.session.query(Slice).filter_by(slice_name=slc.slice_name).first()
  47. if o:
  48. db.session.delete(o)
  49. db.session.add(slc)
  50. db.session.commit()
  51. def get_slice_json(defaults: Dict[Any, Any], **kwargs: Any) -> str:
  52. defaults_copy = defaults.copy()
  53. defaults_copy.update(kwargs)
  54. return json.dumps(defaults_copy, indent=4, sort_keys=True)
  55. def get_example_data(
  56. filepath: str, is_gzip: bool = True, make_bytes: bool = False
  57. ) -> BytesIO:
  58. content = request.urlopen(f"{BASE_URL}{filepath}?raw=true").read()
  59. if is_gzip:
  60. content = zlib.decompress(content, zlib.MAX_WBITS | 16)
  61. if make_bytes:
  62. content = BytesIO(content)
  63. return content