Skip to content

Commit

Permalink
feat(gooddata-sdk): replace "insight" in gooddata-sdk
Browse files Browse the repository at this point in the history
JIRA: PSDK-172
  • Loading branch information
hkad98 committed Mar 4, 2024
1 parent 6cf3fa5 commit 22b2297
Show file tree
Hide file tree
Showing 40 changed files with 345 additions and 144 deletions.
4 changes: 2 additions & 2 deletions gooddata-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ sdk = gooddata_sdk.GoodDataSdk.create(host, token)
workspace_id = "demo"
insight_id = "customers_trend"
# reads insight from workspace
insight = sdk.insights.get_insight(workspace_id, insight_id)
insight = sdk.visualizations.get_visualization(workspace_id, insight_id)
# triggers computation for the insight. the result will be returned in a tabular form
table = sdk.tables.for_insight(workspace_id, insight)
table = sdk.tables.for_visualization(workspace_id, insight)

# and this is how you can read data row-by-row and do something with it
for row in table.read_all():
Expand Down
13 changes: 12 additions & 1 deletion gooddata-sdk/gooddata_sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,21 @@
SimpleMetric,
)
from gooddata_sdk.compute.service import ComputeService
from gooddata_sdk.insight import Insight, InsightAttribute, InsightBucket, InsightMetric, InsightService
from gooddata_sdk.sdk import GoodDataSdk
from gooddata_sdk.table import ExecutionTable, TableService
from gooddata_sdk.utils import SideLoads
from gooddata_sdk.visualization import (
Insight,
InsightAttribute,
InsightBucket,
InsightMetric,
InsightService,
Visualization,
VisualizationAttribute,
VisualizationBucket,
VisualizationMetric,
VisualizationService,
)

# by default don't log anything
logging.getLogger(__name__).addHandler(logging.NullHandler())
78 changes: 62 additions & 16 deletions gooddata-sdk/gooddata_sdk/catalog/export/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import time
from pathlib import Path
from typing import Callable, Optional, Tuple, Union
from warnings import warn

from gooddata_api_client.exceptions import NotFoundException
from gooddata_api_client.model.pdf_export_request import PdfExportRequest
Expand All @@ -16,8 +17,8 @@
SimpleMetric,
)
from gooddata_sdk.catalog.catalog_service_base import CatalogServiceBase
from gooddata_sdk.insight import InsightService
from gooddata_sdk.table import ExecutionTable, TableService
from gooddata_sdk.visualization import VisualizationService


class ExportService(CatalogServiceBase):
Expand All @@ -41,8 +42,8 @@ class ExportService(CatalogServiceBase):
Export a PDF of a GoodData Dashboard.
export_tabular:
Export Tabular data from a GoodData Dashboard.
export_tabular_by_insight_id:
Exports the tabular data of a particular insight id.
export_tabular_by_visualization_id:
Exports the tabular data of a particular visualization id.
"""

def __init__(self, api_client: GoodDataApiClient) -> None:
Expand Down Expand Up @@ -252,7 +253,8 @@ def export_tabular(
@staticmethod
def _custom_overrides_labels(exec_table: ExecutionTable, metrics_format: str = "#,##0") -> ExportCustomOverride:
"""
Insights by default use generated hash as local_id therefore we might want to use dummy logic to replace it.
Visualizations by default use generated hash as local_id,
therefore, we might want to use dummy logic to replace it.
For attributes by label.id
For metrics by item.id
"""
Expand All @@ -267,19 +269,33 @@ def _custom_overrides_labels(exec_table: ExecutionTable, metrics_format: str = "
}
return ExportCustomOverride(labels=labels, metrics=metrics)

def _get_insight_exec_table(self, workspace_id: str, insight_id: str) -> Tuple[ExecutionTable, str]:
def _get_visualization_exec_table(self, workspace_id: str, visualization_id: str) -> Tuple[ExecutionTable, str]:
try:
insight = InsightService(self._client).get_insight(workspace_id=workspace_id, insight_id=insight_id)
return TableService(self._client).for_insight(workspace_id=workspace_id, insight=insight), insight.title
visualization = VisualizationService(self._client).get_visualization(
workspace_id=workspace_id, visualization_id=visualization_id
)
return TableService(self._client).for_visualization(
workspace_id=workspace_id, visualization=visualization
), visualization.title
except NotFoundException:
raise ValueError(
f"Either workspace workspace_id='{workspace_id}' or insight insight_id='{insight_id}' does not exist."
f"Either workspace workspace_id='{workspace_id}' "
f"or visualization visualization_id='{visualization_id}' does not exist."
)

def export_tabular_by_insight_id(
def _get_insight_exec_table(self, workspace_id: str, insight_id: str) -> Tuple[ExecutionTable, str]:
warn(
"This method is deprecated and it will be removed in v1.20.0 release. "
"Please use '_get_visualization_exec_table' method instead.",
DeprecationWarning,
stacklevel=2,
)
return self._get_visualization_exec_table(workspace_id, insight_id)

def export_tabular_by_visualization_id(
self,
workspace_id: str,
insight_id: str,
visualization_id: str,
file_format: str,
file_name: Optional[str] = None,
settings: Optional[ExportSettings] = None,
Expand All @@ -289,15 +305,15 @@ def export_tabular_by_insight_id(
max_retry: float = 5.0,
) -> None:
"""
Exports the tabular data of a particular insight id.
Exports the tabular data of a particular visualization id.
Args:
workspace_id (str): The workspace id from which the insight is to be exported.
insight_id (str): The id of the insight to be exported.
workspace_id (str): The workspace id from which the visualization is to be exported.
visualization_id (str): The id of the visualization to be exported.
file_format (str): The format of the file to be exported.
file_name (Optional[str], optional): The name which the exported file should have. Defaults to None.
settings (Optional[ExportSettings], optional): Any additional settings for the export. Defaults to None.
store_path (Union[str, Path], optional): The path to store the exported file. Defaults to Path.cwd().
store_path (Union[str, Path], optional): The path to store the exported file. Default to Path.cwd().
timeout (float, optional): The maximum time to wait for the export to finish. Defaults to 60.0.
retry (float, optional):
Initial wait time (in seconds) before retrying to get the exported content. Defaults to 0.2.
Expand All @@ -306,9 +322,9 @@ def export_tabular_by_insight_id(
Returns:
None
"""
exec_table, insight_tile = self._get_insight_exec_table(workspace_id, insight_id)
exec_table, visualization_tile = self._get_visualization_exec_table(workspace_id, visualization_id)
custom_override = self._custom_overrides_labels(exec_table)
file_name = file_name if file_name is not None else insight_tile
file_name = file_name if file_name is not None else visualization_tile
export_request = ExportRequest(
format=file_format,
execution_result=exec_table.result_id,
Expand All @@ -324,3 +340,33 @@ def export_tabular_by_insight_id(
retry=retry,
max_retry=max_retry,
)

def export_tabular_by_insight_id(
self,
workspace_id: str,
insight_id: str,
file_format: str,
file_name: Optional[str] = None,
settings: Optional[ExportSettings] = None,
store_path: Union[str, Path] = Path.cwd(),
timeout: float = 60.0,
retry: float = 0.2,
max_retry: float = 5.0,
) -> None:
warn(
"This method is deprecated and it will be removed in v1.20.0 release. "
"Please use 'export_tabular_by_visualization_id' method instead.",
DeprecationWarning,
stacklevel=2,
)
self.export_tabular_by_visualization_id(
workspace_id,
insight_id,
file_format,
file_name,
settings,
store_path,
timeout,
retry,
max_retry,
)
12 changes: 6 additions & 6 deletions gooddata-sdk/gooddata_sdk/catalog/workspace/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,9 +687,9 @@ def get_texts_to_translate(
for metric in workspace_content.analytics.metrics or []:
self.add_title_description(to_translate, metric.title, metric.description)
if workspace_content.analytics:
for insight in workspace_content.analytics.visualization_objects or []:
self.add_title_description(to_translate, insight.title, insight.description)
for bucket in insight.content["buckets"]:
for visualization in workspace_content.analytics.visualization_objects or []:
self.add_title_description(to_translate, visualization.title, visualization.description)
for bucket in visualization.content["buckets"]:
for item in bucket["items"]:
if "measure" in item:
if "alias" in item["measure"]:
Expand Down Expand Up @@ -741,9 +741,9 @@ def set_translated_texts(
for metric in new_workspace_content.analytics.metrics or []:
self.set_title_description(metric, translated)
if new_workspace_content.analytics:
for insight in new_workspace_content.analytics.visualization_objects or []:
self.set_title_description(insight, translated)
for bucket in insight.content["buckets"]:
for visualization in new_workspace_content.analytics.visualization_objects or []:
self.set_title_description(visualization, translated)
for bucket in visualization.content["buckets"]:
for item in bucket["items"]:
if "measure" in item:
if "alias" in item["measure"]:
Expand Down
14 changes: 13 additions & 1 deletion gooddata-sdk/gooddata_sdk/sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from pathlib import Path
from typing import Optional
from warnings import warn

from gooddata_sdk.catalog.data_source.service import CatalogDataSourceService
from gooddata_sdk.catalog.export.service import ExportService
Expand All @@ -13,10 +14,10 @@
from gooddata_sdk.catalog.workspace.service import CatalogWorkspaceService
from gooddata_sdk.client import GoodDataApiClient
from gooddata_sdk.compute.service import ComputeService
from gooddata_sdk.insight import InsightService
from gooddata_sdk.support import SupportService
from gooddata_sdk.table import TableService
from gooddata_sdk.utils import PROFILES_FILE_PATH, profile_content
from gooddata_sdk.visualization import InsightService, VisualizationService


class GoodDataSdk:
Expand Down Expand Up @@ -74,6 +75,7 @@ def __init__(self, client: GoodDataApiClient) -> None:
self._catalog_user = CatalogUserService(self._client)
self._compute = ComputeService(self._client)
self._insights = InsightService(self._client)
self._visualizations = VisualizationService(self._client)
self._tables = TableService(self._client)
self._support = SupportService(self._client)
self._catalog_permission = CatalogPermissionService(self._client)
Expand Down Expand Up @@ -101,8 +103,18 @@ def compute(self) -> ComputeService:

@property
def insights(self) -> InsightService:
warn(
"This property is deprecated and it will be removed in v1.20.0 release. "
"Please use 'visualizations' property instead.",
DeprecationWarning,
stacklevel=2,
)
return self._insights

@property
def visualizations(self) -> VisualizationService:
return self._visualizations

@property
def tables(self) -> TableService:
return self._tables
Expand Down
Loading

0 comments on commit 22b2297

Please sign in to comment.