2023-05-17 16:54:05 +08:00
|
|
|
import logging
|
|
|
|
|
2024-01-27 01:08:02 +08:00
|
|
|
from felicity.apps.iol.fhir.utils import (
|
2023-12-30 19:42:48 +08:00
|
|
|
get_shipment_bundle_resource,
|
|
|
|
get_diagnostic_report_resource,
|
|
|
|
)
|
2024-01-27 01:08:02 +08:00
|
|
|
from felicity.apps.iol.relay import post_data
|
|
|
|
from felicity.apps.job import models as job_models
|
|
|
|
from felicity.apps.job.conf import states as job_states
|
|
|
|
from felicity.apps.shipment import conf, models
|
|
|
|
from felicity.apps.shipment.utils import (
|
2023-09-11 13:02:05 +08:00
|
|
|
shipment_assign,
|
|
|
|
shipment_reset_assigned_count,
|
|
|
|
shipment_receive,
|
|
|
|
shipment_result_update,
|
|
|
|
)
|
2024-01-27 01:08:02 +08:00
|
|
|
from felicity.apps.user.models import User
|
2023-05-17 16:54:05 +08:00
|
|
|
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
async def populate_shipment_manually(job_uid: str):
|
|
|
|
logger.info(f"starting job {job_uid} ....")
|
|
|
|
job: job_models.Job = await job_models.Job.get(uid=job_uid)
|
|
|
|
if not job:
|
|
|
|
return
|
|
|
|
|
|
|
|
if not job.status == job_states.PENDING:
|
|
|
|
return
|
|
|
|
|
|
|
|
await job.change_status(new_status=job_states.RUNNING)
|
|
|
|
shipment_uid = job.job_id
|
|
|
|
|
|
|
|
shipment: models.Shipment = await models.Shipment.get(uid=shipment_uid)
|
|
|
|
if not shipment:
|
|
|
|
await job.change_status(
|
|
|
|
new_status=job_states.FAILED,
|
|
|
|
change_reason=f"Failed to acquire Shipment {shipment_uid}",
|
|
|
|
)
|
|
|
|
logger.warning(f"Failed to acquire Shipment {shipment_uid}")
|
|
|
|
return
|
|
|
|
|
2023-05-26 02:45:25 +08:00
|
|
|
await shipment_reset_assigned_count(shipment.uid)
|
2023-05-17 16:54:05 +08:00
|
|
|
|
|
|
|
# handle Empty and preperation
|
2023-09-11 13:02:05 +08:00
|
|
|
if shipment.state not in [
|
|
|
|
conf.shipment_states.EMPTY,
|
|
|
|
conf.shipment_states.PREPERATION,
|
|
|
|
]:
|
2023-05-17 16:54:05 +08:00
|
|
|
await job.change_status(
|
|
|
|
new_status=job_states.FAILED,
|
|
|
|
change_reason=f"Shipment {shipment_uid} - is already processed",
|
|
|
|
)
|
|
|
|
logger.warning(f"Shipment {shipment_uid} - is already processed")
|
|
|
|
return
|
|
|
|
|
2023-05-26 02:45:25 +08:00
|
|
|
await shipment_assign(shipment.uid, job.data, job.creator_uid)
|
2023-05-17 16:54:05 +08:00
|
|
|
|
|
|
|
await job.change_status(new_status=job_states.FINISHED)
|
|
|
|
logger.info(f"Done !! Job {job_uid} was executed successfully :)")
|
|
|
|
|
|
|
|
|
2023-05-26 02:45:25 +08:00
|
|
|
async def dispatch_shipment(job_uid: str, by_uid=None):
|
2023-06-24 08:10:16 +08:00
|
|
|
logger.info(f"running shipment dispatch job: {job_uid} ....")
|
2023-05-17 16:54:05 +08:00
|
|
|
job: job_models.Job = await job_models.Job.get(uid=job_uid)
|
|
|
|
if not job:
|
|
|
|
return
|
|
|
|
|
|
|
|
if not job.status == job_states.PENDING:
|
2023-05-26 02:45:25 +08:00
|
|
|
return None
|
2023-05-17 16:54:05 +08:00
|
|
|
|
|
|
|
await job.change_status(new_status=job_states.RUNNING)
|
|
|
|
shipment_uid = job.job_id
|
2023-09-11 13:02:05 +08:00
|
|
|
shipment: models.Shipment = await models.Shipment.get(uid=shipment_uid)
|
|
|
|
|
2023-05-17 16:54:05 +08:00
|
|
|
if not shipment.state == conf.shipment_states.AWAITING:
|
2023-05-26 02:45:25 +08:00
|
|
|
await job.change_status(new_status=job_states.FAILED)
|
|
|
|
return None
|
2023-09-11 13:02:05 +08:00
|
|
|
|
2023-05-17 16:54:05 +08:00
|
|
|
resource = await get_shipment_bundle_resource(shipment_uid)
|
2023-05-26 02:45:25 +08:00
|
|
|
success = await post_data(
|
2023-12-30 19:42:48 +08:00
|
|
|
f"{shipment.laboratory.url}/Bundle",
|
|
|
|
resource.model_dump(exclude_none=True),
|
2023-09-11 13:02:05 +08:00
|
|
|
shipment.laboratory.username,
|
|
|
|
shipment.laboratory.password,
|
|
|
|
)
|
|
|
|
|
|
|
|
await job.change_status(
|
|
|
|
new_status=job_states.PENDING if not success else job_states.FINISHED
|
2023-05-26 02:45:25 +08:00
|
|
|
)
|
|
|
|
await shipment.change_state(
|
2023-09-11 13:02:05 +08:00
|
|
|
state=conf.shipment_states.FAILED
|
|
|
|
if not success
|
|
|
|
else conf.shipment_states.SHIPPED,
|
|
|
|
updated_by_uid=by_uid if by_uid else job.creator_uid,
|
2023-05-26 02:45:25 +08:00
|
|
|
)
|
|
|
|
if not success:
|
|
|
|
await job.backoff(1, 10)
|
2023-09-11 13:02:05 +08:00
|
|
|
|
2023-05-26 02:45:25 +08:00
|
|
|
return await models.Shipment.get(uid=shipment_uid)
|
2023-06-24 18:28:27 +08:00
|
|
|
|
|
|
|
|
|
|
|
async def receive_shipment(job_uid: str):
|
2023-12-30 19:42:48 +08:00
|
|
|
logger.info(f"starting job receive shipment: {job_uid} ....")
|
2023-06-24 18:28:27 +08:00
|
|
|
job: job_models.Job = await job_models.Job.get(uid=job_uid)
|
|
|
|
if not job:
|
|
|
|
return
|
|
|
|
|
|
|
|
if not job.status == job_states.PENDING:
|
|
|
|
return
|
|
|
|
|
|
|
|
await job.change_status(new_status=job_states.RUNNING)
|
|
|
|
shipment_uid = job.job_id
|
|
|
|
|
|
|
|
shipment: models.Shipment = await models.Shipment.get(uid=shipment_uid)
|
|
|
|
if not shipment:
|
|
|
|
await job.change_status(
|
|
|
|
new_status=job_states.FAILED,
|
|
|
|
change_reason=f"Failed to acquire Shipment {shipment_uid}",
|
|
|
|
)
|
|
|
|
logger.warning(f"Failed to acquire Shipment {shipment_uid}")
|
|
|
|
return
|
|
|
|
|
|
|
|
await shipment_receive(shipment.uid, job.data, job.creator_uid)
|
|
|
|
|
|
|
|
await job.change_status(new_status=job_states.FINISHED)
|
2023-07-09 23:10:41 +08:00
|
|
|
logger.info(f"Done !! Job {job_uid} was executed successfully :)")
|
|
|
|
|
|
|
|
|
|
|
|
async def return_shipped_report(job_uid: str):
|
|
|
|
logger.info(f"starting job return shipped report: {job_uid} ....")
|
|
|
|
job: job_models.Job = await job_models.Job.get(uid=job_uid)
|
|
|
|
if not job:
|
|
|
|
return
|
|
|
|
|
|
|
|
if not job.status == job_states.PENDING:
|
|
|
|
return
|
|
|
|
|
|
|
|
await job.change_status(new_status=job_states.RUNNING)
|
|
|
|
|
|
|
|
shipped_sample_uid = job.job_id
|
2023-09-11 13:02:05 +08:00
|
|
|
shipped: models.ShippedSample = await models.ShippedSample.get(
|
|
|
|
uid=shipped_sample_uid
|
|
|
|
)
|
|
|
|
|
2023-07-09 23:10:41 +08:00
|
|
|
if not shipped:
|
|
|
|
await job.change_status(
|
|
|
|
new_status=job_states.FAILED,
|
|
|
|
change_reason=f"Failed to acquire shipped sample {shipped_sample_uid}",
|
|
|
|
)
|
|
|
|
logger.warning(f"Failed to acquire shipped sample {shipped_sample_uid}")
|
|
|
|
return
|
2023-09-11 13:02:05 +08:00
|
|
|
|
2023-07-09 23:10:41 +08:00
|
|
|
shipment = await models.Shipment.get(uid=shipped.shipment_uid)
|
|
|
|
if not shipment:
|
|
|
|
await job.change_status(
|
|
|
|
new_status=job_states.FAILED,
|
|
|
|
change_reason=f"Failed to acquire Shipment {shipped.shipment_uid}",
|
|
|
|
)
|
|
|
|
logger.warning(f"Failed to acquire Shipment {shipped.shipment_uid}")
|
|
|
|
return
|
|
|
|
|
|
|
|
result_uids = []
|
|
|
|
if job.data["target"] == "result":
|
|
|
|
result_uids.append(job.data["uid"])
|
|
|
|
|
2023-09-11 13:02:05 +08:00
|
|
|
resource = await get_diagnostic_report_resource(
|
|
|
|
shipped.sample.analysis_request_uid, result_uids, True
|
|
|
|
)
|
2023-07-09 23:10:41 +08:00
|
|
|
success = await post_data(
|
2023-09-11 13:02:05 +08:00
|
|
|
f"{shipment.laboratory.url}DiagnosticReport",
|
2023-12-30 19:42:48 +08:00
|
|
|
resource.model_dump(exclude_none=True),
|
2023-09-11 13:02:05 +08:00
|
|
|
shipment.laboratory.username,
|
|
|
|
shipment.laboratory.password,
|
2023-07-09 23:10:41 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
if not success:
|
|
|
|
await job.change_status(new_status=job_states.FAILED)
|
|
|
|
else:
|
2023-07-15 19:10:52 +08:00
|
|
|
await job.change_status(new_status=job_states.FINISHED)
|
|
|
|
|
|
|
|
|
2023-07-15 20:17:54 +08:00
|
|
|
async def process_shipped_report(job_uid: str):
|
|
|
|
logger.info(f"starting job return shipped report: {job_uid} ....")
|
|
|
|
job: job_models.Job = await job_models.Job.get(uid=job_uid)
|
|
|
|
if not job:
|
|
|
|
return
|
|
|
|
|
|
|
|
if not job.status == job_states.PENDING:
|
|
|
|
return
|
|
|
|
|
|
|
|
await job.change_status(new_status=job_states.RUNNING)
|
|
|
|
data = job.data.get("data", None)
|
|
|
|
|
|
|
|
assert data["resourceType"] == "DiagnosticReport"
|
2023-07-16 14:42:42 +08:00
|
|
|
|
|
|
|
actor = await User.get(uid=job.creator_uid)
|
|
|
|
await shipment_result_update(data, actor)
|
2023-07-15 20:17:54 +08:00
|
|
|
|
2023-09-11 13:02:05 +08:00
|
|
|
await job.change_status(new_status=job_states.FINISHED)
|