Skip to content

[utils]受入/検査がスキップされたタスク履歴番号を取得するメソッドを追加 #108

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 4 commits into from
Jan 22, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
62 changes: 61 additions & 1 deletion annofabapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
import datetime
import logging
from pathlib import Path
from typing import Optional
from typing import List, Optional

import dateutil
import dateutil.tz
import requests

from annofabapi.models import TaskHistory, TaskPhase


def raise_for_status(response: requests.Response):
"""
Expand Down Expand Up @@ -131,3 +133,61 @@ def wrapped(*args, **kwargs):
logging.getLogger("backoff").setLevel(level=backoff_logger_level)

return wrapped


def get_task_history_index_skipped_acceptance(task_history_list: List[TaskHistory]) -> List[int]:
"""
受入がスキップされたタスク履歴のインデックス番号(0始まり)を返す。
Args:
task_history_list: タスク履歴List
Returns:
受入フェーズがスキップされた履歴のインデックス番号(0始まり)。受入がスキップされていない場合は空リストを返す。
"""
index_list = []
for index, history in enumerate(task_history_list):
if not (TaskPhase(history["phase"]) == TaskPhase.ACCEPTANCE and history["account_id"] is None):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

作業時間が0であることも確認した方がよいのでは。ないと困る一例として、受入後のアノテーション一括編集とかがあります。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

メモ:受入完了状態のタスクに対してアノテーション一括編集すると、

  • 受入フェーズが追加される
  • 担当者はなし
  • 作業時間は0でない
    になる

continue

if index + 1 < len(task_history_list):
# 直後の履歴あり
next_history = task_history_list[index + 1]
# 直後の履歴が受入の場合、受入がスキップされた履歴(提出取り消しではない)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

個人的な感想ですが、この説明の正しさを理解するのに少し時間がかかりました。

「直後が受入であること」ではなく「直後が前段のフェーズでないこと」とした方が、確かに提出取消ならそうなるはずだ、と理解しやすいのではないでしょうか。

Copy link
Collaborator Author

@yuji38kwmt yuji38kwmt Jan 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

「直後が受入であること」ではなく「直後が前段のフェーズでないこと」とした方が、確かに提出取消ならそうなるはずだ、と理解しやすいのではないでしょうか。

「前段≠直前」で合っていますか?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

たとえば3番目の履歴の「前段フェーズ」は検査でしょうか?教師付でしょうか?

if TaskPhase(next_history["phase"]) == TaskPhase.ACCEPTANCE:
index_list.append(index)
else:
# 直後の履歴がない
index_list.append(index)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最後の履歴が受入フェーズで担当者なしというのは、受入未着手も含まれるのでは?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

「受入フェーズで担当者なし」のタスクは、受入フェーズのタスク履歴がなかったため、問題ないかと思います。
「受入フェーズの担当者が割り当てられた」タスクは、受入フェーズのタスク履歴がありました。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

とはいえ、「started_datetimeended_datetime`の両方がnullである」という条件を加えた方が確実だと思ったので、修正します。


return index_list


def get_task_history_index_skipped_inspection(task_history_list: List[TaskHistory]) -> List[int]:
"""
検査フェーズがスキップされたタスク履歴のインデックス番号(0始まり)を返す。
Args:
task_history_list: タスク履歴List
Returns:
検査フェーズがスキップされた履歴のインデックス番号(0始まり)。検査がスキップされていない場合は空リストを返す。
"""
index_list = []
for index, history in enumerate(task_history_list):
if not (TaskPhase(history["phase"]) == TaskPhase.INSPECTION and history["account_id"] is None):
continue

if index + 1 < len(task_history_list):
# 直後の履歴あり
next_history = task_history_list[index + 1]
# 直後の履歴が受入/検査の場合、検査がスキップされた履歴(提出取り消しではない)
if TaskPhase(next_history["phase"]) in [TaskPhase.ACCEPTANCE, TaskPhase.INSPECTION]:
index_list.append(index)
else:
# 直後の履歴がない
index_list.append(index)

return index_list
76 changes: 76 additions & 0 deletions tests/test_local_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,79 @@
# d = datetime.datetime(2019, 10, 8, 16, 20, 8, 241762)
# 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


class TestTaskHistoryUtils:
ACCOUNT_ID = "12345678-abcd-1234-abcd-1234abcd5678"

def test_get_task_history_index_skipped_acceptance(self):
task_history_list = [{
"phase": "annotation",
"phase_stage": 1,
"account_id": self.ACCOUNT_ID
}, {
"phase": "acceptance",
"phase_stage": 1,
"account_id": None
}]

actual = get_task_history_index_skipped_acceptance(task_history_list)
expected = [1]
assert all([a == b for a, b in zip(actual, expected)])

def test_get_task_history_index_skipped_acceptance_提出取消は対象外(self):
task_history_list = [{
"phase": "annotation",
"phase_stage": 1,
"account_id": self.ACCOUNT_ID
}, {
"phase": "acceptance",
"phase_stage": 1,
"account_id": None
}, {
"phase": "annotation",
"phase_stage": 1,
"account_id": self.ACCOUNT_ID
}]
actual = get_task_history_index_skipped_acceptance(task_history_list)
expected = []
assert all([a == b for a, b in zip(actual, expected)])

def test_get_task_history_index_skipped_inspection(self):
task_history_list = [{
"phase": "annotation",
"phase_stage": 1,
"account_id": self.ACCOUNT_ID
}, {
"phase": "inspection",
"phase_stage": 1,
"account_id": None
}, {
"phase": "acceptance",
"phase_stage": 1,
"account_id": None
}]

actual = get_task_history_index_skipped_inspection(task_history_list)
expected = [1]
assert all([a == b for a, b in zip(actual, expected)])

def test_get_task_history_index_skipped_inspection_提出取消は対象外(self):
task_history_list = [{
"phase": "annotation",
"phase_stage": 1,
"account_id": self.ACCOUNT_ID
}, {
"phase": "inspection",
"phase_stage": 1,
"account_id": None
}, {
"phase": "annotation",
"phase_stage": 1,
"account_id": self.ACCOUNT_ID
}]
actual = get_task_history_index_skipped_inspection(task_history_list)
expected = []
assert all([a == b for a, b in zip(actual, expected)])