Skip to content

Commit 71709d7

Browse files
authored
annofabapi.util.task_history.get_task_creation_datetimeを追加 (#710)
* 関数追加 * poetry updat * typeを修正
1 parent f91a415 commit 71709d7

File tree

5 files changed

+128
-6
lines changed

5 files changed

+128
-6
lines changed

annofabapi/util/task_history.py

+40
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,43 @@ def find_rejected_task_history_indices(task_history_list: list[dict[str, Any]])
3737
index_list.append(index)
3838

3939
return index_list
40+
41+
42+
def get_task_creation_datetime(task: dict[str, Any], task_history_list: list[dict[str, Any]]) -> str:
43+
"""タスクが作成された日時を取得します。
44+
45+
Args:
46+
task: タスク情報。
47+
タスクが作成された直後は ``task_history_list`` に有効な日時が格納されていないので、
48+
``operation_updated_datetime`` をタスク作成日時とします。
49+
task_history_list: ``get_task_histories`` APIのレスポンス
50+
51+
Returns:
52+
タスクの作成日時
53+
54+
Notes:
55+
2020-12-08以前に作成されたタスクでは、タスクの作成日時を取得できません。
56+
2020-12-08以前に作成されたタスクでは、先頭のタスク履歴は「タスク作成」ではなく、「教師付け作業」の履歴だからです。
57+
https://annofab.com/docs/releases/2020.html#v01020
58+
59+
Raises:
60+
ValueError: 2020-12-08以前に作成されたタスクの情報を指定した場合
61+
"""
62+
assert len(task_history_list) > 0
63+
first_history = task_history_list[0]
64+
65+
if (
66+
first_history["account_id"] is None
67+
and first_history["accumulated_labor_time_milliseconds"] == "PT0S"
68+
and first_history["phase"] == TaskPhase.ANNOTATION.value
69+
):
70+
if len(task_history_list) == 1:
71+
# 一度も作業されていないタスクは、先頭のタスク履歴のstarted_datetimeはNoneである
72+
# 替わりにタスクの`operation_updated_datetime`をタスク作成日時とする
73+
assert task["operation_updated_datetime"] is not None
74+
return task["operation_updated_datetime"]
75+
76+
assert first_history["started_datetime"] is not None
77+
return first_history["started_datetime"]
78+
79+
raise ValueError("2020-12-08以前に作成されたタスクのため、タスクの作成日時を取得できません。")

docs/api_reference/util.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ annofabapi.util.attribute\_restrictions module
2121
:show-inheritance:
2222

2323
annofabapi.util.task\_history module
24-
---------------------------------
24+
------------------------------------------------------------------
2525

2626
.. automodule:: annofabapi.util.task_history
2727
:members:

poetry.lock

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[tool.poetry]
22
name = "annofabapi"
33
version = "0.0.0" # `poetry-dynamic-versioning`を使ってGitHubのバージョンタグを取得している。変更不要
4-
description = "Python Clinet Library of Annofab WebAPI (https://annofab.com/docs/api/)"
4+
description = "Python Client Library of Annofab WebAPI (https://annofab.com/docs/api/)"
55
authors = ["Kurusugawa Computer Inc."]
66
license = "MIT"
77
keywords=["annofab", "api"]

tests/util/test_local_task_history.py

+83-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Any
22

3-
from annofabapi.util.task_history import find_rejected_task_history_indices
3+
from annofabapi.util.task_history import find_rejected_task_history_indices, get_task_creation_datetime
44

55

66
def test__find_rejected_task_history_indices():
@@ -184,3 +184,85 @@ def test__find_rejected_task_history_indices():
184184
]
185185
actual = find_rejected_task_history_indices(task_histories)
186186
assert actual == [4, 12]
187+
188+
189+
def test__get_task_creation_datetime__タスク作成直後の状態から算出():
190+
task = {
191+
"project_id": "58a2a621-7d4b-41e7-927b-cdc570c1114a",
192+
"task_id": "20250327_02",
193+
"phase": "annotation",
194+
"phase_stage": 1,
195+
"status": "not_started",
196+
"input_data_id_list": ["bbc5faf4-f22f-48ac-9393-b524cb3e0034"],
197+
"account_id": None,
198+
"histories_by_phase": [],
199+
"work_time_span": 0,
200+
"number_of_rejections": 0,
201+
"started_datetime": None,
202+
"updated_datetime": "2025-03-27T11:31:23.513+09:00",
203+
"operation_updated_datetime": "2025-03-27T11:31:23.513+09:00",
204+
"sampling": None,
205+
"metadata": {},
206+
}
207+
task_history_list = [
208+
{
209+
"project_id": "58a2a621-7d4b-41e7-927b-cdc570c1114a",
210+
"task_id": "20250327_02",
211+
"task_history_id": "f8e21d05-4726-4706-865c-52a85fd8f2ae",
212+
"started_datetime": None,
213+
"ended_datetime": None,
214+
"accumulated_labor_time_milliseconds": "PT0S",
215+
"phase": "annotation",
216+
"phase_stage": 1,
217+
"account_id": None,
218+
}
219+
]
220+
221+
assert get_task_creation_datetime(task, task_history_list) == "2025-03-27T11:31:23.513+09:00"
222+
223+
224+
def test__get_task_creation_datetime__タスクに担当者を割り当てた状態から算出():
225+
task = {
226+
"project_id": "58a2a621-7d4b-41e7-927b-cdc570c1114a",
227+
"task_id": "20250327_32",
228+
"phase": "annotation",
229+
"phase_stage": 1,
230+
"status": "not_started",
231+
"input_data_id_list": ["dcd56d5f-77a8-4a78-b191-755e109032fa"],
232+
"account_id": "00589ed0-dd63-40db-abb2-dfe5e13c8299",
233+
"histories_by_phase": [{"account_id": "00589ed0-dd63-40db-abb2-dfe5e13c8299", "phase": "annotation", "phase_stage": 1, "worked": False}],
234+
"work_time_span": 0,
235+
"number_of_rejections": 0,
236+
"started_datetime": None,
237+
"updated_datetime": "2025-03-27T13:07:35.04+09:00",
238+
"operation_updated_datetime": "2025-03-27T11:31:23.482+09:00",
239+
"sampling": None,
240+
"metadata": {},
241+
}
242+
243+
task_history_list = [
244+
{
245+
"project_id": "58a2a621-7d4b-41e7-927b-cdc570c1114a",
246+
"task_id": "20250327_32",
247+
"task_history_id": "ff451241-2ace-40fa-8a5d-f1d688a6d588",
248+
"started_datetime": "2025-03-27T11:31:22.91+09:00",
249+
"ended_datetime": None,
250+
"accumulated_labor_time_milliseconds": "PT0S",
251+
"phase": "annotation",
252+
"phase_stage": 1,
253+
"account_id": None,
254+
},
255+
{
256+
"project_id": "58a2a621-7d4b-41e7-927b-cdc570c1114a",
257+
"task_id": "20250327_32",
258+
"task_history_id": "c239ce9a-36da-4330-ad09-154705c308a5",
259+
"started_datetime": "2025-03-27T13:07:35.133+09:00",
260+
"ended_datetime": None,
261+
"accumulated_labor_time_milliseconds": "PT0S",
262+
"phase": "annotation",
263+
"phase_stage": 1,
264+
"account_id": "00589ed0-dd63-40db-abb2-dfe5e13c8299",
265+
},
266+
]
267+
268+
assert get_task_creation_datetime(task, task_history_list) == "2025-03-27T11:31:22.91+09:00"

0 commit comments

Comments
 (0)