2020-10-07 11:29:37 -04:00
|
|
|
# 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.
|
|
|
|
"""Utils to provide dashboards for tests"""
|
|
|
|
|
|
|
|
import json
|
2021-03-09 10:27:46 -05:00
|
|
|
from typing import Any, Dict, List, Optional
|
2020-10-07 11:29:37 -04:00
|
|
|
|
|
|
|
from pandas import DataFrame
|
|
|
|
|
|
|
|
from superset import ConnectorRegistry, db
|
|
|
|
from superset.connectors.sqla.models import SqlaTable
|
|
|
|
from superset.models.core import Database
|
|
|
|
from superset.models.dashboard import Dashboard
|
|
|
|
from superset.models.slice import Slice
|
2021-11-04 14:09:08 -04:00
|
|
|
from superset.utils.core import get_example_default_schema
|
2020-10-07 11:29:37 -04:00
|
|
|
|
|
|
|
|
|
|
|
def create_table_for_dashboard(
|
2020-12-09 15:02:29 -05:00
|
|
|
df: DataFrame,
|
|
|
|
table_name: str,
|
|
|
|
database: Database,
|
|
|
|
dtype: Dict[str, Any],
|
|
|
|
table_description: str = "",
|
2021-03-09 10:27:46 -05:00
|
|
|
fetch_values_predicate: Optional[str] = None,
|
2021-08-02 15:45:55 -04:00
|
|
|
schema: Optional[str] = None,
|
2020-10-07 11:29:37 -04:00
|
|
|
) -> SqlaTable:
|
2021-11-04 14:09:08 -04:00
|
|
|
schema = schema or get_example_default_schema()
|
|
|
|
|
2020-10-07 11:29:37 -04:00
|
|
|
df.to_sql(
|
|
|
|
table_name,
|
|
|
|
database.get_sqla_engine(),
|
|
|
|
if_exists="replace",
|
|
|
|
chunksize=500,
|
|
|
|
dtype=dtype,
|
|
|
|
index=False,
|
|
|
|
method="multi",
|
2021-08-02 15:45:55 -04:00
|
|
|
schema=schema,
|
2020-10-07 11:29:37 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
table_source = ConnectorRegistry.sources["table"]
|
|
|
|
table = (
|
2021-08-02 15:45:55 -04:00
|
|
|
db.session.query(table_source)
|
|
|
|
.filter_by(database_id=database.id, schema=schema, table_name=table_name)
|
|
|
|
.one_or_none()
|
2020-10-07 11:29:37 -04:00
|
|
|
)
|
|
|
|
if not table:
|
2021-08-02 15:45:55 -04:00
|
|
|
table = table_source(schema=schema, table_name=table_name)
|
2021-03-09 10:27:46 -05:00
|
|
|
if fetch_values_predicate:
|
|
|
|
table.fetch_values_predicate = fetch_values_predicate
|
2020-10-07 11:29:37 -04:00
|
|
|
table.database = database
|
2020-12-09 15:02:29 -05:00
|
|
|
table.description = table_description
|
2020-10-07 11:29:37 -04:00
|
|
|
db.session.merge(table)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return table
|
|
|
|
|
|
|
|
|
|
|
|
def create_slice(
|
|
|
|
title: str, viz_type: str, table: SqlaTable, slices_dict: Dict[str, str]
|
|
|
|
) -> Slice:
|
|
|
|
return Slice(
|
|
|
|
slice_name=title,
|
|
|
|
viz_type=viz_type,
|
|
|
|
datasource_type="table",
|
|
|
|
datasource_id=table.id,
|
|
|
|
params=json.dumps(slices_dict, indent=4, sort_keys=True),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-01-13 17:20:05 -05:00
|
|
|
def create_dashboard(
|
|
|
|
slug: str, title: str, position: str, slices: List[Slice]
|
|
|
|
) -> Dashboard:
|
2020-10-07 11:29:37 -04:00
|
|
|
dash = db.session.query(Dashboard).filter_by(slug=slug).one_or_none()
|
|
|
|
|
|
|
|
if not dash:
|
|
|
|
dash = Dashboard()
|
|
|
|
dash.dashboard_title = title
|
|
|
|
if position is not None:
|
|
|
|
js = position
|
|
|
|
pos = json.loads(js)
|
|
|
|
dash.position_json = json.dumps(pos, indent=4)
|
|
|
|
dash.slug = slug
|
2021-01-13 17:20:05 -05:00
|
|
|
if slices is not None:
|
|
|
|
dash.slices = slices
|
2020-10-07 11:29:37 -04:00
|
|
|
db.session.merge(dash)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return dash
|