Skip to content

Commit

Permalink
Remove race condition between different evaluation periods by deletin…
Browse files Browse the repository at this point in the history
…g Parameters parameter from evaluateMeasure(). (#556)

* Remove race condition between different evaluation periods by deleting Parameters parameter from evaluateMeasure().

* Spotless.

* Adjust DateTime precision values based on ZonedDateTime passed in.

* Spotless.
  • Loading branch information
lukedegruchy authored Oct 10, 2024
1 parent 6a2cfb0 commit f236c13
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.List;
import java.util.Objects;
import org.hl7.elm.r1.VersionedIdentifier;
import org.hl7.fhir.instance.model.api.IBaseParameters;
import org.opencds.cqf.cql.engine.execution.CqlEngine;
import org.opencds.cqf.cql.engine.runtime.Interval;
import org.opencds.cqf.fhir.cql.LibraryEngine;
Expand All @@ -17,7 +16,6 @@ public abstract class BaseMeasureEvaluation<MeasureT, MeasureReportT, SubjectT>
protected MeasureT measure;
protected LibraryEngine libraryEngine;
protected String measurementPeriodParameterName;
protected IBaseParameters parameters;
protected VersionedIdentifier versionIdentifier;

protected BaseMeasureEvaluation(
Expand All @@ -26,17 +24,15 @@ protected BaseMeasureEvaluation(
MeasureDefBuilder<MeasureT> measureDefBuilder,
MeasureReportBuilder<MeasureT, MeasureReportT, SubjectT> measureReportBuilder,
LibraryEngine libraryEngine,
VersionedIdentifier versionIdentifier,
IBaseParameters parameters) {
VersionedIdentifier versionIdentifier) {
this(
context,
measure,
measureDefBuilder,
measureReportBuilder,
MeasureConstants.MEASUREMENT_PERIOD_PARAMETER_NAME,
libraryEngine,
versionIdentifier,
parameters);
versionIdentifier);
}

protected BaseMeasureEvaluation(
Expand All @@ -46,8 +42,7 @@ protected BaseMeasureEvaluation(
MeasureReportBuilder<MeasureT, MeasureReportT, SubjectT> measureReportBuilder,
String measurementPeriodParameterName,
LibraryEngine libraryEngine,
VersionedIdentifier versionIdentifier,
IBaseParameters parameters) {
VersionedIdentifier versionIdentifier) {
this.context = Objects.requireNonNull(context, "context is a required argument");
this.measure = Objects.requireNonNull(measure, "measure is a required argument");
this.measureDefBuilder = Objects.requireNonNull(measureDefBuilder, "measureDefBuilder is a required argument");
Expand All @@ -56,34 +51,30 @@ protected BaseMeasureEvaluation(
this.measurementPeriodParameterName = Objects.requireNonNull(
measurementPeriodParameterName, "measurementPeriodParameterName is a required argument");
this.libraryEngine = libraryEngine;
this.parameters = parameters;
this.versionIdentifier = versionIdentifier;
}

public MeasureReportT evaluate(
MeasureEvalType measureEvalType,
List<String> subjectIds,
LibraryEngine libraryEngine,
VersionedIdentifier id,
IBaseParameters parameters) {
return this.evaluate(measureEvalType, subjectIds, null, libraryEngine, id, parameters);
VersionedIdentifier id) {
return this.evaluate(measureEvalType, subjectIds, null, libraryEngine, id);
}

public MeasureReportT evaluate(
MeasureEvalType measureEvalType,
List<String> subjectIds,
Interval measurementPeriod,
LibraryEngine libraryEngine,
VersionedIdentifier id,
IBaseParameters parameters) {
VersionedIdentifier id) {
Objects.requireNonNull(subjectIds, "subjectIds is a required parameter");
Objects.requireNonNull(measureEvalType, "measureEvalType is a required parameter");

MeasureDef measureDef = this.measureDefBuilder.build(measure);
MeasureEvaluator measureEvaluation =
new MeasureEvaluator(context, this.measurementPeriodParameterName, libraryEngine);
measureDef =
measureEvaluation.evaluate(measureDef, measureEvalType, subjectIds, measurementPeriod, parameters, id);
measureDef = measureEvaluation.evaluate(measureDef, measureEvalType, subjectIds, measurementPeriod, id);

Interval measurementPeriodInterval;
if (measurementPeriod == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.hl7.elm.r1.NamedTypeSpecifier;
import org.hl7.elm.r1.ParameterDef;
import org.hl7.elm.r1.VersionedIdentifier;
import org.hl7.fhir.instance.model.api.IBaseParameters;
import org.opencds.cqf.cql.engine.execution.CqlEngine;
import org.opencds.cqf.cql.engine.execution.EvaluationResult;
import org.opencds.cqf.cql.engine.execution.ExpressionResult;
Expand Down Expand Up @@ -83,7 +82,6 @@ public MeasureDef evaluate(
MeasureEvalType measureEvalType,
List<String> subjectIds,
@Nullable Interval measurementPeriod,
IBaseParameters parameters,
VersionedIdentifier versionedIdentifier) {
Objects.requireNonNull(measureDef, "measureDef is a required argument");
Objects.requireNonNull(subjectIds, "subjectIds is a required argument");
Expand All @@ -97,37 +95,17 @@ public MeasureDef evaluate(
case PATIENT:
case SUBJECT:
return this.evaluate(
measureDef,
MeasureReportType.INDIVIDUAL,
subjectIds,
parameters,
versionedIdentifier,
zonedDateTime);
measureDef, MeasureReportType.INDIVIDUAL, subjectIds, versionedIdentifier, zonedDateTime);
case SUBJECTLIST:
return this.evaluate(
measureDef,
MeasureReportType.SUBJECTLIST,
subjectIds,
parameters,
versionedIdentifier,
zonedDateTime);
measureDef, MeasureReportType.SUBJECTLIST, subjectIds, versionedIdentifier, zonedDateTime);
case PATIENTLIST:
// DSTU3 Only
return this.evaluate(
measureDef,
MeasureReportType.PATIENTLIST,
subjectIds,
parameters,
versionedIdentifier,
zonedDateTime);
measureDef, MeasureReportType.PATIENTLIST, subjectIds, versionedIdentifier, zonedDateTime);
case POPULATION:
return this.evaluate(
measureDef,
MeasureReportType.SUMMARY,
subjectIds,
parameters,
versionedIdentifier,
zonedDateTime);
measureDef, MeasureReportType.SUMMARY, subjectIds, versionedIdentifier, zonedDateTime);
default:
// never hit because this value is set upstream
throw new IllegalArgumentException(
Expand Down Expand Up @@ -300,7 +278,6 @@ protected MeasureDef evaluate(
MeasureDef measureDef,
MeasureReportType type,
List<String> subjectIds,
IBaseParameters parameters,
VersionedIdentifier id,
ZonedDateTime zonedDateTime) {
var subjectSize = subjectIds.size();
Expand All @@ -321,8 +298,8 @@ protected MeasureDef evaluate(
String subjectIdPart = subjectInfo.getRight();
context.getState().setContextValue(subjectTypePart, subjectIdPart);

EvaluationResult result = libraryEngine.getEvaluationResult(
id, subjectId, parameters, null, null, null, zonedDateTime, context);
EvaluationResult result =
libraryEngine.getEvaluationResult(id, subjectId, null, null, null, null, zonedDateTime, context);

evaluateSubject(measureDef, subjectTypePart, subjectIdPart, subjectSize, type, result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.hl7.fhir.dstu3.model.DomainResource;
import org.hl7.fhir.dstu3.model.Measure;
import org.hl7.fhir.dstu3.model.MeasureReport;
import org.hl7.fhir.dstu3.model.Parameters;
import org.opencds.cqf.cql.engine.execution.CqlEngine;
import org.opencds.cqf.fhir.cql.LibraryEngine;
import org.opencds.cqf.fhir.cr.measure.common.BaseMeasureEvaluation;
Expand All @@ -15,18 +14,13 @@
public class Dstu3MeasureEvaluation extends BaseMeasureEvaluation<Measure, MeasureReport, DomainResource> {

public Dstu3MeasureEvaluation(
CqlEngine context,
Measure measure,
LibraryEngine libraryEngine,
VersionedIdentifier versionIdentifier,
Parameters parameters) {
CqlEngine context, Measure measure, LibraryEngine libraryEngine, VersionedIdentifier versionIdentifier) {
super(
context,
measure,
new Dstu3MeasureDefBuilder(),
new Dstu3MeasureReportBuilder(),
libraryEngine,
versionIdentifier,
parameters);
versionIdentifier);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.opencds.cqf.fhir.cr.measure.dstu3;

import static org.opencds.cqf.fhir.cr.measure.constant.MeasureConstants.MEASUREMENT_PERIOD_PARAMETER_NAME;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
Expand All @@ -12,13 +10,11 @@
import org.cqframework.cql.cql2elm.CqlIncludeException;
import org.cqframework.cql.cql2elm.model.CompiledLibrary;
import org.hl7.elm.r1.VersionedIdentifier;
import org.hl7.fhir.dstu3.model.DateTimeType;
import org.hl7.fhir.dstu3.model.IdType;
import org.hl7.fhir.dstu3.model.Library;
import org.hl7.fhir.dstu3.model.Measure;
import org.hl7.fhir.dstu3.model.MeasureReport;
import org.hl7.fhir.dstu3.model.Parameters;
import org.hl7.fhir.dstu3.model.Period;
import org.hl7.fhir.instance.model.api.IBaseBundle;
import org.hl7.fhir.instance.model.api.IPrimitiveType;
import org.opencds.cqf.cql.engine.fhir.model.Dstu3FhirModelResolver;
Expand Down Expand Up @@ -138,22 +134,8 @@ protected MeasureReport evaluateMeasure(
var subjects =
subjectProvider.getSubjects(actualRepo, evalType, subjectIds).collect(Collectors.toList());
var libraryEngine = new LibraryEngine(repository, this.measureEvaluationOptions.getEvaluationSettings());
var params = makeParameters(measurementPeriod);
Dstu3MeasureEvaluation measureEvaluator =
new Dstu3MeasureEvaluation(context, measure, libraryEngine, id, params);
return measureEvaluator.evaluate(evalType, subjects, measurementPeriod, libraryEngine, id, params);
}

public Parameters makeParameters(Interval measurementPeriod) {
Parameters parameters = new Parameters();
if (measurementPeriod != null) {

Period period = new Period();
period.setStartElement(new DateTimeType(measurementPeriod.getStart().toString()));
period.setEndElement(new DateTimeType(measurementPeriod.getEnd().toString()));
parameters.addParameter().setName(MEASUREMENT_PERIOD_PARAMETER_NAME).setValue(period);
}
return parameters;
Dstu3MeasureEvaluation measureEvaluator = new Dstu3MeasureEvaluation(context, measure, libraryEngine, id);
return measureEvaluator.evaluate(evalType, subjects, measurementPeriod, libraryEngine, id);
}

protected MeasureReportType evalTypeToReportType(MeasureEvalType measureEvalType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.hl7.fhir.r4.model.DomainResource;
import org.hl7.fhir.r4.model.Measure;
import org.hl7.fhir.r4.model.MeasureReport;
import org.hl7.fhir.r4.model.Parameters;
import org.opencds.cqf.cql.engine.execution.CqlEngine;
import org.opencds.cqf.fhir.cql.LibraryEngine;
import org.opencds.cqf.fhir.cr.measure.common.BaseMeasureEvaluation;
Expand All @@ -15,18 +14,13 @@
public class R4MeasureEvaluation extends BaseMeasureEvaluation<Measure, MeasureReport, DomainResource> {

public R4MeasureEvaluation(
CqlEngine context,
Measure measure,
LibraryEngine libraryEngine,
VersionedIdentifier versionIdentifier,
Parameters parameters) {
CqlEngine context, Measure measure, LibraryEngine libraryEngine, VersionedIdentifier versionIdentifier) {
super(
context,
measure,
new R4MeasureDefBuilder(),
new R4MeasureReportBuilder(),
libraryEngine,
versionIdentifier,
parameters);
versionIdentifier);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.opencds.cqf.fhir.cr.measure.r4;

import static org.opencds.cqf.fhir.cr.measure.constant.MeasureConstants.MEASUREMENT_PERIOD_PARAMETER_NAME;

import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
import jakarta.annotation.Nullable;
import java.time.ZonedDateTime;
Expand Down Expand Up @@ -164,19 +162,8 @@ protected MeasureReport evaluateMeasure(
}
// Library Evaluate
var libraryEngine = new LibraryEngine(repository, this.measureEvaluationOptions.getEvaluationSettings());
var params = makeParameters(measurementPeriod);
R4MeasureEvaluation measureEvaluator = new R4MeasureEvaluation(context, measure, libraryEngine, id, params);
return measureEvaluator.evaluate(evalType, subjectIds, measurementPeriod, libraryEngine, id, params);
}

public Parameters makeParameters(Interval measurementPeriod) {
Parameters parameters = new Parameters();
if (measurementPeriod != null) {
var helper = new R4DateHelper();
parameters.setParameter(
MEASUREMENT_PERIOD_PARAMETER_NAME, helper.buildMeasurementPeriod(measurementPeriod));
}
return parameters;
R4MeasureEvaluation measureEvaluator = new R4MeasureEvaluation(context, measure, libraryEngine, id);
return measureEvaluator.evaluate(evalType, subjectIds, measurementPeriod, libraryEngine, id);
}

protected Measure resolveByUrl(CanonicalType url) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.opencds.cqf.cql.engine.runtime.Date;
import org.opencds.cqf.cql.engine.runtime.DateTime;
import org.opencds.cqf.cql.engine.runtime.Interval;
import org.opencds.cqf.cql.engine.runtime.Precision;

public class R4DateHelper {

Expand Down Expand Up @@ -38,6 +39,8 @@ public Period buildMeasurementPeriod(Interval measurementPeriodInterval) {

private DateTime convertToDateTime(ZonedDateTime zonedDateTime) {
final OffsetDateTime offsetDateTime = zonedDateTime.toOffsetDateTime();
return new DateTime(offsetDateTime);
final DateTime convertedDateTime = new DateTime(offsetDateTime);
convertedDateTime.setPrecision(Precision.SECOND);
return convertedDateTime;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,13 @@ private MeasureReport runTest(
var lib = engine.getEnvironment().getLibraryManager().resolveLibrary(id);
engine.getState().init(lib.getLibrary());
var libraryEngine = new LibraryEngine(repository, this.evaluationOptions.getEvaluationSettings());
Dstu3MeasureEvaluation evaluation = new Dstu3MeasureEvaluation(engine, measure, libraryEngine, id, null);
Dstu3MeasureEvaluation evaluation = new Dstu3MeasureEvaluation(engine, measure, libraryEngine, id);
MeasureReport report = evaluation.evaluate(
subjectIds.size() == 1 ? MeasureEvalType.PATIENT : MeasureEvalType.POPULATION,
subjectIds,
measurementPeriod,
libraryEngine,
id,
null);
id);
assertNotNull(report);

// Simulate sending it across the wire
Expand Down
Loading

0 comments on commit f236c13

Please sign in to comment.