"""Two-phase bulk record submission workflow for UAT."""
from __future__ import annotations
import functools
import inspect
import logging
from collections.abc import Iterator
from dataclasses import dataclass
from datetime import UTC, datetime
from enum import StrEnum
from typing import Any, Literal
from pydantic import Field
from imednet.spi.facade import AsyncImednetFacade, ImednetFacade
from imednet.spi.models import ImednetBaseModel, Job, JobState, normalize_job_state
from imednet.spi.utils import (
DEFAULT_BATCH_CHUNK_SIZE,
AsyncJobPoller,
JobFailedError,
JobPoller,
JobPollSummary,
JobTimeoutError,
call_async_facade_method,
chunk_records,
run_sync_over_async,
)
from imednet.spi.validation import ValidationSchemaProtocol
from ._contracts import (
iter_matching_uat_subjects,
subject_has_keyword,
subject_matches_site,
)
from .generator import GeneratedRecordSet, SyntheticRecordGenerator
from .models import RecordTestType
logger = logging.getLogger(__name__)
_FAILED_ENTRY_STATUSES = {"FAIL", "FAILED", "ERROR"}
_ENTRY_ID_FIELDS = (
"subjectKey",
"subject_key",
"recordId",
"record_id",
"recordOid",
"record_oid",
"subjectId",
"subject_id",
"subjectOid",
"subject_oid",
)
@dataclass
class _SubmittedRegistrationItem:
"""Metadata for a single registration record submitted in Phase 1."""
placeholder_key: str | None
form_key: str
site_name: str | None
site_id: int | None
subject_oid: str | None
data: dict[str, Any]
batch_id: str | None = None
@dataclass
class _PreExistingSubjectsSnapshot:
"""Identifiers of subjects present in the study prior to Phase 1 submission."""
subject_keys: set[str]
subject_ids: set[int]
subject_oids: set[str]
def is_unconfigured_mock_method(fn: Any) -> bool:
"""Return True if ``fn`` is an unconfigured unittest.mock child method."""
if not callable(fn):
return True
if not type(fn).__module__.startswith("unittest.mock"):
return False
if getattr(fn, "side_effect", None) is not None:
return False
if getattr(fn, "_mock_wraps", None) is not None:
return False
ret = getattr(fn, "return_value", None)
return type(ret).__module__.startswith("unittest.mock")
_is_unconfigured_mock_method = is_unconfigured_mock_method
def _clean_str(val: Any) -> str | None:
"""Return a stripped non-empty string if ``val`` is a non-blank string."""
if isinstance(val, str) and val.strip():
return val.strip()
return None
def _clean_int(val: Any) -> int | None:
"""Return an integer identifier if ``val`` is an int (excluding bool) or numeric string."""
if isinstance(val, bool) or val is None:
return None
if isinstance(val, int):
return val
if isinstance(val, str) and val.strip().isdigit():
return int(val.strip())
return None
def _get_attr_or_key(obj: Any, *names: str) -> Any:
"""Read the first non-None attribute or mapping key from ``obj``."""
for name in names:
val = obj.get(name) if isinstance(obj, dict) else getattr(obj, name, None)
if val is not None:
return val
return None
def _values_match(submitted_val: Any, stored_val: Any) -> bool:
"""Compare a submitted variable value with a stored record variable value."""
if isinstance(submitted_val, bool) != isinstance(stored_val, bool):
return str(submitted_val).lower() == str(stored_val).lower()
if submitted_val == stored_val:
return True
if submitted_val is None or stored_val is None:
return False
s_sub = str(submitted_val).strip()
s_sto = str(stored_val).strip()
if s_sub == s_sto:
return True
if not isinstance(submitted_val, bool) and not isinstance(stored_val, bool):
try:
return float(s_sub) == float(s_sto)
except (TypeError, ValueError):
return False
return False
def _record_matches_submitted_item(rec: Any, item: _SubmittedRegistrationItem) -> bool:
"""Return True if ``rec`` matches the form, site, and submitted field values of ``item``."""
if not item.data:
return False
if bool(_get_attr_or_key(rec, "deleted")):
return False
rec_fk = _clean_str(_get_attr_or_key(rec, "form_key", "formKey"))
if item.form_key and rec_fk != item.form_key:
return False
rec_site_id = _clean_int(_get_attr_or_key(rec, "site_id", "siteId"))
if item.site_id is not None and rec_site_id is not None and rec_site_id != item.site_id:
return False
rec_site_name = _clean_str(_get_attr_or_key(rec, "site_name", "siteName"))
if item.site_name and rec_site_name and rec_site_name != item.site_name:
return False
rec_data = _get_attr_or_key(rec, "record_data", "recordData", "data")
if not isinstance(rec_data, dict) or not rec_data:
return False
return all(k in rec_data and _values_match(v, rec_data[k]) for k, v in item.data.items())
def _entry_has_identifier(entry: Any) -> bool:
"""Return True if a job result entry carries any subject or record identifier."""
if isinstance(entry, str):
return bool(entry.strip())
if isinstance(entry, dict):
return any(_get_attr_or_key(entry, field) is not None for field in _ENTRY_ID_FIELDS)
return False
def _filter_non_failed_entries(items: list[Any]) -> list[Any]:
"""Filter out per-record entries marked as failed."""
return [
entry
for entry in items
if not (
isinstance(entry, dict)
and str(entry.get("status") or "").upper() in _FAILED_ENTRY_STATUSES
)
]
def _extract_batch_result_entries(results_payload: Any) -> list[Any]:
"""Extract non-failed per-record result entries from a polled JobStatus.results payload."""
if isinstance(results_payload, list):
return _filter_non_failed_entries(results_payload)
if isinstance(results_payload, dict):
for list_field in ("subjectKeys", "subject_keys", "subjects", "records", "data"):
nested = results_payload.get(list_field)
if isinstance(nested, list):
return _filter_non_failed_entries(nested)
if (
_entry_has_identifier(results_payload)
and str(results_payload.get("status") or "").upper() not in _FAILED_ENTRY_STATUSES
):
return [results_payload]
return []
[docs]class SubmissionPhase(StrEnum):
"""Phases of a two-phase bulk submission workflow."""
PENDING = "pending"
REGISTRATION = "registration"
AWAITING_REGISTRATION = "awaiting_registration"
DATA = "data"
COMPLETED = "completed"
FAILED = "failed"
[docs] def can_transition_to(self, target: SubmissionPhase | str) -> bool:
"""Return True if transitioning from this phase to ``target`` is permitted."""
return is_valid_submission_transition(self, target)
[docs] def validate_transition_to(self, target: SubmissionPhase | str) -> bool:
"""Validate that transitioning from this phase to ``target`` is permitted."""
return validate_submission_transition(self, target)
@property
def allowed_transitions(self) -> set[SubmissionPhase]:
"""Return the set of allowed target phases from this phase."""
return ALLOWED_SUBMISSION_TRANSITIONS.get(self, set())
@property
def is_terminal(self) -> bool:
"""Return True if this submission phase is terminal."""
return self in TERMINAL_SUBMISSION_PHASES
TERMINAL_SUBMISSION_PHASES: set[SubmissionPhase] = {
SubmissionPhase.COMPLETED,
SubmissionPhase.FAILED,
}
ACTIVE_SUBMISSION_PHASES: set[SubmissionPhase] = {
SubmissionPhase.PENDING,
SubmissionPhase.REGISTRATION,
SubmissionPhase.AWAITING_REGISTRATION,
SubmissionPhase.DATA,
}
ALLOWED_SUBMISSION_TRANSITIONS: dict[SubmissionPhase, set[SubmissionPhase]] = {
SubmissionPhase.PENDING: {
SubmissionPhase.REGISTRATION,
SubmissionPhase.FAILED,
},
SubmissionPhase.REGISTRATION: {
SubmissionPhase.AWAITING_REGISTRATION,
SubmissionPhase.DATA,
SubmissionPhase.FAILED,
},
SubmissionPhase.AWAITING_REGISTRATION: {
SubmissionPhase.DATA,
SubmissionPhase.FAILED,
},
SubmissionPhase.DATA: {
SubmissionPhase.COMPLETED,
SubmissionPhase.FAILED,
},
SubmissionPhase.COMPLETED: set(),
SubmissionPhase.FAILED: set(),
}
[docs]class InvalidSubmissionPhaseTransitionError(ValueError):
"""Raised when an invalid bulk submission phase transition is attempted."""
[docs]def normalize_submission_phase(phase: SubmissionPhase | str) -> SubmissionPhase:
"""Normalize a phase value into a ``SubmissionPhase`` enum member."""
if isinstance(phase, SubmissionPhase):
return phase
if isinstance(phase, str):
try:
return SubmissionPhase(phase.strip().lower())
except ValueError as err:
valid = sorted(p.value for p in SubmissionPhase)
raise InvalidSubmissionPhaseTransitionError(
f"Invalid submission phase: '{phase}'. Must be one of: {valid}"
) from err
raise InvalidSubmissionPhaseTransitionError(
f"Invalid submission phase type: {type(phase).__name__}"
)
[docs]def is_valid_submission_transition(
from_phase: SubmissionPhase | str,
to_phase: SubmissionPhase | str,
) -> bool:
"""Return True if transitioning ``from_phase`` to ``to_phase`` is allowed."""
try:
src = normalize_submission_phase(from_phase)
dst = normalize_submission_phase(to_phase)
except InvalidSubmissionPhaseTransitionError:
return False
return dst in ALLOWED_SUBMISSION_TRANSITIONS.get(src, set())
[docs]def validate_submission_transition(
from_phase: SubmissionPhase | str,
to_phase: SubmissionPhase | str,
) -> bool:
"""Validate that ``from_phase`` may transition to ``to_phase``."""
src = normalize_submission_phase(from_phase)
dst = normalize_submission_phase(to_phase)
permitted = ALLOWED_SUBMISSION_TRANSITIONS.get(src, set())
if dst not in permitted:
allowed_values = sorted(p.value for p in permitted)
raise InvalidSubmissionPhaseTransitionError(
f"Cannot transition submission workflow from {src.value} to {dst.value}. "
f"Allowed transitions from {src.value}: {allowed_values}"
)
return True
[docs]class BatchSubmission(ImednetBaseModel):
"""Tracks a single API call to records.create()."""
phase: Literal["registration", "data"] | SubmissionPhase
form_key: str | None = None
form_name: str | None = None
record_count: int
job: Job
submitted_at: datetime = Field(default_factory=lambda: datetime.now(UTC))
[docs]class SubmissionResult(ImednetBaseModel):
"""Aggregate result of a full UAT submission run."""
study_key: str
started_at: datetime
completed_at: datetime | None = None
registration_batches: list[BatchSubmission] = Field(default_factory=list)
data_batches: list[BatchSubmission] = Field(default_factory=list)
enrolled_subject_keys: list[str] = Field(default_factory=list)
skipped_forms: list[str] = Field(default_factory=list)
warnings: list[str] = Field(default_factory=list)
@property
def all_batches(self) -> list[BatchSubmission]:
"""Return all submission batches from both phases."""
return self.registration_batches + self.data_batches
@property
def all_batch_ids(self) -> list[str]:
"""Return all batch IDs from all submitted jobs."""
return [b.job.batch_id for b in self.all_batches if b.job.batch_id]
@property
def total_records_submitted(self) -> int:
"""Return the total count of records submitted across all batches."""
return sum(b.record_count for b in self.all_batches)
[docs]class BulkSubmissionError(Exception):
"""Raised when Phase 1 registration jobs fail, blocking Phase 2 submission."""
[docs] def __init__(self, message: str, failed_batches: list[BatchSubmission]) -> None:
"""Initialize the error.
Args:
message: The error message.
failed_batches: List of batches that failed during submission.
"""
super().__init__(message)
self.failed_batches = failed_batches
async def _await_if_needed(val: Any) -> Any:
return await val if inspect.isawaitable(val) else val
class _PreloadedSdkShim:
def __init__(self, subjects: list[Any] | None, records: list[Any] | None) -> None:
if subjects is not None:
self.get_subjects = lambda *_a, **_kw: subjects
if records is not None:
self.get_records = lambda *_a, **_kw: records
def _callable_accepts_kwarg(fn: Any, kwarg: str) -> bool:
"""Return True if *fn* accepts *kwarg* explicitly or via ``**kwargs``."""
try:
sig = inspect.signature(fn)
except (TypeError, ValueError):
return False
return kwarg in sig.parameters or any(
p.kind == inspect.Parameter.VAR_KEYWORD for p in sig.parameters.values()
)
[docs]class BulkRecordSubmissionWorkflow:
"""Two-phase bulk record submission for UAT scenarios.
Phase 1: Submit all RegisterSubjectRequest payloads and await job completion.
Phase 2: Submit all UpdateScheduledRecordRequest and CreateNewRecordRequest payloads.
Parameters
----------
sdk : ImednetFacade
Initialized synchronous SDK instance.
batch_size : int
Maximum number of records per API call. Default: 100.
await_registration : bool
If True, block until all Phase 1 jobs reach terminal state before
submitting Phase 2 records. Default: True.
registration_timeout : float
Seconds to wait for Phase 1 jobs. Default: 600.
skip_existing_subjects : bool
If True, query the API for existing UAT-tagged subjects and skip
registration for any that already exist. Default: True.
"""
[docs] def __init__(
self,
sdk: ImednetFacade,
batch_size: int = DEFAULT_BATCH_CHUNK_SIZE,
await_registration: bool = True,
registration_timeout: float = 600.0,
skip_existing_subjects: bool = True,
registration_poll_interval: float | None = None,
resolve_results: bool = True,
) -> None:
"""Initialize the workflow."""
self.sdk = sdk
self.batch_size = batch_size
self.await_registration = await_registration
self.registration_timeout = registration_timeout
self.skip_existing_subjects = skip_existing_subjects
self.registration_poll_interval = registration_poll_interval
self.resolve_results = resolve_results
self._phase: SubmissionPhase = SubmissionPhase.PENDING
self._initial_subjects_snapshot: _PreExistingSubjectsSnapshot | None = None
self._ordered_existing_uat_keys: list[str] = []
self._existing_uat_subjects: list[tuple[str, Any]] = []
@property
def phase(self) -> SubmissionPhase:
"""Return the current lifecycle phase of the submission workflow."""
return self._phase
[docs] def transition_to(self, target: SubmissionPhase | str) -> SubmissionPhase:
"""Transition the submission workflow to an approved target phase."""
target_phase = normalize_submission_phase(target)
validate_submission_transition(self._phase, target_phase)
self._phase = target_phase
return self._phase
[docs] def reset(self) -> None:
"""Reset the submission workflow phase back to ``PENDING``."""
self._phase = SubmissionPhase.PENDING
self._initial_subjects_snapshot = None
self._ordered_existing_uat_keys = []
self._existing_uat_subjects = []
[docs] def submit(
self,
study_key: str,
record_sets: list[GeneratedRecordSet],
*,
email_notify: bool | str | None = None,
keyword_tag: str = "UAT",
schema: ValidationSchemaProtocol | None = None,
resolve_results: bool | None = None,
) -> SubmissionResult:
"""Execute the two-phase submission synchronously."""
return run_sync_over_async(
lambda: self._execute_submit_async(
study_key,
record_sets,
email_notify=email_notify,
keyword_tag=keyword_tag,
schema=schema,
resolve_results=resolve_results,
)
)
async def _execute_submit_async(
self,
study_key: str,
record_sets: list[GeneratedRecordSet],
*,
email_notify: bool | str | None = None,
keyword_tag: str = "UAT",
schema: ValidationSchemaProtocol | None = None,
resolve_results: bool | None = None,
) -> SubmissionResult:
"""Execute the two-phase submission.
Returns a SubmissionResult with all jobs and metadata.
Raises BulkSubmissionError if Phase 1 jobs fail and Phase 2 depends on them.
"""
validate_submission_transition(self._phase, SubmissionPhase.REGISTRATION)
result = SubmissionResult(
study_key=study_key,
started_at=datetime.now(UTC),
)
registration_sets = [
rs for rs in record_sets if rs.test_type == RecordTestType.REGISTER_SUBJECT
]
data_sets = [rs for rs in record_sets if rs.test_type != RecordTestType.REGISTER_SUBJECT]
self._initial_subjects_snapshot = None
self._ordered_existing_uat_keys = []
self._existing_uat_subjects = []
existing_subjects: set[str] = set()
try:
ordered_existing_pairs: list[tuple[str, Any]] = []
if self.skip_existing_subjects:
existing_subjects = await _await_if_needed(
self._find_existing_uat_subjects(study_key, keyword_tag)
)
if self._existing_uat_subjects:
ordered_existing_pairs = [
(k, subj)
for k, subj in self._existing_uat_subjects
if k in existing_subjects
]
else:
ordered_keys = [
k for k in self._ordered_existing_uat_keys if k in existing_subjects
]
if len(ordered_keys) < len(existing_subjects):
seen_ord = set(ordered_keys)
ordered_keys.extend(
sorted(k for k in existing_subjects if k not in seen_ord)
)
ordered_existing_pairs = [(k, {"subjectKey": k}) for k in ordered_keys]
pre_existing = _PreExistingSubjectsSnapshot(
subject_keys=set(existing_subjects),
subject_ids=set(),
subject_oids=set(),
)
if self._initial_subjects_snapshot is not None:
pre_existing.subject_keys.update(self._initial_subjects_snapshot.subject_keys)
pre_existing.subject_ids.update(self._initial_subjects_snapshot.subject_ids)
pre_existing.subject_oids.update(self._initial_subjects_snapshot.subject_oids)
elif registration_sets and data_sets:
snap = await _await_if_needed(self._snapshot_existing_subjects(study_key))
pre_existing.subject_keys.update(snap.subject_keys)
pre_existing.subject_ids.update(snap.subject_ids)
pre_existing.subject_oids.update(snap.subject_oids)
reused_key_map: dict[str, str] = {}
if registration_sets and ordered_existing_pairs:
placeholder_order: list[str] = []
placeholder_sites: dict[str, list[tuple[str | None, int | None]]] = {}
for rs in registration_sets:
for i, payload in enumerate(rs.payloads):
sk = rs.subject_keys[i] if i < len(rs.subject_keys) else None
if not isinstance(sk, str) or not sk:
continue
if sk not in placeholder_sites:
placeholder_order.append(sk)
placeholder_sites[sk] = []
p_site_name = _clean_str(_get_attr_or_key(payload, "siteName", "site_name"))
p_site_id = _clean_int(_get_attr_or_key(payload, "siteId", "site_id"))
placeholder_sites[sk].append((p_site_name, p_site_id))
def _candidate_fits_placeholder(cand_subj: Any, ph_key: str) -> bool:
return all(
subject_matches_site(cand_subj, site_name=s_name, site_id=s_id)
for s_name, s_id in placeholder_sites.get(ph_key, [])
)
remaining_pairs = list(ordered_existing_pairs)
for ph in placeholder_order:
for idx_cand, (cand_key, cand_subj) in enumerate(remaining_pairs):
if cand_key == ph and _candidate_fits_placeholder(cand_subj, ph):
reused_key_map[ph] = cand_key
remaining_pairs.pop(idx_cand)
break
for ph in placeholder_order:
if ph in reused_key_map:
continue
for idx_cand, (cand_key, cand_subj) in enumerate(remaining_pairs):
if _candidate_fits_placeholder(cand_subj, ph):
reused_key_map[ph] = cand_key
remaining_pairs.pop(idx_cand)
break
submitted_placeholders: list[str | None] = []
skipped_placeholders: set[str] = set()
batch_item_groups: list[tuple[BatchSubmission, list[_SubmittedRegistrationItem]]] = []
# Phase 1: Registration
self.transition_to(SubmissionPhase.REGISTRATION)
for rs in registration_sets:
filtered_payloads: list[dict[str, Any]] = []
filtered_items: list[_SubmittedRegistrationItem] = []
for i, payload in enumerate(rs.payloads):
subj_key = rs.subject_keys[i] if i < len(rs.subject_keys) else None
if subj_key and subj_key in reused_key_map:
logger.info(
"Skipping registration for placeholder %s (reusing UAT subject %s)",
subj_key,
reused_key_map[subj_key],
)
skipped_placeholders.add(subj_key)
continue
clean_payload = dict(payload)
clean_payload.pop("subjectKey", None)
clean_payload.pop("subject_key", None)
raw_data = (
clean_payload.get("data")
if "data" in clean_payload
else clean_payload.get("recordData")
)
item = _SubmittedRegistrationItem(
placeholder_key=subj_key,
form_key=str(
clean_payload.get("formKey")
or clean_payload.get("form_key")
or rs.form_key
or ""
),
site_name=_clean_str(
_get_attr_or_key(clean_payload, "siteName", "site_name")
),
site_id=_clean_int(_get_attr_or_key(clean_payload, "siteId", "site_id")),
subject_oid=_clean_str(
_get_attr_or_key(clean_payload, "subjectOid", "subject_oid")
),
data=dict(raw_data) if isinstance(raw_data, dict) else {},
)
filtered_payloads.append(clean_payload)
filtered_items.append(item)
submitted_placeholders.append(subj_key)
if not filtered_payloads:
result.skipped_forms.append(rs.form_key)
continue
offset = 0
for chunk in self._chunk_payloads(filtered_payloads):
chunk_items = filtered_items[offset : offset + len(chunk)]
offset += len(chunk)
batch = await _await_if_needed(
self._submit_batch(
study_key,
chunk,
phase="registration",
form_key=rs.form_key,
form_name=rs.form_name,
email_notify=email_notify,
schema=schema,
)
)
for item in chunk_items:
item.batch_id = batch.job.batch_id
result.registration_batches.append(batch)
batch_item_groups.append((batch, chunk_items))
# Determine transition from registration phase
poll_summary: JobPollSummary | None = None
if self.await_registration and result.registration_batches:
self.transition_to(SubmissionPhase.AWAITING_REGISTRATION)
poll_call = (
self._poll_and_validate_registration_batches(
study_key, result.registration_batches
)
if resolve_results is None
else self._poll_and_validate_registration_batches(
study_key,
result.registration_batches,
resolve_results=resolve_results,
)
)
poll_summary = await _await_if_needed(poll_call)
if batch_item_groups:
must_correlate = self._requires_phase2_correlation(data_sets, poll_summary)
enrolled_keys, correlation_error = await _await_if_needed(
self._correlate_enrolled_subjects(
study_key=study_key,
batch_item_groups=batch_item_groups,
summary=poll_summary,
pre_existing=pre_existing,
allow_record_lookup=bool(data_sets),
)
)
if enrolled_keys is not None:
if reused_key_map:
full_placeholders: list[str | None] = [
*reused_key_map.keys(),
*submitted_placeholders,
]
full_enrolled = [*reused_key_map.values(), *enrolled_keys]
result.enrolled_subject_keys = list(
dict.fromkeys([*reused_key_map.values(), *enrolled_keys])
)
SyntheticRecordGenerator.resolve_subject_keys(
record_sets,
full_enrolled,
placeholder_keys=full_placeholders,
)
else:
result.enrolled_subject_keys = list(enrolled_keys)
SyntheticRecordGenerator.resolve_subject_keys(
record_sets,
enrolled_keys,
placeholder_keys=submitted_placeholders,
preserved_keys=skipped_placeholders,
)
elif must_correlate:
raise BulkSubmissionError(
correlation_error
or "Unable to correlate enrolled subjects to this registration batch.",
failed_batches=list(result.registration_batches),
)
self.transition_to(SubmissionPhase.DATA)
else:
if reused_key_map and not result.registration_batches:
result.enrolled_subject_keys = list(dict.fromkeys(reused_key_map.values()))
SyntheticRecordGenerator.resolve_subject_keys(
record_sets,
list(reused_key_map.values()),
placeholder_keys=list(reused_key_map.keys()),
)
self.transition_to(SubmissionPhase.DATA)
if batch_item_groups and self._requires_phase2_correlation(data_sets, poll_summary):
raise BulkSubmissionError(
"Cannot correlate enrolled subjects for Phase 2 records when await_registration is False.",
failed_batches=list(result.registration_batches),
)
# Phase 2: Data Submission
for rs in data_sets:
for chunk in self._chunk_payloads(rs.payloads):
batch = await _await_if_needed(
self._submit_batch(
study_key,
chunk,
phase="data",
form_key=rs.form_key,
form_name=rs.form_name,
email_notify=email_notify,
schema=schema,
)
)
result.data_batches.append(batch)
self.transition_to(SubmissionPhase.COMPLETED)
except InvalidSubmissionPhaseTransitionError:
raise
except Exception:
if not self._phase.is_terminal:
self.transition_to(SubmissionPhase.FAILED)
raise
result.completed_at = datetime.now(UTC)
return result
def _chunk_payloads(self, payloads: list[dict[str, Any]]) -> Iterator[list[dict[str, Any]]]:
"""Yield successive batch_size chunks of payloads."""
yield from chunk_records(payloads, self.batch_size)
def _submit_batch(
self,
study_key: str,
payloads: list[dict[str, Any]],
phase: Literal["registration", "data"] | SubmissionPhase,
form_key: str | None,
form_name: str | None,
email_notify: bool | str | None,
schema: ValidationSchemaProtocol | None = None,
) -> BatchSubmission:
"""Submit a single batch and return a BatchSubmission."""
if schema is not None:
job = self.sdk.create_record(
study_key,
payloads,
email_notify=email_notify,
schema=schema,
)
else:
job = self.sdk.create_record(study_key, payloads, email_notify=email_notify)
return BatchSubmission(
phase=phase,
form_key=form_key,
form_name=form_name,
record_count=len(payloads),
job=job,
)
def _poll_registration_batches(
self,
study_key: str,
batch_ids: list[str],
*,
resolve_results: bool | None = None,
) -> JobPollSummary:
"""Poll Phase 1 batches via facade poll_jobs with mock fallback."""
effective_resolve = self.resolve_results if resolve_results is None else resolve_results
poll_kwargs: dict[str, Any] = {
"timeout": float(self.registration_timeout),
"fail_fast": False,
}
if self.registration_poll_interval is not None:
poll_kwargs["interval"] = float(self.registration_poll_interval)
poll_jobs_fn = getattr(self.sdk, "poll_jobs", None)
if callable(poll_jobs_fn):
facade_kwargs = dict(poll_kwargs)
if effective_resolve and _callable_accepts_kwarg(poll_jobs_fn, "resolve_results"):
facade_kwargs["resolve_results"] = True
candidate = poll_jobs_fn(
study_key,
batch_ids,
**facade_kwargs,
)
if isinstance(candidate, JobPollSummary):
return candidate
jobs_ep = getattr(self.sdk, "jobs", None)
fetch_result_fn = getattr(jobs_ep, "fetch_result", None) if jobs_ep is not None else None
if (
effective_resolve
and callable(fetch_result_fn)
and not _is_unconfigured_mock_method(fetch_result_fn)
):
poller = JobPoller(get_job=self.sdk.get_job, fetch_result=fetch_result_fn)
return poller.poll_many(
study_key,
batch_ids,
**poll_kwargs,
)
poller = JobPoller(get_job=self.sdk.get_job)
return poller.poll_many(
study_key,
batch_ids,
**poll_kwargs,
)
def _poll_and_validate_registration_batches(
self,
study_key: str,
batches: list[BatchSubmission],
*,
resolve_results: bool | None = None,
) -> JobPollSummary | None:
"""Poll Phase 1 batches to terminal state and raise BulkSubmissionError on failure."""
batches_to_poll = [b for b in batches if b.job.batch_id]
if not batches_to_poll:
return None
batch_ids = [b.job.batch_id for b in batches_to_poll if b.job.batch_id is not None]
if resolve_results is None:
summary = self._poll_registration_batches(study_key, batch_ids)
else:
summary = self._poll_registration_batches(
study_key, batch_ids, resolve_results=resolve_results
)
return self._finalize_polled_batches(batches_to_poll, summary)
@staticmethod
def _finalize_polled_batches(
batches_to_poll: list[BatchSubmission],
summary: JobPollSummary,
) -> JobPollSummary:
failed_batches: list[BatchSubmission] = []
for batch in batches_to_poll:
batch_id = batch.job.batch_id
if batch_id in summary.failures:
exc = summary.failures[batch_id]
if isinstance(exc, JobTimeoutError):
target_state = JobState.TIMEOUT
elif isinstance(exc, JobFailedError):
status = getattr(exc, "status", None)
raw_state = getattr(status, "state", None)
status_failed_records = getattr(status, "failed_records", 0)
status_result_err = getattr(status, "result_error", None)
if (
isinstance(status_failed_records, int) and status_failed_records > 0
) or status_result_err:
target_state = JobState.FAILED
else:
target_state = (
normalize_job_state(raw_state) if raw_state else JobState.FAILED
)
else:
batch.job.transition_to(JobState.FAILED)
if not isinstance(batch.job, Job):
batch.job.state = JobState.FAILED
raise exc
batch.job.transition_to(target_state)
if not isinstance(batch.job, Job):
batch.job.state = target_state
failed_batches.append(batch)
elif batch_id in summary.results:
status = summary.results[batch_id]
raw_state = getattr(status, "state", None)
target_state = normalize_job_state(raw_state) if raw_state else JobState.FAILED
batch.job.transition_to(target_state)
if not isinstance(batch.job, Job):
batch.job.state = target_state
status_success = getattr(status, "is_successful", None)
is_success = (
status_success
if isinstance(status_success, bool)
else (
batch.job.is_successful
if isinstance(batch.job.is_successful, bool)
else (
getattr(batch.job, "state", None)
in (
JobState.COMPLETED,
JobState.COMPLETE,
JobState.SUCCESS,
"COMPLETED",
"COMPLETE",
"SUCCESS",
)
)
)
)
if not is_success:
failed_batches.append(batch)
else:
batch.job.transition_to(JobState.FAILED)
if not isinstance(batch.job, Job):
batch.job.state = JobState.FAILED
failed_batches.append(batch)
if failed_batches:
raise BulkSubmissionError(
f"{len(failed_batches)} registration batches failed.",
failed_batches=failed_batches,
)
return summary
@staticmethod
def _extract_snapshot_from_subjects(subjects: Any) -> _PreExistingSubjectsSnapshot:
"""Extract subject keys, IDs, and OIDs from a list of Subject models or dicts."""
keys: set[str] = set()
ids: set[int] = set()
oids: set[str] = set()
if isinstance(subjects, list):
for subj in subjects:
key = _clean_str(_get_attr_or_key(subj, "subject_key", "subjectKey"))
if key:
keys.add(key)
sid = _clean_int(_get_attr_or_key(subj, "subject_id", "subjectId"))
if sid is not None:
ids.add(sid)
subj_oid = _clean_str(_get_attr_or_key(subj, "subject_oid", "subjectOid"))
if subj_oid:
oids.add(subj_oid)
return _PreExistingSubjectsSnapshot(keys, ids, oids)
def _snapshot_existing_subjects(self, study_key: str) -> _PreExistingSubjectsSnapshot:
"""Capture pre-Phase-1 study subject identifiers."""
get_subjects_fn = getattr(self.sdk, "get_subjects", None)
if callable(get_subjects_fn) and not _is_unconfigured_mock_method(get_subjects_fn):
subjects = get_subjects_fn(study_key)
return self._extract_snapshot_from_subjects(subjects)
return _PreExistingSubjectsSnapshot(set(), set(), set())
def _requires_phase2_correlation(
self,
data_sets: list[GeneratedRecordSet],
summary: JobPollSummary | None,
) -> bool:
"""Return True if Phase 2 record submission requires verified Phase 1 subject correlation."""
if not data_sets:
return False
if any(
"subjectKey" in payload or "subject_key" in payload
for rs in data_sets
for payload in rs.payloads
):
return True
if summary is not None and any(
getattr(status, "results", None) is not None for status in summary.results.values()
):
return True
if not _is_unconfigured_mock_method(getattr(self.sdk, "get_subjects", None)):
return True
return not _is_unconfigured_mock_method(getattr(self.sdk, "get_records", None))
def _correlate_enrolled_subjects(
self,
*,
study_key: str,
batch_item_groups: list[tuple[BatchSubmission, list[_SubmittedRegistrationItem]]],
summary: JobPollSummary | None,
pre_existing: _PreExistingSubjectsSnapshot,
allow_record_lookup: bool,
) -> tuple[list[str] | None, str | None]:
"""Correlate submitted registration items to enrolled subject keys, or fail closed."""
total_submitted = sum(len(items) for _, items in batch_item_groups)
if total_submitted == 0:
return [], None
subjects_loaded = False
cached_subjects: list[Any] | None = None
records_loaded = False
cached_records: list[Any] | None = None
def _load_subjects() -> list[Any] | None:
nonlocal subjects_loaded, cached_subjects
if not subjects_loaded:
subjects_loaded = True
fn = getattr(self.sdk, "get_subjects", None)
if callable(fn) and not _is_unconfigured_mock_method(fn):
res = fn(study_key)
if isinstance(res, list):
cached_subjects = res
return cached_subjects
def _load_records() -> list[Any] | None:
nonlocal records_loaded, cached_records
if not records_loaded:
records_loaded = True
fn = getattr(self.sdk, "get_records", None)
if callable(fn) and not _is_unconfigured_mock_method(fn):
res = fn(study_key)
if isinstance(res, list):
cached_records = res
return cached_records
def _subject_key_from_subject_lookup(
subj_id: int | None,
subj_oid: str | None,
item: _SubmittedRegistrationItem | None = None,
) -> str | None:
if subj_id is None and not subj_oid:
return None
if subj_id is not None and subj_id in pre_existing.subject_ids:
return None
if subj_oid and subj_oid in pre_existing.subject_oids:
return None
subjects = _load_subjects()
if not subjects:
return None
matches: list[str] = []
for subj in subjects:
if bool(_get_attr_or_key(subj, "deleted")):
continue
cand_id = _clean_int(_get_attr_or_key(subj, "subject_id", "subjectId"))
cand_oid = _clean_str(_get_attr_or_key(subj, "subject_oid", "subjectOid"))
if subj_id is not None and cand_id != subj_id:
continue
if subj_oid is not None and cand_oid != subj_oid:
continue
s_name = _clean_str(_get_attr_or_key(subj, "site_name", "siteName"))
if item is not None and item.site_name and s_name and s_name != item.site_name:
continue
s_id = _clean_int(_get_attr_or_key(subj, "site_id", "siteId"))
if (
item is not None
and item.site_id is not None
and s_id is not None
and s_id != item.site_id
):
continue
cand_key = _clean_str(_get_attr_or_key(subj, "subject_key", "subjectKey"))
if cand_key and cand_key not in matches:
matches.append(cand_key)
return matches[0] if len(matches) == 1 else None
def _subject_key_from_record_lookup(
rec_id: int | None,
rec_oid: str | None,
item: _SubmittedRegistrationItem,
) -> str | None:
records = _load_records()
if not records:
return None
matches: list[str] = []
for rec in records:
if bool(_get_attr_or_key(rec, "deleted")):
continue
cand_id = _clean_int(_get_attr_or_key(rec, "record_id", "recordId"))
cand_oid = _clean_str(_get_attr_or_key(rec, "record_oid", "recordOid"))
if rec_id is not None and cand_id != rec_id:
continue
if rec_oid is not None and cand_oid != rec_oid:
continue
rec_fk = _clean_str(_get_attr_or_key(rec, "form_key", "formKey"))
if item.form_key and rec_fk and rec_fk != item.form_key:
continue
rec_data = _get_attr_or_key(rec, "record_data", "recordData", "data")
if (
item.data
and isinstance(rec_data, dict)
and rec_data
and not _record_matches_submitted_item(rec, item)
):
continue
cand_key = _clean_str(_get_attr_or_key(rec, "subject_key", "subjectKey"))
if not cand_key:
cand_key = _subject_key_from_subject_lookup(
_clean_int(_get_attr_or_key(rec, "subject_id", "subjectId")),
_clean_str(_get_attr_or_key(rec, "subject_oid", "subjectOid")),
item,
)
if cand_key and cand_key not in matches:
matches.append(cand_key)
return matches[0] if len(matches) == 1 else None
resolved_by_index: dict[int, str] = {}
assigned_keys: set[str] = set()
unresolved: list[tuple[int, _SubmittedRegistrationItem]] = []
global_idx = 0
for batch, items in batch_item_groups:
batch_indices = list(range(global_idx, global_idx + len(items)))
global_idx += len(items)
batch_id = batch.job.batch_id
status = summary.results.get(batch_id) if (summary and batch_id) else None
entries = (
_extract_batch_result_entries(getattr(status, "results", None))
if status is not None
else []
)
if not entries or not any(_entry_has_identifier(e) for e in entries):
for idx, item in zip(batch_indices, items, strict=False):
unresolved.append((idx, item))
continue
if len(entries) != len(items):
return (
None,
f"Registration batch '{batch_id}' returned {len(entries)} result entries "
f"for {len(items)} submitted subject(s).",
)
for idx, item, entry in zip(batch_indices, items, entries, strict=False):
if not _entry_has_identifier(entry):
unresolved.append((idx, item))
continue
subj_key: str | None = None
if isinstance(entry, str):
subj_key = _clean_str(entry)
elif isinstance(entry, dict):
entry_fk = _clean_str(_get_attr_or_key(entry, "formKey", "form_key"))
if entry_fk and item.form_key and entry_fk != item.form_key:
return (
None,
f"Registration batch '{batch_id}' result formKey '{entry_fk}' "
f"did not match submitted form '{item.form_key}'.",
)
subj_key = _clean_str(_get_attr_or_key(entry, "subjectKey", "subject_key"))
if not subj_key:
rec_id = _clean_int(_get_attr_or_key(entry, "recordId", "record_id"))
rec_oid = _clean_str(_get_attr_or_key(entry, "recordOid", "record_oid"))
if rec_id is not None or rec_oid is not None:
subj_key = _subject_key_from_record_lookup(rec_id, rec_oid, item)
if not subj_key:
subj_id = _clean_int(_get_attr_or_key(entry, "subjectId", "subject_id"))
subj_oid = _clean_str(_get_attr_or_key(entry, "subjectOid", "subject_oid"))
if subj_id is not None or subj_oid is not None:
subj_key = _subject_key_from_subject_lookup(subj_id, subj_oid, item)
if not subj_key:
return (
None,
f"Unable to correlate registration result entry in batch '{batch_id}' "
"to an enrolled subject key.",
)
if subj_key in pre_existing.subject_keys:
return (
None,
f"Registration batch '{batch_id}' resolved to pre-existing subject "
f"'{subj_key}'.",
)
if subj_key in assigned_keys:
return (
None,
f"Ambiguous duplicate subject key '{subj_key}' in registration results.",
)
resolved_by_index[idx] = subj_key
assigned_keys.add(subj_key)
if unresolved:
if not allow_record_lookup:
return None, "Registration results omitted subject keys."
remaining: list[tuple[int, _SubmittedRegistrationItem]] = []
for idx, item in unresolved:
if item.subject_oid:
oid_match = _subject_key_from_subject_lookup(None, item.subject_oid, item)
if (
oid_match
and oid_match not in pre_existing.subject_keys
and oid_match not in assigned_keys
):
resolved_by_index[idx] = oid_match
assigned_keys.add(oid_match)
continue
remaining.append((idx, item))
if remaining:
if any(not item.data for _, item in remaining):
return (
None,
"Registration results omitted subject keys and submitted registration "
"payload lacks distinguishing data to correlate batch ownership.",
)
records = _load_records()
if not records:
return (
None,
"Registration results omitted subject keys and study records are "
"unavailable to correlate enrolled subjects to this batch.",
)
subjects = _load_subjects()
subjects_by_key: dict[str, Any] = {}
if subjects is not None:
for subj in subjects:
sk = _clean_str(_get_attr_or_key(subj, "subject_key", "subjectKey"))
if sk:
subjects_by_key[sk] = subj
candidate_records: list[tuple[Any, str]] = []
for rec in records:
if bool(_get_attr_or_key(rec, "deleted")):
continue
sk = _clean_str(_get_attr_or_key(rec, "subject_key", "subjectKey"))
if not sk:
sk = _subject_key_from_subject_lookup(
_clean_int(_get_attr_or_key(rec, "subject_id", "subjectId")),
_clean_str(_get_attr_or_key(rec, "subject_oid", "subjectOid")),
)
if not sk or sk in pre_existing.subject_keys or sk in assigned_keys:
continue
if sk in subjects_by_key:
subj_obj = subjects_by_key[sk]
if bool(_get_attr_or_key(subj_obj, "deleted")):
continue
sid = _clean_int(_get_attr_or_key(subj_obj, "subject_id", "subjectId"))
if sid is not None and sid in pre_existing.subject_ids:
continue
subj_oid = _clean_str(
_get_attr_or_key(subj_obj, "subject_oid", "subjectOid")
)
if subj_oid and subj_oid in pre_existing.subject_oids:
continue
candidate_records.append((rec, sk))
groups: dict[
tuple[str, str, int | None, tuple[tuple[str, str, str], ...]],
list[tuple[int, _SubmittedRegistrationItem]],
] = {}
for idx, item in remaining:
sig = (
item.form_key,
item.site_name or "",
item.site_id,
tuple(sorted((k, type(v).__name__, str(v)) for k, v in item.data.items())),
)
groups.setdefault(sig, []).append((idx, item))
tier2_assigned: set[str] = set()
for group in groups.values():
sample = group[0][1]
matched_keys: list[str] = []
for rec, sk in candidate_records:
if not _record_matches_submitted_item(rec, sample):
continue
if sk in subjects_by_key:
subj_obj = subjects_by_key[sk]
s_name = _clean_str(_get_attr_or_key(subj_obj, "site_name", "siteName"))
if sample.site_name and s_name and s_name != sample.site_name:
continue
s_id = _clean_int(_get_attr_or_key(subj_obj, "site_id", "siteId"))
if (
sample.site_id is not None
and s_id is not None
and s_id != sample.site_id
):
continue
if sk not in matched_keys:
matched_keys.append(sk)
expected_k = len(group)
if expected_k > 1:
return (
None,
f"Ambiguous enrolled subject correlation for form '{sample.form_key}': "
f"{expected_k} submitted registration payloads are indistinguishable "
"without batch result identifiers.",
)
if len(matched_keys) == 0:
return (
None,
f"Unable to correlate enrolled subject for form '{sample.form_key}': "
"no matching enrollment record found for this batch.",
)
if len(matched_keys) != 1:
return (
None,
f"Ambiguous enrolled subject correlation for form '{sample.form_key}': "
f"expected 1 matching subject, found {len(matched_keys)}.",
)
if not set(matched_keys).isdisjoint(tier2_assigned):
return (
None,
f"Ambiguous overlapping subject correlation across registration "
f"payloads for form '{sample.form_key}'.",
)
for (idx, _), sk in zip(group, matched_keys, strict=False):
resolved_by_index[idx] = sk
tier2_assigned.add(sk)
assigned_keys.add(sk)
ordered_keys = [
resolved_by_index[idx] for idx in range(total_submitted) if idx in resolved_by_index
]
if len(ordered_keys) != total_submitted or len(set(ordered_keys)) != total_submitted:
return None, "Incomplete or ambiguous enrolled subject correlation for Phase 1 batch."
return ordered_keys, None
@staticmethod
def _subject_has_keyword(subj: Any, keyword_tag: str) -> bool:
"""Return True if the subject has a keyword matching keyword_tag."""
return subject_has_keyword(subj, keyword_tag)
def _find_existing_uat_subjects(
self,
study_key: str,
keyword_tag: str,
*,
site_name: str | None = None,
site_id: int | None = None,
) -> set[str]:
"""Query non-deleted subjects and inspect Subject keywords/sites locally for UAT tags."""
subjects = self.sdk.get_subjects(study_key, deleted=False)
self._initial_subjects_snapshot = self._extract_snapshot_from_subjects(subjects)
matched = iter_matching_uat_subjects(
subjects,
keyword_tag=keyword_tag,
require_keyword=bool(keyword_tag),
site_name=site_name,
site_id=site_id,
)
self._existing_uat_subjects = matched
self._ordered_existing_uat_keys = [key for key, _ in matched]
return set(self._ordered_existing_uat_keys)
[docs]class AsyncBulkRecordSubmissionWorkflow(BulkRecordSubmissionWorkflow):
"""Asynchronous two-phase bulk record submission for UAT scenarios."""
supports_async = True
sdk: AsyncImednetFacade # type: ignore[assignment]
[docs] def __init__(
self,
sdk: AsyncImednetFacade,
batch_size: int = DEFAULT_BATCH_CHUNK_SIZE,
await_registration: bool = True,
registration_timeout: float = 600.0,
skip_existing_subjects: bool = True,
registration_poll_interval: float | None = None,
resolve_results: bool = True,
) -> None:
"""Initialize the async bulk record submission workflow."""
super().__init__(
sdk, # type: ignore[arg-type]
batch_size=batch_size,
await_registration=await_registration,
registration_timeout=registration_timeout,
skip_existing_subjects=skip_existing_subjects,
registration_poll_interval=registration_poll_interval,
resolve_results=resolve_results,
)
def _has_sdk_method(self, canonical_name: str) -> bool:
legacy_name = f"async_{canonical_name}"
inst = getattr(self.sdk, "__dict__", {})
if isinstance(inst, dict) and legacy_name in inst:
return not _is_unconfigured_mock_method(inst[legacy_name])
if not _is_unconfigured_mock_method(getattr(self.sdk, canonical_name, None)):
return True
return not _is_unconfigured_mock_method(getattr(self.sdk, legacy_name, None))
def _requires_phase2_correlation(
self, data_sets: list[GeneratedRecordSet], summary: JobPollSummary | None
) -> bool:
if not data_sets:
return False
if any("subjectKey" in p or "subject_key" in p for rs in data_sets for p in rs.payloads):
return True
if summary is not None and any(
getattr(st, "results", None) is not None for st in summary.results.values()
):
return True
return self._has_sdk_method("get_subjects") or self._has_sdk_method("get_records")
[docs] async def submit( # type: ignore[override]
self,
study_key: str,
record_sets: list[GeneratedRecordSet],
*,
email_notify: bool | str | None = None,
keyword_tag: str = "UAT",
schema: ValidationSchemaProtocol | None = None,
resolve_results: bool | None = None,
) -> SubmissionResult:
"""Asynchronously execute the two-phase submission."""
return await self._execute_submit_async(
study_key,
record_sets,
email_notify=email_notify,
keyword_tag=keyword_tag,
schema=schema,
resolve_results=resolve_results,
)
async def _submit_batch( # type: ignore[override]
self,
study_key: str,
payloads: list[dict[str, Any]],
phase: Literal["registration", "data"] | SubmissionPhase,
form_key: str | None,
form_name: str | None,
email_notify: bool | str | None,
schema: ValidationSchemaProtocol | None = None,
) -> BatchSubmission:
kwargs: dict[str, Any] = {"email_notify": email_notify}
if schema is not None:
kwargs["schema"] = schema
job = await call_async_facade_method(
self.sdk, "create_record", study_key, payloads, **kwargs
)
return BatchSubmission(
phase=phase,
form_key=form_key,
form_name=form_name,
record_count=len(payloads),
job=job,
)
async def _poll_registration_batches( # type: ignore[override]
self,
study_key: str,
batch_ids: list[str],
*,
resolve_results: bool | None = None,
) -> JobPollSummary:
effective_resolve = self.resolve_results if resolve_results is None else resolve_results
poll_kwargs: dict[str, Any] = {
"timeout": float(self.registration_timeout),
"fail_fast": False,
}
if self.registration_poll_interval is not None:
poll_kwargs["interval"] = float(self.registration_poll_interval)
if self._has_sdk_method("poll_jobs"):
facade_kwargs = dict(poll_kwargs)
target_poll = getattr(self.sdk, "poll_jobs", None) or getattr(
self.sdk, "async_poll_jobs", None
)
if effective_resolve and _callable_accepts_kwarg(target_poll, "resolve_results"):
facade_kwargs["resolve_results"] = True
candidate = await call_async_facade_method(
self.sdk, "poll_jobs", study_key, batch_ids, **facade_kwargs
)
if isinstance(candidate, JobPollSummary):
return candidate
jobs_ep = getattr(self.sdk, "jobs", None)
fetch_result_fn = getattr(jobs_ep, "fetch_result", None) if jobs_ep is not None else None
if (
effective_resolve
and callable(fetch_result_fn)
and not _is_unconfigured_mock_method(fetch_result_fn)
):
poller = AsyncJobPoller(
get_job=functools.partial(call_async_facade_method, self.sdk, "get_job"),
fetch_result=fetch_result_fn,
)
return await poller.async_poll_many(study_key, batch_ids, **poll_kwargs)
poller = AsyncJobPoller(
get_job=functools.partial(call_async_facade_method, self.sdk, "get_job")
)
return await poller.async_poll_many(study_key, batch_ids, **poll_kwargs)
async def _poll_and_validate_registration_batches( # type: ignore[override]
self,
study_key: str,
batches: list[BatchSubmission],
*,
resolve_results: bool | None = None,
) -> JobPollSummary | None:
batches_to_poll = [b for b in batches if b.job.batch_id]
if not batches_to_poll:
return None
batch_ids = [b.job.batch_id for b in batches_to_poll if b.job.batch_id is not None]
if resolve_results is None:
summary = await _await_if_needed(self._poll_registration_batches(study_key, batch_ids))
else:
summary = await _await_if_needed(
self._poll_registration_batches(
study_key, batch_ids, resolve_results=resolve_results
)
)
return self._finalize_polled_batches(batches_to_poll, summary)
async def _snapshot_existing_subjects( # type: ignore[override]
self, study_key: str
) -> _PreExistingSubjectsSnapshot:
if self._has_sdk_method("get_subjects"):
subjects = await call_async_facade_method(self.sdk, "get_subjects", study_key)
return self._extract_snapshot_from_subjects(subjects)
return _PreExistingSubjectsSnapshot(set(), set(), set())
async def _correlate_enrolled_subjects( # type: ignore[override]
self,
*,
study_key: str,
batch_item_groups: list[tuple[BatchSubmission, list[_SubmittedRegistrationItem]]],
summary: JobPollSummary | None,
pre_existing: _PreExistingSubjectsSnapshot,
allow_record_lookup: bool,
) -> tuple[list[str] | None, str | None]:
subjects: list[Any] | None = None
records: list[Any] | None = None
if self._has_sdk_method("get_subjects"):
res_s = await call_async_facade_method(self.sdk, "get_subjects", study_key)
if isinstance(res_s, list):
subjects = res_s
if allow_record_lookup and self._has_sdk_method("get_records"):
res_r = await call_async_facade_method(self.sdk, "get_records", study_key)
if isinstance(res_r, list):
records = res_r
orig_sdk = self.sdk
try:
self.sdk = _PreloadedSdkShim(subjects, records) # type: ignore[assignment]
return super()._correlate_enrolled_subjects(
study_key=study_key,
batch_item_groups=batch_item_groups,
summary=summary,
pre_existing=pre_existing,
allow_record_lookup=allow_record_lookup,
)
finally:
self.sdk = orig_sdk
async def _find_existing_uat_subjects( # type: ignore[override]
self,
study_key: str,
keyword_tag: str,
*,
site_name: str | None = None,
site_id: int | None = None,
) -> set[str]:
subjects = await call_async_facade_method(
self.sdk, "get_subjects", study_key, deleted=False
)
self._initial_subjects_snapshot = self._extract_snapshot_from_subjects(subjects)
matched = iter_matching_uat_subjects(
subjects,
keyword_tag=keyword_tag,
require_keyword=bool(keyword_tag),
site_name=site_name,
site_id=site_id,
)
self._existing_uat_subjects = matched
self._ordered_existing_uat_keys = [key for key, _ in matched]
return set(self._ordered_existing_uat_keys)
__all__ = [
"ACTIVE_SUBMISSION_PHASES",
"ALLOWED_SUBMISSION_TRANSITIONS",
"TERMINAL_SUBMISSION_PHASES",
"AsyncBulkRecordSubmissionWorkflow",
"BatchSubmission",
"BulkRecordSubmissionWorkflow",
"BulkSubmissionError",
"InvalidSubmissionPhaseTransitionError",
"SubmissionPhase",
"SubmissionResult",
"is_valid_submission_transition",
"normalize_submission_phase",
"validate_submission_transition",
]