Skip to content

Commit

Permalink
feat: batch question creation (#1416)
Browse files Browse the repository at this point in the history
Co-authored-by: theodu <[email protected]>
  • Loading branch information
theodu and theodu authored Aug 29, 2023
1 parent cf5c24b commit ffeccc4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 50 deletions.
33 changes: 17 additions & 16 deletions src/kili/entrypoints/mutations/issue/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
from kili.core.graphql import QueryOptions
from kili.core.graphql.operations.label.queries import LabelQuery, LabelWhere
from kili.core.helpers import deprecate
from kili.core.utils.pagination import BatchIteratorBuilder
from kili.entrypoints.base import BaseOperationEntrypointMixin
from kili.entrypoints.mutations.asset.helpers import get_asset_ids_or_throw_error
from kili.gateways.kili_api_gateway.issue.operations import GQL_CREATE_ISSUES
from kili.services.helpers import assert_all_arrays_have_same_size
from kili.utils import tqdm
from kili.utils.logcontext import for_all_methods, log_call

from .helpers import get_issue_numbers


@for_all_methods(log_call, exclude=["__init__"])
class MutationsIssue(BaseOperationEntrypointMixin):
Expand Down Expand Up @@ -56,7 +56,6 @@ def append_to_issues(
A result object which indicates if the mutation was successful,
or an error message.
"""
issue_number = get_issue_numbers(self, project_id, type_, 1)[0]
try:
options = QueryOptions(disable_tqdm=True)
where = LabelWhere(
Expand All @@ -76,7 +75,7 @@ def append_to_issues(
variables = {
"issues": [
{
"issueNumber": issue_number,
"issueNumber": 0,
"labelID": label_id,
"objectMid": object_mid,
"type": type_,
Expand Down Expand Up @@ -111,19 +110,21 @@ def create_questions(
A list of dictionary with the `id` key of the created questions.
"""
assert_all_arrays_have_same_size([text_array, asset_id_array])
issue_number_array = get_issue_numbers(self, project_id, "QUESTION", len(text_array))
asset_id_array = get_asset_ids_or_throw_error(
self, asset_id_array, asset_external_id_array, project_id
)
variables = {
"issues": [
{"issueNumber": issue_number, "type": "QUESTION", "assetId": asset_id, "text": text}
for (asset_id, text, issue_number) in zip(
asset_id_array, text_array, issue_number_array
)
],
"where": {"idIn": asset_id_array},
}
created_questions: List[Dict[str, str]] = []
with tqdm.tqdm(total=len(text_array), desc="Creating questions") as pbar:
for batch_questions in BatchIteratorBuilder(list(zip(asset_id_array, text_array))):
variables = {
"issues": [
{"issueNumber": 0, "type": "QUESTION", "assetId": asset_id, "text": text}
for (asset_id, text) in batch_questions
],
"where": {"idIn": asset_id_array},
}

result = self.graphql_client.execute(GQL_CREATE_ISSUES, variables)
return self.format_result("data", result)
result = self.graphql_client.execute(GQL_CREATE_ISSUES, variables)
created_questions.extend(self.format_result("data", result))
pbar.update(len(batch_questions))
return created_questions
34 changes: 1 addition & 33 deletions src/kili/entrypoints/mutations/issue/helpers.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,13 @@
"""Helpers for the issue mutations."""


from typing import List, Literal, cast
from typing import List

from kili.core.graphql import QueryOptions
from kili.core.graphql.operations.issue.queries import IssueQuery, IssueWhere
from kili.core.graphql.operations.label.queries import LabelQuery, LabelWhere
from kili.exceptions import NotFound


# pylint: disable=missing-type-doc
def get_issue_numbers(kili, project_id: str, type_: Literal["QUESTION", "ISSUE"], size: int):
"""Get the next available issue number.
Args:
kili: Kili instance.
project_id: Id of the project.
type_: type of the issue to add.
size: the number of issue to add.
"""
issues = IssueQuery(kili.graphql_client, kili.http_client)(
IssueWhere(
project_id=project_id,
),
["type", "issueNumber"],
QueryOptions(disable_tqdm=True),
)
first_issue_number = (
cast(
int,
max(
(issue["issueNumber"] for issue in issues if issue["type"] == type_),
default=-1,
),
)
+ 1
)

return list(range(first_issue_number, first_issue_number + size))


def get_labels_asset_ids_map(kili, project_id: str, label_id_array: List[str]):
"""Return a dictionary that gives for every label id, its associated asset id.
Expand Down
2 changes: 1 addition & 1 deletion src/kili/gateways/kili_api_gateway/issue/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def create_issues(
) -> List[Issue]:
"""Send a GraphQL request calling createIssues resolver."""
created_issue_entities: List[Issue] = []
with tqdm.tqdm(total=len(issues)) as pbar:
with tqdm.tqdm(total=len(issues), desc="Creating issues") as pbar:
for issues_batch in BatchIteratorBuilder(issues):
batch_targeted_asset_ids = [issue.asset_id for issue in issues_batch]
payload = {
Expand Down

0 comments on commit ffeccc4

Please sign in to comment.