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

chore: Migrate /superset/csv/<client_id> to API v1 #22913

Merged
merged 4 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
90 changes: 73 additions & 17 deletions docs/static/resources/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@
"AnnotationLayerRestApi.get_list": {
"properties": {
"changed_by": {
"$ref": "#/components/schemas/AnnotationLayerRestApi.get_list.User1"
"$ref": "#/components/schemas/AnnotationLayerRestApi.get_list.User"
},
"changed_on": {
"format": "date-time",
Expand All @@ -356,7 +356,7 @@
"readOnly": true
},
"created_by": {
"$ref": "#/components/schemas/AnnotationLayerRestApi.get_list.User"
"$ref": "#/components/schemas/AnnotationLayerRestApi.get_list.User1"
},
"created_on": {
"format": "date-time",
Expand Down Expand Up @@ -502,13 +502,13 @@
"AnnotationRestApi.get_list": {
"properties": {
"changed_by": {
"$ref": "#/components/schemas/AnnotationRestApi.get_list.User1"
"$ref": "#/components/schemas/AnnotationRestApi.get_list.User"
},
"changed_on_delta_humanized": {
"readOnly": true
},
"created_by": {
"$ref": "#/components/schemas/AnnotationRestApi.get_list.User"
"$ref": "#/components/schemas/AnnotationRestApi.get_list.User1"
},
"end_dttm": {
"format": "date-time",
Expand Down Expand Up @@ -1223,6 +1223,7 @@
"example": false
},
"periods": {
"description": "Time periods (in units of `time_grain`) to predict into the future",
"example": 7,
"format": "int32",
"type": "integer"
Expand Down Expand Up @@ -1578,6 +1579,7 @@
"type": "string"
},
"from_dttm": {
"description": "Start timestamp of time range",
"format": "int32",
"nullable": true,
"type": "integer"
Expand All @@ -1603,6 +1605,7 @@
"type": "integer"
},
"stacktrace": {
"description": "Stacktrace if there was an error",
"nullable": true,
"type": "string"
},
Expand All @@ -1620,6 +1623,7 @@
"type": "string"
},
"to_dttm": {
"description": "End timestamp of time range",
"format": "int32",
"nullable": true,
"type": "integer"
Expand Down Expand Up @@ -2232,6 +2236,7 @@
"type": "string"
},
"rolling_type_options": {
"description": "Optional options to pass to rolling method. Needed for e.g. quantile operation.",
"example": {},
"type": "object"
},
Expand Down Expand Up @@ -3027,13 +3032,13 @@
"CssTemplateRestApi.get_list": {
"properties": {
"changed_by": {
"$ref": "#/components/schemas/CssTemplateRestApi.get_list.User1"
"$ref": "#/components/schemas/CssTemplateRestApi.get_list.User"
},
"changed_on_delta_humanized": {
"readOnly": true
},
"created_by": {
"$ref": "#/components/schemas/CssTemplateRestApi.get_list.User"
"$ref": "#/components/schemas/CssTemplateRestApi.get_list.User1"
},
"created_on": {
"format": "date-time",
Expand Down Expand Up @@ -5230,7 +5235,7 @@
"DatasetRestApi.get_list": {
"properties": {
"changed_by": {
"$ref": "#/components/schemas/DatasetRestApi.get_list.User1"
"$ref": "#/components/schemas/DatasetRestApi.get_list.User"
},
"changed_by_name": {
"readOnly": true
Expand Down Expand Up @@ -5273,7 +5278,7 @@
"readOnly": true
},
"owners": {
"$ref": "#/components/schemas/DatasetRestApi.get_list.User"
"$ref": "#/components/schemas/DatasetRestApi.get_list.User1"
},
"schema": {
"maxLength": 255,
Expand Down Expand Up @@ -5317,22 +5322,13 @@
"maxLength": 64,
"type": "string"
},
"id": {
"format": "int32",
"type": "integer"
},
"last_name": {
"maxLength": 64,
"type": "string"
},
"username": {
"maxLength": 64,
"type": "string"
}
},
"required": [
"first_name",
"last_name",
"username"
],
"type": "object"
Expand All @@ -5343,13 +5339,22 @@
"maxLength": 64,
"type": "string"
},
"id": {
"format": "int32",
"type": "integer"
},
"last_name": {
"maxLength": 64,
"type": "string"
},
"username": {
"maxLength": 64,
"type": "string"
}
},
"required": [
"first_name",
"last_name",
"username"
],
"type": "object"
Expand Down Expand Up @@ -19997,6 +20002,57 @@
]
}
},
"/api/v1/sqllab/export/{client_id}/": {
"get": {
"parameters": [
{
"description": "The SQL query result identifier",
"in": "path",
"name": "client_id",
"required": true,
"schema": {
"type": "integer"
}
}
],
"responses": {
"200": {
"content": {
"text/csv": {
"schema": {
"type": "string"
}
}
},
"description": "SQL query results"
},
"400": {
"$ref": "#/components/responses/400"
},
"401": {
"$ref": "#/components/responses/401"
},
"403": {
"$ref": "#/components/responses/403"
},
"404": {
"$ref": "#/components/responses/404"
},
"500": {
"$ref": "#/components/responses/500"
}
},
"security": [
{
"jwt": []
}
],
"summary": "Exports the SQL query results to a CSV",
"tags": [
"SQL Lab"
]
}
},
"/api/v1/sqllab/results/": {
"get": {
"parameters": [
Expand Down
5 changes: 4 additions & 1 deletion superset-frontend/src/SqlLab/components/ResultSet/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ const ResultSet = ({
}
};

const getExportCsvUrl = (clientId: string) =>
`/api/v1/sqllab/export/${clientId}/`;

const renderControls = () => {
if (search || visualize || csv) {
let { data } = query.results;
Expand Down Expand Up @@ -257,7 +260,7 @@ const ResultSet = ({
/>
)}
{csv && (
<Button buttonSize="small" href={`/superset/csv/${query.id}`}>
<Button buttonSize="small" href={getExportCsvUrl(query.id)}>
<i className="fa fa-file-text-o" /> {t('Download to CSV')}
</Button>
)}
Expand Down
67 changes: 66 additions & 1 deletion superset/sqllab/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# under the License.
import logging
from typing import Any, cast, Dict, Optional
from urllib import parse

import simplejson as json
from flask import request
Expand All @@ -32,6 +33,7 @@
from superset.sql_lab import get_sql_results
from superset.sqllab.command_status import SqlJsonExecutionStatus
from superset.sqllab.commands.execute import CommandResult, ExecuteSqlCommand
from superset.sqllab.commands.export import SqlResultExportCommand
from superset.sqllab.commands.results import SqlExecutionResultsCommand
from superset.sqllab.exceptions import (
QueryIsForbiddenToAccessException,
Expand All @@ -53,7 +55,7 @@
from superset.sqllab.validators import CanAccessQueryValidatorImpl
from superset.superset_typing import FlaskResponse
from superset.utils import core as utils
from superset.views.base import json_success
from superset.views.base import CsvResponse, generate_download_headers, json_success
from superset.views.base_api import BaseSupersetApi, requires_json, statsd_metrics

config = app.config
Expand All @@ -79,6 +81,69 @@ class SqlLabRestApi(BaseSupersetApi):
QueryExecutionResponseSchema,
)

@expose("/export/<string:client_id>/")
@protect()
@statsd_metrics
@event_logger.log_this_with_context(
action=lambda self, *args, **kwargs: f"{self.__class__.__name__}"
f".export_csv",
log_to_statsd=False,
)
def export_csv(self, client_id: str) -> CsvResponse:
"""Exports the SQL query results to a CSV
---
get:
summary: >-
Exports the SQL query results to a CSV
parameters:
- in: path
schema:
type: integer
name: client_id
description: The SQL query result identifier
responses:
200:
description: SQL query results
content:
text/csv:
schema:
type: string
400:
$ref: '#/components/responses/400'
401:
$ref: '#/components/responses/401'
403:
$ref: '#/components/responses/403'
404:
$ref: '#/components/responses/404'
500:
$ref: '#/components/responses/500'
"""
result = SqlResultExportCommand(client_id=client_id).run()

query = result.get("query")
data = result.get("data")
row_count = result.get("count")

quoted_csv_name = parse.quote(query.name)
response = CsvResponse(
data, headers=generate_download_headers("csv", quoted_csv_name)
)
event_info = {
"event_type": "data_export",
"client_id": client_id,
"row_count": row_count,
"database": query.database.name,
"schema": query.schema,
"sql": query.sql,
"exported_format": "csv",
}
event_rep = repr(event_info)
logger.debug(
"CSV exported: %s", event_rep, extra={"superset_event": event_info}
)
return response

@expose("/results/")
@protect()
@statsd_metrics
Expand Down
Loading