Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Metabase
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Engineering Digital Service
Metabase
Commits
fb266e0a
Commit
fb266e0a
authored
8 years ago
by
Cam Saül
Browse files
Options
Downloads
Patches
Plain Diff
New POST /api/card/:id/query/csv endpoint
parent
3761a9df
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/metabase/api/card.clj
+16
-9
16 additions, 9 deletions
src/metabase/api/card.clj
src/metabase/api/dataset.clj
+19
-13
19 additions, 13 deletions
src/metabase/api/dataset.clj
with
35 additions
and
22 deletions
src/metabase/api/card.clj
+
16
−
9
View file @
fb266e0a
...
...
@@ -262,17 +262,24 @@
;;; ------------------------------------------------------------ Running a Query ------------------------------------------------------------
(
defn-
run-query-for-card
[
card-id
parameters
]
(
let
[
card
(
read-check
Card
card-id
)
query
(
assoc
(
:dataset_query
card
)
:parameters
parameters
:constraints
dataset-api/query-constraints
)
options
{
:executed-by
*current-user-id*
:card-id
card-id
}]
(
qp/dataset-query
query
options
)))
(
defendpoint
POST
"/:card-id/query"
"Run the query associated with a Card."
[
card-id
:as
{{
:keys
[
parameters
timeout
]}
:body
}]
(
let
[
card
(
read-check
Card
card-id
)
query
(
assoc
(
:dataset_query
card
)
:parameters
parameters
:constraints
dataset-api/query-constraints
)]
;; Now run the query!
(
let
[
options
{
:executed-by
*current-user-id*
:card-id
card-id
}]
(
qp/dataset-query
query
options
))))
[
card-id
:as
{{
:keys
[
parameters
]}
:body
}]
(
run-query-for-card
card-id
parameters
))
(
defendpoint
POST
"/:card-id/query/csv"
"Run the query associated with a Card, and return its results as CSV."
[
card-id
:as
{{
:keys
[
parameters
]}
:body
}]
(
dataset-api/as-csv
(
run-query-for-card
card-id
parameters
)))
(
define-routes
)
This diff is collapsed.
Click to expand it.
src/metabase/api/dataset.clj
+
19
−
13
View file @
fb266e0a
...
...
@@ -48,26 +48,32 @@
(
float
(
/
(
reduce
+
running-times
)
(
count
running-times
))))}))
(
defn
as-csv
"Return a CSV response containing the RESULTS of a query."
{
:arglists
'
([
results
])}
[{{
:keys
[
columns
rows
]}
:data,
:keys
[
status
]
,
:as
response
}]
(
if
(
=
status
:completed
)
;; successful query, send CSV file
{
:status
200
:body
(
with-out-str
;; turn keywords into strings, otherwise we get colons in our output
(
csv/write-csv
*out*
(
into
[(
mapv
name
columns
)]
rows
)))
:headers
{
"Content-Type"
"text/csv; charset=utf-8"
"Content-Disposition"
(
str
"attachment; filename=\"query_result_"
(
u/date->iso-8601
)
".csv\""
)}}
;; failed query, send error message
{
:status
500
:body
(
:error
response
)}))
(
defendpoint
POST
"/csv"
"Execute a
n MQL
query and download the result data as a CSV file."
"Execute a query and download the result data as a CSV file."
[
query
]
{
query
[
Required
String->Dict
]}
(
read-check
Database
(
:database
query
))
(
let
[{{
:keys
[
columns
rows
]}
:data
:keys
[
status
]
:as
response
}
(
qp/dataset-query
query
{
:executed-by
*current-user-id*
})
columns
(
map
name
columns
)]
; turn keywords into strings, otherwise we get colons in our output
(
if
(
=
status
:completed
)
;; successful query, send CSV file
{
:status
200
:body
(
with-out-str
(
csv/write-csv
*out*
(
into
[
columns
]
rows
)))
:headers
{
"Content-Type"
"text/csv; charset=utf-8"
"Content-Disposition"
(
str
"attachment; filename=\"query_result_"
(
u/date->iso-8601
)
".csv\""
)}}
;; failed query, send error message
{
:status
500
:body
(
:error
response
)})))
(
as-csv
(
qp/dataset-query
query
{
:executed-by
*current-user-id*
})))
;; TODO - AFAIK this endpoint is no longer used. Remove it? </3
;; (We have POST /api/card/:id/query now which is used instead)
(
defendpoint
GET
"/card/:id"
"Execute the MQL query for a given `Card` and retrieve both the `Card` and the execution results as JSON.
This is a convenience endpoint which simplifies the normal 2 api calls to fetch the `Card` then execute its query."
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment