Skip to content

Commit 09f20a2

Browse files
authored
Merge pull request #2036 from kili-technology/feature/lab-4023-aa-kd-i-investigate-performance-with-high-number-of-assets
feat(lab-4023): update assignation to run in batch
2 parents cb2d249 + 6ff9b73 commit 09f20a2

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/kili/entrypoints/mutations/asset/__init__.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Asset mutations."""
2+
23
import warnings
34
from typing import Any, Literal, Optional, Union, cast
45

@@ -252,13 +253,24 @@ def assign_assets_to_labelers(
252253
if len(resolved_asset_ids) != len(to_be_labeled_by_array):
253254
raise MutationError("There must be as many assets as there are lists of labelers.")
254255

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]] = {}
256259
for asset_id, to_be_labeled_by in zip(
257260
resolved_asset_ids, to_be_labeled_by_array, strict=False
258261
):
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)
262274
return formated_results
263275

264276
@typechecked

0 commit comments

Comments
 (0)