-
Notifications
You must be signed in to change notification settings - Fork 3
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import sys | ||
import warnings | ||
from collections import defaultdict | ||
from typing import * | ||
|
||
|
@@ -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, | ||
|
@@ -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}} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. **{"x":"y"} is commonly also known as x="y" There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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]] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.""" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤗