|
1 | 1 | """Asset mutations.""" |
| 2 | + |
2 | 3 | import warnings |
3 | 4 | from typing import Any, Literal, Optional, Union, cast |
4 | 5 |
|
@@ -252,13 +253,24 @@ def assign_assets_to_labelers( |
252 | 253 | if len(resolved_asset_ids) != len(to_be_labeled_by_array): |
253 | 254 | raise MutationError("There must be as many assets as there are lists of labelers.") |
254 | 255 |
|
255 | | - formated_results = [] |
| 256 | + # Group assets by assignee list to batch |
| 257 | + # assignee combination instead of 1 call per asset. |
| 258 | + groups: dict[tuple, list[str]] = {} |
256 | 259 | for asset_id, to_be_labeled_by in zip( |
257 | 260 | resolved_asset_ids, to_be_labeled_by_array, strict=False |
258 | 261 | ): |
259 | | - payload = {"userIds": to_be_labeled_by, "where": {"id": asset_id}} |
260 | | - results = self.graphql_client.execute(GQL_ASSIGN_ASSETS, payload) |
261 | | - formated_results.append(results) |
| 262 | + key = tuple(to_be_labeled_by) |
| 263 | + if key not in groups: |
| 264 | + groups[key] = [] |
| 265 | + groups[key].append(asset_id) |
| 266 | + |
| 267 | + formated_results = [] |
| 268 | + for user_ids_tuple, ids_for_group in groups.items(): |
| 269 | + for i in range(0, len(ids_for_group), 100): |
| 270 | + chunk = ids_for_group[i : i + 100] |
| 271 | + payload = {"userIds": list(user_ids_tuple), "where": {"idIn": chunk}} |
| 272 | + results = self.graphql_client.execute(GQL_ASSIGN_ASSETS, payload) |
| 273 | + formated_results.append(results) |
262 | 274 | return formated_results |
263 | 275 |
|
264 | 276 | @typechecked |
|
0 commit comments