Skip to content

Commit aba04d4

Browse files
committed
feat: add tags to asset migration
1 parent 518c6a9 commit aba04d4

1 file changed

Lines changed: 59 additions & 38 deletions

File tree

nominal/experimental/migration/migrator/asset_migrator.py

Lines changed: 59 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -46,67 +46,88 @@ def default_copy_options(self) -> AssetCopyOptions:
4646
include_video=True,
4747
)
4848

49-
def _copy_from_impl(self, source: Asset, options: AssetCopyOptions) -> Asset:
49+
def _copy_from_impl(self, source_asset: Asset, options: AssetCopyOptions) -> Asset:
5050
if options.include_checklists and not options.include_runs:
5151
raise ValueError("include_checklists set to True requires include_runs to be set to True.")
5252
new_asset = self.ctx.destination_client.create_asset(
53-
name=options.new_asset_name if options.new_asset_name is not None else source.name,
53+
name=options.new_asset_name if options.new_asset_name is not None else source_asset.name,
5454
description=options.new_asset_description
5555
if options.new_asset_description is not None
56-
else source.description,
57-
properties=options.new_asset_properties if options.new_asset_properties is not None else source.properties,
58-
labels=options.new_asset_labels if options.new_asset_labels is not None else source.labels,
56+
else source_asset.description,
57+
properties=options.new_asset_properties
58+
if options.new_asset_properties is not None
59+
else source_asset.properties,
60+
labels=options.new_asset_labels if options.new_asset_labels is not None else source_asset.labels,
5961
)
6062

61-
resolved_dataset_config = options.dataset_config
62-
if resolved_dataset_config is not None:
63-
dataset_migrator = DatasetMigrator(
64-
MigrationContext(
65-
destination_client=self.ctx.destination_client,
66-
migration_state=self.ctx.migration_state,
67-
)
68-
)
69-
source_datasets = source.list_datasets()
70-
dataset_mapping = options.old_to_new_dataset_rid_mapping
71-
for data_scope, source_dataset in source_datasets:
72-
if source_dataset.rid in dataset_mapping:
73-
new_dataset_rid = dataset_mapping[source_dataset.rid]
74-
new_dataset = self.ctx.destination_client.get_dataset(new_dataset_rid)
75-
else:
76-
new_dataset = dataset_migrator.copy_from(
77-
source_dataset,
78-
DatasetCopyOptions(
79-
include_files=resolved_dataset_config.include_dataset_files,
80-
preserve_uuid=resolved_dataset_config.preserve_dataset_uuid,
81-
),
82-
)
83-
dataset_mapping[source_dataset.rid] = new_dataset.rid
84-
new_asset.add_dataset(data_scope, new_dataset)
63+
if options.dataset_config is not None:
64+
self._copy_asset_datasets(source_asset, new_asset, options)
8565

8666
run_mapping: Dict[str, str] = {}
8767

8868
if options.include_events:
89-
logger.info("Copying events for asset %s (rid: %s)", source.name, source.rid)
90-
self._copy_asset_events(source, new_asset)
69+
logger.info("Copying events for asset %s (rid: %s)", source_asset.name, source_asset.rid)
70+
self._copy_asset_events(source_asset, new_asset)
9171

9272
if options.include_runs:
93-
logger.info("Copying runs for asset %s (rid: %s)", source.name, source.rid)
94-
run_mapping = self._copy_asset_runs(source, new_asset)
73+
logger.info("Copying runs for asset %s (rid: %s)", source_asset.name, source_asset.rid)
74+
run_mapping = self._copy_asset_runs(source_asset, new_asset)
9575

9676
if options.include_checklists:
97-
logger.info("Copying checklists for asset %s (rid: %s)", source.name, source.rid)
98-
self._copy_asset_checklists(source, run_mapping)
77+
logger.info("Copying checklists for asset %s (rid: %s)", source_asset.name, source_asset.rid)
78+
self._copy_asset_checklists(source_asset, run_mapping)
9979

10080
if options.include_video:
101-
logger.info("Copying videos for asset %s (rid: %s)", source.name, source.rid)
102-
self._copy_asset_videos(source, new_asset)
81+
logger.info("Copying videos for asset %s (rid: %s)", source_asset.name, source_asset.rid)
82+
self._copy_asset_videos(source_asset, new_asset)
10383

104-
self._copy_asset_and_run_workbooks(source, new_asset, run_mapping)
84+
self._copy_asset_and_run_workbooks(source_asset, new_asset, run_mapping)
10585
return new_asset
10686

10787
def _get_resource_name(self, resource: Asset) -> str:
10888
return resource.name
10989

90+
def _copy_asset_datasets(self, source_asset: Asset, new_asset: Asset, options: AssetCopyOptions) -> None:
91+
if options.dataset_config is None:
92+
return
93+
94+
dataset_migrator = DatasetMigrator(
95+
MigrationContext(
96+
destination_client=self.ctx.destination_client,
97+
migration_state=self.ctx.migration_state,
98+
)
99+
)
100+
101+
dataset_mapping = options.old_to_new_dataset_rid_mapping
102+
103+
source_data_scopes = source_asset._list_dataset_scopes()
104+
source_datasets = {data_scope[1].rid: data_scope[1] for data_scope in source_asset.list_datasets()}
105+
106+
for source_data_scope in source_data_scopes:
107+
source_data_scope_name = source_data_scope.data_scope_name
108+
source_dataset_rid = source_data_scope.data_source.dataset
109+
if source_dataset_rid is None or source_dataset_rid not in source_datasets:
110+
raise ValueError(
111+
f"Data scope {source_data_scope_name} on asset {source_asset.rid} does not have a dataset"
112+
)
113+
114+
source_dataset = source_datasets[source_dataset_rid]
115+
source_series_tags = source_data_scope.series_tags
116+
if source_dataset_rid in dataset_mapping:
117+
new_dataset_rid = dataset_mapping[source_dataset_rid]
118+
new_dataset = self.ctx.destination_client.get_dataset(new_dataset_rid)
119+
else:
120+
new_dataset = dataset_migrator.copy_from(
121+
source_dataset,
122+
DatasetCopyOptions(
123+
include_files=options.dataset_config.include_dataset_files,
124+
preserve_uuid=options.dataset_config.preserve_dataset_uuid,
125+
),
126+
)
127+
128+
dataset_mapping[source_dataset.rid] = new_dataset.rid
129+
new_asset.add_dataset(source_data_scope_name, new_dataset, series_tags=source_series_tags)
130+
110131
def _copy_asset_events(self, source_asset: Asset, new_asset: Asset) -> None:
111132
event_migrator = EventMigrator(
112133
MigrationContext(

0 commit comments

Comments
 (0)