Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Virtual dataset duplication #20309

Merged
merged 21 commits into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,277 changes: 1,067 additions & 210 deletions docs/static/resources/openapi.json

Large diffs are not rendered by default.

70 changes: 67 additions & 3 deletions superset-frontend/src/views/CRUD/data/dataset/DatasetList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import {
PASSWORDS_NEEDED_MESSAGE,
CONFIRM_OVERWRITE_MESSAGE,
} from './constants';
import DuplicateDatasetModal from './DuplicateDatasetModal';

const FlexRowContainer = styled.div`
align-items: center;
Expand Down Expand Up @@ -118,6 +119,11 @@ type Dataset = {
table_name: string;
};

interface VirtualDataset extends Dataset {
extra: any;
reesercollins marked this conversation as resolved.
Show resolved Hide resolved
sql: string;
}

interface DatasetListProps {
addDangerToast: (msg: string) => void;
addSuccessToast: (msg: string) => void;
Expand Down Expand Up @@ -156,6 +162,9 @@ const DatasetList: FunctionComponent<DatasetListProps> = ({
const [datasetCurrentlyEditing, setDatasetCurrentlyEditing] =
useState<Dataset | null>(null);

const [datasetCurrentlyDuplicating, setDatasetCurrentlyDuplicating] =
useState<VirtualDataset | null>(null);

const [importingDataset, showImportModal] = useState<boolean>(false);
const [passwordFields, setPasswordFields] = useState<string[]>([]);
const [preparingExport, setPreparingExport] = useState<boolean>(false);
Expand All @@ -177,6 +186,7 @@ const DatasetList: FunctionComponent<DatasetListProps> = ({
const canEdit = hasPerm('can_write');
const canDelete = hasPerm('can_write');
const canCreate = hasPerm('can_write');
const canDuplicate = hasPerm('can_duplicate');
const canExport =
hasPerm('can_export') && isFeatureEnabled(FeatureFlag.VERSIONED_EXPORT);

Expand Down Expand Up @@ -240,6 +250,10 @@ const DatasetList: FunctionComponent<DatasetListProps> = ({
),
);

const openDatasetDuplicateModal = (dataset: VirtualDataset) => {
setDatasetCurrentlyDuplicating(dataset);
};

const handleBulkDatasetExport = (datasetsToExport: Dataset[]) => {
const ids = datasetsToExport.map(({ id }) => id);
handleResourceExport('dataset', ids, () => {
Expand Down Expand Up @@ -392,7 +406,8 @@ const DatasetList: FunctionComponent<DatasetListProps> = ({
const handleEdit = () => openDatasetEditModal(original);
const handleDelete = () => openDatasetDeleteModal(original);
const handleExport = () => handleBulkDatasetExport([original]);
if (!canEdit && !canDelete && !canExport) {
const handleDuplicate = () => openDatasetDuplicateModal(original);
if (!canEdit && !canDelete && !canExport && !canDuplicate) {
return null;
}
return (
Expand Down Expand Up @@ -451,16 +466,32 @@ const DatasetList: FunctionComponent<DatasetListProps> = ({
</span>
</Tooltip>
)}
{canDuplicate && original.kind === 'virtual' && (
<Tooltip
id="duplicate-action-tooltop"
title={t('Duplicate')}
placement="bottom"
>
<span
role="button"
tabIndex={0}
className="action-button"
onClick={handleDuplicate}
>
<Icons.Copy />
</span>
</Tooltip>
)}
</Actions>
);
},
Header: t('Actions'),
id: 'actions',
hidden: !canEdit && !canDelete,
hidden: !canEdit && !canDelete && !canDuplicate,
disableSortBy: true,
},
],
[canEdit, canDelete, canExport, openDatasetEditModal],
[canEdit, canDelete, canExport, openDatasetEditModal, canDuplicate],
);

const filterTypes: Filters = useMemo(
Expand Down Expand Up @@ -620,6 +651,10 @@ const DatasetList: FunctionComponent<DatasetListProps> = ({
setDatasetCurrentlyEditing(null);
};

const closeDatasetDuplicateModal = () => {
setDatasetCurrentlyDuplicating(null);
};

const handleDatasetDelete = ({ id, table_name: tableName }: Dataset) => {
SupersetClient.delete({
endpoint: `/api/v1/dataset/${id}`,
Expand Down Expand Up @@ -655,6 +690,30 @@ const DatasetList: FunctionComponent<DatasetListProps> = ({
);
};

const handleDatasetDuplicate = (newDatasetName: string) => {
if (datasetCurrentlyDuplicating === null) {
addDangerToast(t('There was an issue duplicating the dataset.'));
}

SupersetClient.post({
endpoint: `/api/v1/dataset/duplicate`,
postPayload: {
base_model_id: datasetCurrentlyDuplicating?.id,
table_name: newDatasetName,
},
}).then(
() => {
setDatasetCurrentlyDuplicating(null);
refreshData();
},
createErrorHandler(errMsg =>
addDangerToast(
t('There was an issue duplicating the selected datasets: %s', errMsg),
),
),
);
};

return (
<>
<SubMenu {...menuData} />
Expand Down Expand Up @@ -689,6 +748,11 @@ const DatasetList: FunctionComponent<DatasetListProps> = ({
show
/>
)}
<DuplicateDatasetModal
dataset={datasetCurrentlyDuplicating}
onHide={closeDatasetDuplicateModal}
onDuplicate={handleDatasetDuplicate}
/>
<ConfirmStatusChange
title={t('Please confirm')}
description={t(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { t } from '@superset-ui/core';
import React, { FunctionComponent, useEffect, useState } from 'react';
import { FormLabel } from 'src/components/Form';
import { Input } from 'src/components/Input';
import Modal from 'src/components/Modal';
import Dataset from 'src/types/Dataset';

interface DuplicateDatasetModalProps {
dataset: Dataset | null;
onHide: () => void;
onDuplicate: (newDatasetName: string) => void;
}

const DuplicateDatasetModal: FunctionComponent<DuplicateDatasetModalProps> = ({
dataset,
onHide,
onDuplicate,
}) => {
const [show, setShow] = useState<boolean>(false);
const [disableSave, setDisableSave] = useState<boolean>(false);
const [newDuplicateDatasetName, setNewDuplicateDatasetName] =
useState<string>('');

const onChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const targetValue = event.target.value ?? '';
setNewDuplicateDatasetName(targetValue);
setDisableSave(targetValue === '');
};

const duplicateDataset = () => {
onDuplicate(newDuplicateDatasetName);
};

useEffect(() => {
setNewDuplicateDatasetName('');
setShow(dataset !== null);
}, [dataset]);

return (
<Modal
show={show}
onHide={onHide}
title={t('Duplicate dataset')}
disablePrimaryButton={disableSave}
onHandledPrimaryAction={duplicateDataset}
primaryButtonName={t('Duplicate')}
>
<FormLabel htmlFor="duplicate">{t('New dataset name')}</FormLabel>
<Input
data-test="duplicate-modal-input"
type="text"
id="duplicate"
autoComplete="off"
value={newDuplicateDatasetName}
onChange={onChange}
onPressEnter={duplicateDataset}
/>
</Modal>
);
};

export default DuplicateDatasetModal;
82 changes: 80 additions & 2 deletions superset/datasets/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from zipfile import is_zipfile, ZipFile

import yaml
from flask import request, Response, send_file
from flask import g, request, Response, send_file
from flask_appbuilder.api import expose, protect, rison, safe
from flask_appbuilder.models.sqla.interface import SQLAInterface
from flask_babel import ngettext
Expand All @@ -37,6 +37,7 @@
from superset.datasets.commands.bulk_delete import BulkDeleteDatasetCommand
from superset.datasets.commands.create import CreateDatasetCommand
from superset.datasets.commands.delete import DeleteDatasetCommand
from superset.datasets.commands.duplicate import DuplicateDatasetCommand
from superset.datasets.commands.exceptions import (
DatasetBulkDeleteFailedError,
DatasetCreateFailedError,
Expand All @@ -54,6 +55,7 @@
from superset.datasets.dao import DatasetDAO
from superset.datasets.filters import DatasetCertifiedFilter, DatasetIsNullOrEmptyFilter
from superset.datasets.schemas import (
DatasetDuplicateSchema,
DatasetPostSchema,
DatasetPutSchema,
DatasetRelatedObjectsResponse,
Expand Down Expand Up @@ -90,6 +92,7 @@ class DatasetRestApi(BaseSupersetModelRestApi):
"bulk_delete",
"refresh",
"related_objects",
"duplicate",
}
list_columns = [
"id",
Expand Down Expand Up @@ -184,6 +187,7 @@ class DatasetRestApi(BaseSupersetModelRestApi):
]
add_model_schema = DatasetPostSchema()
edit_model_schema = DatasetPutSchema()
duplicate_model_schema = DatasetDuplicateSchema()
add_columns = ["database", "schema", "table_name", "owners"]
edit_columns = [
"table_name",
Expand Down Expand Up @@ -220,7 +224,10 @@ class DatasetRestApi(BaseSupersetModelRestApi):
apispec_parameter_schemas = {
"get_export_ids_schema": get_export_ids_schema,
}
openapi_spec_component_schemas = (DatasetRelatedObjectsResponse,)
openapi_spec_component_schemas = (
DatasetRelatedObjectsResponse,
DatasetDuplicateSchema,
)

@expose("/", methods=["POST"])
@protect()
Expand Down Expand Up @@ -512,6 +519,77 @@ def export(self, **kwargs: Any) -> Response:
mimetype="application/text",
)

@expose("/duplicate", methods=["POST"])
@protect()
@safe
@statsd_metrics
@event_logger.log_this_with_context(
action=lambda self, *args, **kwargs: f"{self.__class__.__name__}" f".duplicate",
log_to_statsd=False,
)
@requires_json
def duplicate(self) -> Response:
"""Duplicates a Dataset
---
post:
description: >-
Duplicates a Dataset
requestBody:
description: Dataset schema
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/DatasetDuplicateSchema'
responses:
201:
description: Dataset duplicated
content:
application/json:
schema:
type: object
properties:
id:
type: number
result:
$ref: '#/components/schemas/DatasetDuplicateSchema'
400:
$ref: '#/components/responses/400'
401:
$ref: '#/components/responses/401'
403:
$ref: '#/components/responses/403'
404:
$ref: '#/components/responses/404'
422:
$ref: '#/components/responses/422'
500:
$ref: '#/components/responses/500'
"""
try:
item = self.duplicate_model_schema.load(request.json)
# This validates custom Schema with custom validations
except ValidationError as error:
return self.response_400(message=error.messages)

try:
new_model = DuplicateDatasetCommand([g.user.id], item).run()
return self.response(201, id=new_model.id, result=item)
except DatasetInvalidError as ex:
return self.response_422(
message=ex.normalized_messages()
if isinstance(ex, ValidationError)
else str(ex)
)
except DatasetCreateFailedError as ex:
logger.error(
"Error creating model %s: %s",
self.__class__.__name__,
str(ex),
exc_info=True,
)
return self.response_422(message=str(ex))

@expose("/<pk>/refresh", methods=["PUT"])
@protect()
@safe
Expand Down
Loading