Class ObservationService
- Namespace
- adas_core.Application.Services
- Assembly
- adas-core.Application.dll
Provides the concrete implementation of the IObservationService contract, encapsulating the business logic required to manage and expose observation-related operations.
public class ObservationService : IObservationService, IApiRequestService
- Inheritance
-
ObservationService
- Implements
- Inherited Members
- Extension Methods
Constructors
ObservationService(IPatientService, IConfigObservationService, IObservationRepository, IObservationArchiveRepository, IConfigUnitsService, IDiagnosisService, IOptions<ApiSettings>, IOptions<CacheSettings>, ILightBeaconService, IRelayService, IRecordingService, ILogger<ObservationService>, IGroupedObservationService, IAlarmService, IClientMessageService, ISubscribersService, ISubscriberGroupedService, Lazy<ICalculatedObservationsService>, IHttpContextAccessor, ILocalAuditService, IPointOfCareService, ICacheService)
public ObservationService(IPatientService patientService, IConfigObservationService configObservationService, IObservationRepository observationRepository, IObservationArchiveRepository observationArchiveRepository, IConfigUnitsService configUnitsService, IDiagnosisService diagnosisService, IOptions<ApiSettings> apiSettings, IOptions<CacheSettings> cacheSettings, ILightBeaconService lightBeaconService, IRelayService relayService, IRecordingService recordingService, ILogger<ObservationService> logger, IGroupedObservationService groupedObservationService, IAlarmService alarmService, IClientMessageService clientMessageService, ISubscribersService subscribersService, ISubscriberGroupedService subscriberGroupedService, Lazy<ICalculatedObservationsService> calculatedObservationsService, IHttpContextAccessor httpContextAccessor, ILocalAuditService auditService, IPointOfCareService pointOfCareService, ICacheService cacheService)
Parameters
patientServiceIPatientServiceconfigObservationServiceIConfigObservationServiceobservationRepositoryIObservationRepositoryobservationArchiveRepositoryIObservationArchiveRepositoryconfigUnitsServiceIConfigUnitsServicediagnosisServiceIDiagnosisServiceapiSettingsIOptions<ApiSettings>cacheSettingsIOptions<CacheSettings>lightBeaconServiceILightBeaconServicerelayServiceIRelayServicerecordingServiceIRecordingServiceloggerILogger<ObservationService>groupedObservationServiceIGroupedObservationServicealarmServiceIAlarmServiceclientMessageServiceIClientMessageServicesubscribersServiceISubscribersServicesubscriberGroupedServiceISubscriberGroupedServicecalculatedObservationsServiceLazy<ICalculatedObservationsService>httpContextAccessorIHttpContextAccessorauditServiceILocalAuditServicepointOfCareServiceIPointOfCareServicecacheServiceICacheService
Methods
Archive(Patient)
Archives the specified patient by delegating to the archive operation keyed by the patient's identifier.
public Task Archive(Patient patient)
Parameters
patientPatientThe patient to be archived. Its identifier is used to locate and archive the corresponding record.
Returns
Archive(PatientObservation)
Archives a patient observation by persisting it to the archive repository, removing it from the active observations, and invalidating the related cache entries for the patient's latest observations.
public Task Archive(PatientObservation observation)
Parameters
observationPatientObservationThe patient observation to be archived.
Returns
ArchiveByPatientId(ObjectId)
Archives all observations associated with the specified patient by copying them into the archive repository with newly generated identifiers, then removes the originals and invalidates the related cache entries.
public Task ArchiveByPatientId(ObjectId id)
Parameters
idObjectIdThe unique identifier of the patient whose observations should be archived.
Returns
CheckAndExpireObservations()
Retrieve all observations with expire time from configObservation service Check all of this observations in patient_observations and expire them if (obs.time + config.expires) smaller than current time mark as expired in bd.
public Task CheckAndExpireObservations()
Returns
DeleteByPatientId(ObjectId)
Deletes all observations associated with the specified patient identifier, clears the related cache entries, and records an audit log. If no observations exist for the given patient, the method returns without performing any deletion, cache invalidation, or audit logging. Any exception encountered during the process is logged and swallowed without being rethrown.
public Task DeleteByPatientId(ObjectId id)
Parameters
idObjectIdThe unique identifier of the patient whose observations should be deleted.
Returns
ExpireAlertsAndPowerOffAsync()
Asynchronously processes alert expiration and powers off beacon LEDs for points of care that are not currently in use. Skips patients located in virtual/moved/deleted/pushed/unknown locations and ignores emulated beacons and configurations with disabled alarms.
public Task ExpireAlertsAndPowerOffAsync()
Returns
ExpireObservations()
Retrieves all observation configurations and expires those whose Expires value is set to a positive number, ignoring configurations with a null or non-positive expiry.
public Task ExpireObservations()
Returns
ExpireObservationsAndRecalculateAsync()
Expires patient observations that should no longer be active and recalculates the latest observation values per configured field. Sets a global flag while running to signal that expiration is in progress, processes expirations in batches of 1000 to limit memory usage, and clears the patient observations cache once finished; any error is logged and swallowed without rethrowing.
public Task ExpireObservationsAndRecalculateAsync()
Returns
FindAllAfterDate(ObjectId, DateTime, List<string>?)
Retrieves all patient observations recorded after the specified date, optionally filtered by a list of observation types.
public Task<List<PatientObservation>> FindAllAfterDate(ObjectId patientId, DateTime date, List<string>? filterObservations = null)
Parameters
patientIdObjectIdThe unique identifier of the patient whose observations are being retrieved.
dateDateTimeThe cutoff date; only observations recorded after this date will be returned.
filterObservationsList<string>An optional list of observation identifiers used to narrow the returned results.
Returns
- Task<List<PatientObservation>>
A task that represents the asynchronous operation, containing a list of patient observations matching the criteria.
Exceptions
- NotImplementedException
Thrown when the method is invoked, as the implementation has not yet been provided.
FindAllBeforeDate(ObjectId, DateTime, List<string>?)
Retrieves all patient observations recorded before the specified date by delegating to the observation repository.
public Task<List<PatientObservation>> FindAllBeforeDate(ObjectId patientId, DateTime date, List<string>? filterObservations = null)
Parameters
patientIdObjectIdThe unique identifier of the patient whose observations are being queried.
dateDateTimeThe cutoff date; observations recorded before this date will be returned.
filterObservationsList<string>An optional list of observation identifiers to filter the results.
Returns
- Task<List<PatientObservation>>
A task representing the asynchronous operation, containing a list of PatientObservation entries found before the specified date.
FindAllBetweenDates(ObjectId, DateTime?, DateTime?, List<string>?, bool, PaginationFilter?)
Retrieves a paginated list of patient observations for a specific patient within an optional date range, optionally filtering by observation names and supporting both active and archived collections.
public Task<List<PatientObservation>> FindAllBetweenDates(ObjectId patientId, DateTime? startDate = null, DateTime? endDate = null, List<string>? filterObservations = null, bool fromArchived = false, PaginationFilter? filter = null)
Parameters
patientIdObjectIdThe unique identifier of the patient whose observations are being queried.
startDateDateTime?The inclusive lower bound of the observation time range. Falls back to MinValue when null.
endDateDateTime?The exclusive upper bound of the observation time range. Falls back to MaxValue when null.
filterObservationsList<string>Optional list of observation names to restrict the results to. When null or empty, no name-based filter is applied.
fromArchivedboolWhen true, queries the archived observation collection; otherwise, queries the active observation collection.
filterPaginationFilterOptional pagination settings controlling the page number and page size of the returned results.
Returns
- Task<List<PatientObservation>>
A task that resolves to the list of PatientObservation records matching the specified criteria.
FindAllLastPatientObservationTime()
Retrieves the most recent observation time for all patients by delegating to the observation repository.
public Task<Dictionary<ObjectId, DateTime>> FindAllLastPatientObservationTime()
Returns
- Task<Dictionary<ObjectId, DateTime>>
A task that represents the asynchronous operation, containing a dictionary mapping patient MongoDB.Bson.ObjectId values to their last observation DateTime.
FindAnyWithSameDate(ObjectId, DateTime, string?)
Asynchronously retrieves any patient observations matching the specified patient, date, and optional observation name by delegating to the underlying observation repository. Returns null when no matching observations are found for the given criteria.
public Task<List<PatientObservation>?> FindAnyWithSameDate(ObjectId patientId, DateTime date, string? obsName)
Parameters
patientIdObjectIdThe unique identifier of the patient whose observations are being queried.
dateDateTimeThe date used to find observations recorded on the same day.
obsNamestringThe optional name of the observation to filter by; when null, observations of any name on the given date are considered.
Returns
- Task<List<PatientObservation>>
A task that resolves to a list of matching PatientObservation instances, or null if no observations match the specified criteria.
FindByPatientIdAndCodingSystemAsync(ObjectId, string, string)
Asynchronously retrieves patient observations filtered by the specified patient identifier, coding system, and name.
public Task<IAsyncCursor<PatientObservation>> FindByPatientIdAndCodingSystemAsync(ObjectId patientId, string codingSystem, string name)
Parameters
patientIdObjectIdThe unique identifier of the patient whose observations are being queried.
codingSystemstringThe coding system used to classify the observations.
namestringThe name associated with the observations to filter by.
Returns
- Task<IAsyncCursor<PatientObservation>>
An asynchronous cursor over the matching PatientObservation documents.
FindByPatientIdAsync(ObjectId)
Retrieves all PatientObservation records associated with the specified patient by delegating to the observation repository.
public Task<IAsyncCursor<PatientObservation>> FindByPatientIdAsync(ObjectId patientId)
Parameters
patientIdObjectIdThe unique identifier of the patient whose observations are being queried.
Returns
- Task<IAsyncCursor<PatientObservation>>
An MongoDB.Driver.IAsyncCursor<TDocument> that iterates over the matching patient observations.
FindLastBeforeDate(ObjectId, DateTime, string?)
Retrieves the most recent patient observation recorded before the specified date, optionally filtered by observation name.
Returns null when no matching observation exists.
public Task<PatientObservation?> FindLastBeforeDate(ObjectId patientId, DateTime date, string? obsName)
Parameters
patientIdObjectIdThe unique identifier of the patient whose observation history is being queried.
dateDateTimeThe upper bound date; only observations recorded prior to this date are considered.
obsNamestringThe optional name of the observation to filter by. When
null, observations of any name are considered.
Returns
- Task<PatientObservation>
The latest PatientObservation recorded before the specified date, or
nullif none was found.
FindLastIntravenousLinesObservationByLocation(ObjectId)
Retrieves the most recent active intravenous lines observations for a patient, aggregated by location.
public Task<List<PatientObservation?>> FindLastIntravenousLinesObservationByLocation(ObjectId patientId)
Parameters
patientIdObjectIdThe unique identifier of the patient whose intravenous lines observations are being queried.
Returns
- Task<List<PatientObservation>>
A task that represents the asynchronous operation. The task result contains a list of PatientObservation objects, which may include null entries, representing the latest active intravenous lines observations grouped by location.
FindLastNotExpiredObservatonsByPatient(ObjectId, string, int?, int?)
Retrieves the most recent non-expired patient observations matching the specified name, optionally filtered by an end-after threshold and limited in count.
public Task<IEnumerable<PatientObservation>> FindLastNotExpiredObservatonsByPatient(ObjectId patientId, string name, int? endAfter = null, int? num = null)
Parameters
patientIdObjectIdThe identifier of the patient whose observations are being queried.
namestringThe name of the observation to filter by.
endAfterint?Optional threshold used to restrict which observations are considered; if null, no end-after filter is applied.
numint?Optional maximum number of observations to return; if null, all matching observations are returned.
Returns
- Task<IEnumerable<PatientObservation>>
A task that represents the asynchronous operation, containing an enumerable collection of matching PatientObservation instances.
FindLastObservations(ObjectId, int, List<string>?)
Retrieves the most recent observations for a specified patient by delegating to the observation repository's aggregation pipeline.
public Task<List<PatientObservation>> FindLastObservations(ObjectId patientId, int num = 2, List<string>? filterObservations = null)
Parameters
patientIdObjectIdThe unique identifier of the patient whose observations are being queried.
numintThe maximum number of recent observations to return. Defaults to 2.
filterObservationsList<string>An optional list of observation codes/names used to narrow down which observations are considered.
Returns
- Task<List<PatientObservation>>
A task that represents the asynchronous operation, containing a list of the patient's most recent PatientObservation entries.
FindLastObservationsByField(ObjectId, List<Field>?, bool, CancellationToken)
Retrieves the most recent observations for a patient from the aggregated cache, optionally filtered by specific fields, and optionally enriched through a name-based mapping.
When mapped is false, the raw cached observations are returned directly; otherwise each observation is individually mapped and those that yield no result are excluded from the output.
public Task<List<PatientObservation>> FindLastObservationsByField(ObjectId patientId, List<Field>? filterObservations = null, bool mapped = true, CancellationToken ct = default)
Parameters
patientIdObjectIdThe identifier of the patient whose latest observations are being queried.
filterObservationsList<Field>Optional list of fields used to restrict which observations are retrieved from the cache.
mappedboolWhen
true(default), applies a name-based mapping to each observation; whenfalse, returns the raw results as they come from the cache.ctCancellationTokenCancellation token to cancel the asynchronous operation.
Returns
- Task<List<PatientObservation>>
A task containing the list of patient observations, either as raw cached entries or as mapped values depending on
mapped.
FindLatestUniqueValuesByName(ObjectId, string, int?)
Retrieves the latest unique observation values for a specified patient and observation name, delegating the lookup to the underlying observation repository.
public Task<List<PatientObservation>> FindLatestUniqueValuesByName(ObjectId patientId, string name, int? expires)
Parameters
patientIdObjectIdThe unique identifier of the patient whose observations are being queried.
namestringThe name of the observation to search for.
expiresint?An optional expiration value (in seconds) applied to the query.
Returns
- Task<List<PatientObservation>>
A task that represents the asynchronous operation, containing a list of the latest unique PatientObservation values.
FindNotExpiredObservationsShouldBeExpired()
Retrieves observations that are currently marked as not expired but should be expired based on their configured expiration thresholds. For each candidate observation, the patient is validated; missing patients trigger cleanup of their observations and cached entries. Observations with missing names or unparsable expiration values are skipped, and only those whose expected expiration time (observation time plus configured minutes) has passed are yielded.
public IAsyncEnumerable<PatientObservation> FindNotExpiredObservationsShouldBeExpired()
Returns
- IAsyncEnumerable<PatientObservation>
An asynchronous stream of PatientObservation instances that are not expired in storage but whose effective expiration time has elapsed.
GetPaginatedObservations(PaginationFilter)
Retrieves a paginated collection of patient observations from the repository, returning both the requested page of data and the total document count to support client-side pagination.
public Task<PaginationResponse<PatientObservation>> GetPaginatedObservations(PaginationFilter filter)
Parameters
filterPaginationFilterThe pagination filter that specifies the page number and page size used to compute the skip/limit range.
Returns
- Task<PaginationResponse<PatientObservation>>
A PaginationResponse<T> containing the page of patient observations along with pagination metadata.
InsertIfChanged(string, PatientObservation, bool, bool)
Inserts a new patient observation only when its value differs from the most recent observation recorded for the same observation name; otherwise the existing record is kept and no insertion is performed.
public Task<bool> InsertIfChanged(string name, PatientObservation observation, bool persistObs = true, bool mapObs = true)
Parameters
namestringThe name of the observation used to look up the latest existing value for the patient.
observationPatientObservationThe patient observation to compare against the most recent value and to insert when a change is detected.
persistObsboolIndicates whether the new observation should be persisted when inserted.
mapObsboolIndicates whether the new observation should be mapped when inserted.
Returns
- Task<bool>
A task that resolves to
trueif the observation was inserted because the value changed, orfalseif the most recent observation already has the same value and no insertion was made.
InsertNurseObservation(PatientObservation)
Inserts a nurse observation by mapping it, persisting it via the observation repository, updating the latest-observations cache, broadcasting the change, and creating an audit log entry. If mapping returns a null result or an observation with a null name, the method logs the error and returns without inserting. Any exception thrown during the operation is caught and logged.
public Task InsertNurseObservation(PatientObservation obs)
Parameters
obsPatientObservationThe patient observation provided by the nurse to be mapped, persisted, cached, broadcast, and audited.
Returns
InsertObservation(PatientObservation, bool, bool)
Inserts a new observation if needed
public Task InsertObservation(PatientObservation obs, bool persistObs = true, bool mapObs = true)
Parameters
obsPatientObservationObservation
persistObsboolmapObsboolTrue if it needs to be inserted
Returns
InsertSimpleObservation(PatientObservation)
Inserts a simple patient observation into the repository and records an audit log entry for the operation using the current HTTP context user.
public Task InsertSimpleObservation(PatientObservation observation)
Parameters
observationPatientObservationThe patient observation to insert.
Returns
MapObservation(PatientObservation, bool)
Maps a PatientObservation through a sequence of configuration, units, and calculated observations services to produce a fully mapped observation, returning null if any mapping step yields no result or if an error occurs.
public Task<PatientObservation?> MapObservation(PatientObservation obs, bool onlyByName = false)
Parameters
obsPatientObservationThe patient observation to be mapped.
onlyByNameboolWhen
true, restricts the mapping to name-based lookups only.
Returns
- Task<PatientObservation>
A task containing the mapped PatientObservation, or
nullif the observation is ignored, not found, or an exception is raised during processing.
MapObservationsByName(PatientObservation)
Maps the specified patient observation by name by delegating to the configuration observation service using the by-name mapping mode.
public Task<PatientObservation?> MapObservationsByName(PatientObservation obs)
Parameters
obsPatientObservationThe patient observation to be mapped.
Returns
- Task<PatientObservation>
A task that represents the asynchronous operation. The task result contains the mapped PatientObservation, or null if no matching mapping is found.
ProcessObservations(List<PatientObservation>, Patient, DateTime, ObservationData?)
Processes a batch of patient observations by attaching patient, parent data, and message metadata, then persists them asynchronously through the calculated observations mapping service. Missing observation or message timestamps default to UtcNow, and any errors during processing are logged without being rethrown.
public void ProcessObservations(List<PatientObservation> observations, Patient patient, DateTime messageTime, ObservationData? observationData = null)
Parameters
observationsList<PatientObservation>The list of patient observations to be processed and inserted.
patientPatientThe patient to whom the observations belong.
messageTimeDateTimeThe timestamp associated with the source message.
observationDataObservationDataOptional parent observation metadata used to populate the parent data of each observation.
SaveRequest(ApiRequest)
Processes and persists an inbound medical API request (HL7), routing ORU_R40 alerts to the alarm service
and ORU_R01 observations to the appropriate handler (intravenous lines, allergies, drainage, isolation,
position, diagnosis, or generic observations) based on the observation code. Throws when both patient and
location are missing or when the request type is not supported, and silently returns when no matching patient
is found.
public Task SaveRequest(ApiRequest apiRequest)
Parameters
apiRequestApiRequestThe incoming API request containing patient/location identifiers, message type, and observation data.
Returns
Exceptions
- ApiRequestException
Thrown when both the patient number and location are null or empty, or when the request type is not valid for observations.
SaveRequestAsync(ApiRequest)
Asynchronously saves the provided API request by executing the save operation on a background thread.
public Task SaveRequestAsync(ApiRequest apiRequest)
Parameters
apiRequestApiRequestThe API request to be saved.
Returns
- Task
A task that represents the asynchronous save operation.
SaveRequestNurseObs(ApiRequest)
Saves manual nurse observations following the logic opposite to that of observations received from the census.
public Task SaveRequestNurseObs(ApiRequest apiRequest)
Parameters
apiRequestApiRequestThe API request containing the data required to locate the patient and the observations to be saved.
Returns
- Task
A task that represents the asynchronous save operation.
Exceptions
- ArgumentNullException
Thrown when
apiRequestis null.
SaveRequestNurseObsAsync(ApiRequest)
Asynchronously saves nurse observations from the provided API request by executing the save operation on a background thread.
public Task SaveRequestNurseObsAsync(ApiRequest request)
Parameters
requestApiRequestThe API request containing the nurse observation data to be persisted.
Returns
- Task
A task that represents the asynchronous nurse observation save operation.
SendObsBroadcast(List<PatientObservation>, ObjectId)
Broadcasts a list of patient observations to all display subscribers whose location identifiers match the specified point of care identifier. Subscribers with null or empty location identifiers are excluded, and each observation is dispatched asynchronously to every matching subscriber.
public Task SendObsBroadcast(List<PatientObservation> obsList, ObjectId pocId)
Parameters
obsListList<PatientObservation>The collection of patient observations to be sent to the matched subscribers.
pocIdObjectIdThe point of care identifier used to filter the subscribers by their configured location identifiers.
Returns
SendObsBroadcast(List<PatientObservation>, PatientLocation)
Broadcasts a list of patient observations to all display subscribers whose registered locations match the specified patient location by unit name, bed, and room. Subscribers without any registered locations are excluded from the broadcast.
public Task SendObsBroadcast(List<PatientObservation> obsList, PatientLocation location)
Parameters
obsListList<PatientObservation>The list of patient observations to send to matching subscribers.
locationPatientLocationThe patient location used to identify subscribers to notify.
Returns
SendObsBroadcast(BasePatientObservation)
Broadcasts a patient observation to all display subscribers whose location matches the patient's point of care. The method skips the broadcast if the observation has no name, and falls back to looking up the patient by id when it is not included in the observation.
public Task SendObsBroadcast(BasePatientObservation obs)
Parameters
obsBasePatientObservationThe patient observation to broadcast. May include the patient or require a lookup via PatientId.
Returns
UpdateExpiredObservations(List<PatientObservation>)
Updates the repository to mark a list of patient observations as expired, invalidates the corresponding cache entries, and creates audit log entries capturing the pre-update state of each observation.
public Task UpdateExpiredObservations(List<PatientObservation> patientObservations)
Parameters
patientObservationsList<PatientObservation>The list of patient observations to be marked as expired.
Returns
UpdateManyObjectId(string, ObjectId, ObjectId)
Updates multiple observation records by replacing the specified old object identifier with a new one for the given name identifier.
public Task UpdateManyObjectId(string nameId, ObjectId id, ObjectId oldId)
Parameters
nameIdstringThe name identifier of the field whose value should be updated across matching records.
idObjectIdThe new MongoDB.Bson.ObjectId value to assign to the matching records.
oldIdObjectIdThe existing MongoDB.Bson.ObjectId value to be replaced.
Returns
UpdateObservation(PatientObservation)
Updates an existing patient observation if it exists in the repository, invalidates the related cache entries, and records an audit log of the change. If no observation with the specified identifier is found, the method performs no action.
public Task UpdateObservation(PatientObservation observation)
Parameters
observationPatientObservationThe patient observation containing the updated data to be persisted.