Skip to content

utils.pyにget_rejected_countを追加する。 #155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 13, 2020
Merged
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
2 changes: 1 addition & 1 deletion annofabapi/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.30.0'
__version__ = '0.30.1'
28 changes: 27 additions & 1 deletion annofabapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import requests
from requests.structures import CaseInsensitiveDict

from annofabapi.models import Task, TaskHistory, TaskPhase
from annofabapi.models import Task, TaskHistory, TaskHistoryShort, TaskPhase


def _raise_for_status(response: requests.Response) -> None:
Expand Down Expand Up @@ -273,6 +273,32 @@ def get_task_history_index_skipped_inspection(task_history_list: List[TaskHistor
return index_list


def get_number_of_rejections(task_histories: List[TaskHistoryShort], phase: TaskPhase, phase_stage: int = 1) -> int:
"""
タスク履歴から、指定されたタスクフェーズでの差し戻し回数を取得する。

Args:
task_histories: タスク履歴
phase: どのフェーズで差し戻されたか(TaskPhase.INSPECTIONかTaskPhase.ACCEPTANCE)
phase_stage: どのフェーズステージで差し戻されたか。デフォルトは1。

Returns:
差し戻し回数
"""
if phase not in [TaskPhase.INSPECTION, TaskPhase.ACCEPTANCE]:
raise ValueError("引数'phase'には、'TaskPhase.INSPECTION'か'TaskPhase.ACCEPTANCE'を指定してください。")

rejections_by_phase = 0
for i, history in enumerate(task_histories):
if not (history["phase"] == phase.value and history["phase_stage"] == phase_stage and history["worked"]):
continue

if i + 1 < len(task_histories) and task_histories[i + 1]["phase"] == TaskPhase.ANNOTATION.value:
rejections_by_phase += 1

return rejections_by_phase


def can_put_annotation(task: Task, my_account_id: str) -> bool:
"""
対象タスクが、`put_annotation` APIで、アノテーションを更新できる状態かどうか。
Expand Down
5 changes: 1 addition & 4 deletions generate/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ usage_exit() {
}

FLAG_DOWNLOAD=false
FLAG_DOCKER_PULL=false


if [ $# -gt 0 ]; then
for OPT in "$@"
Expand Down Expand Up @@ -41,9 +41,6 @@ fi
SCRIPT_DIR=$(cd $(dirname $0); pwd)
pushd ${SCRIPT_DIR}

if "${FLAG_DOCKER_PULL}"; then
docker pull openapitools/openapi-generator-cli
fi

# swagger.yamlを修正したいときがあるので、
if "${FLAG_DOWNLOAD}"; then
Expand Down
112 changes: 111 additions & 1 deletion tests/test_local_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
# tz_jst = datetime.timezone(datetime.timedelta(hours=9))
# assert to_iso8601_extension(d, tz_jst) == "2019-10-08T16:20:08.241+09:00"

from annofabapi.utils import get_task_history_index_skipped_acceptance, get_task_history_index_skipped_inspection
from annofabapi.models import TaskPhase
from annofabapi.utils import (get_number_of_rejections, get_task_history_index_skipped_acceptance,
get_task_history_index_skipped_inspection)


class TestTaskHistoryUtils:
Expand Down Expand Up @@ -236,3 +238,111 @@ def test_get_task_history_index_skipped_inspection_検査2回_検査1回目で
actual = get_task_history_index_skipped_inspection(task_history_list)
expected = []
assert all([a == b for a, b in zip(actual, expected)])

def test_get_number_of_rejections_教師付1回目(self):
task_history_short_list = [{
"account_id": self.ACCOUNT_ID,
"phase": "annotation",
"phase_stage": 1,
"worked": True
}]

actual = get_number_of_rejections(task_history_short_list, TaskPhase.ACCEPTANCE)
assert actual == 0

def test_get_number_of_rejections_受入で1回差戻(self):
task_history_short_list = [
{
"account_id": self.ACCOUNT_ID,
'phase': 'annotation',
'phase_stage': 1,
'worked': True
},
{
"account_id": self.ACCOUNT_ID,
'phase': 'acceptance',
'phase_stage': 1,
'worked': True
},
{
"account_id": self.ACCOUNT_ID,
'phase': 'annotation',
'phase_stage': 1,
'worked': True
},
]

actual = get_number_of_rejections(task_history_short_list, TaskPhase.ACCEPTANCE)
assert actual == 1

def test_get_number_of_rejections_検査で1回差戻(self):
task_history_short_list = [
{
"account_id": self.ACCOUNT_ID,
'phase': 'annotation',
'phase_stage': 1,
'worked': True
},
{
"account_id": self.ACCOUNT_ID,
'phase': 'inspection',
'phase_stage': 1,
'worked': True
},
{
"account_id": self.ACCOUNT_ID,
'phase': 'annotation',
'phase_stage': 1,
'worked': True
},
]

actual = get_number_of_rejections(task_history_short_list, TaskPhase.INSPECTION)
assert actual == 1

def test_get_number_of_rejections_検査と受入で1回差戻(self):
task_history_short_list = [{
"account_id": self.ACCOUNT_ID,
"phase": "annotation",
"phase_stage": 1,
"worked": True
}, {
"account_id": self.ACCOUNT_ID,
"phase": "inspection",
"phase_stage": 1,
"worked": True
}, {
"account_id": self.ACCOUNT_ID,
"phase": "annotation",
"phase_stage": 1,
"worked": True
}, {
"account_id": self.ACCOUNT_ID,
"phase": "inspection",
"phase_stage": 1,
"worked": True
}, {
"account_id": self.ACCOUNT_ID,
"phase": "acceptance",
"phase_stage": 1,
"worked": True
}, {
"account_id": self.ACCOUNT_ID,
"phase": "annotation",
"phase_stage": 1,
"worked": True
}, {
"account_id": "00589ed0-dd63-40db-abb2-dfe5e13c8299",
"phase": "inspection",
"phase_stage": 1,
"worked": True
}, {
"account_id": self.ACCOUNT_ID,
"phase": "acceptance",
"phase_stage": 1,
"worked": True
}]
actual_inspection = get_number_of_rejections(task_history_short_list, TaskPhase.INSPECTION)
assert actual_inspection == 1
actual_acceptance = get_number_of_rejections(task_history_short_list, TaskPhase.ACCEPTANCE)
assert actual_acceptance == 1