From 787c5137cc2d076c8aefeadb9f60086518906550 Mon Sep 17 00:00:00 2001 From: Beto Dealmeida Date: Thu, 4 Aug 2022 18:05:17 -0700 Subject: [PATCH] feat: per-db add metrics --- superset/connectors/sqla/models.py | 13 ++++------- superset/db_engine_specs/base.py | 35 ++++++++++++++++++++++++++++++ superset/models/core.py | 9 +++++++- 3 files changed, 47 insertions(+), 10 deletions(-) diff --git a/superset/connectors/sqla/models.py b/superset/connectors/sqla/models.py index cb1673c8287a7..7ac5675e6ecd2 100644 --- a/superset/connectors/sqla/models.py +++ b/superset/connectors/sqla/models.py @@ -1932,7 +1932,10 @@ def fetch_metadata(self, commit: bool = True) -> MetadataResult: :return: Tuple with lists of added, removed and modified column names. """ new_columns = self.external_metadata() - metrics = [] + metrics = [ + SqlMetric(**metric) + for metric in self.database.get_metrics(self.table_name, self.schema) + ] any_date_col = None db_engine_spec = self.db_engine_spec @@ -1989,14 +1992,6 @@ def fetch_metadata(self, commit: bool = True) -> MetadataResult: columns.extend([col for col in old_columns if col.expression]) self.columns = columns - metrics.append( - SqlMetric( - metric_name="count", - verbose_name="COUNT(*)", - metric_type="count", - expression="COUNT(*)", - ) - ) if not self.main_dttm_col: self.main_dttm_col = any_date_col self.add_missing_metrics(metrics) diff --git a/superset/db_engine_specs/base.py b/superset/db_engine_specs/base.py index e95e39c1fb50f..368770e2612f5 100644 --- a/superset/db_engine_specs/base.py +++ b/superset/db_engine_specs/base.py @@ -154,6 +154,21 @@ class LimitMethod: # pylint: disable=too-few-public-methods FORCE_LIMIT = "force_limit" +class MetricType(TypedDict, total=False): + """ + Type for metrics return by `get_metrics`. + """ + + metric_name: str + expression: str + verbose_name: Optional[str] + metric_type: Optional[str] + description: Optional[str] + d3format: Optional[str] + warning_text: Optional[str] + extra: Optional[str] + + class BaseEngineSpec: # pylint: disable=too-many-public-methods """Abstract class for database engine specific configurations @@ -1054,6 +1069,26 @@ def get_columns( """ return inspector.get_columns(table_name, schema) + @classmethod + def get_metrics( # pylint: disable=unused-argument + cls, + database: "Database", + inspector: Inspector, + table_name: str, + schema: Optional[str], + ) -> List[MetricType]: + """ + Get all metrics from a given schema and table. + """ + return [ + { + "metric_name": "count", + "verbose_name": "COUNT(*)", + "metric_type": "count", + "expression": "COUNT(*)", + } + ] + @classmethod def where_latest_partition( # pylint: disable=too-many-arguments,unused-argument cls, diff --git a/superset/models/core.py b/superset/models/core.py index 617c23ef9edb1..b5a4aa6537da2 100755 --- a/superset/models/core.py +++ b/superset/models/core.py @@ -55,7 +55,7 @@ from superset import app, db_engine_specs, is_feature_enabled from superset.databases.utils import make_url_safe -from superset.db_engine_specs.base import TimeGrain +from superset.db_engine_specs.base import MetricType, TimeGrain from superset.extensions import cache_manager, encrypted_field_factory, security_manager from superset.models.helpers import AuditMixinNullable, ImportExportMixin from superset.models.tags import FavStarUpdater @@ -693,6 +693,13 @@ def get_columns( ) -> List[Dict[str, Any]]: return self.db_engine_spec.get_columns(self.inspector, table_name, schema) + def get_metrics( + self, + table_name: str, + schema: Optional[str] = None, + ) -> List[MetricType]: + return self.db_engine_spec.get_metrics(self, self.inspector, table_name, schema) + def get_indexes( self, table_name: str, schema: Optional[str] = None ) -> List[Dict[str, Any]]: