public static Func <string, bool> Exporter(int maxExport, Guid qid, PatientCohort cohort) { // small cohort, export them all var csize = cohort.PatientIds.Count; if (csize <= maxExport) { return((string _) => true); } // need a subset var tmp = new HashSet <string>(cohort.PatientIds); var set = new HashSet <string>(); var rnd = new Random(qid.GetHashCode()); foreach (var _ in Enumerable.Range(0, maxExport)) { var done = false; string candidate; do { candidate = tmp.ElementAt(rnd.Next(csize - 1)); if (set.Add(candidate)) { tmp.Remove(candidate); csize--; done = true; } } while (!done); } return((string patid) => set.Contains(patid)); }
/// <summary> /// Provide an zero count of patients in the specified query. /// Converts the query into a local validation context. /// Validates the resulting context to ensure sensible construction. /// Creates a queryId and empty cohort. /// </summary> /// <returns><see cref="Result">The count of patients in the cohort.</see></returns> /// <param name="queryDTO">Abstract query representation.</param> /// <param name="token">Cancellation token.</param> /// <exception cref="OperationCanceledException"/> /// <exception cref="LeafCompilerException"/> /// <exception cref="ArgumentNullException"/> /// <exception cref="LeafRPCException"/> /// <exception cref="System.Data.Common.DbException"/> async Task <Result> GatewayCount(IPatientCountQueryDTO queryDTO, CancellationToken token) { log.LogInformation("GatewayCount starting. DTO:{@DTO}", queryDTO); Ensure.NotNull(queryDTO, nameof(queryDTO)); var ctx = await converter.GetPanelsAsync(queryDTO, token); log.LogInformation("GatewayCount panel validation context. Context:{@Context}", ctx); if (!ctx.PreflightPassed) { return(new Result { ValidationContext = ctx }); } validator.Validate(ctx); token.ThrowIfCancellationRequested(); var cohort = new PatientCohort(); var qid = await CacheCohort(cohort); return(new Result { ValidationContext = ctx, Count = new PatientCount { QueryId = qid, Value = cohort.Count, SqlStatements = cohort.SqlStatements } }); }
async Task <Guid> CacheCohort(PatientCohort cohort) { try { log.LogInformation("Cohort caching started."); var qid = await cohortCache.CreateUnsavedQueryAsync(cohort, user); log.LogInformation("Cohort caching complete. QueryId:{@QueryId}, Panels:{@Panels}", qid, cohort.Panels.ToArray()); return(qid); } catch (InvalidOperationException ie) { log.LogError("Failed to cache cohort. Error:{Error}", ie.Message); throw new LeafRPCException(LeafErrorCode.Internal, ie.Message, ie); } }