123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- """Loads datasets, dashboards and slices in a new superset instance"""
- import json
- import os
- import zlib
- from io import BytesIO
- from typing import Any, Dict, List, Set
- from urllib import request
- from superset import app, db
- from superset.connectors.connector_registry import ConnectorRegistry
- from superset.models import core as models
- from superset.models.slice import Slice
- BASE_URL = "https://github.com/apache-superset/examples-data/blob/master/"
- DB = models.Database
- TBL = ConnectorRegistry.sources["table"]
- config = app.config
- EXAMPLES_FOLDER = os.path.join(config["BASE_DIR"], "examples")
- misc_dash_slices: Set[str] = set()
- def update_slice_ids(layout_dict: Dict[Any, Any], slices: List[Slice]) -> None:
- charts = [
- component
- for component in layout_dict.values()
- if isinstance(component, dict) and component["type"] == "CHART"
- ]
- sorted_charts = sorted(charts, key=lambda k: k["meta"]["chartId"])
- for i, chart_component in enumerate(sorted_charts):
- if i < len(slices):
- chart_component["meta"]["chartId"] = int(slices[i].id)
- def merge_slice(slc: Slice) -> None:
- o = db.session.query(Slice).filter_by(slice_name=slc.slice_name).first()
- if o:
- db.session.delete(o)
- db.session.add(slc)
- db.session.commit()
- def get_slice_json(defaults: Dict[Any, Any], **kwargs: Any) -> str:
- defaults_copy = defaults.copy()
- defaults_copy.update(kwargs)
- return json.dumps(defaults_copy, indent=4, sort_keys=True)
- def get_example_data(
- filepath: str, is_gzip: bool = True, make_bytes: bool = False
- ) -> BytesIO:
- content = request.urlopen(f"{BASE_URL}{filepath}?raw=true").read()
- if is_gzip:
- content = zlib.decompress(content, zlib.MAX_WBITS | 16)
- if make_bytes:
- content = BytesIO(content)
- return content
|