From 8e170562564eb8c3fe3d08e2ee81e61fd453b7c6 Mon Sep 17 00:00:00 2001 From: parthp2107 Date: Thu, 9 Sep 2021 11:23:43 +0530 Subject: [PATCH 1/4] added redash connector --- ingestion/examples/workflows/redash.json | 29 ++++ ingestion/setup.py | 1 + .../src/metadata/ingestion/source/redash.py | 135 ++++++++++++++++++ 3 files changed, 165 insertions(+) create mode 100644 ingestion/examples/workflows/redash.json create mode 100644 ingestion/src/metadata/ingestion/source/redash.py diff --git a/ingestion/examples/workflows/redash.json b/ingestion/examples/workflows/redash.json new file mode 100644 index 000000000000..861c4ef04a8c --- /dev/null +++ b/ingestion/examples/workflows/redash.json @@ -0,0 +1,29 @@ +{ + "source": { + "type": "redash", + "config": { + "api_key": "api_key", + "uri": "http://localhost:5000", + "service_name": "redash", + "service_type": "Redash" + } + }, + "sink": { + "type": "metadata-rest", + "config": {} + }, + "metadata_server": { + "type": "metadata-server", + "config": { + "api_endpoint": "http://localhost:8585/api", + "auth_provider_type": "no-auth" + } + }, + "cron": { + "minute": "*/5", + "hour": null, + "day": null, + "month": null, + "day_of_week": null + } +} diff --git a/ingestion/setup.py b/ingestion/setup.py index a30f0e3b6670..5c036859c35d 100644 --- a/ingestion/setup.py +++ b/ingestion/setup.py @@ -88,6 +88,7 @@ def get_long_description(): "oracle": {"cx_Oracle"}, "presto": {"pyhive~=0.6.3"}, "postgres": {"pymysql>=1.0.2", "psycopg2-binary", "GeoAlchemy2"}, + "redash": {"redash-toolbelt"}, "redshift": {"sqlalchemy-redshift", "psycopg2-binary", "GeoAlchemy2"}, "redshift-usage": {"sqlalchemy-redshift", "psycopg2-binary", "GeoAlchemy2"}, "scheduler": scheduler_requirements, diff --git a/ingestion/src/metadata/ingestion/source/redash.py b/ingestion/src/metadata/ingestion/source/redash.py new file mode 100644 index 000000000000..f71506ba0641 --- /dev/null +++ b/ingestion/src/metadata/ingestion/source/redash.py @@ -0,0 +1,135 @@ +# 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. +import sys +import uuid +from dataclasses import dataclass +from dataclasses import field +from typing import List, Iterable, Optional +from redash_toolbelt import Redash + +from metadata.ingestion.api.common import IncludeFilterPattern +from metadata.ingestion.api.source import SourceStatus +from metadata.ingestion.api.common import ConfigModel, Record, WorkflowContext +from metadata.ingestion.api.source import Source +from metadata.ingestion.ometa.openmetadata_rest import MetadataServerConfig +from metadata.generated.schema.entity.data.chart import Chart + +from metadata.generated.schema.type.entityReference import EntityReference +from metadata.ingestion.models.table_metadata import Dashboard + + +class RedashSourceConfig(ConfigModel): + uri: str = "http://localhost:5000" + api_key: str + env: str = "DEV" + dashboard_patterns: IncludeFilterPattern = IncludeFilterPattern.allow_all() + chart_patterns: IncludeFilterPattern = IncludeFilterPattern.allow_all() + skip_draft: bool = True + api_page_limit: int = sys.maxsize + service_name: str + service_type: str + + +@dataclass +class RedashSourceStatus(SourceStatus): + items_scanned: int = 0 + filtered: List[str] = field(default_factory=list) + + def item_scanned_status(self) -> None: + self.items_scanned += 1 + + def item_dropped_status(self, item: str) -> None: + self.filtered.append(item) + + +class RedashSource(Source): + config: RedashSourceConfig + metadata_config: MetadataServerConfig + status: RedashSourceStatus + platform = "redash" + + def __init__(self, config: RedashSourceConfig, metadata_config: MetadataServerConfig, ctx: WorkflowContext): + super().__init__(ctx) + self.config = config + self.metadata_config = metadata_config + self.status = RedashSourceStatus() + + self.config.uri = self.config.uri.strip("/") + + self.client = Redash(self.config.uri, self.config.api_key) + print(self.client.dashboards()) + self.client.session.headers.update( + { + "Content-Type": "application/json", + "Accept": "application/json", + } + ) + + @classmethod + def create(cls, config_dict: dict, metadata_config_dict: dict, ctx: WorkflowContext): + config = RedashSourceConfig.parse_obj(config_dict) + metadata_config = MetadataServerConfig.parse_obj(metadata_config_dict) + return cls(config, metadata_config, ctx) + + def prepare(self): + pass + + def next_record(self) -> Iterable[Record]: + yield self._get_redash_charts() + yield self.get_redash_dashboard() + + def _get_redash_charts(self) -> Optional[Chart]: + query_response = self.client.queries() + for query_response in query_response["results"]: + query_id = query_response["id"] + query_name = query_response["name"] + query_data = self.client._get(f"/api/queries/{query_id}").json() + for visualization in query_data.get("Visualizations", []): + chart_type = visualization.get("type", "") + chart_description = visualization.get("description", "") if visualization.get("description", "") else "" + yield Chart( + id=uuid.uuid4(), + name=query_id, + displayName=query_name, + chartType=chart_type, + service=EntityReference(id=self.service.id, type="dashboardService"), + description=chart_description, + ) + + def get_redash_dashboard(self) -> Dashboard: + charts: List[Chart] = [] + dashboard_response = self.client.dashboards() + for dashboard_response in dashboard_response["results"]: + if dashboard_response["id"] is not None: + if not self.config.chart_pattern.allowed(dashboard_response["id"]): + self.status.item_dropped_status(dashboard_response["id"]) + continue + self.status.item_scanned_status() + dashboard_data = self.client.dashboard(dashboard_response["slug"]) + widgets = dashboard_data.get("widgets", []) + dashboard_description = widgets.get("text") + yield Dashboard( + id=uuid.uuid4(), + name=dashboard_response["id"], + displayName=dashboard_response["name"], + description=dashboard_description if dashboard_response else "", + charts=charts, + usageSummary=None, + service=EntityReference(id=self.service.id, type="dashboardService"), + href=dashboard_response["slug"] + ) + + def get_status(self) -> SourceStatus: + return self.status From 016a12e8f6895f57be7e9d33f8b4b39b21a2203a Mon Sep 17 00:00:00 2001 From: parthp2107 Date: Thu, 9 Sep 2021 12:16:56 +0530 Subject: [PATCH 2/4] added redash connector --- ingestion/src/metadata/ingestion/source/redash.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ingestion/src/metadata/ingestion/source/redash.py b/ingestion/src/metadata/ingestion/source/redash.py index f71506ba0641..bff6d2d84fa7 100644 --- a/ingestion/src/metadata/ingestion/source/redash.py +++ b/ingestion/src/metadata/ingestion/source/redash.py @@ -69,7 +69,6 @@ def __init__(self, config: RedashSourceConfig, metadata_config: MetadataServerCo self.config.uri = self.config.uri.strip("/") self.client = Redash(self.config.uri, self.config.api_key) - print(self.client.dashboards()) self.client.session.headers.update( { "Content-Type": "application/json", @@ -87,10 +86,10 @@ def prepare(self): pass def next_record(self) -> Iterable[Record]: - yield self._get_redash_charts() + yield self.get_redash_charts() yield self.get_redash_dashboard() - def _get_redash_charts(self) -> Optional[Chart]: + def get_redash_charts(self) -> Optional[Chart]: query_response = self.client.queries() for query_response in query_response["results"]: query_id = query_response["id"] From 295bf1a078cf5c7bccc229c00599ecf0df485423 Mon Sep 17 00:00:00 2001 From: parthp2107 Date: Mon, 13 Sep 2021 13:54:20 +0530 Subject: [PATCH 3/4] Added Redash Connector --- ingestion/examples/workflows/redash.json | 5 +- .../src/metadata/ingestion/source/redash.py | 96 +++++++++---------- 2 files changed, 46 insertions(+), 55 deletions(-) diff --git a/ingestion/examples/workflows/redash.json b/ingestion/examples/workflows/redash.json index 861c4ef04a8c..88e64681f630 100644 --- a/ingestion/examples/workflows/redash.json +++ b/ingestion/examples/workflows/redash.json @@ -4,8 +4,7 @@ "config": { "api_key": "api_key", "uri": "http://localhost:5000", - "service_name": "redash", - "service_type": "Redash" + "service_name": "redash" } }, "sink": { @@ -26,4 +25,4 @@ "month": null, "day_of_week": null } -} +} \ No newline at end of file diff --git a/ingestion/src/metadata/ingestion/source/redash.py b/ingestion/src/metadata/ingestion/source/redash.py index bff6d2d84fa7..e7eed6408e29 100644 --- a/ingestion/src/metadata/ingestion/source/redash.py +++ b/ingestion/src/metadata/ingestion/source/redash.py @@ -12,34 +12,30 @@ # 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. -import sys import uuid from dataclasses import dataclass from dataclasses import field -from typing import List, Iterable, Optional -from redash_toolbelt import Redash +from typing import List, Iterable -from metadata.ingestion.api.common import IncludeFilterPattern -from metadata.ingestion.api.source import SourceStatus -from metadata.ingestion.api.common import ConfigModel, Record, WorkflowContext -from metadata.ingestion.api.source import Source -from metadata.ingestion.ometa.openmetadata_rest import MetadataServerConfig +import requests from metadata.generated.schema.entity.data.chart import Chart - from metadata.generated.schema.type.entityReference import EntityReference +from metadata.ingestion.api.common import ConfigModel, Record, WorkflowContext +from metadata.ingestion.api.source import Source +from metadata.ingestion.api.source import SourceStatus from metadata.ingestion.models.table_metadata import Dashboard +from metadata.ingestion.ometa.openmetadata_rest import MetadataServerConfig +from redash_toolbelt import Redash +from metadata.utils.helpers import get_dashboard_service_or_create +from metadata.generated.schema.entity.services.dashboardService import DashboardServiceType class RedashSourceConfig(ConfigModel): uri: str = "http://localhost:5000" + username: str = "" api_key: str - env: str = "DEV" - dashboard_patterns: IncludeFilterPattern = IncludeFilterPattern.allow_all() - chart_patterns: IncludeFilterPattern = IncludeFilterPattern.allow_all() - skip_draft: bool = True - api_page_limit: int = sys.maxsize service_name: str - service_type: str + service_type: str = "Redash" @dataclass @@ -65,16 +61,13 @@ def __init__(self, config: RedashSourceConfig, metadata_config: MetadataServerCo self.config = config self.metadata_config = metadata_config self.status = RedashSourceStatus() - - self.config.uri = self.config.uri.strip("/") - self.client = Redash(self.config.uri, self.config.api_key) - self.client.session.headers.update( - { - "Content-Type": "application/json", - "Accept": "application/json", - } - ) + self.service = get_dashboard_service_or_create(config.service_name, + DashboardServiceType.Redash.name, + config.username, + config.api_key, + config.uri, + metadata_config) @classmethod def create(cls, config_dict: dict, metadata_config_dict: dict, ctx: WorkflowContext): @@ -86,15 +79,15 @@ def prepare(self): pass def next_record(self) -> Iterable[Record]: - yield self.get_redash_charts() - yield self.get_redash_dashboard() - - def get_redash_charts(self) -> Optional[Chart]: - query_response = self.client.queries() - for query_response in query_response["results"]: - query_id = query_response["id"] - query_name = query_response["name"] - query_data = self.client._get(f"/api/queries/{query_id}").json() + yield from self.get_redash_charts() + yield from self.get_redash_dashboard() + + def get_redash_charts(self) -> Chart: + query_info = self.client.queries() + for query_info in query_info["results"]: + query_id = query_info["id"] + query_name = query_info["name"] + query_data = requests.get(f"{self.config.uri}/api/queries/{query_id}").json() for visualization in query_data.get("Visualizations", []): chart_type = visualization.get("type", "") chart_description = visualization.get("description", "") if visualization.get("description", "") else "" @@ -109,26 +102,25 @@ def get_redash_charts(self) -> Optional[Chart]: def get_redash_dashboard(self) -> Dashboard: charts: List[Chart] = [] - dashboard_response = self.client.dashboards() - for dashboard_response in dashboard_response["results"]: - if dashboard_response["id"] is not None: - if not self.config.chart_pattern.allowed(dashboard_response["id"]): - self.status.item_dropped_status(dashboard_response["id"]) - continue + dashboard_info = self.client.dashboards() + for dashboard_info in dashboard_info["results"]: + dashboard_id = dashboard_info["id"] + if dashboard_info["id"] is not None: self.status.item_scanned_status() - dashboard_data = self.client.dashboard(dashboard_response["slug"]) - widgets = dashboard_data.get("widgets", []) - dashboard_description = widgets.get("text") - yield Dashboard( - id=uuid.uuid4(), - name=dashboard_response["id"], - displayName=dashboard_response["name"], - description=dashboard_description if dashboard_response else "", - charts=charts, - usageSummary=None, - service=EntityReference(id=self.service.id, type="dashboardService"), - href=dashboard_response["slug"] - ) + dashboard_data = self.client.dashboard(dashboard_id) + dashboard_url = f"{self.config.uri}/dashboard/{dashboard_data.get('slug', '')}" + for widgets in dashboard_data.get("widgets", []): + dashboard_description = widgets.get("text") + yield Dashboard( + id=uuid.uuid4(), + name=dashboard_info["id"], + displayName=dashboard_info["name"], + description=dashboard_description if dashboard_info else "", + charts=charts, + usageSummary=None, + service=EntityReference(id=self.service.id, type="dashboardService"), + url=dashboard_url + ) def get_status(self) -> SourceStatus: return self.status From 58f0baeafa7f6754538d3e5377a465729c4fc2b8 Mon Sep 17 00:00:00 2001 From: parthp2107 Date: Mon, 13 Sep 2021 14:17:57 +0530 Subject: [PATCH 4/4] minor changes --- ingestion/setup.py | 2 +- .../src/metadata/ingestion/source/redash.py | 20 +++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/ingestion/setup.py b/ingestion/setup.py index 5c036859c35d..fa6eea3e03bb 100644 --- a/ingestion/setup.py +++ b/ingestion/setup.py @@ -88,7 +88,7 @@ def get_long_description(): "oracle": {"cx_Oracle"}, "presto": {"pyhive~=0.6.3"}, "postgres": {"pymysql>=1.0.2", "psycopg2-binary", "GeoAlchemy2"}, - "redash": {"redash-toolbelt"}, + "redash": {"redash-toolbelt==0.1.4"}, "redshift": {"sqlalchemy-redshift", "psycopg2-binary", "GeoAlchemy2"}, "redshift-usage": {"sqlalchemy-redshift", "psycopg2-binary", "GeoAlchemy2"}, "scheduler": scheduler_requirements, diff --git a/ingestion/src/metadata/ingestion/source/redash.py b/ingestion/src/metadata/ingestion/source/redash.py index e7eed6408e29..f77e420a795f 100644 --- a/ingestion/src/metadata/ingestion/source/redash.py +++ b/ingestion/src/metadata/ingestion/source/redash.py @@ -111,16 +111,16 @@ def get_redash_dashboard(self) -> Dashboard: dashboard_url = f"{self.config.uri}/dashboard/{dashboard_data.get('slug', '')}" for widgets in dashboard_data.get("widgets", []): dashboard_description = widgets.get("text") - yield Dashboard( - id=uuid.uuid4(), - name=dashboard_info["id"], - displayName=dashboard_info["name"], - description=dashboard_description if dashboard_info else "", - charts=charts, - usageSummary=None, - service=EntityReference(id=self.service.id, type="dashboardService"), - url=dashboard_url - ) + yield Dashboard( + id=uuid.uuid4(), + name=dashboard_info["id"], + displayName=dashboard_info["name"], + description=dashboard_description if dashboard_info else "", + charts=charts, + usageSummary=None, + service=EntityReference(id=self.service.id, type="dashboardService"), + url=dashboard_url + ) def get_status(self) -> SourceStatus: return self.status