Skip to content

Commit 9d0793c

Browse files
authored
✨ add completed_at property to job (#388)
1 parent 6414eab commit 9d0793c

4 files changed

Lines changed: 34 additions & 4 deletions

File tree

mindee/client_v2.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def get_result(
118118
:param response_type: Class of the product to instantiate.
119119
:return: An inference response.
120120
"""
121-
logger.debug("Fetching inference: %s", inference_id)
121+
logger.debug("Fetching result: %s", inference_id)
122122

123123
response = self.mindee_api.req_get_inference(
124124
inference_id, response_type.get_result_slug()
@@ -152,7 +152,7 @@ def enqueue_and_get_result(
152152
)
153153
enqueue_response = self.enqueue_inference(input_source, params, True)
154154
logger.debug(
155-
"Successfully enqueued document with job id: %s", enqueue_response.job.id
155+
"Successfully enqueued document with job ID: %s", enqueue_response.job.id
156156
)
157157
sleep(params.polling_options.initial_delay_sec)
158158
try_counter = 0
@@ -168,6 +168,11 @@ def enqueue_and_get_result(
168168
f"Parsing failed for job {job_response.job.id}: {detail}"
169169
)
170170
if job_response.job.status == CommonStatus.PROCESSED.value:
171+
logger.debug(
172+
"Job ID %s completed processing at: %s",
173+
job_response.job.id,
174+
job_response.job.completed_at,
175+
)
171176
result = self.get_result(
172177
response_type or InferenceResponse, job_response.job.id
173178
)

mindee/parsing/v2/job.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ class Job:
1414
error: Optional[ErrorResponse]
1515
"""Error response if any."""
1616
created_at: datetime
17-
"""Timestamp of the job creation."""
17+
"""Date and time of the Job creation."""
18+
completed_at: Optional[datetime] = None
19+
"""Date and time of the Job completion. Filled once processing is finished."""
1820
model_id: str
1921
"""ID of the model."""
2022
filename: str
@@ -39,6 +41,11 @@ def __init__(self, raw_response: StringDict) -> None:
3941
self.created_at = datetime.fromisoformat(
4042
raw_response["created_at"].replace("Z", "+00:00")
4143
)
44+
completed_at = raw_response.get("completed_at")
45+
if completed_at:
46+
self.completed_at = datetime.fromisoformat(
47+
completed_at.replace("Z", "+00:00")
48+
)
4249
self.model_id = raw_response["model_id"]
4350
self.polling_url = raw_response["polling_url"]
4451
self.filename = raw_response["filename"]

tests/v2/parsing/test_job_response.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
from datetime import datetime
23

34
import pytest
45

@@ -21,6 +22,20 @@ def test_should_load_when_status_is_processing():
2122
response = JobResponse(json_sample)
2223

2324
assert response.job is not None
25+
assert response.job.status == "Processing"
26+
assert response.job.completed_at is None
27+
assert response.job.error is None
28+
29+
30+
@pytest.mark.v2
31+
def test_should_load_when_status_is_processed():
32+
"""Should load when status is Processing."""
33+
json_sample = _get_job_samples("ok_processed_webhooks_ok.json")
34+
response = JobResponse(json_sample)
35+
36+
assert response.job is not None
37+
assert response.job.status == "Processed"
38+
assert isinstance(response.job.completed_at, datetime)
2439
assert response.job.error is None
2540

2641

@@ -31,6 +46,9 @@ def test_should_load_with_422_error():
3146
response = JobResponse(json_sample)
3247

3348
assert response.job is not None
49+
assert response.job.status == "Failed"
50+
assert isinstance(response.job.completed_at, datetime)
51+
3452
assert isinstance(response.job.error, ErrorResponse)
3553
assert response.job.error.status == 422
3654
assert response.job.error.code.startswith("422-")

0 commit comments

Comments
 (0)