@@ -1111,22 +1111,15 @@ def filter_containers(containers, project_name):
11111111 'invalid' are invalid containers (invalid content) and 'not_found' has
11121112 some missing entity in database.
11131113
1114- Todos:
1115- Respect 'project_name' on containers if is available.
1116-
11171114 Args:
11181115 containers (Iterable[dict]): List of containers referenced into scene.
1119- project_name (str): Name of project in which context shoud look for
1120- versions.
1116+ project_name (str): Current project name.
11211117
11221118 Returns:
11231119 ContainersFilterResult: Named tuple with 'latest', 'outdated',
11241120 'invalid' and 'not_found' containers.
11251121
11261122 """
1127- # Make sure containers is list that won't change
1128- containers = list (containers )
1129-
11301123 outdated_containers = []
11311124 uptodate_containers = []
11321125 not_found_containers = []
@@ -1138,98 +1131,102 @@ def filter_containers(containers, project_name):
11381131 invalid_containers
11391132 )
11401133 # Query representation docs to get it's version ids
1141- repre_ids = {
1142- container ["representation" ]
1143- for container in containers
1144- if _is_valid_representation_id (container ["representation" ])
1145- }
1146- if not repre_ids :
1147- if containers :
1148- invalid_containers .extend (containers )
1149- return output
1150-
1151- repre_entities = ayon_api .get_representations (
1152- project_name ,
1153- representation_ids = repre_ids ,
1154- fields = {"id" , "versionId" }
1155- )
1156- # Store representations by stringified representation id
1157- repre_entities_by_id = {}
1158- repre_entities_by_version_id = collections .defaultdict (list )
1159- for repre_entity in repre_entities :
1160- repre_id = repre_entity ["id" ]
1161- version_id = repre_entity ["versionId" ]
1162- repre_entities_by_id [repre_id ] = repre_entity
1163- repre_entities_by_version_id [version_id ].append (repre_entity )
1164-
1165- # Query version docs to get it's product ids
1166- # - also query hero version to be able identify if representation
1167- # belongs to existing version
1168- version_entities = ayon_api .get_versions (
1169- project_name ,
1170- version_ids = repre_entities_by_version_id .keys (),
1171- hero = True ,
1172- fields = {"id" , "productId" , "version" }
1173- )
1174- versions_by_id = {}
1175- versions_by_product_id = collections .defaultdict (list )
1176- hero_version_ids = set ()
1177- for version_entity in version_entities :
1178- version_id = version_entity ["id" ]
1179- # Store versions by their ids
1180- versions_by_id [version_id ] = version_entity
1181- # There's no need to query products for hero versions
1182- # - they are considered as latest?
1183- if version_entity ["version" ] < 0 :
1184- hero_version_ids .add (version_id )
1134+ containers_by_project_name = collections .defaultdict (list )
1135+ for container in containers :
1136+ if not _is_valid_representation_id (container ["representation" ]):
1137+ invalid_containers .append (container )
11851138 continue
1186- product_id = version_entity ["productId" ]
1187- versions_by_product_id [product_id ].append (version_entity )
1139+ container_project = container .get ("project_name" )
1140+ if not container_project :
1141+ container_project = project_name
1142+ containers_by_project_name [container_project ].append (container )
11881143
1189- last_versions = ayon_api .get_last_versions (
1190- project_name ,
1191- versions_by_product_id .keys (),
1192- fields = {"id" }
1193- )
1194- # Figure out which versions are outdated
1195- outdated_version_ids = set ()
1196- for product_id , last_version_entity in last_versions .items ():
1197- for version_entity in versions_by_product_id [product_id ]:
1144+ if not containers_by_project_name :
1145+ return output
1146+
1147+ for project_name , p_containers in containers_by_project_name .items ():
1148+ repre_ids = {
1149+ container ["representaiton" ]
1150+ for container in p_containers
1151+ }
1152+ repre_entities = ayon_api .get_representations (
1153+ project_name ,
1154+ representation_ids = repre_ids ,
1155+ fields = {"id" , "versionId" }
1156+ )
1157+ version_ids = set ()
1158+ repre_entities_by_id = {}
1159+ for repre_entity in repre_entities :
1160+ repre_id = repre_entity ["id" ]
1161+ version_id = repre_entity ["versionId" ]
1162+ version_ids .add (version_id )
1163+ repre_entities_by_id [repre_id ] = repre_entity
1164+
1165+ # Query version docs to get it's product ids
1166+ # - also query hero version to be able identify if representation
1167+ # belongs to existing version
1168+ version_entities = ayon_api .get_versions (
1169+ project_name ,
1170+ version_ids = version_ids ,
1171+ hero = True ,
1172+ fields = {"id" , "productId" , "version" }
1173+ )
1174+ versions_by_id = {}
1175+ versions_by_product_id = collections .defaultdict (list )
1176+ hero_version_ids = set ()
1177+ for version_entity in version_entities :
11981178 version_id = version_entity ["id" ]
1199- if version_id in hero_version_ids :
1179+ # Store versions by their ids
1180+ versions_by_id [version_id ] = version_entity
1181+ # There's no need to query products for hero versions
1182+ # - they are considered as latest?
1183+ if version_entity ["version" ] < 0 :
1184+ hero_version_ids .add (version_id )
12001185 continue
1201- if version_id != last_version_entity [ "id" ]:
1202- outdated_version_ids . add ( version_id )
1186+ product_id = version_entity [ "productId" ]
1187+ versions_by_product_id [ product_id ]. append ( version_entity )
12031188
1204- # Based on all collected data figure out which containers are outdated
1205- # - log out if there are missing representation or version documents
1206- for container in containers :
1207- container_name = container ["objectName" ]
1208- repre_id = container ["representation" ]
1209- if not _is_valid_representation_id (repre_id ):
1210- invalid_containers .append (container )
1211- continue
1189+ last_versions = ayon_api .get_last_versions (
1190+ project_name ,
1191+ versions_by_product_id .keys (),
1192+ fields = {"id" }
1193+ )
12121194
1213- repre_entity = repre_entities_by_id .get (repre_id )
1214- if not repre_entity :
1215- log .debug (
1216- f"Container '{ container_name } ' has an invalid representation."
1217- " It is missing in the database."
1218- )
1219- not_found_containers .append (container )
1220- continue
1195+ # Figure out which versions are outdated
1196+ outdated_version_ids = set ()
1197+ for product_id , last_version_entity in last_versions .items ():
1198+ for version_entity in versions_by_product_id [product_id ]:
1199+ version_id = version_entity ["id" ]
1200+ if version_id in hero_version_ids :
1201+ continue
1202+ if version_id != last_version_entity ["id" ]:
1203+ outdated_version_ids .add (version_id )
1204+
1205+ # Based on all collected data figure out which containers are outdated
1206+ # - log out if there are missing representation or version documents
1207+ for container in p_containers :
1208+ container_name = container ["objectName" ]
1209+ repre_id = container ["representation" ]
1210+ repre_entity = repre_entities_by_id .get (repre_id )
1211+ if not repre_entity :
1212+ log .debug (
1213+ f"Container '{ container_name } ' has an invalid representation."
1214+ " It is missing in the database."
1215+ )
1216+ not_found_containers .append (container )
1217+ continue
12211218
1222- version_id = repre_entity ["versionId" ]
1223- if version_id not in versions_by_id :
1224- log .debug (
1225- f"Representation on container '{ container_name } ' has an"
1226- " invalid version. It is missing in the database."
1227- )
1228- not_found_containers .append (container )
1219+ version_id = repre_entity ["versionId" ]
1220+ if version_id not in versions_by_id :
1221+ log .debug (
1222+ f"Representation on container '{ container_name } ' has an"
1223+ " invalid version. It is missing in the database."
1224+ )
1225+ not_found_containers .append (container )
12291226
1230- elif version_id in outdated_version_ids :
1231- outdated_containers .append (container )
1232- else :
1233- uptodate_containers .append (container )
1227+ elif version_id in outdated_version_ids :
1228+ outdated_containers .append (container )
1229+ else :
1230+ uptodate_containers .append (container )
12341231
12351232 return output
0 commit comments