helpers.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 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, slices):
  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=lambda k: 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):
  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, **kwargs):
  52. d = defaults.copy()
  53. d.update(kwargs)
  54. return json.dumps(d, indent=4, sort_keys=True)
  55. def get_example_data(filepath, is_gzip=True, make_bytes=False):
  56. module_path = os.path.dirname(__file__)
  57. filename = module_path + filepath
  58. # with open(fr'E:\\projects\\work\\trunk\\incubator-superset0.99\\superset\\examples-data\\{filepath}', 'rb', buffering=-1) as f: #/会被转义,加r
  59. with open(fr'{filename}', 'rb', buffering=-1) as f: #/会被转义,加r
  60. content = f.read()
  61. # content = request.urlopen(f"{BASE_URL}{filepath}?raw=true").read()
  62. if is_gzip:
  63. content = zlib.decompress(content, zlib.MAX_WBITS | 16)
  64. if make_bytes:
  65. content = BytesIO(content)
  66. return content