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
|
2023-06-01 15:01:10 -04:00
|
|
|
from typing import Optional
|
2020-10-07 11:29:37 -04:00
|
|
|
|
2024-04-24 20:19:53 -04:00
|
|
|
from pandas import DataFrame # noqa: F401
|
2020-10-07 11:29:37 -04:00
|
|
|
|
2022-06-21 07:22:39 -04:00
|
|
|
from superset import db
|
2020-10-07 11:29:37 -04:00
|
|
|
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
|
2022-06-02 19:48:16 -04:00
|
|
|
from superset.utils.core import DatasourceType, get_example_default_schema
|
2020-10-07 11:29:37 -04:00
|
|
|
|
|
|
|
|
2021-12-16 19:11:47 -05:00
|
|
|
def get_table(
|
2022-03-29 13:03:09 -04:00
|
|
|
table_name: str,
|
|
|
|
database: Database,
|
|
|
|
schema: Optional[str] = None,
|
2021-12-16 19:11:47 -05:00
|
|
|
):
|
|
|
|
schema = schema or get_example_default_schema()
|
|
|
|
return (
|
2022-06-21 07:22:39 -04:00
|
|
|
db.session.query(SqlaTable)
|
2021-12-16 19:11:47 -05:00
|
|
|
.filter_by(database_id=database.id, schema=schema, table_name=table_name)
|
|
|
|
.one_or_none()
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def create_table_metadata(
|
2020-12-09 15:02:29 -05:00
|
|
|
table_name: str,
|
|
|
|
database: Database,
|
|
|
|
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()
|
|
|
|
|
2021-12-16 19:11:47 -05:00
|
|
|
table = get_table(table_name, database, schema)
|
2020-10-07 11:29:37 -04:00
|
|
|
if not table:
|
2023-09-14 18:57:08 -04:00
|
|
|
table = SqlaTable(
|
|
|
|
schema=schema,
|
|
|
|
table_name=table_name,
|
|
|
|
normalize_columns=False,
|
|
|
|
always_filter_main_dttm=False,
|
|
|
|
)
|
2023-11-20 20:25:41 -05:00
|
|
|
db.session.add(table)
|
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.commit()
|
|
|
|
|
|
|
|
return table
|
|
|
|
|
|
|
|
|
|
|
|
def create_slice(
|
2023-06-01 15:01:10 -04:00
|
|
|
title: str, viz_type: str, table: SqlaTable, slices_dict: dict[str, str]
|
2020-10-07 11:29:37 -04:00
|
|
|
) -> Slice:
|
|
|
|
return Slice(
|
|
|
|
slice_name=title,
|
|
|
|
viz_type=viz_type,
|
2022-06-02 19:48:16 -04:00
|
|
|
datasource_type=DatasourceType.TABLE,
|
2020-10-07 11:29:37 -04:00
|
|
|
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(
|
2023-06-01 15:01:10 -04:00
|
|
|
slug: str, title: str, position: str, slices: list[Slice]
|
2021-01-13 17:20:05 -05:00
|
|
|
) -> Dashboard:
|
2020-10-07 11:29:37 -04:00
|
|
|
dash = db.session.query(Dashboard).filter_by(slug=slug).one_or_none()
|
2023-01-25 04:26:50 -05:00
|
|
|
if dash:
|
|
|
|
return dash
|
|
|
|
dash = Dashboard()
|
2020-10-07 11:29:37 -04:00
|
|
|
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
|
2023-01-25 04:26:50 -05:00
|
|
|
db.session.add(dash)
|
2020-10-07 11:29:37 -04:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return dash
|