Table of Contents

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

apiSettings IOptions<ApiSettings>
database IMongoDatabase

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

patientId ObjectId

The identifier of the patient whose observations are being retrieved.

num int

The 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

Task

A Task representing the asynchronous index creation operation.

DeleteByPatientId(ObjectId?)

Deletes all records whose PatientId matches the specified patient identifier.

public Task DeleteByPatientId(ObjectId? patientId)

Parameters

patientId ObjectId?

The patient identifier whose associated records should be removed; may be null.

Returns

Task

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

maxCount int

The maximum number of most recent records to keep per device.

Returns

Task<long>

The total number of observations deleted across all devices.

DeleteOlderNumberAsync(string, int)

public Task<long> DeleteOlderNumberAsync(string name, int maxCount)

Parameters

name string
maxCount int

Returns

Task<long>

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

days int

The age threshold in days. Observations with a Time older than DateTime.UtcNow - days are eligible for deletion.

name string

Optional name used to further restrict the deletion to observations with a matching Name value. 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

deviceId string

The identifier of the device whose observations should be retrieved.

from DateTime?

Optional start timestamp; when provided, only observations with a time greater than or equal to this value are returned.

to DateTime?

Optional end timestamp; when provided, only observations with a time less than or equal to this value are returned.

limit int?

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

patientId ObjectId

The identifier of the patient whose pump observations should be retrieved.

from DateTime?

Optional inclusive lower bound for the observation time. When null, no lower time bound is applied.

to DateTime?

Optional inclusive upper bound for the observation time. When null, no upper time bound is applied.

limit int?

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

patientId ObjectId?

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

deviceId string

The 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 null if 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

patientId ObjectId

The unique identifier of the patient whose observations are being queried.

name string

The 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

obs PumpObservation

The pump observation document to be persisted.

Returns

Task

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

observations IEnumerable<PumpObservation>

The pump observations to insert. A null or empty collection results in a no-op.

Returns

Task

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

fieldName string

The name of the field to update. Must not be null or empty.

newId ObjectId

The new MongoDB.Bson.ObjectId value to assign to the field.

oldId ObjectId?

The current MongoDB.Bson.ObjectId value used to match documents; if null, the filter matches documents where the field is null.

Returns

Task<long>

The number of documents that were modified by the update operation.

Exceptions

ArgumentException

Thrown when fieldName is null, empty, or whitespace.