Workflow Interactions

The helpers in imednet_workflows combine multiple endpoint calls to automate common tasks. The diagrams below outline the main steps in each workflow.

Data Extraction

DataExtractionWorkflow coordinates cross-endpoint filtering across SubjectsEndpoint, VisitsEndpoint, RecordsEndpoint, and RecordRevisionsEndpoint so callers can extract targeted Record cohorts or 21 CFR Part 11 audit trails without manually joining identifiers across paginated responses. Pair extracted Record lists with RecordMapper (sdk.workflows.record_mapper) to project Variable columns into analytical DataFrames.

graph TD accTitle: Criteria-Based Record Extraction Workflow accDescr: Flowchart showing extract_records_by_criteria fetching subjects, visits, and records in parallel to apply filters and return matching records A[extract_records_by_criteria] --> B[subjects.list] A --> C[visits.list] A --> D[records.list] B --> E{subject keys} C --> F{visit ids} D --> G{apply filters} E --> G F --> G G --> H[return records]
from typing import Any, cast
from imednet import ImednetSDK
from imednet_workflows.data_extraction import DataExtractionWorkflow

ext_sdk = ImednetSDK(simulation_mode=True)
extractor = DataExtractionWorkflow(cast(Any, ext_sdk))
matched_records = extractor.extract_records_by_criteria(
    study_key="STUDY",
    record_filter={"form_key": "F_VS"},
    subject_filter={"site_name": "SITE"},
)
assert len(matched_records) >= 1

Record Update

RecordUpdateWorkflow validates Form and Variable schemas once against SchemaValidator before submitting chunked Record payloads through RecordsEndpoint and polling asynchronous Job batches via JobsEndpoint. Use create_or_update_records_batch (or await async_sdk.workflows.record_update.create_or_update_records_batch(...) on AsyncImednetSDK) when submitting multi-chunk updates and inspecting BatchSubmissionSummary.

graph TD accTitle: Batch Record Creation and Optional Job Polling Flow accDescr: Flowchart showing create_or_update_records validating batches, calling records.create, and optionally polling via JobPoller.run A[create_or_update_records] --> B[validate_batch] B --> C[records.create] C --> D{wait?} D -- Yes --> E[JobPoller.run] D -- No --> F[return Job] E --> F
from typing import Any, cast
from imednet import ImednetSDK
from imednet.simulation import reset_default_simulation_state
from imednet_workflows.record_update import RecordUpdateWorkflow

upd_sdk = ImednetSDK(simulation_mode=True)
updater = RecordUpdateWorkflow(cast(Any, upd_sdk))
update_summary = updater.create_or_update_records_batch(
    study_key="STUDY",
    records_data=[
        {
            "formKey": "F_VS",
            "siteName": "SITE",
            "subjectKey": "SUB1",
            "intervalName": "Baseline",
            "data": {"V_WEIGHT": 73.0},
        }
    ],
    wait_for_completion=True,
    poll_interval=1,
)
assert update_summary.all_successful is True
reset_default_simulation_state()

Subject Data

SubjectDataWorkflow aggregates a single Subject dossier across SubjectsEndpoint, VisitsEndpoint, RecordsEndpoint (excluding voided deleted=True items unless include_deleted=True), and QueriesEndpoint into one typed SubjectComprehensiveData container.

graph TD accTitle: Comprehensive Subject Data Aggregation Workflow accDescr: Flowchart showing get_all_subject_data aggregating subjects, visits, records, and queries into SubjectComprehensiveData A[get_all_subject_data] --> B[subjects.list] A --> C[visits.list] A --> D[records.list] A --> E[queries.list] B --> F[aggregate] C --> F D --> F E --> F F --> G[return SubjectComprehensiveData]
from typing import Any, cast
from imednet import ImednetSDK
from imednet_workflows.subject_data import SubjectDataWorkflow

subj_sdk = ImednetSDK(simulation_mode=True)
subject_workflow = SubjectDataWorkflow(cast(Any, subj_sdk))
dossier = subject_workflow.get_all_subject_data("STUDY", "SUB1")
assert dossier.subject_details is not None

Query Management

QueryManagementWorkflow wraps QueriesEndpoint and SubjectsEndpoint to filter open Query items, resolve Site-scoped Query queues via enrolled Subject keys, and compute case-insensitive QueryStateCounts summaries across active and closed states.

graph TD accTitle: Open Clinical Query Filtering Workflow accDescr: Flowchart showing get_open_queries iterating over queries and inspecting the latest comment closed flag to return open queries A[get_open_queries] --> B[queries.list] B --> C{Iterate Queries} C --> D{Check latest comment} D -- closed=False --> E[Add to Open list] D -- closed=True --> F[Skip] E --> G[Return open queries]
from typing import Any, cast
from imednet import ImednetSDK
from imednet_workflows.query_management import QueryManagementWorkflow

qry_sdk = ImednetSDK(simulation_mode=True)
query_workflow = QueryManagementWorkflow(cast(Any, qry_sdk))
open_queries = query_workflow.get_open_queries("STUDY")
state_counts = query_workflow.get_query_state_counts("STUDY")
assert len(open_queries) == state_counts["Open"]

Background Sync Worker

SyncWorker runs incremental record synchronisation in a background thread so that the main application thread (e.g. a CLI or TUI event loop) never blocks on API calls.

graph TD accTitle: Epoch-Fenced Delta Sync and SyncWorker Health Lifecycle accDescr: Flowchart showing SyncWorker running lock-free remote delta, projection, and missing-record fetches before a single atomic FileLock SQLite commit for upserts, epoch-fenced orphan reconciliation, and watermark updates across RUNNING, DEGRADED, FAILED, and STOPPED states A[run_forever] --> B{stop_event set?} B -- No --> C[_fetch_delta_records] C --> D[_fetch_active_records_projection] D --> E[_fetch_missing_records] E --> F["FileLock: atomic _upsert_records_unlocked + reconcile_cache + watermark"] F --> H{cycle succeeded?} H -- Yes --> I[state = RUNNING] I --> M[wait interval_seconds] H -- No --> J{failure limit reached?} J -- No --> K[state = DEGRADED] K --> M M --> B J -- Yes --> L[state = FAILED] B -- Yes --> N[state = STOPPED]

Concurrency and epoch-fencing model

SQLite is opened in WAL mode (PRAGMA journal_mode=WAL) so multiple reader connections can query record_cache while a single writer commits changes. Both record_cache and study_sync_state are partitioned by normalized (base_url, study_key) keys (migrating pre-existing unpartitioned caches in place) so training and production SDK instances can safely share a cache directory without cross-origin contamination. CachedRecordsLoader executes all remote SDK HTTP calls (active-only initial loads, incremental delta fetches, lightweight metadata projections, and missing or stale record backfills) completely outside filelock.FileLock so network latency never blocks readers or concurrent writers.

Write transactions acquire filelock.FileLock once after remote calls finish for a single atomic SQLite commit:

  • Active delta and backfill batches increment the per-origin, per-study sync_epoch in study_sync_state and stamp updated rows with upserted_epoch (storing date_modified=None as SQL NULL), while deleting any newly voided Record IDs without persisting deleted=True payloads.

  • During reconciliation, sync_records captures the pre-reconcile sync_epoch before fetching upstream active metadata projections, backfills missing or newly completed Record payloads, and fences reconcile_cache deletions to upserted_epoch <= max_epoch so concurrent RecordMapper, SchemaProfiler, or SyncWorker writes are never pruned by a stale snapshot.

  • study_sync_state advances high_water_mark monotonically in the same atomic commit only after delta, backfill, and reconciliation succeed, rolling back partial changes and preserving the previous watermark whenever a step fails.

Worker lifecycle and health states

SyncWorker leaves loader.lock.timeout unchanged and tracks health across SyncWorkerState values (IDLE, RUNNING, DEGRADED, STOPPED, FAILED), exposing consecutive_failures, last_error, on_error callbacks, and max_consecutive_failures to stop automatically during persistent outages. Permanent client API errors (HTTP 400, 401, 403, 404) transition the worker immediately into terminal FAILED state without retrying.

Usage example

from imednet import ImednetSDK
from imednet_workflows.cached_loader import CachedRecordsLoader
from imednet_workflows.sync_worker import (
    SyncWorker,
    SyncWorkerConfig,
    SyncWorkerState,
)
import threading

sdk = ImednetSDK(api_key="...", security_key="...")
from typing import Any, cast
loader = CachedRecordsLoader(cast(Any, sdk))
worker = SyncWorker(
    loader,
    config=SyncWorkerConfig(
        study_key="PROT-01",
        interval_seconds=900,
        max_consecutive_failures=5,
    ),
    on_error=lambda exc, failures: None,
)

# Start the worker in a daemon thread so it stops when the main process exits.
t = threading.Thread(target=worker.run_forever, daemon=True)
t.start()

# Your application reads from the cache and inspects worker health without blocking.
records = loader.get_cached_records("PROT-01")
is_healthy = worker.state in (SyncWorkerState.IDLE, SyncWorkerState.RUNNING)

worker.stop()
t.join()