"""In-memory fixture state for offline simulation mode."""
from __future__ import annotations
import copy
import threading
from typing import Any
from imednet.simulation._fixtures import build_initial_fixtures
from imednet.simulation._jobs import _SimulationJobsMixin
from imednet.simulation._records import _SimulationRecordsMixin
from imednet.simulation._synthesis import _SimulationSynthesisMixin
[docs]class SimulationState(_SimulationSynthesisMixin, _SimulationRecordsMixin, _SimulationJobsMixin):
"""Thread-safe in-memory store representing simulated EDC server state."""
[docs] def __init__(self) -> None:
"""Initialize simulation state with deterministic fixtures."""
self._lock = threading.Lock()
self.reset()
[docs] def reset(self) -> None:
"""Reset all in-memory fixtures to clean initial state."""
with getattr(self, "_lock", threading.Lock()):
self._next_subject_id: int = 202
self._next_record_id: int = 1000
self._next_revision_id: int = 1203
self._next_batch_num: int = 1
self._job_poll_counts: dict[str, int] = {}
self._study_job_poll_counts: dict[tuple[str, str], int] = {}
self._study_jobs: dict[tuple[str, str], tuple[int, dict[str, Any]]] = {}
fixtures = build_initial_fixtures()
self.studies: list[dict[str, Any]] = fixtures["studies"]
self.sites: list[dict[str, Any]] = fixtures["sites"]
self.subjects: list[dict[str, Any]] = fixtures["subjects"]
self.forms: list[dict[str, Any]] = fixtures["forms"]
self.visits: list[dict[str, Any]] = fixtures["visits"]
self.records: list[dict[str, Any]] = fixtures["records"]
self.queries: list[dict[str, Any]] = fixtures["queries"]
self.intervals: list[dict[str, Any]] = fixtures["intervals"]
self.variables: list[dict[str, Any]] = fixtures["variables"]
self.users: list[dict[str, Any]] = fixtures["users"]
self.codings: list[dict[str, Any]] = fixtures["codings"]
self.record_revisions: list[dict[str, Any]] = fixtures["record_revisions"]
self.jobs: dict[str, dict[str, Any]] = fixtures["jobs"]
[docs] def get_studies(self) -> list[dict[str, Any]]:
"""Return list of simulated studies."""
with self._lock:
return copy.deepcopy(self.studies)
[docs] def get_sites(self, study_key: str | None = None) -> list[dict[str, Any]]:
"""Return list of sites, ensuring sites exist for the requested study."""
with self._lock:
if not study_key:
return copy.deepcopy(self.sites)
return self._synthesize_sites_unlocked(study_key)
[docs] def get_subjects(self, study_key: str | None = None) -> list[dict[str, Any]]:
"""Return list of subjects for a study."""
with self._lock:
if not study_key:
return copy.deepcopy(self.subjects)
return self._synthesize_subjects_unlocked(study_key)
[docs] def get_visits(self, study_key: str | None = None) -> list[dict[str, Any]]:
"""Return list of visits for a study."""
with self._lock:
if not study_key:
return copy.deepcopy(self.visits)
return self._synthesize_visits_unlocked(study_key)
[docs] def get_records(self, study_key: str | None = None) -> list[dict[str, Any]]:
"""Return list of records for a study."""
with self._lock:
if not study_key:
return copy.deepcopy(self.records)
return self._synthesize_records_unlocked(study_key)
[docs] def get_queries(self, study_key: str | None = None) -> list[dict[str, Any]]:
"""Return list of queries for a study."""
with self._lock:
if not study_key:
return copy.deepcopy(self.queries)
return self._synthesize_queries_unlocked(study_key)
[docs] def get_intervals(self, study_key: str | None = None) -> list[dict[str, Any]]:
"""Return list of intervals for a study."""
with self._lock:
intervals = copy.deepcopy(self.intervals)
if not study_key:
return intervals
return [dict(i, studyKey=study_key) for i in intervals]
[docs] def get_variables(self, study_key: str | None = None) -> list[dict[str, Any]]:
"""Return list of variables for a study."""
with self._lock:
variables = copy.deepcopy(self.variables)
if not study_key:
return variables
return [dict(v, studyKey=study_key) for v in variables]
[docs] def get_codings(self, study_key: str | None = None) -> list[dict[str, Any]]:
"""Return list of medical codings for a study."""
with self._lock:
if not study_key:
return copy.deepcopy(self.codings)
return self._synthesize_codings_unlocked(study_key)
[docs] def get_record_revisions(self, study_key: str | None = None) -> list[dict[str, Any]]:
"""Return list of record audit revisions for a study."""
with self._lock:
if not study_key:
return copy.deepcopy(self.record_revisions)
return self._synthesize_record_revisions_unlocked(study_key)
[docs] def get_users(self, study_key: str | None = None) -> list[dict[str, Any]]:
"""Return list of users for a study."""
with self._lock:
users = copy.deepcopy(self.users)
if not study_key:
return users
return [u for u in users if "studyKey" not in u or u.get("studyKey") == study_key]
_default_state: SimulationState | None = None
_state_lock = threading.Lock()
[docs]def get_default_simulation_state() -> SimulationState:
"""Return the global default simulation state instance."""
global _default_state
with _state_lock:
if _default_state is None:
_default_state = SimulationState()
return _default_state
[docs]def reset_default_simulation_state() -> None:
"""Reset the global default simulation state."""
global _default_state
with _state_lock:
if _default_state is not None:
_default_state.reset()
else:
_default_state = SimulationState()