Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternate way of fetching resources #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 19 additions & 40 deletions cognite/power/data_classes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import sys
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import sys
import grouphug

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤗

import warnings
from collections import defaultdict
from typing import *

Expand Down Expand Up @@ -561,29 +560,6 @@ def _load_assets(
base_voltage=base_voltage, grid_type=grid_type, x_filter=x_filter
)

@staticmethod
def _filter_and_convert(
client,
external_ids,
power_type,
base_voltage: Iterable = None,
grid_type: str = None,
x_filter: Callable = None,
) -> "PowerAssetList":
external_ids = list(np.unique(external_ids))
assets = client.assets.retrieve_multiple(external_ids=external_ids, ignore_unknown_ids=True)
if len(assets) != len(external_ids):
warnings.warn(
"{} assets not found when looking up {}s among {}".format(
len(external_ids) - len(assets), power_type, external_ids
)
)
if power_type:
assets = [a for a in assets if (a.metadata or {}).get("type") == power_type]
return PowerAssetList._load_assets(
assets, power_type, cognite_client=client, base_voltage=base_voltage, grid_type=grid_type, x_filter=x_filter
)

def relationships(
self,
power_type: str = None,
Expand All @@ -610,25 +586,28 @@ def relationships(
raise ValueError("Can not combine _sources and _targets.")
if not _source_external_ids and not _target_external_ids:
return PowerAssetList([], cognite_client=self._cognite_client)
rels = self._cognite_client.relationships.list(
source_external_ids=_source_external_ids,
target_external_ids=_target_external_ids,
labels=LabelFilter(contains_all=[label]),
limit=None,
assets = self._cognite_client.power_assets.list(
grid_type=grid_type, base_voltage=base_voltage, **{"metadata": {"type": power_type}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

**{"x":"y"} is commonly also known as x="y"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note power_type may be None

)
if _source_external_ids:
asset_ids = [r.target_external_id for r in rels]
_target_external_ids = [a.external_id for a in assets]
rels = self._cognite_client.relationships.list(
source_external_ids=_source_external_ids,
target_external_ids=_target_external_ids,
labels=LabelFilter(contains_all=[label]),
limit=None,
)
assets = [a for a in assets if a.external_id in [r.target_external_id for r in rels]]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

target_external_ids = {r.target_external_id for r in rels}
and then in (set) here please

else:
asset_ids = [r.source_external_id for r in rels]

return PowerAssetList._filter_and_convert(
self._cognite_client,
asset_ids,
power_type,
base_voltage=base_voltage,
grid_type=grid_type,
x_filter=x_filter,
)
_source_external_ids = [a.external_id for a in assets]
rels = self._cognite_client.relationships.list(
source_external_ids=_source_external_ids,
target_external_ids=_target_external_ids,
labels=LabelFilter(contains_all=[label]),
limit=None,
)
assets = [a for a in assets if a.external_id in [r.source_external_id for r in rels]]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

likewise

return PowerAssetList._load_assets(assets, cognite_client=self._cognite_client, x_filter=x_filter)

def relationship_sources(self, *args, **kwargs) -> "PowerAssetList":
"""Shortcut for finding all assets that are a source, with the current assets as targets. See PowerAssetList.relationships for list of arguments."""
Expand Down