2022-11-15 20:31:10 +05:30
|
|
|
# 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.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
Interfaces with database for all database engine
|
|
|
|
|
supporting sqlalchemy abstraction layer
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import traceback
|
|
|
|
|
from collections import defaultdict
|
|
|
|
|
from datetime import datetime, timezone
|
2022-12-14 21:14:51 +05:30
|
|
|
from typing import Dict, List
|
2022-11-15 20:31:10 +05:30
|
|
|
|
|
|
|
|
from sqlalchemy import Column
|
|
|
|
|
|
2022-12-14 21:14:51 +05:30
|
|
|
from metadata.generated.schema.entity.data.table import TableData
|
2022-11-15 20:31:10 +05:30
|
|
|
from metadata.ingestion.api.processor import ProfilerProcessorStatus
|
2023-01-02 13:52:27 +01:00
|
|
|
from metadata.ingestion.source.connections import get_connection
|
2022-12-27 15:00:22 +01:00
|
|
|
from metadata.ingestion.source.database.datalake.metadata import ometa_to_dataframe
|
2022-11-15 20:31:10 +05:30
|
|
|
from metadata.interfaces.profiler_protocol import (
|
|
|
|
|
ProfilerInterfaceArgs,
|
|
|
|
|
ProfilerProtocol,
|
|
|
|
|
)
|
2022-12-07 14:33:30 +01:00
|
|
|
from metadata.orm_profiler.metrics.core import MetricTypes
|
2022-11-15 20:31:10 +05:30
|
|
|
from metadata.orm_profiler.metrics.registry import Metrics
|
|
|
|
|
from metadata.orm_profiler.profiler.datalake_sampler import DatalakeSampler
|
2022-12-14 21:14:51 +05:30
|
|
|
from metadata.utils.column_base_model import ColumnBaseModel
|
2022-12-07 14:33:30 +01:00
|
|
|
from metadata.utils.dispatch import valuedispatch
|
2022-11-15 20:31:10 +05:30
|
|
|
from metadata.utils.logger import profiler_interface_registry_logger
|
|
|
|
|
|
|
|
|
|
logger = profiler_interface_registry_logger()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DataLakeProfilerInterface(ProfilerProtocol):
|
|
|
|
|
"""
|
|
|
|
|
Interface to interact with registry supporting
|
|
|
|
|
sqlalchemy.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, profiler_interface_args: ProfilerInterfaceArgs):
|
|
|
|
|
"""Instantiate SQA Interface object"""
|
|
|
|
|
self._thread_count = profiler_interface_args.thread_count
|
|
|
|
|
self.table_entity = profiler_interface_args.table_entity
|
|
|
|
|
self.ometa_client = profiler_interface_args.ometa_client
|
|
|
|
|
self.service_connection_config = (
|
|
|
|
|
profiler_interface_args.service_connection_config
|
|
|
|
|
)
|
|
|
|
|
self.client = get_connection(self.service_connection_config).client
|
|
|
|
|
self.processor_status = ProfilerProcessorStatus()
|
|
|
|
|
self.processor_status.entity = (
|
|
|
|
|
self.table_entity.fullyQualifiedName.__root__
|
|
|
|
|
if self.table_entity.fullyQualifiedName
|
|
|
|
|
else None
|
|
|
|
|
)
|
|
|
|
|
|
2022-12-20 14:55:11 +05:30
|
|
|
self.profile_sample_config = profiler_interface_args.profile_sample_config
|
2022-11-15 20:31:10 +05:30
|
|
|
self.profile_query = profiler_interface_args.table_sample_query
|
|
|
|
|
self.partition_details = None
|
|
|
|
|
self._table = profiler_interface_args.table_entity
|
2022-12-14 21:14:51 +05:30
|
|
|
self.data_frame_list = ometa_to_dataframe(
|
|
|
|
|
config_source=self.service_connection_config.configSource,
|
|
|
|
|
client=self.client,
|
|
|
|
|
table=self.table,
|
2022-11-15 20:31:10 +05:30
|
|
|
)
|
|
|
|
|
|
2022-12-07 14:33:30 +01:00
|
|
|
@valuedispatch
|
|
|
|
|
def _get_metrics(self, *args, **kwargs):
|
|
|
|
|
"""Generic getter method for metrics. To be used with
|
|
|
|
|
specific dispatch methods
|
|
|
|
|
"""
|
|
|
|
|
logger.warning("Could not get metric. No function registered.")
|
|
|
|
|
|
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
|
@_get_metrics.register(MetricTypes.Table.value)
|
|
|
|
|
def _(
|
|
|
|
|
self,
|
|
|
|
|
metric_type: str,
|
|
|
|
|
metrics: List[Metrics],
|
|
|
|
|
data_frame_list,
|
|
|
|
|
*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
|
|
|
|
|
"""
|
|
|
|
|
import pandas as pd # pylint: disable=import-outside-toplevel
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
row = []
|
|
|
|
|
for metric in metrics:
|
|
|
|
|
for data_frame in data_frame_list:
|
|
|
|
|
row.append(
|
|
|
|
|
metric().dl_fn(
|
|
|
|
|
data_frame.astype(object).where(
|
|
|
|
|
pd.notnull(data_frame), None
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
if row:
|
|
|
|
|
if isinstance(row, list):
|
|
|
|
|
row_dict = {}
|
|
|
|
|
for index, table_metric in enumerate(metrics):
|
|
|
|
|
row_dict[table_metric.name()] = row[index]
|
|
|
|
|
return row_dict
|
|
|
|
|
return dict(row)
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
logger.debug(traceback.format_exc())
|
|
|
|
|
logger.warning(f"Error trying to compute profile for {exc}")
|
|
|
|
|
raise RuntimeError(exc)
|
|
|
|
|
|
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
|
@_get_metrics.register(MetricTypes.Static.value)
|
|
|
|
|
def _(
|
|
|
|
|
self,
|
|
|
|
|
metric_type: str,
|
|
|
|
|
metrics: List[Metrics],
|
|
|
|
|
column,
|
|
|
|
|
data_frame_list,
|
|
|
|
|
*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
|
|
|
|
|
"""
|
|
|
|
|
import pandas as pd # pylint: disable=import-outside-toplevel
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
row = []
|
|
|
|
|
for metric in metrics:
|
|
|
|
|
for data_frame in data_frame_list:
|
|
|
|
|
row.append(
|
|
|
|
|
metric(column).dl_fn(
|
|
|
|
|
data_frame.astype(object).where(
|
|
|
|
|
pd.notnull(data_frame), None
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
row_dict = {}
|
|
|
|
|
for index, column_metric in enumerate(metrics):
|
|
|
|
|
row_dict[column_metric.name()] = row[index]
|
|
|
|
|
return row_dict
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
logger.debug(
|
|
|
|
|
f"{traceback.format_exc()}\nError trying to compute profile for {exc}"
|
|
|
|
|
)
|
|
|
|
|
raise RuntimeError(exc)
|
|
|
|
|
|
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
|
@_get_metrics.register(MetricTypes.Query.value)
|
|
|
|
|
def _(
|
|
|
|
|
self,
|
|
|
|
|
metric_type: str,
|
|
|
|
|
metrics: Metrics,
|
|
|
|
|
column,
|
|
|
|
|
data_frame_list,
|
|
|
|
|
*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
|
|
|
|
|
"""
|
|
|
|
|
col_metric = None
|
|
|
|
|
for data_frame in data_frame_list:
|
|
|
|
|
col_metric = metrics(column).dl_query(data_frame)
|
|
|
|
|
if not col_metric:
|
|
|
|
|
return None
|
|
|
|
|
return {metrics.name(): col_metric}
|
|
|
|
|
|
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
|
@_get_metrics.register(MetricTypes.Window.value)
|
|
|
|
|
def _(
|
|
|
|
|
self,
|
|
|
|
|
*args,
|
|
|
|
|
**kwargs,
|
|
|
|
|
):
|
|
|
|
|
"""
|
|
|
|
|
Given a list of metrics, compute the given results
|
|
|
|
|
and returns the values
|
|
|
|
|
"""
|
|
|
|
|
return None # to be implemented
|
|
|
|
|
|
|
|
|
|
@_get_metrics.register(MetricTypes.System.value)
|
|
|
|
|
def _(
|
|
|
|
|
self,
|
|
|
|
|
*args,
|
|
|
|
|
**kwargs,
|
|
|
|
|
):
|
|
|
|
|
"""
|
|
|
|
|
Given a list of metrics, compute the given results
|
|
|
|
|
and returns the values
|
|
|
|
|
"""
|
|
|
|
|
return None # to be implemented
|
|
|
|
|
|
2022-11-15 20:31:10 +05:30
|
|
|
def compute_metrics(
|
|
|
|
|
self,
|
2022-12-07 14:33:30 +01:00
|
|
|
metrics,
|
|
|
|
|
metric_type,
|
|
|
|
|
column,
|
|
|
|
|
table,
|
2022-11-15 20:31:10 +05:30
|
|
|
):
|
|
|
|
|
"""Run metrics in processor worker"""
|
|
|
|
|
logger.debug(f"Running profiler for {table}")
|
|
|
|
|
try:
|
2022-12-07 14:33:30 +01:00
|
|
|
row = self._get_metrics(
|
|
|
|
|
metric_type.value,
|
2022-11-15 20:31:10 +05:30
|
|
|
metrics,
|
|
|
|
|
session=self.client,
|
|
|
|
|
data_frame_list=self.data_frame_list,
|
|
|
|
|
column=column,
|
|
|
|
|
)
|
2022-12-07 14:33:30 +01:00
|
|
|
except Exception as exc:
|
|
|
|
|
logger.error(exc)
|
|
|
|
|
self.processor_status.failure(
|
|
|
|
|
f"{column if column is not None else table}",
|
|
|
|
|
"metric_type.value",
|
|
|
|
|
f"{exc}",
|
|
|
|
|
)
|
2022-11-15 20:31:10 +05:30
|
|
|
row = None
|
|
|
|
|
if column:
|
|
|
|
|
column = column.name
|
2022-12-07 14:33:30 +01:00
|
|
|
return row, column, metric_type.value
|
2022-11-15 20:31:10 +05:30
|
|
|
|
|
|
|
|
def fetch_sample_data(self, table) -> TableData:
|
|
|
|
|
"""Fetch sample data from database
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
table: ORM declarative table
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
TableData: sample table data
|
|
|
|
|
"""
|
|
|
|
|
sampler = DatalakeSampler(
|
|
|
|
|
session=self.client,
|
|
|
|
|
table=self.data_frame_list,
|
2023-01-16 22:17:46 +05:30
|
|
|
profile_sample_config=self.profile_sample_config,
|
2022-11-15 20:31:10 +05:30
|
|
|
partition_details=self.partition_details,
|
|
|
|
|
profile_sample_query=self.profile_query,
|
|
|
|
|
)
|
|
|
|
|
return sampler.fetch_dl_sample_data()
|
|
|
|
|
|
|
|
|
|
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
|
2022-11-17 10:11:54 +01:00
|
|
|
metric: list of metrics to compute
|
|
|
|
|
column_results: computed values for the column
|
2022-11-15 20:31:10 +05:30
|
|
|
Returns:
|
|
|
|
|
dictionary 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}")
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
def get_all_metrics(
|
|
|
|
|
self,
|
|
|
|
|
metric_funcs: list,
|
|
|
|
|
):
|
|
|
|
|
"""get all profiler metrics"""
|
|
|
|
|
|
|
|
|
|
profile_results = {"table": {}, "columns": defaultdict(dict)}
|
|
|
|
|
metric_list = [
|
2022-12-07 14:33:30 +01:00
|
|
|
self.compute_metrics(*metric_func) for metric_func in metric_funcs
|
2022-11-15 20:31:10 +05:30
|
|
|
]
|
|
|
|
|
for metric_result in metric_list:
|
2022-12-07 14:33:30 +01:00
|
|
|
profile, column, metric_type = metric_result
|
2022-11-15 20:31:10 +05:30
|
|
|
|
2022-12-07 14:33:30 +01:00
|
|
|
if metric_type == MetricTypes.Table.value:
|
2022-11-15 20:31:10 +05:30
|
|
|
profile_results["table"].update(profile)
|
2022-12-07 14:33:30 +01:00
|
|
|
if metric_type == MetricTypes.System.value:
|
|
|
|
|
profile_results["system"] = profile
|
2022-11-15 20:31:10 +05:30
|
|
|
else:
|
|
|
|
|
if profile:
|
|
|
|
|
profile_results["columns"][column].update(
|
|
|
|
|
{
|
|
|
|
|
"name": column,
|
|
|
|
|
"timestamp": datetime.now(tz=timezone.utc).timestamp(),
|
|
|
|
|
**profile,
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
return profile_results
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def table(self):
|
|
|
|
|
"""OM Table entity"""
|
|
|
|
|
return self._table
|
|
|
|
|
|
|
|
|
|
def get_columns(self):
|
2022-12-14 21:14:51 +05:30
|
|
|
return ColumnBaseModel.col_base_model_list(self.data_frame_list)
|
2022-11-15 20:31:10 +05:30
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
|
pass
|