Skip to content

Commit

Permalink
feat: improve GoodPandas perf
Browse files Browse the repository at this point in the history
Noticed that it is not necessary to call heavy `get_full_catalog`. `get_attributes_catalog` can be called instead.

JIRA: PSDK-205
risk: low
  • Loading branch information
hkad98 committed Oct 7, 2024
1 parent 2edc83c commit c3f22ac
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
26 changes: 17 additions & 9 deletions gooddata-pandas/gooddata_pandas/data_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from gooddata_sdk import (
Attribute,
AttributeFilter,
CatalogWorkspaceContent,
CatalogAttribute,
ExecutionDefinition,
ExecutionResponse,
Filter,
Expand All @@ -16,6 +16,7 @@
ObjId,
TableDimension,
)
from gooddata_sdk.utils import IdObjType

from gooddata_pandas.utils import (
ColumnsDef,
Expand Down Expand Up @@ -319,27 +320,34 @@ def _extract_for_metrics_only(response: ExecutionResponse, cols: list, col_to_me
return data


def _typed_result(catalog: CatalogWorkspaceContent, attribute: Attribute, result_values: list[Any]) -> list[Any]:
def _find_attribute(attributes: list[CatalogAttribute], id_obj: IdObjType) -> Union[CatalogAttribute, None]:
for attribute in attributes:
if attribute.find_label(id_obj) is not None:
return attribute
return None


def _typed_result(attributes: list[CatalogAttribute], attribute: Attribute, result_values: list[Any]) -> list[Any]:
"""
Internal function to convert result_values to proper data types.
Args:
catalog (CatalogWorkspaceContent): The catalog workspace content.
attributes (list[CatalogAttribute]): The catalog of attributes.
attribute (Attribute): The attribute for which the typed result will be computed.
result_values (list[Any]): A list of raw values.
Returns:
list[Any]: A list of converted values with proper data types.
"""
catalog_attribute = catalog.find_label_attribute(attribute.label)
catalog_attribute = _find_attribute(attributes, attribute.label)
if catalog_attribute is None:
raise ValueError(f"Unable to find attribute {attribute.label} in catalog")
return [_typed_attribute_value(catalog_attribute, value) for value in result_values]


def _extract_from_attributes_and_maybe_metrics(
response: ExecutionResponse,
catalog: CatalogWorkspaceContent,
attributes: list[CatalogAttribute],
cols: list[str],
col_to_attr_idx: dict[str, int],
col_to_metric_idx: dict[str, int],
Expand Down Expand Up @@ -382,12 +390,12 @@ def _extract_from_attributes_and_maybe_metrics(
for idx_name in index:
rs = result.get_all_header_values(attribute_dim, safe_index_to_attr_idx[idx_name])
attribute = index_to_attribute[idx_name]
index[idx_name] += _typed_result(catalog, attribute, rs)
index[idx_name] += _typed_result(attributes, attribute, rs)
for col in cols:
if col in col_to_attr_idx:
rs = result.get_all_header_values(attribute_dim, col_to_attr_idx[col])
attribute = col_to_attribute[col]
data[col] += _typed_result(catalog, attribute, rs)
data[col] += _typed_result(attributes, attribute, rs)
elif col_to_metric_idx[col] < len(result.data):
data[col] += result.data[col_to_metric_idx[col]]
if result.is_complete(attribute_dim):
Expand Down Expand Up @@ -440,10 +448,10 @@ def compute_and_extract(
if not exec_def.has_attributes():
return _extract_for_metrics_only(response, cols, col_to_metric_idx), dict()
else:
catalog = sdk.catalog_workspace_content.get_full_catalog(workspace_id)
attributes = sdk.catalog_workspace_content.get_attributes_catalog(workspace_id, include=["labels", "datasets"])
return _extract_from_attributes_and_maybe_metrics(
response,
catalog,
attributes,
cols,
col_to_attr_idx,
col_to_metric_idx,
Expand Down
11 changes: 9 additions & 2 deletions gooddata-sdk/gooddata_sdk/catalog/workspace/content_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,28 @@ def get_full_catalog(self, workspace_id: str, inject_valid_objects_func: bool =

return CatalogWorkspaceContent.create_workspace_content_catalog(valid_obj_fun, datasets, attributes, metrics)

def get_attributes_catalog(self, workspace_id: str) -> list[CatalogAttribute]:
def get_attributes_catalog(self, workspace_id: str, include: Optional[list[str]] = None) -> list[CatalogAttribute]:
"""Retrieve all attributes in a given workspace.
Args:
workspace_id (str):
Workspace identification string e.g. "demo"
include (list[str]):
Entities to include.
Available: datasets, labels, attributeHierarchies, dataset, defaultView, ALL
Returns:
list[CatalogAttribute]:
List of all attributes in a given workspace.
"""
available_includes = {"datasets", "labels", "attributeHierarchies", "dataset", "defaultView", "ALL"}
include = include if include is not None else ["labels"]
if not set(include).issubset(available_includes):
raise ValueError(f"Invalid include parameter. Available values: {available_includes}, got: {include}")
get_attributes = functools.partial(
self._entities_api.get_all_entities_attributes,
workspace_id,
include=["labels"],
include=include,
_check_return_type=False,
)
attributes = load_all_entities(get_attributes)
Expand Down

0 comments on commit c3f22ac

Please sign in to comment.