2022-10-11 15:57:25 +02:00
|
|
|
# Copyright 2021 Collate
|
|
|
|
# Licensed 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.
|
2023-09-13 16:32:55 +02:00
|
|
|
# pylint: disable=arguments-differ
|
2022-10-11 15:57:25 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
Interfaces with database for all database engine
|
|
|
|
supporting sqlalchemy abstraction layer
|
|
|
|
"""
|
|
|
|
|
|
|
|
import concurrent.futures
|
|
|
|
import threading
|
|
|
|
import traceback
|
|
|
|
from collections import defaultdict
|
|
|
|
from datetime import datetime, timezone
|
2023-07-13 13:35:37 +02:00
|
|
|
from typing import Dict, List
|
2022-10-11 15:57:25 +02:00
|
|
|
|
2022-11-15 20:31:10 +05:30
|
|
|
from sqlalchemy import Column
|
2023-03-08 18:01:25 +01:00
|
|
|
from sqlalchemy.exc import ProgrammingError
|
2023-03-01 08:20:38 +01:00
|
|
|
from sqlalchemy.orm import scoped_session
|
2022-10-11 15:57:25 +02:00
|
|
|
|
2022-11-15 20:31:10 +05:30
|
|
|
from metadata.generated.schema.entity.data.table import TableData
|
2023-01-02 13:52:27 +01:00
|
|
|
from metadata.ingestion.connections.session import create_and_bind_thread_safe_session
|
2023-04-04 17:16:44 +02:00
|
|
|
from metadata.mixins.sqalchemy.sqa_mixin import SQAInterfaceMixin
|
2023-07-12 17:02:32 +02:00
|
|
|
from metadata.profiler.interface.profiler_interface import ProfilerInterface
|
2023-03-03 21:56:32 +01:00
|
|
|
from metadata.profiler.metrics.core import MetricTypes
|
2023-03-01 08:20:38 +01:00
|
|
|
from metadata.profiler.metrics.registry import Metrics
|
2023-03-08 18:01:25 +01:00
|
|
|
from metadata.profiler.metrics.static.mean import Mean
|
|
|
|
from metadata.profiler.metrics.static.stddev import StdDev
|
|
|
|
from metadata.profiler.metrics.static.sum import Sum
|
2023-05-22 09:04:18 +02:00
|
|
|
from metadata.profiler.orm.functions.table_metric_construct import (
|
|
|
|
table_metric_construct_factory,
|
|
|
|
)
|
2023-04-04 17:16:44 +02:00
|
|
|
from metadata.profiler.processor.runner import QueryRunner
|
2023-07-13 13:35:37 +02:00
|
|
|
from metadata.profiler.processor.sampler.sampler_factory import sampler_factory_
|
2022-12-16 17:01:12 +01:00
|
|
|
from metadata.utils.custom_thread_pool import CustomThreadPoolExecutor
|
2022-11-15 20:31:10 +05:30
|
|
|
from metadata.utils.logger import profiler_interface_registry_logger
|
2022-10-11 15:57:25 +02:00
|
|
|
|
2022-11-15 20:31:10 +05:30
|
|
|
logger = profiler_interface_registry_logger()
|
2022-10-11 15:57:25 +02:00
|
|
|
thread_local = threading.local()
|
|
|
|
|
2023-03-08 18:01:25 +01:00
|
|
|
OVERFLOW_ERROR_CODES = {
|
|
|
|
"snowflake": {100046, 100058},
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def handle_query_exception(msg, exc, session):
|
|
|
|
"""Handle exception for query runs"""
|
|
|
|
logger.debug(traceback.format_exc())
|
|
|
|
logger.warning(msg)
|
|
|
|
session.rollback()
|
|
|
|
raise RuntimeError(exc)
|
|
|
|
|
2022-10-11 15:57:25 +02:00
|
|
|
|
2023-07-12 17:02:32 +02:00
|
|
|
class SQAProfilerInterface(ProfilerInterface, SQAInterfaceMixin):
|
2022-10-11 15:57:25 +02:00
|
|
|
"""
|
|
|
|
Interface to interact with registry supporting
|
|
|
|
sqlalchemy.
|
|
|
|
"""
|
|
|
|
|
2023-07-12 17:02:32 +02:00
|
|
|
# pylint: disable=too-many-arguments
|
2023-03-01 08:20:38 +01:00
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
service_connection_config,
|
|
|
|
ometa_client,
|
|
|
|
entity,
|
|
|
|
profile_sample_config,
|
2023-03-03 18:33:18 +05:30
|
|
|
source_config,
|
2023-03-01 08:20:38 +01:00
|
|
|
sample_query,
|
|
|
|
table_partition_config,
|
2023-07-12 17:02:32 +02:00
|
|
|
thread_count: int = 5,
|
|
|
|
timeout_seconds: int = 43200,
|
2023-03-01 08:20:38 +01:00
|
|
|
sqa_metadata=None,
|
2023-07-12 17:02:32 +02:00
|
|
|
**kwargs,
|
2023-03-01 08:20:38 +01:00
|
|
|
):
|
2022-10-11 15:57:25 +02:00
|
|
|
"""Instantiate SQA Interface object"""
|
|
|
|
|
2023-07-12 17:02:32 +02:00
|
|
|
super().__init__(
|
|
|
|
service_connection_config,
|
|
|
|
ometa_client,
|
|
|
|
entity,
|
|
|
|
profile_sample_config,
|
|
|
|
source_config,
|
|
|
|
sample_query,
|
|
|
|
table_partition_config,
|
|
|
|
thread_count,
|
|
|
|
timeout_seconds,
|
|
|
|
)
|
2022-10-11 15:57:25 +02:00
|
|
|
|
2023-07-12 17:02:32 +02:00
|
|
|
self._table = self._convert_table_to_orm_object(sqa_metadata)
|
2023-06-16 16:49:55 +02:00
|
|
|
self.session_factory = self._session_factory()
|
2022-10-11 15:57:25 +02:00
|
|
|
self.session = self.session_factory()
|
|
|
|
self.set_session_tag(self.session)
|
2023-04-05 18:47:18 +02:00
|
|
|
self.set_catalog(self.session)
|
2022-10-11 15:57:25 +02:00
|
|
|
|
2023-03-01 08:20:38 +01:00
|
|
|
@property
|
|
|
|
def table(self):
|
|
|
|
return self._table
|
2022-12-16 17:01:12 +01:00
|
|
|
|
2023-07-12 17:02:32 +02:00
|
|
|
def _get_sampler(self, **kwargs):
|
|
|
|
"""get sampler object"""
|
|
|
|
session = kwargs.get("session")
|
|
|
|
table = kwargs["table"]
|
|
|
|
|
2023-07-13 13:35:37 +02:00
|
|
|
return sampler_factory_.create(
|
2023-07-12 17:02:32 +02:00
|
|
|
self.service_connection_config.__class__.__name__,
|
|
|
|
client=session or self.session,
|
|
|
|
table=table,
|
|
|
|
profile_sample_config=self.profile_sample_config,
|
|
|
|
partition_details=self.partition_details,
|
|
|
|
profile_sample_query=self.profile_query,
|
|
|
|
)
|
|
|
|
|
2023-06-16 16:49:55 +02:00
|
|
|
def _session_factory(self) -> scoped_session:
|
2022-10-11 15:57:25 +02:00
|
|
|
"""Create thread safe session that will be automatically
|
|
|
|
garbage collected once the application thread ends
|
|
|
|
"""
|
2023-07-12 17:02:32 +02:00
|
|
|
return create_and_bind_thread_safe_session(self.connection)
|
2022-10-11 15:57:25 +02:00
|
|
|
|
2023-03-08 18:01:25 +01:00
|
|
|
@staticmethod
|
|
|
|
def _compute_static_metrics_wo_sum(
|
|
|
|
metrics: List[Metrics],
|
|
|
|
runner: QueryRunner,
|
|
|
|
session,
|
|
|
|
column: Column,
|
|
|
|
):
|
|
|
|
"""If we catch an overflow error, we will try to compute the static
|
|
|
|
metrics without the sum, mean and stddev
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
_type_: _description_
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
row = runner.select_first_from_sample(
|
|
|
|
*[
|
|
|
|
metric(column).fn()
|
|
|
|
for metric in metrics
|
|
|
|
if not metric.is_window_metric()
|
|
|
|
and metric not in {Sum, StdDev, Mean}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
return dict(row)
|
|
|
|
except Exception as exc:
|
|
|
|
msg = f"Error trying to compute profile for {runner.table.__tablename__}.{column.name}: {exc}"
|
|
|
|
handle_query_exception(msg, exc, session)
|
2023-06-22 12:51:56 +05:30
|
|
|
return None
|
2023-03-08 18:01:25 +01:00
|
|
|
|
2023-09-13 16:32:55 +02:00
|
|
|
def _compute_table_metrics(
|
2022-12-07 14:33:30 +01:00
|
|
|
self,
|
|
|
|
metrics: List[Metrics],
|
|
|
|
runner: QueryRunner,
|
|
|
|
session,
|
|
|
|
*args,
|
|
|
|
**kwargs,
|
|
|
|
):
|
|
|
|
"""Given a list of metrics, compute the given results
|
|
|
|
and returns the values
|
|
|
|
|
|
|
|
Args:
|
|
|
|
metrics: list of metrics to compute
|
|
|
|
Returns:
|
|
|
|
dictionnary of results
|
|
|
|
"""
|
2023-06-22 12:51:56 +05:30
|
|
|
# pylint: disable=protected-access
|
2022-12-07 14:33:30 +01:00
|
|
|
try:
|
2023-05-22 09:04:18 +02:00
|
|
|
dialect = runner._session.get_bind().dialect.name
|
|
|
|
row = table_metric_construct_factory.construct(
|
|
|
|
dialect,
|
|
|
|
runner=runner,
|
|
|
|
metrics=metrics,
|
|
|
|
conn_config=self.service_connection_config,
|
2022-12-07 14:33:30 +01:00
|
|
|
)
|
|
|
|
if row:
|
|
|
|
return dict(row)
|
|
|
|
return None
|
|
|
|
|
|
|
|
except Exception as exc:
|
|
|
|
logger.debug(traceback.format_exc())
|
|
|
|
logger.warning(
|
2023-03-01 08:20:38 +01:00
|
|
|
f"Error trying to compute profile for {runner.table.__tablename__}: {exc}" # type: ignore
|
2022-12-07 14:33:30 +01:00
|
|
|
)
|
|
|
|
session.rollback()
|
|
|
|
raise RuntimeError(exc)
|
|
|
|
|
2023-09-13 16:32:55 +02:00
|
|
|
def _compute_static_metrics(
|
2022-12-07 14:33:30 +01:00
|
|
|
self,
|
|
|
|
metrics: List[Metrics],
|
|
|
|
runner: QueryRunner,
|
2023-09-13 16:32:55 +02:00
|
|
|
column,
|
2022-12-07 14:33:30 +01:00
|
|
|
session,
|
|
|
|
*args,
|
|
|
|
**kwargs,
|
|
|
|
):
|
|
|
|
"""Given a list of metrics, compute the given results
|
|
|
|
and returns the values
|
|
|
|
|
|
|
|
Args:
|
|
|
|
column: the column to compute the metrics against
|
|
|
|
metrics: list of metrics to compute
|
|
|
|
Returns:
|
|
|
|
dictionnary of results
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
row = runner.select_first_from_sample(
|
|
|
|
*[
|
|
|
|
metric(column).fn()
|
|
|
|
for metric in metrics
|
|
|
|
if not metric.is_window_metric()
|
2023-04-14 15:59:26 +02:00
|
|
|
],
|
2022-12-07 14:33:30 +01:00
|
|
|
)
|
|
|
|
return dict(row)
|
2023-06-22 12:51:56 +05:30
|
|
|
except ProgrammingError as exc:
|
2023-10-04 18:39:39 +05:30
|
|
|
return self._programming_error_static_metric(
|
|
|
|
runner, column, exc, session, metrics
|
|
|
|
)
|
2023-06-22 12:51:56 +05:30
|
|
|
except Exception as exc:
|
2023-03-08 18:01:25 +01:00
|
|
|
msg = f"Error trying to compute profile for {runner.table.__tablename__}.{column.name}: {exc}"
|
|
|
|
handle_query_exception(msg, exc, session)
|
2023-06-22 12:51:56 +05:30
|
|
|
return None
|
2022-12-07 14:33:30 +01:00
|
|
|
|
2023-09-13 16:32:55 +02:00
|
|
|
def _compute_query_metrics(
|
2022-12-07 14:33:30 +01:00
|
|
|
self,
|
|
|
|
metric: Metrics,
|
|
|
|
runner: QueryRunner,
|
2023-09-13 16:32:55 +02:00
|
|
|
column,
|
2022-12-07 14:33:30 +01:00
|
|
|
session,
|
|
|
|
sample,
|
2023-09-13 16:32:55 +02:00
|
|
|
*args,
|
|
|
|
**kwargs,
|
2022-12-07 14:33:30 +01:00
|
|
|
):
|
|
|
|
"""Given a list of metrics, compute the given results
|
|
|
|
and returns the values
|
|
|
|
|
|
|
|
Args:
|
|
|
|
column: the column to compute the metrics against
|
|
|
|
metrics: list of metrics to compute
|
|
|
|
Returns:
|
|
|
|
dictionnary of results
|
|
|
|
"""
|
2023-09-13 16:32:55 +02:00
|
|
|
|
2022-12-07 14:33:30 +01:00
|
|
|
try:
|
|
|
|
col_metric = metric(column)
|
|
|
|
metric_query = col_metric.query(sample=sample, session=session)
|
|
|
|
if not metric_query:
|
|
|
|
return None
|
|
|
|
if col_metric.metric_type == dict:
|
|
|
|
results = runner.select_all_from_query(metric_query)
|
|
|
|
data = {k: [result[k] for result in results] for k in dict(results[0])}
|
|
|
|
return {metric.name(): data}
|
|
|
|
|
|
|
|
row = runner.select_first_from_query(metric_query)
|
|
|
|
return dict(row)
|
|
|
|
except Exception as exc:
|
2023-03-08 18:01:25 +01:00
|
|
|
msg = f"Error trying to compute profile for {runner.table.__tablename__}.{column.name}: {exc}"
|
|
|
|
handle_query_exception(msg, exc, session)
|
2023-06-22 12:51:56 +05:30
|
|
|
return None
|
2022-12-07 14:33:30 +01:00
|
|
|
|
2023-09-13 16:32:55 +02:00
|
|
|
def _compute_window_metrics(
|
2022-12-07 14:33:30 +01:00
|
|
|
self,
|
2023-03-03 21:56:32 +01:00
|
|
|
metrics: List[Metrics],
|
2022-12-07 14:33:30 +01:00
|
|
|
runner: QueryRunner,
|
2023-09-13 16:32:55 +02:00
|
|
|
column,
|
2022-12-07 14:33:30 +01:00
|
|
|
session,
|
|
|
|
*args,
|
|
|
|
**kwargs,
|
|
|
|
):
|
|
|
|
"""Given a list of metrics, compute the given results
|
|
|
|
and returns the values
|
|
|
|
|
|
|
|
Args:
|
|
|
|
column: the column to compute the metrics against
|
|
|
|
metrics: list of metrics to compute
|
|
|
|
Returns:
|
|
|
|
dictionnary of results
|
|
|
|
"""
|
2023-09-13 16:32:55 +02:00
|
|
|
|
2023-03-03 21:56:32 +01:00
|
|
|
if not metrics:
|
|
|
|
return None
|
2022-12-07 14:33:30 +01:00
|
|
|
try:
|
2023-03-03 21:56:32 +01:00
|
|
|
row = runner.select_first_from_sample(
|
2023-04-14 15:59:26 +02:00
|
|
|
*[metric(column).fn() for metric in metrics],
|
2023-03-03 21:56:32 +01:00
|
|
|
)
|
2023-06-22 12:51:56 +05:30
|
|
|
except ProgrammingError as exc:
|
2023-10-04 18:39:39 +05:30
|
|
|
logger.info(
|
|
|
|
f"Skipping metrics for {runner.table.__tablename__}.{column.name} due to {exc}"
|
|
|
|
)
|
2023-06-22 12:51:56 +05:30
|
|
|
except Exception as exc:
|
2023-03-08 18:01:25 +01:00
|
|
|
msg = f"Error trying to compute profile for {runner.table.__tablename__}.{column.name}: {exc}"
|
|
|
|
handle_query_exception(msg, exc, session)
|
2023-03-06 15:09:02 +01:00
|
|
|
if row:
|
|
|
|
return dict(row)
|
|
|
|
return None
|
2022-12-07 14:33:30 +01:00
|
|
|
|
2023-09-13 16:32:55 +02:00
|
|
|
def _compute_system_metrics(
|
2022-12-07 14:33:30 +01:00
|
|
|
self,
|
2023-09-13 16:32:55 +02:00
|
|
|
metrics: Metrics,
|
2022-12-07 14:33:30 +01:00
|
|
|
runner: QueryRunner,
|
|
|
|
session,
|
|
|
|
*args,
|
|
|
|
**kwargs,
|
|
|
|
):
|
|
|
|
"""Get system metric for tables
|
|
|
|
|
|
|
|
Args:
|
|
|
|
metric_type: type of metric
|
|
|
|
metrics: list of metrics to compute
|
|
|
|
session: SQA session object
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
dictionnary of results
|
|
|
|
"""
|
|
|
|
try:
|
2023-09-13 16:32:55 +02:00
|
|
|
rows = metrics().sql(session, conn_config=self.service_connection_config)
|
2022-12-07 14:33:30 +01:00
|
|
|
return rows
|
|
|
|
except Exception as exc:
|
2023-03-08 18:01:25 +01:00
|
|
|
msg = f"Error trying to compute profile for {runner.table.__tablename__}: {exc}"
|
|
|
|
handle_query_exception(msg, exc, session)
|
2023-06-22 12:51:56 +05:30
|
|
|
return None
|
2022-12-07 14:33:30 +01:00
|
|
|
|
2022-10-11 15:57:25 +02:00
|
|
|
def _create_thread_safe_sampler(
|
|
|
|
self,
|
|
|
|
session,
|
|
|
|
table,
|
|
|
|
):
|
|
|
|
"""Create thread safe runner"""
|
|
|
|
if not hasattr(thread_local, "sampler"):
|
2023-07-12 17:02:32 +02:00
|
|
|
thread_local.sampler = self._get_sampler(
|
2022-10-11 15:57:25 +02:00
|
|
|
table=table,
|
2023-07-12 17:02:32 +02:00
|
|
|
session=session,
|
2022-10-11 15:57:25 +02:00
|
|
|
)
|
|
|
|
return thread_local.sampler
|
|
|
|
|
|
|
|
def _create_thread_safe_runner(
|
|
|
|
self,
|
|
|
|
session,
|
|
|
|
table,
|
|
|
|
sample,
|
|
|
|
):
|
|
|
|
"""Create thread safe runner"""
|
|
|
|
if not hasattr(thread_local, "runner"):
|
|
|
|
thread_local.runner = QueryRunner(
|
|
|
|
session=session,
|
|
|
|
table=table,
|
|
|
|
sample=sample,
|
|
|
|
partition_details=self.partition_details,
|
|
|
|
profile_sample_query=self.profile_query,
|
|
|
|
)
|
|
|
|
return thread_local.runner
|
|
|
|
|
|
|
|
def compute_metrics_in_thread(
|
|
|
|
self,
|
2022-12-07 14:33:30 +01:00
|
|
|
metrics,
|
|
|
|
metric_type,
|
|
|
|
column,
|
|
|
|
table,
|
2022-10-11 15:57:25 +02:00
|
|
|
):
|
|
|
|
"""Run metrics in processor worker"""
|
|
|
|
logger.debug(
|
|
|
|
f"Running profiler for {table.__tablename__} on thread {threading.current_thread()}"
|
|
|
|
)
|
|
|
|
Session = self.session_factory # pylint: disable=invalid-name
|
|
|
|
with Session() as session:
|
|
|
|
self.set_session_tag(session)
|
2023-04-05 18:47:18 +02:00
|
|
|
self.set_catalog(session)
|
2022-10-11 15:57:25 +02:00
|
|
|
sampler = self._create_thread_safe_sampler(
|
|
|
|
session,
|
|
|
|
table,
|
|
|
|
)
|
|
|
|
sample = sampler.random_sample()
|
|
|
|
runner = self._create_thread_safe_runner(
|
|
|
|
session,
|
|
|
|
table,
|
|
|
|
sample,
|
|
|
|
)
|
|
|
|
|
2022-12-07 14:33:30 +01:00
|
|
|
try:
|
2023-09-13 16:32:55 +02:00
|
|
|
row = self._get_metric_fn[metric_type.value](
|
2022-12-07 14:33:30 +01:00
|
|
|
metrics,
|
|
|
|
runner=runner,
|
|
|
|
session=session,
|
|
|
|
column=column,
|
|
|
|
sample=sample,
|
|
|
|
)
|
|
|
|
except Exception as exc:
|
2023-03-24 17:59:06 +01:00
|
|
|
error = f"{column if column is not None else runner.table.__tablename__} metric_type.value: {exc}"
|
|
|
|
logger.error(error)
|
2023-09-04 11:02:57 +02:00
|
|
|
self.status.failed_profiler(error, traceback.format_exc())
|
2022-12-07 14:33:30 +01:00
|
|
|
row = None
|
2022-10-11 15:57:25 +02:00
|
|
|
|
|
|
|
if column is not None:
|
|
|
|
column = column.name
|
2023-09-04 11:02:57 +02:00
|
|
|
self.status.scanned(f"{table.__tablename__}.{column}")
|
2023-04-27 18:18:33 +02:00
|
|
|
else:
|
2023-09-04 11:02:57 +02:00
|
|
|
self.status.scanned(table.__tablename__)
|
2022-10-11 15:57:25 +02:00
|
|
|
|
2022-12-07 14:33:30 +01:00
|
|
|
return row, column, metric_type.value
|
2022-10-11 15:57:25 +02:00
|
|
|
|
|
|
|
# pylint: disable=use-dict-literal
|
|
|
|
def get_all_metrics(
|
|
|
|
self,
|
|
|
|
metric_funcs: list,
|
|
|
|
):
|
|
|
|
"""get all profiler metrics"""
|
2023-04-27 18:18:33 +02:00
|
|
|
logger.debug(f"Computing metrics with {self._thread_count} threads.")
|
2022-10-11 15:57:25 +02:00
|
|
|
profile_results = {"table": dict(), "columns": defaultdict(dict)}
|
2022-12-16 17:01:12 +01:00
|
|
|
with CustomThreadPoolExecutor(max_workers=self._thread_count) as pool:
|
2022-10-11 15:57:25 +02:00
|
|
|
futures = [
|
2022-12-16 17:01:12 +01:00
|
|
|
pool.submit(
|
2022-10-11 15:57:25 +02:00
|
|
|
self.compute_metrics_in_thread,
|
2022-12-07 14:33:30 +01:00
|
|
|
*metric_func,
|
2022-10-11 15:57:25 +02:00
|
|
|
)
|
|
|
|
for metric_func in metric_funcs
|
|
|
|
]
|
|
|
|
|
2022-12-16 17:01:12 +01:00
|
|
|
for future in futures:
|
|
|
|
if future.cancelled():
|
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
|
|
|
profile, column, metric_type = future.result(
|
|
|
|
timeout=self.timeout_seconds
|
|
|
|
)
|
|
|
|
if metric_type != MetricTypes.System.value and not isinstance(
|
|
|
|
profile, dict
|
|
|
|
):
|
|
|
|
profile = dict()
|
|
|
|
if metric_type == MetricTypes.Table.value:
|
|
|
|
profile_results["table"].update(profile)
|
|
|
|
elif metric_type == MetricTypes.System.value:
|
|
|
|
profile_results["system"] = profile
|
|
|
|
else:
|
|
|
|
profile_results["columns"][column].update(
|
|
|
|
{
|
|
|
|
"name": column,
|
2023-08-25 08:47:16 +02:00
|
|
|
"timestamp": int(
|
|
|
|
datetime.now(tz=timezone.utc).timestamp() * 1000
|
|
|
|
),
|
2022-12-16 17:01:12 +01:00
|
|
|
**profile,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
except concurrent.futures.TimeoutError as exc:
|
|
|
|
pool.shutdown39(wait=True, cancel_futures=True)
|
|
|
|
logger.debug(traceback.format_exc())
|
|
|
|
logger.error(f"Operation was cancelled due to TimeoutError - {exc}")
|
|
|
|
raise concurrent.futures.TimeoutError
|
|
|
|
|
2022-10-11 15:57:25 +02:00
|
|
|
return profile_results
|
|
|
|
|
|
|
|
def fetch_sample_data(self, table) -> TableData:
|
|
|
|
"""Fetch sample data from database
|
|
|
|
|
|
|
|
Args:
|
|
|
|
table: ORM declarative table
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
TableData: sample table data
|
|
|
|
"""
|
2023-07-12 17:02:32 +02:00
|
|
|
sampler = self._get_sampler(
|
2022-10-11 15:57:25 +02:00
|
|
|
table=table,
|
|
|
|
)
|
2023-03-29 12:06:34 +02:00
|
|
|
|
2023-07-12 17:02:32 +02:00
|
|
|
return sampler.fetch_sample_data()
|
2022-10-11 15:57:25 +02:00
|
|
|
|
|
|
|
def get_composed_metrics(
|
|
|
|
self, column: Column, metric: Metrics, column_results: Dict
|
|
|
|
):
|
|
|
|
"""Given a list of metrics, compute the given results
|
|
|
|
and returns the values
|
|
|
|
|
|
|
|
Args:
|
|
|
|
column: the column to compute the metrics against
|
|
|
|
metrics: list of metrics to compute
|
|
|
|
Returns:
|
|
|
|
dictionnary of results
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return metric(column).fn(column_results)
|
|
|
|
except Exception as exc:
|
|
|
|
logger.debug(traceback.format_exc())
|
|
|
|
logger.warning(f"Unexpected exception computing metrics: {exc}")
|
|
|
|
self.session.rollback()
|
|
|
|
return None
|
2023-03-03 21:56:32 +01:00
|
|
|
|
|
|
|
def get_hybrid_metrics(
|
2023-06-22 12:51:56 +05:30
|
|
|
self, column: Column, metric: Metrics, column_results: Dict, **kwargs
|
2023-03-03 21:56:32 +01:00
|
|
|
):
|
|
|
|
"""Given a list of metrics, compute the given results
|
|
|
|
and returns the values
|
|
|
|
|
|
|
|
Args:
|
|
|
|
column: the column to compute the metrics against
|
|
|
|
metrics: list of metrics to compute
|
|
|
|
Returns:
|
|
|
|
dictionnary of results
|
|
|
|
"""
|
2023-07-12 17:02:32 +02:00
|
|
|
sampler = self._get_sampler(table=kwargs.get("table"))
|
2023-03-03 21:56:32 +01:00
|
|
|
sample = sampler.random_sample()
|
|
|
|
try:
|
|
|
|
return metric(column).fn(sample, column_results, self.session)
|
|
|
|
except Exception as exc:
|
|
|
|
logger.debug(traceback.format_exc())
|
|
|
|
logger.warning(f"Unexpected exception computing metrics: {exc}")
|
|
|
|
self.session.rollback()
|
|
|
|
return None
|
2023-06-16 16:49:55 +02:00
|
|
|
|
2023-10-04 18:39:39 +05:30
|
|
|
def _programming_error_static_metric(self, runner, column, exc, _, __):
|
|
|
|
"""
|
|
|
|
Override Programming Error for Static Metrics
|
|
|
|
"""
|
|
|
|
logger.error(
|
|
|
|
f"Skipping metrics due to {exc} for {runner.table.__tablename__}.{column.name}"
|
|
|
|
)
|
|
|
|
|
2023-06-16 16:49:55 +02:00
|
|
|
def close(self):
|
|
|
|
"""Clean up session"""
|
|
|
|
self.session.close()
|
2023-07-12 17:02:32 +02:00
|
|
|
self.connection.pool.dispose()
|