2021-12-01 12:46:28 +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
|
2021-08-02 15:08:30 +05:30
|
|
|
# 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.
|
2022-06-03 11:43:40 +02:00
|
|
|
import re
|
2021-08-01 14:27:44 -07:00
|
|
|
from datetime import datetime, timedelta
|
2022-06-08 14:33:48 +05:30
|
|
|
from typing import Any, Dict, Iterable, List, Optional
|
2021-08-01 14:27:44 -07:00
|
|
|
|
2021-10-14 16:48:42 +02:00
|
|
|
from metadata.generated.schema.api.services.createDashboardService import (
|
2022-02-01 01:29:56 +01:00
|
|
|
CreateDashboardServiceRequest,
|
2021-10-14 16:48:42 +02:00
|
|
|
)
|
|
|
|
from metadata.generated.schema.api.services.createDatabaseService import (
|
2022-02-01 01:29:56 +01:00
|
|
|
CreateDatabaseServiceRequest,
|
2021-10-14 16:48:42 +02:00
|
|
|
)
|
|
|
|
from metadata.generated.schema.api.services.createMessagingService import (
|
2022-02-01 01:29:56 +01:00
|
|
|
CreateMessagingServiceRequest,
|
2021-10-14 16:48:42 +02:00
|
|
|
)
|
2021-12-03 02:13:13 +01:00
|
|
|
from metadata.generated.schema.api.services.createStorageService import (
|
2022-02-01 01:29:56 +01:00
|
|
|
CreateStorageServiceRequest,
|
2021-12-03 02:13:13 +01:00
|
|
|
)
|
2022-06-08 14:33:48 +05:30
|
|
|
from metadata.generated.schema.entity.data.chart import Chart, ChartType
|
2022-07-19 12:58:58 +02:00
|
|
|
from metadata.generated.schema.entity.data.table import Column, Table
|
2021-08-27 21:33:42 -07:00
|
|
|
from metadata.generated.schema.entity.services.dashboardService import DashboardService
|
2021-08-17 08:45:46 -07:00
|
|
|
from metadata.generated.schema.entity.services.databaseService import DatabaseService
|
2021-08-21 17:52:24 -07:00
|
|
|
from metadata.generated.schema.entity.services.messagingService import MessagingService
|
2021-12-03 02:13:13 +01:00
|
|
|
from metadata.generated.schema.entity.services.storageService import StorageService
|
2022-04-06 03:33:25 +02:00
|
|
|
from metadata.generated.schema.metadataIngestion.workflow import (
|
|
|
|
Source as WorkflowSource,
|
|
|
|
)
|
2022-06-08 14:33:48 +05:30
|
|
|
from metadata.generated.schema.type.entityReference import (
|
|
|
|
EntityReference,
|
|
|
|
EntityReferenceList,
|
|
|
|
)
|
2021-11-03 21:02:34 +01:00
|
|
|
from metadata.ingestion.ometa.ometa_api import OpenMetadata
|
2022-06-08 14:33:48 +05:30
|
|
|
from metadata.utils import fqn
|
2022-04-29 06:54:30 +02:00
|
|
|
from metadata.utils.logger import utils_logger
|
2021-08-01 14:27:44 -07:00
|
|
|
|
2022-04-29 06:54:30 +02:00
|
|
|
logger = utils_logger()
|
2022-02-23 00:20:36 +05:30
|
|
|
|
2022-06-08 14:33:48 +05:30
|
|
|
om_chart_type_dict = {
|
|
|
|
"line": ChartType.Line,
|
|
|
|
"big_number": ChartType.Line,
|
|
|
|
"big_number_total": ChartType.Line,
|
|
|
|
"dual_line": ChartType.Line,
|
|
|
|
"line_multi": ChartType.Line,
|
|
|
|
"table": ChartType.Table,
|
|
|
|
"dist_bar": ChartType.Bar,
|
|
|
|
"bar": ChartType.Bar,
|
|
|
|
"box_plot": ChartType.BoxPlot,
|
|
|
|
"boxplot": ChartType.BoxPlot,
|
|
|
|
"histogram": ChartType.Histogram,
|
|
|
|
"treemap": ChartType.Area,
|
|
|
|
"area": ChartType.Area,
|
|
|
|
"pie": ChartType.Pie,
|
|
|
|
"text": ChartType.Text,
|
|
|
|
"scatter": ChartType.Scatter,
|
|
|
|
}
|
|
|
|
|
2021-08-01 14:27:44 -07:00
|
|
|
|
|
|
|
def get_start_and_end(duration):
|
|
|
|
today = datetime.utcnow()
|
2021-10-14 16:48:42 +02:00
|
|
|
start = (today + timedelta(0 - duration)).replace(
|
|
|
|
hour=0, minute=0, second=0, microsecond=0
|
|
|
|
)
|
2022-06-21 18:02:50 +02:00
|
|
|
# Add one day to make sure we are handling today's queries
|
|
|
|
end = (today + timedelta(days=1)).replace(hour=0, minute=0, second=0, microsecond=0)
|
2021-08-01 14:27:44 -07:00
|
|
|
return start, end
|
|
|
|
|
|
|
|
|
|
|
|
def snake_to_camel(s):
|
2021-10-14 16:48:42 +02:00
|
|
|
a = s.split("_")
|
2021-08-01 14:27:44 -07:00
|
|
|
a[0] = a[0].capitalize()
|
|
|
|
if len(a) > 1:
|
|
|
|
a[1:] = [u.title() for u in a[1:]]
|
2021-10-14 16:48:42 +02:00
|
|
|
return "".join(a)
|
2021-08-01 14:27:44 -07:00
|
|
|
|
|
|
|
|
2021-11-21 02:16:18 +05:30
|
|
|
def get_database_service_or_create(
|
2022-04-06 03:33:25 +02:00
|
|
|
config: WorkflowSource, metadata_config, service_name=None
|
2021-11-21 02:16:18 +05:30
|
|
|
) -> DatabaseService:
|
2021-11-03 21:02:34 +01:00
|
|
|
metadata = OpenMetadata(metadata_config)
|
2022-04-04 12:46:09 -07:00
|
|
|
if not service_name:
|
|
|
|
service_name = config.serviceName
|
2022-04-05 14:32:45 +02:00
|
|
|
service: DatabaseService = metadata.get_by_name(
|
2022-05-26 21:00:18 +02:00
|
|
|
entity=DatabaseService, fqn=service_name
|
2022-04-05 14:32:45 +02:00
|
|
|
)
|
2022-04-06 03:33:25 +02:00
|
|
|
if not service:
|
|
|
|
config_dict = config.dict()
|
|
|
|
service_connection_config = config_dict.get("serviceConnection").get("config")
|
2022-01-25 01:59:54 +05:30
|
|
|
password = (
|
2022-04-06 03:33:25 +02:00
|
|
|
service_connection_config.get("password").get_secret_value()
|
|
|
|
if service_connection_config and service_connection_config.get("password")
|
2022-01-25 14:30:09 +05:30
|
|
|
else None
|
2022-01-25 01:59:54 +05:30
|
|
|
)
|
2022-04-05 14:32:45 +02:00
|
|
|
|
|
|
|
# Use a JSON to dynamically parse the pydantic model
|
|
|
|
# based on the serviceType
|
2022-04-06 03:33:25 +02:00
|
|
|
# TODO revisit me
|
2022-04-05 14:32:45 +02:00
|
|
|
service_json = {
|
2022-04-04 12:46:09 -07:00
|
|
|
"connection": {
|
|
|
|
"config": {
|
2022-04-06 18:26:54 +05:30
|
|
|
"hostPort": service_connection_config.get("hostPort")
|
|
|
|
if service_connection_config
|
|
|
|
else None,
|
|
|
|
"username": service_connection_config.get("username")
|
|
|
|
if service_connection_config
|
|
|
|
else None,
|
2022-04-04 12:46:09 -07:00
|
|
|
"password": password,
|
2022-04-06 18:26:54 +05:30
|
|
|
"database": service_connection_config.get("database")
|
|
|
|
if service_connection_config
|
|
|
|
else None,
|
2022-04-06 03:33:25 +02:00
|
|
|
"connectionOptions": service_connection_config.get(
|
|
|
|
"connectionOptions"
|
2022-04-06 18:26:54 +05:30
|
|
|
)
|
|
|
|
if service_connection_config
|
|
|
|
else None,
|
2022-04-06 03:33:25 +02:00
|
|
|
"connectionArguments": service_connection_config.get(
|
|
|
|
"connectionArguments"
|
2022-04-06 18:26:54 +05:30
|
|
|
)
|
|
|
|
if service_connection_config
|
|
|
|
else None,
|
2022-04-04 12:46:09 -07:00
|
|
|
}
|
2021-10-14 16:48:42 +02:00
|
|
|
},
|
2022-04-04 12:46:09 -07:00
|
|
|
"name": service_name,
|
2021-10-14 16:48:42 +02:00
|
|
|
"description": "",
|
2022-04-06 18:26:54 +05:30
|
|
|
"serviceType": service_connection_config.get("type").value
|
|
|
|
if service_connection_config
|
|
|
|
else None,
|
2021-10-14 16:48:42 +02:00
|
|
|
}
|
2022-04-06 03:33:25 +02:00
|
|
|
|
2022-04-05 14:32:45 +02:00
|
|
|
created_service: DatabaseService = metadata.create_or_update(
|
|
|
|
CreateDatabaseServiceRequest(**service_json)
|
2021-10-04 11:06:35 -07:00
|
|
|
)
|
2022-04-04 12:46:09 -07:00
|
|
|
logger.info(f"Creating DatabaseService instance for {service_name}")
|
2021-08-01 14:27:44 -07:00
|
|
|
return created_service
|
2022-04-06 03:33:25 +02:00
|
|
|
return service
|
2021-08-21 17:52:24 -07:00
|
|
|
|
|
|
|
|
2021-10-04 11:06:35 -07:00
|
|
|
def get_messaging_service_or_create(
|
2021-10-14 16:48:42 +02:00
|
|
|
service_name: str,
|
|
|
|
message_service_type: str,
|
2022-04-04 12:46:09 -07:00
|
|
|
config: dict,
|
2021-10-14 16:48:42 +02:00
|
|
|
metadata_config,
|
2021-10-04 11:06:35 -07:00
|
|
|
) -> MessagingService:
|
2021-11-03 21:02:34 +01:00
|
|
|
metadata = OpenMetadata(metadata_config)
|
2022-04-05 14:32:45 +02:00
|
|
|
service: MessagingService = metadata.get_by_name(
|
2022-05-26 21:00:18 +02:00
|
|
|
entity=MessagingService, fqn=service_name
|
2022-04-05 14:32:45 +02:00
|
|
|
)
|
2021-08-21 17:52:24 -07:00
|
|
|
if service is not None:
|
|
|
|
return service
|
|
|
|
else:
|
2021-11-03 21:02:34 +01:00
|
|
|
created_service = metadata.create_or_update(
|
2022-02-01 01:29:56 +01:00
|
|
|
CreateMessagingServiceRequest(
|
2022-04-04 12:46:09 -07:00
|
|
|
name=service_name, serviceType=message_service_type, connection=config
|
2021-11-03 21:02:34 +01:00
|
|
|
)
|
2021-08-21 17:52:24 -07:00
|
|
|
)
|
|
|
|
return created_service
|
2021-08-27 21:33:42 -07:00
|
|
|
|
|
|
|
|
2021-10-04 11:06:35 -07:00
|
|
|
def get_dashboard_service_or_create(
|
2021-10-14 16:48:42 +02:00
|
|
|
service_name: str,
|
|
|
|
dashboard_service_type: str,
|
2022-04-04 12:46:09 -07:00
|
|
|
config: dict,
|
2021-10-14 16:48:42 +02:00
|
|
|
metadata_config,
|
2021-10-04 11:06:35 -07:00
|
|
|
) -> DashboardService:
|
2021-11-03 21:02:34 +01:00
|
|
|
metadata = OpenMetadata(metadata_config)
|
2022-04-05 14:32:45 +02:00
|
|
|
service: DashboardService = metadata.get_by_name(
|
2022-05-26 21:00:18 +02:00
|
|
|
entity=DashboardService, fqn=service_name
|
2022-04-05 14:32:45 +02:00
|
|
|
)
|
2021-08-27 21:33:42 -07:00
|
|
|
if service is not None:
|
|
|
|
return service
|
|
|
|
else:
|
2022-04-09 04:26:12 -07:00
|
|
|
dashboard_config = {"config": config}
|
2021-11-03 21:02:34 +01:00
|
|
|
created_service = metadata.create_or_update(
|
2022-02-01 01:29:56 +01:00
|
|
|
CreateDashboardServiceRequest(
|
2022-04-09 04:26:12 -07:00
|
|
|
name=service_name,
|
|
|
|
serviceType=dashboard_service_type,
|
|
|
|
connection=dashboard_config,
|
2021-11-03 21:02:34 +01:00
|
|
|
)
|
2021-08-27 21:33:42 -07:00
|
|
|
)
|
|
|
|
return created_service
|
2021-10-02 17:12:18 -07:00
|
|
|
|
2021-10-14 16:48:42 +02:00
|
|
|
|
2021-12-03 02:13:13 +01:00
|
|
|
def get_storage_service_or_create(service_json, metadata_config) -> StorageService:
|
|
|
|
metadata = OpenMetadata(metadata_config)
|
2022-04-05 14:32:45 +02:00
|
|
|
service: StorageService = metadata.get_by_name(
|
2022-05-26 21:00:18 +02:00
|
|
|
entity=StorageService, fqn=service_json["name"]
|
2022-04-05 14:32:45 +02:00
|
|
|
)
|
2021-12-03 02:13:13 +01:00
|
|
|
if service is not None:
|
|
|
|
return service
|
|
|
|
else:
|
|
|
|
created_service = metadata.create_or_update(
|
2022-02-01 01:29:56 +01:00
|
|
|
CreateStorageServiceRequest(**service_json)
|
2021-12-03 02:13:13 +01:00
|
|
|
)
|
|
|
|
return created_service
|
|
|
|
|
|
|
|
|
2022-07-27 07:47:25 +02:00
|
|
|
def datetime_to_ts(date: Optional[datetime]) -> Optional[int]:
|
2022-02-13 17:51:25 +01:00
|
|
|
"""
|
2022-04-18 15:13:26 +02:00
|
|
|
Convert a given date to a timestamp as an Int in milliseconds
|
2022-02-13 17:51:25 +01:00
|
|
|
"""
|
2022-07-27 07:47:25 +02:00
|
|
|
return int(date.timestamp() * 1_000) if date else None
|
2022-02-24 01:01:03 +05:30
|
|
|
|
|
|
|
|
2022-06-03 13:42:28 +05:30
|
|
|
def get_formatted_entity_name(name: str) -> Optional[str]:
|
2022-06-21 18:02:50 +02:00
|
|
|
return (
|
|
|
|
name.replace("[", "").replace("]", "").replace("<default>.", "")
|
|
|
|
if name
|
|
|
|
else None
|
|
|
|
)
|
2022-03-22 18:29:03 +05:30
|
|
|
|
|
|
|
|
2022-02-24 01:01:03 +05:30
|
|
|
def get_raw_extract_iter(alchemy_helper) -> Iterable[Dict[str, Any]]:
|
|
|
|
"""
|
|
|
|
Provides iterator of result row from SQLAlchemy helper
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
rows = alchemy_helper.execute_query()
|
|
|
|
for row in rows:
|
|
|
|
yield row
|
2022-06-03 11:43:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
def replace_special_with(raw: str, replacement: str) -> str:
|
|
|
|
"""
|
|
|
|
Replace special characters in a string by a hyphen
|
|
|
|
:param raw: raw string to clean
|
|
|
|
:param replacement: string used to replace
|
|
|
|
:return: clean string
|
|
|
|
"""
|
|
|
|
return re.sub(r"[^a-zA-Z0-9]", replacement, raw)
|
2022-06-08 14:33:48 +05:30
|
|
|
|
|
|
|
|
|
|
|
def get_standard_chart_type(raw_chart_type: str) -> str:
|
|
|
|
"""
|
|
|
|
Get standard chart type supported by OpenMetadata based on raw chart type input
|
|
|
|
:param raw_chart_type: raw chart type to be standardize
|
|
|
|
:return: standard chart type
|
|
|
|
"""
|
|
|
|
return om_chart_type_dict.get(raw_chart_type.lower(), ChartType.Other)
|
|
|
|
|
|
|
|
|
|
|
|
def get_chart_entities_from_id(
|
|
|
|
chart_ids: List[str], metadata: OpenMetadata, service_name: str
|
|
|
|
) -> List[EntityReferenceList]:
|
|
|
|
entities = []
|
|
|
|
for id in chart_ids:
|
|
|
|
chart: Chart = metadata.get_by_name(
|
|
|
|
entity=Chart,
|
|
|
|
fqn=fqn.build(
|
|
|
|
metadata, Chart, chart_name=str(id), service_name=service_name
|
|
|
|
),
|
|
|
|
)
|
|
|
|
if chart:
|
|
|
|
entity = EntityReference(id=chart.id, type="chart")
|
|
|
|
entities.append(entity)
|
|
|
|
return entities
|
2022-06-21 18:02:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
def find_in_list(element: Any, container: Iterable[Any]) -> Optional[Any]:
|
|
|
|
"""
|
|
|
|
If the element is in the container, return it.
|
|
|
|
Otherwise, return None
|
|
|
|
:param element: to find
|
|
|
|
:param container: container with element
|
|
|
|
:return: element or None
|
|
|
|
"""
|
|
|
|
return next(iter([elem for elem in container if elem == element]), None)
|
2022-07-19 12:58:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
def find_column_in_table(column_name: str, table: Table) -> Optional[Column]:
|
|
|
|
"""
|
|
|
|
If the column exists in the table, return it
|
|
|
|
"""
|
|
|
|
return next(
|
|
|
|
(col for col in table.columns if col.name.__root__ == column_name), None
|
|
|
|
)
|