feat: dataset api endpoint for charts and dashboards count (#10235)

* create GET endpoint for charts and dashboards count associated to a dataset

* add test for chart and dashboard count dataset
This commit is contained in:
Lily Kuang
2020-07-06 16:25:57 -07:00
committed by GitHub
parent 84f8a51458
commit b9e0678752
4 changed files with 136 additions and 0 deletions

View File

@@ -37,6 +37,7 @@ from superset.datasets.commands.exceptions import (
)
from superset.datasets.commands.refresh import RefreshDatasetCommand
from superset.datasets.commands.update import UpdateDatasetCommand
from superset.datasets.dao import DatasetDAO
from superset.datasets.schemas import (
DatasetPostSchema,
DatasetPutSchema,
@@ -66,6 +67,7 @@ class DatasetRestApi(BaseSupersetModelRestApi):
RouteMethod.EXPORT,
RouteMethod.RELATED,
"refresh",
"related_objects",
}
list_columns = [
"id",
@@ -412,3 +414,98 @@ class DatasetRestApi(BaseSupersetModelRestApi):
"Error refreshing dataset %s: %s", self.__class__.__name__, str(ex)
)
return self.response_422(message=str(ex))
@expose("/<pk>/related_objects", methods=["GET"])
@protect()
@safe
@statsd_metrics
def related_objects(self, pk: int) -> Response:
"""Get charts and dashboards count associated to a dataset
---
get:
description:
Get charts and dashboards count associated to a dataset
parameters:
- in: path
name: pk
schema:
type: integer
responses:
200:
description: chart and dashboard counts
content:
application/json:
schema:
type: object
properties:
charts:
type: object
properties:
count:
type: integer
result:
type: array
items:
type: object
properties:
id:
type: integer
slice_name:
type: string
viz_type:
type: string
dashboards:
type: object
properties:
count:
type: integer
result:
type: array
items:
type: object
properties:
id:
type: integer
json_metadata:
type: object
slug:
type: string
title:
type: string
400:
$ref: '#/components/responses/400'
401:
$ref: '#/components/responses/401'
404:
$ref: '#/components/responses/404'
500:
$ref: '#/components/responses/500'
"""
try:
data = DatasetDAO.get_related_objects(pk)
charts = [
{
"id": chart.id,
"slice_name": chart.slice_name,
"viz_type": chart.viz_type,
}
for chart in data["charts"]
]
dashboards = [
{
"id": dashboard.id,
"json_metadata": dashboard.json_metadata,
"slug": dashboard.slug,
"title": dashboard.dashboard_title,
}
for dashboard in data["dashboards"]
]
return self.response(
200,
charts={"count": len(charts), "result": charts},
dashboards={"count": len(dashboards), "result": dashboards},
)
except DatasetNotFoundError:
return self.response_404()
except DatasetForbiddenError:
return self.response_403()