示例#1
0
    def export(model: Dashboard) -> Iterator[Tuple[str, str]]:
        dashboard_slug = sanitize(model.dashboard_title)
        file_name = f"dashboards/{dashboard_slug}.yaml"

        payload = model.export_to_dict(
            recursive=False,
            include_parent_ref=False,
            include_defaults=True,
            export_uuids=True,
        )
        # TODO (betodealmeida): move this logic to export_to_dict once this
        # becomes the default export endpoint
        for key, new_name in JSON_KEYS.items():
            if payload.get(key):
                value = payload.pop(key)
                try:
                    payload[new_name] = json.loads(value)
                except json.decoder.JSONDecodeError:
                    logger.info("Unable to decode `%s` field: %s", key, value)

        payload["version"] = IMPORT_EXPORT_VERSION

        file_content = yaml.safe_dump(payload, sort_keys=False)
        yield file_name, file_content

        chart_ids = [chart.id for chart in model.slices]
        yield from ExportChartsCommand(chart_ids).run()
示例#2
0
    def export_chart(chart: Slice) -> Iterator[Tuple[str, str]]:
        chart_slug = sanitize(chart.slice_name)
        file_name = f"charts/{chart_slug}.yaml"

        payload = chart.export_to_dict(
            recursive=False,
            include_parent_ref=False,
            include_defaults=True,
            export_uuids=True,
        )
        # TODO (betodealmeida): move this logic to export_to_dict once this
        # becomes the default export endpoint
        for key in REMOVE_KEYS:
            del payload[key]
        if "params" in payload:
            try:
                payload["params"] = json.loads(payload["params"])
            except json.decoder.JSONDecodeError:
                pass

        payload["version"] = IMPORT_EXPORT_VERSION
        if chart.table:
            payload["dataset_uuid"] = str(chart.table.uuid)

        file_content = yaml.safe_dump(payload, sort_keys=False)
        yield file_name, file_content

        if chart.table:
            yield from ExportDatasetsCommand([chart.table.id]).run()
示例#3
0
    def export_database(database: Database) -> Iterator[Tuple[str, str]]:
        name = sanitize(database.database_name)
        file_name = f"databases/{name}.yaml"

        payload = database.export_to_dict(
            recursive=False,
            include_parent_ref=False,
            include_defaults=True,
            export_uuids=True,
        )
        # TODO (betodealmeida): move this logic to export_to_dict once this
        # becomes the default export endpoint
        if "extra" in payload:
            try:
                payload["extra"] = json.loads(payload["extra"])
            except json.decoder.JSONDecodeError:
                pass

        payload["version"] = IMPORT_EXPORT_VERSION

        file_content = yaml.safe_dump(payload, sort_keys=False)
        yield file_name, file_content

        # TODO (betodealmeida): reuse logic from ExportDatasetCommand once
        # it's implemented
        for dataset in database.tables:
            name = sanitize(dataset.table_name)
            file_name = f"datasets/{name}.yaml"

            payload = dataset.export_to_dict(
                recursive=True,
                include_parent_ref=False,
                include_defaults=True,
                export_uuids=True,
            )
            payload["version"] = IMPORT_EXPORT_VERSION
            payload["database_uuid"] = str(database.uuid)

            file_content = yaml.safe_dump(payload, sort_keys=False)
            yield file_name, file_content
示例#4
0
    def export(model: SqlaTable) -> Iterator[Tuple[str, str]]:
        database_slug = sanitize(model.database.database_name)
        dataset_slug = sanitize(model.table_name)
        file_name = f"datasets/{database_slug}/{dataset_slug}.yaml"

        payload = model.export_to_dict(
            recursive=True,
            include_parent_ref=False,
            include_defaults=True,
            export_uuids=True,
        )

        payload["version"] = IMPORT_EXPORT_VERSION
        payload["database_uuid"] = str(model.database.uuid)

        file_content = yaml.safe_dump(payload, sort_keys=False)
        yield file_name, file_content

        # include database as well
        file_name = f"databases/{database_slug}.yaml"

        payload = model.database.export_to_dict(
            recursive=False,
            include_parent_ref=False,
            include_defaults=True,
            export_uuids=True,
        )
        # TODO (betodealmeida): move this logic to export_to_dict once this
        # becomes the default export endpoint
        if "extra" in payload:
            try:
                payload["extra"] = json.loads(payload["extra"])
            except json.decoder.JSONDecodeError:
                logger.info("Unable to decode `extra` field: %s",
                            payload["extra"])

        payload["version"] = IMPORT_EXPORT_VERSION

        file_content = yaml.safe_dump(payload, sort_keys=False)
        yield file_name, file_content