We are having a multilevel asset hierarchy in cdf, and ground level assets are only having timeseries linked.
we are trying to create Relationships between every parent asset and its child asset’s Timeseries.
we used python sdk code snippets to do this. Which are been attached to this for your reference.
In python sdk, after running the python relationship code, it is showing relationships are established. But in UI it is not showing from Assets, But we are able to see under Timeseries.
even after we tried interchanging the Source and target between assets and timeseries, same result.
one unique observation is that we could see the relationship established just for 1 asset which is having 3 timeseries linkage. rest others are not showing(they have more than 20 relations, even 500 for root asset.)
we would like to know are we following correct manner, or any restrictions are from CDF tool. plz help us in this regard.
from cognite.client import CogniteClient
from cognite.client.data_classes import Relationship
# Initialize the Cognite client
#client = CogniteClient()
# Define your project-specific data set ID (replace with your actual data set ID)
data_set_id = 434501746932720
# Define the maximum chunk size for asset_external_ids
max_chunk_size = 100
def establish_relationships_for_asset_new(asset_id):
# Retrieve the subtree assets
subtree_assets = list(client.assets.retrieve_subtree(external_id=asset_id).to_pandas()s'externalId'])
# Split the subtree assets into chunks of max_chunk_size
chunks = subtree_assetsei:i + max_chunk_size] for i in range(0, len(subtree_assets), max_chunk_size)]
relationships_to_create = ]
# Process each chunk of asset_external_ids
for chunk in chunks:
time_series_list = client.time_series.list(asset_external_ids=chunk)
for ts in time_series_list:
relationship = Relationship(
external_id=f"Linking-Asset{asset_id}-TS{ts.external_id}",
source_external_id=asset_id,
source_type="asset",
target_external_id=ts.external_id,
target_type="timeseries",
confidence=1.0,
data_set_id=data_set_id,
labels=l"LINKED TIMESERIES"]
)
relationships_to_create.append(relationship)
# Create the relationships in Cognite
if relationships_to_create:
created_relationships = client.relationships.create(relationships_to_create)
print(f"Upserted {len(created_relationships)} relationships for asset {asset_id}")
else:
print(f"No Timeseries in asset {asset_id} to upsert")