Class PumpObservationRepository
- Namespace
- adas_core.Infrastructure.Repositories
- Assembly
- adas-core.Infrastructure.dll
A repository for managing PumpObservation entities in a MongoDB data store. This class extends the generic MongoRepository<T> base class and implements the IPumpObservationRepository contract to provide persistence operations for pump observation data.
public class PumpObservationRepository : MongoRepository<PumpObservation>, IMongoRepository<PumpObservation>, IPumpObservationRepository
- Inheritance
-
PumpObservationRepository
- Implements
- Inherited Members
- Extension Methods
Remarks
As a specialized repository inheriting from MongoRepository<T>, this class reuses the base MongoDB storage capabilities while exposing the pump observation-specific repository contract.
Constructors
PumpObservationRepository(IOptions<ApiSettings>, IMongoDatabase)
public PumpObservationRepository(IOptions<ApiSettings> apiSettings, IMongoDatabase database)
Parameters
apiSettingsIOptions<ApiSettings>databaseIMongoDatabase
Methods
AggregatedPatientLastObservations(ObjectId, int)
Retrieves the most recent pump observations for a specified patient, deduplicated by code and name, and sorted by time in descending order.
public Task<List<PumpObservation>> AggregatedPatientLastObservations(ObjectId patientId, int num = 100)
Parameters
patientIdObjectIdThe identifier of the patient whose observations are being retrieved.
numintThe maximum number of observations to consider before deduplication. Defaults to 100.
Returns
- Task<List<PumpObservation>>
A task that represents the asynchronous operation. The task result contains a list of distinct PumpObservation entries for the patient, ordered from most recent to oldest.
CreateIndexes()
Creates the indexes required for the collection. The base implementation is a no-op; derived classes should override this method to define their own indexes.
public override Task CreateIndexes()
Returns
DeleteByPatientId(ObjectId?)
Deletes all records whose PatientId matches the specified patient identifier.
public Task DeleteByPatientId(ObjectId? patientId)
Parameters
patientIdObjectId?The patient identifier whose associated records should be removed; may be
null.
Returns
DeleteKeepLastNAsync(int)
Deletes older pump observations while retaining only the most recent maxCount records for each device.
Devices whose observation count is less than or equal to the threshold are left untouched.
public Task<long> DeleteKeepLastNAsync(int maxCount)
Parameters
maxCountintThe maximum number of most recent records to keep per device.
Returns
DeleteOlderNumberAsync(string, int)
public Task<long> DeleteOlderNumberAsync(string name, int maxCount)
Parameters
Returns
DeleteOlderThanDaysAsync(int, string?)
Deletes PumpObservation documents whose timestamp is older than the specified number of days, optionally filtered by name.
When name is provided, only observations matching that name are removed; otherwise, all observations older than the cutoff are deleted.
The cutoff date is computed using UTC time.
public Task<long> DeleteOlderThanDaysAsync(int days, string? name = null)
Parameters
daysintThe age threshold in days. Observations with a
Timeolder thanDateTime.UtcNow - daysare eligible for deletion.namestringOptional name used to further restrict the deletion to observations with a matching
Namevalue. If null or empty, the name filter is not applied.
Returns
- Task<long>
The number of PumpObservation documents that were deleted.
FindAllLastPatientObservationTimeAsync()
Asynchronously retrieves the most recent observation time for all patients, returning a mapping of patient identifiers to their last observation timestamps.
public Task<Dictionary<ObjectId, DateTime>> FindAllLastPatientObservationTimeAsync()
Returns
- Task<Dictionary<ObjectId, DateTime>>
A task that represents the asynchronous operation. The task result contains a dictionary where each key is a patient MongoDB.Bson.ObjectId and the associated value is the DateTime of that patient's last observation.
FindByDeviceIdAsync(string, DateTime?, DateTime?, int?)
Retrieves pump observations for a specific device, optionally filtered by a time range and limited to a maximum number of results, sorted by time in descending order.
public Task<IEnumerable<PumpObservation>> FindByDeviceIdAsync(string deviceId, DateTime? from = null, DateTime? to = null, int? limit = null)
Parameters
deviceIdstringThe identifier of the device whose observations should be retrieved.
fromDateTime?Optional start timestamp; when provided, only observations with a time greater than or equal to this value are returned.
toDateTime?Optional end timestamp; when provided, only observations with a time less than or equal to this value are returned.
limitint?Optional maximum number of observations to return; when not provided, all matching observations are returned.
Returns
- Task<IEnumerable<PumpObservation>>
A task that represents the asynchronous operation, containing the collection of matching PumpObservation records.
FindByPatientAsync(ObjectId, DateTime?, DateTime?, int?)
Retrieves pump observations for a specific patient, optionally filtered by a time range and optionally capped to a maximum number of results. Results are ordered from most recent to oldest by observation time.
public Task<IEnumerable<PumpObservation>> FindByPatientAsync(ObjectId patientId, DateTime? from = null, DateTime? to = null, int? limit = null)
Parameters
patientIdObjectIdThe identifier of the patient whose pump observations should be retrieved.
fromDateTime?Optional inclusive lower bound for the observation time. When null, no lower time bound is applied.
toDateTime?Optional inclusive upper bound for the observation time. When null, no upper time bound is applied.
limitint?Optional maximum number of observations to return. When null, all matching observations are returned.
Returns
- Task<IEnumerable<PumpObservation>>
A task that represents the asynchronous operation. The task result contains the matching pump observations sorted by time in descending order.
FindByPatientId(ObjectId?)
Retrieves all pump observations associated with the specified patient identifier.
public Task<IEnumerable<PumpObservation>> FindByPatientId(ObjectId? patientId)
Parameters
patientIdObjectId?The optional patient identifier used to filter the pump observations.
Returns
- Task<IEnumerable<PumpObservation>>
A task that represents the asynchronous operation, containing a collection of pump observations matching the given patient identifier.
FindLastByDeviceIdAsync(string)
Retrieves the most recent pump observation associated with the specified device identifier by querying the collection, filtering by device, and returning the observation with the latest timestamp. Returns null when no matching observation exists for the device.
public Task<PumpObservation?> FindLastByDeviceIdAsync(string deviceId)
Parameters
deviceIdstringThe unique identifier of the device whose latest pump observation should be retrieved.
Returns
- Task<PumpObservation>
A task that resolves to the most recent PumpObservation for the device, or
nullif no observation is found.
FindLastObservations(ObjectId, string)
Retrieves up to the two most recent pump observations for the specified patient and observation name, ordered by time descending. Returns an empty list if the name is null, empty, or whitespace.
public Task<List<PumpObservation>> FindLastObservations(ObjectId patientId, string name)
Parameters
patientIdObjectIdThe unique identifier of the patient whose observations are being queried.
namestringThe name of the pump observation to filter by. If null, empty, or whitespace, an empty list is returned.
Returns
- Task<List<PumpObservation>>
A task representing the asynchronous operation, containing a list of the matching pump observations (at most two) sorted from newest to oldest.
GetCollectionName()
Retrieves the collection name used for pump observations, returning the configured value from API settings if available, or falling back to the default "pump_observations" name when no custom configuration is provided.
public override string GetCollectionName()
Returns
- string
The configured pump observations collection name from API settings, or the default "pump_observations" string if the setting is null.
InsertAsync(PumpObservation)
Asynchronously inserts a pump observation into the underlying collection.
public Task InsertAsync(PumpObservation obs)
Parameters
obsPumpObservationThe pump observation document to be persisted.
Returns
InsertManyAsync(IEnumerable<PumpObservation>?)
Inserts a batch of pump observations into the underlying collection in a single operation. Returns immediately when the input is null or contains no elements, performing no insertion in those cases.
public Task InsertManyAsync(IEnumerable<PumpObservation>? observations)
Parameters
observationsIEnumerable<PumpObservation>The pump observations to insert. A null or empty collection results in a no-op.
Returns
UpdateManyObjectIdByFieldAsync(string, ObjectId, ObjectId?)
Updates the specified field in multiple PumpObservation documents, setting it to a new MongoDB.Bson.ObjectId where it currently matches the optional old MongoDB.Bson.ObjectId.
public Task<long> UpdateManyObjectIdByFieldAsync(string fieldName, ObjectId newId, ObjectId? oldId)
Parameters
fieldNamestringThe name of the field to update. Must not be null or empty.
newIdObjectIdThe new MongoDB.Bson.ObjectId value to assign to the field.
oldIdObjectId?The current MongoDB.Bson.ObjectId value used to match documents; if
null, the filter matches documents where the field is null.
Returns
Exceptions
- ArgumentException
Thrown when
fieldNameis null, empty, or whitespace.