Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 78 additions & 5 deletions src/kili/adapters/kili_api_gateway/project_workflow/mappers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
"""GraphQL payload data mappers for project operations."""

from typing import Union

from kili.domain.project import WorkflowStepCreate, WorkflowStepUpdate

from .types import ProjectWorkflowDataKiliAPIGatewayInput
from .types import (
AddReviewStepInput,
DeleteStepInput,
ProjectWorkflowDataKiliAPIGatewayInput,
RenameStepInput,
UpdateLabelingStepPropertiesInput,
UpdateReviewStepPropertiesInput,
)


def project_input_mapper(data: ProjectWorkflowDataKiliAPIGatewayInput) -> dict:
Expand All @@ -18,12 +23,12 @@ def project_input_mapper(data: ProjectWorkflowDataKiliAPIGatewayInput) -> dict:
"updates": [update_step_mapper(step) for step in data.update_steps]
if data.update_steps
else [],
"deletes": data.delete_steps if data.delete_steps else [],
"deletes": data.delete_steps or [],
},
}


def update_step_mapper(data: Union[WorkflowStepCreate, WorkflowStepUpdate]) -> dict:
def update_step_mapper(data: WorkflowStepCreate | WorkflowStepUpdate) -> dict:
"""Build the GraphQL create StepData variable to be sent in an operation."""
step = {
"id": data["id"] if "id" in data else None,
Expand All @@ -35,5 +40,73 @@ def update_step_mapper(data: Union[WorkflowStepCreate, WorkflowStepUpdate]) -> d
"stepCoverage": data["step_coverage"] if "step_coverage" in data else None,
"type": data["type"] if "type" in data else None,
"assignees": data["assignees"] if "assignees" in data else None,
"sendBackStepId": data["send_back_step_id"] if "send_back_step_id" in data else None,
}
return {k: v for k, v in step.items() if v is not None}


def add_review_step_input_mapper(data: AddReviewStepInput) -> dict:
"""Build the GraphQL AddReviewStepInput variable."""
result: dict = {
"projectId": data.project_id,
"name": data.name,
"assignees": data.assignees,
}
if data.consensus_coverage is not None:
result["consensusCoverage"] = data.consensus_coverage
if data.number_of_expected_labels_for_consensus is not None:
result["numberOfExpectedLabelsForConsensus"] = data.number_of_expected_labels_for_consensus
if data.step_coverage is not None:
result["stepCoverage"] = data.step_coverage
if data.use_honeypot is not None:
result["useHoneypot"] = data.use_honeypot
if data.send_back_to_step is not None:
result["sendBackToStep"] = data.send_back_to_step
return result


def update_labeling_step_properties_input_mapper(data: UpdateLabelingStepPropertiesInput) -> dict:
"""Build the GraphQL UpdateLabelingStepPropertiesInput variable."""
result: dict = {
"projectId": data.project_id,
"stepName": data.step_name,
}
if data.consensus_coverage is not None:
result["consensusCoverage"] = data.consensus_coverage
if data.number_of_expected_labels_for_consensus is not None:
result["numberOfExpectedLabelsForConsensus"] = data.number_of_expected_labels_for_consensus
if data.use_honeypot is not None:
result["useHoneypot"] = data.use_honeypot
return result


def update_review_step_properties_input_mapper(data: UpdateReviewStepPropertiesInput) -> dict:
"""Build the GraphQL UpdateReviewStepPropertiesInput variable."""
result: dict = {
"projectId": data.project_id,
"stepName": data.step_name,
}
if data.step_coverage is not None:
result["stepCoverage"] = data.step_coverage
if data.send_back_to_step is not None:
result["sendBackToStep"] = data.send_back_to_step
if data.use_honeypot is not None:
result["useHoneypot"] = data.use_honeypot
return result


def delete_step_input_mapper(data: DeleteStepInput) -> dict:
"""Build the GraphQL DeleteStepInput variable."""
return {
"projectId": data.project_id,
"stepName": data.step_name,
}


def rename_step_input_mapper(data: RenameStepInput) -> dict:
"""Build the GraphQL RenameStepInput variable."""
return {
"projectId": data.project_id,
"stepName": data.step_name,
"newName": data.new_name,
}
59 changes: 59 additions & 0 deletions src/kili/adapters/kili_api_gateway/project_workflow/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,62 @@ def get_steps_query(fragment: str) -> str:
}}
}}
"""


def get_add_review_step_mutation() -> str:
"""Return the GraphQL addReviewStep mutation."""
return """
mutation addReviewStep($input: AddReviewStepInput!) {
data: addReviewStep(input: $input) {
id
name
}
}
"""


def get_update_labeling_step_properties_mutation() -> str:
"""Return the GraphQL updateLabelingStepProperties mutation."""
return """
mutation updateLabelingStepProperties($input: UpdateLabelingStepPropertiesInput!) {
data: updateLabelingStepProperties(input: $input) {
id
name
}
}
"""


def get_update_review_step_properties_mutation() -> str:
"""Return the GraphQL updateReviewStepProperties mutation."""
return """
mutation updateReviewStepProperties($input: UpdateReviewStepPropertiesInput!) {
data: updateReviewStepProperties(input: $input) {
id
name
}
}
"""


def get_delete_step_mutation() -> str:
"""Return the GraphQL deleteStep mutation."""
return """
mutation deleteStep($input: DeleteStepInput!) {
data: deleteStep(input: $input) {
id
}
}
"""


def get_rename_step_mutation() -> str:
"""Return the GraphQL renameStep mutation."""
return """
mutation renameStep($input: RenameStepInput!) {
data: renameStep(input: $input) {
id
name
}
}
"""
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Mixin extending Kili API Gateway class with Projects related operations."""

import warnings

from kili.adapters.kili_api_gateway.base import BaseOperationMixin
Expand All @@ -11,12 +12,31 @@
from kili.domain.types import ListOrTuple
from kili.exceptions import NotFound

from .mappers import project_input_mapper
from .mappers import (
add_review_step_input_mapper,
delete_step_input_mapper,
project_input_mapper,
rename_step_input_mapper,
update_labeling_step_properties_input_mapper,
update_review_step_properties_input_mapper,
)
from .operations import (
get_add_review_step_mutation,
get_delete_step_mutation,
get_rename_step_mutation,
get_steps_query,
get_update_labeling_step_properties_mutation,
get_update_project_workflow_mutation,
get_update_review_step_properties_mutation,
)
from .types import (
AddReviewStepInput,
DeleteStepInput,
ProjectWorkflowDataKiliAPIGatewayInput,
RenameStepInput,
UpdateLabelingStepPropertiesInput,
UpdateReviewStepPropertiesInput,
)
from .types import ProjectWorkflowDataKiliAPIGatewayInput


class ProjectWorkflowOperationMixin(BaseOperationMixin):
Expand Down Expand Up @@ -160,3 +180,38 @@ def remove_reviewers_from_step(
)

return removed_emails

def add_review_step(self, data: AddReviewStepInput) -> dict:
"""Add a review step to a project workflow."""
variables = {"input": add_review_step_input_mapper(data)}
mutation = get_add_review_step_mutation()
result = self.graphql_client.execute(mutation, variables)
return result["data"]

def update_labeling_step_properties(self, data: UpdateLabelingStepPropertiesInput) -> dict:
"""Update properties of a labeling step."""
variables = {"input": update_labeling_step_properties_input_mapper(data)}
mutation = get_update_labeling_step_properties_mutation()
result = self.graphql_client.execute(mutation, variables)
return result["data"]

def update_review_step_properties(self, data: UpdateReviewStepPropertiesInput) -> dict:
"""Update properties of a review step."""
variables = {"input": update_review_step_properties_input_mapper(data)}
mutation = get_update_review_step_properties_mutation()
result = self.graphql_client.execute(mutation, variables)
return result["data"]

def delete_step(self, data: DeleteStepInput) -> dict:
"""Delete a step from a project workflow."""
variables = {"input": delete_step_input_mapper(data)}
mutation = get_delete_step_mutation()
result = self.graphql_client.execute(mutation, variables)
return result["data"]

def rename_step(self, data: RenameStepInput) -> dict:
"""Rename a step in a project workflow."""
variables = {"input": rename_step_input_mapper(data)}
mutation = get_rename_step_mutation()
result = self.graphql_client.execute(mutation, variables)
return result["data"]
62 changes: 57 additions & 5 deletions src/kili/adapters/kili_api_gateway/project_workflow/types.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Types for the ProjectWorkflow-related Kili API gateway functions."""

from dataclasses import dataclass
from typing import Optional

from kili.domain.project import WorkflowStepCreate, WorkflowStepUpdate

Expand All @@ -10,7 +9,60 @@
class ProjectWorkflowDataKiliAPIGatewayInput:
"""ProjectWorkflow input data for Kili API Gateway."""

enforce_step_separation: Optional[bool]
create_steps: Optional[list[WorkflowStepCreate]]
update_steps: Optional[list[WorkflowStepUpdate]]
delete_steps: Optional[list[str]]
enforce_step_separation: bool | None
create_steps: list[WorkflowStepCreate] | None
update_steps: list[WorkflowStepUpdate] | None
delete_steps: list[str] | None


@dataclass
class AddReviewStepInput:
"""Input data for adding a review step to a project workflow."""

project_id: str
name: str
assignees: list[str]
consensus_coverage: int | None = None
number_of_expected_labels_for_consensus: int | None = None
step_coverage: int | None = None
use_honeypot: bool | None = None
send_back_to_step: str | None = None


@dataclass
class UpdateLabelingStepPropertiesInput:
"""Input data for updating labeling step properties."""

project_id: str
step_name: str
consensus_coverage: int | None = None
number_of_expected_labels_for_consensus: int | None = None
use_honeypot: bool | None = None


@dataclass
class UpdateReviewStepPropertiesInput:
"""Input data for updating review step properties."""

project_id: str
step_name: str
step_coverage: int | None = None
send_back_to_step: str | None = None
use_honeypot: bool | None = None


@dataclass
class DeleteStepInput:
"""Input data for deleting a step from a project workflow."""

project_id: str
step_name: str


@dataclass
class RenameStepInput:
"""Input data for renaming a step in a project workflow."""

project_id: str
step_name: str
new_name: str
Loading
Loading