Skip to content

Commit

Permalink
Merge pull request #153 from MeasureAuthoringTool/MAT-4516
Browse files Browse the repository at this point in the history
MAT-4516 Get the return types for cql definitions
  • Loading branch information
adongare committed Aug 19, 2022
2 parents 435b1dc + a7744c0 commit a0be17f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 deletions.
7 changes: 3 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions src/api/MeasureServiceApi.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { MeasureServiceApi } from "./useMeasureServiceApi";
import { libraryElm } from "./__mocks__/cqlLibraryElm";

describe("MeasureServiceApi Tests", () => {
const baseUrl = "madie.com";
const accessToken = jest.fn();
const measureServiceApi = new MeasureServiceApi(baseUrl, accessToken);
it("get return types for all cql definitions when no definition is found", () => {
let returnTypes = measureServiceApi.getReturnTypesForAllCqlDefinitions("");
expect(returnTypes).toEqual({});

returnTypes =
measureServiceApi.getReturnTypesForAllCqlDefinitions('{"library":{}}');
expect(returnTypes).toEqual({});

returnTypes = measureServiceApi.getReturnTypesForAllCqlDefinitions(
'{"library":{"statements": {}}}'
);
expect(returnTypes).toEqual({});
});

it("get return types for all cql definitions", () => {
let returnTypes =
measureServiceApi.getReturnTypesForAllCqlDefinitions(libraryElm);
expect(returnTypes["initialPopulation"]).toEqual(
"{http://hl7.org/fhir}Encounter"
);

expect(returnTypes["measurePopulation"]).toEqual(
"{http://hl7.org/fhir}Encounter"
);

expect(returnTypes["measurePopulationExclusions"]).toEqual(
"{http://hl7.org/fhir}Encounter"
);

expect(returnTypes["measureObservation"]).toEqual(
"{urn:hl7-org:elm-types:r1}Integer"
);
});
});
1 change: 1 addition & 0 deletions src/api/__mocks__/cqlLibraryElm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const libraryElm = `{"errorExceptions":[{"startLine":22,"startChar":27,"endLine":24,"endChar":21,"errorType":"ELM","errorSeverity":"Error","targetIncludeLibraryId":"EXM124v7QICore4","targetIncludeLibraryVersionId":"7.0.000","type":null,"message":"Could not resolve call to operator Exists with signature (QICore.Encounter.Diagnosis)."},{"startLine":22,"startChar":21,"endLine":24,"endChar":21,"errorType":"ELM","errorSeverity":"Error","targetIncludeLibraryId":"EXM124v7QICore4","targetIncludeLibraryVersionId":"7.0.000","type":null,"message":"Expected an expression of type 'System.Boolean', but found an expression of type '<unknown>'."}],"library":{"annotation":[{"libraryId":"EXM124v7QICore4","libraryVersion":"7.0.000","startLine":22,"startChar":27,"endLine":24,"endChar":21,"message":"Could not resolve call to operator Exists with signature (QICore.Encounter.Diagnosis).","errorType":"semantic","errorSeverity":"Error","type":"CqlToElmError"},{"libraryId":"EXM124v7QICore4","libraryVersion":"7.0.000","startLine":22,"startChar":21,"endLine":24,"endChar":21,"message":"Expected an expression of type 'System.Boolean', but found an expression of type '<unknown>'.","errorType":"semantic","errorSeverity":"Error","type":"CqlToElmError"}],"identifier":{"id":"EXM124v7QICore4","version":"7.0.000"},"schemaIdentifier":{"id":"urn:hl7-org:elm","version":"r1"},"usings":{"def":[{"localIdentifier":"System","uri":"urn:hl7-org:elm-types:r1"},{"localId":"1","locator":"3:1-3:28","localIdentifier":"QICore","uri":"http://hl7.org/fhir","annotation":[{"type":"Annotation","s":{"r":"1","s":[{"value":["","using "]},{"s":[{"value":["QICore"]}]},{"value":[" version ","'4.1.0'"]}]}}]}]},"includes":{"def":[{"localId":"2","locator":"5:1-5:37","localIdentifier":"FHIRHelpers","path":"FHIRHelpers","version":"4.0.001","annotation":[{"type":"Annotation","s":{"r":"2","s":[{"value":["","include "]},{"s":[{"value":["FHIRHelpers"]}]},{"value":[" version ","'4.0.001'"]}]}}]}]},"parameters":{"def":[{"localId":"6","locator":"9:1-9:49","name":"Measurement Period","accessLevel":"Public","annotation":[{"type":"Annotation","s":{"r":"6","s":[{"value":["","parameter ","\\"Measurement Period\\""," "]},{"r":"5","s":[{"value":["Interval<"]},{"r":"4","s":[{"value":["DateTime"]}]},{"value":[">"]}]}]}}],"resultTypeSpecifier":{"type":"IntervalTypeSpecifier","pointType":{"name":"{urn:hl7-org:elm-types:r1}DateTime","type":"NamedTypeSpecifier"}},"parameterTypeSpecifier":{"localId":"5","locator":"9:32-9:49","type":"IntervalTypeSpecifier","resultTypeSpecifier":{"type":"IntervalTypeSpecifier","pointType":{"name":"{urn:hl7-org:elm-types:r1}DateTime","type":"NamedTypeSpecifier"}},"pointType":{"localId":"4","locator":"9:41-9:48","resultTypeName":"{urn:hl7-org:elm-types:r1}DateTime","name":"{urn:hl7-org:elm-types:r1}DateTime","type":"NamedTypeSpecifier"}}}]},"valueSets":{"def":[{"localId":"3","locator":"7:1-7:100","resultTypeName":"{urn:hl7-org:elm-types:r1}ValueSet","name":"Encounter Inpatient","id":"http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113883.3.666.5.307","accessLevel":"Public","annotation":[{"type":"Annotation","s":{"r":"3","s":[{"value":["","valueset ","\\"Encounter Inpatient\\"",": ","'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113883.3.666.5.307'"]}]}}]}]},"contexts":{"def":[{"locator":"11:1-11:15","name":"Patient"}]},"statements":{"def":[{"locator":"11:1-11:15","name":"Patient","context":"Patient","expression":{"type":"SingletonFrom","operand":{"locator":"11:1-11:15","dataType":"{http://hl7.org/fhir}Patient","templateId":"http://hl7.org/fhir/us/qicore/StructureDefinition/qicore-patient","type":"Retrieve"}}},{"localId":"14","locator":"13:1-15:69","name":"Initial Population","context":"Patient","accessLevel":"Public","annotation":[{"type":"Annotation","s":{"r":"14","s":[{"value":["","define ","\\"Initial Population\\"",":\\n "]},{"r":"13","s":[{"s":[{"r":"8","s":[{"r":"7","s":[{"r":"7","s":[{"value":["[","\\"Encounter\\"",": "]},{"s":[{"value":["\\"Encounter Inpatient\\""]}]},{"value":["]"]}]}]},{"value":[" ","Enc"]}]}]},{"value":["\\n "]},{"r":"12","s":[{"value":["where "]},{"r":"12","s":[{"r":"10","s":[{"r":"9","s":[{"value":["Enc"]}]},{"value":["."]},{"r":"10","s":[{"value":["period"]}]}]},{"r":"12","value":[" ","ends during"," "]},{"r":"11","s":[{"value":["\\"Measurement Period\\""]}]}]}]}]}]}}],"resultTypeSpecifier":{"type":"ListTypeSpecifier","elementType":{"name":"{http://hl7.org/fhir}Encounter","type":"NamedTypeSpecifier"}},"expression":{"localId":"13","locator":"14:3-15:69","type":"Query","resultTypeSpecifier":{"type":"ListTypeSpecifier","elementType":{"name":"{http://hl7.org/fhir}Encounter","type":"NamedTypeSpecifier"}},"source":[{"localId":"8","locator":"14:3-14:42","alias":"Enc","resultTypeSpecifier":{"type":"ListTypeSpecifier","elementType":{"name":"{http://hl7.org/fhir}Encounter","type":"NamedTypeSpecifier"}},"expression":{"localId":"7","locator":"14:3-14:38","dataType":"{http://hl7.org/fhir}Encounter","templateId":"http://hl7.org/fhir/us/qicore/StructureDefinition/qicore-encounter","codeProperty":"type","codeComparator":"in","type":"Retrieve","resultTypeSpecifier":{"type":"ListTypeSpecifier","elementType":{"name":"{http://hl7.org/fhir}Encounter","type":"NamedTypeSpecifier"}},"codes":{"locator":"14:17-14:37","resultTypeName":"{urn:hl7-org:elm-types:r1}ValueSet","name":"Encounter Inpatient","preserve":true,"type":"ValueSetRef"}}}],"relationship":[],"where":{"localId":"12","locator":"15:21-15:69","resultTypeName":"{urn:hl7-org:elm-types:r1}Boolean","type":"In","operand":[{"locator":"15:38-15:41","type":"End","operand":{"localId":"10","locator":"15:27-15:36","path":"period","scope":"Enc","type":"Property","resultTypeSpecifier":{"type":"IntervalTypeSpecifier","pointType":{"name":"{urn:hl7-org:elm-types:r1}DateTime","type":"NamedTypeSpecifier"}}}},{"localId":"11","locator":"15:50-15:69","name":"Measurement Period","type":"ParameterRef","resultTypeSpecifier":{"type":"IntervalTypeSpecifier","pointType":{"name":"{urn:hl7-org:elm-types:r1}DateTime","type":"NamedTypeSpecifier"}}}]}}},{"localId":"16","locator":"17:1-18:22","name":"Measure Population","context":"Patient","accessLevel":"Public","annotation":[{"type":"Annotation","s":{"r":"16","s":[{"value":["","define ","\\"Measure Population\\"",":\\n "]},{"r":"15","s":[{"value":["\\"Initial Population\\""]}]}]}}],"resultTypeSpecifier":{"type":"ListTypeSpecifier","elementType":{"name":"{http://hl7.org/fhir}Encounter","type":"NamedTypeSpecifier"}},"expression":{"localId":"15","locator":"18:3-18:22","name":"Initial Population","type":"ExpressionRef","resultTypeSpecifier":{"type":"ListTypeSpecifier","elementType":{"name":"{http://hl7.org/fhir}Encounter","type":"NamedTypeSpecifier"}}}},{"localId":"29","locator":"20:1-24:21","name":"Measure Population Exclusions","context":"Patient","accessLevel":"Public","annotation":[{"type":"Annotation","s":{"r":"29","s":[{"value":["","define ","\\"Measure Population Exclusions\\"",":\\n "]},{"r":"28","s":[{"s":[{"r":"18","s":[{"r":"17","s":[{"s":[{"value":["\\"Initial Population\\""]}]}]},{"value":[" ","Pop"]}]}]},{"value":["\\n "]},{"r":"27","s":[{"value":["where "]},{"r":"26","s":[{"value":["exists "]},{"r":"25","s":[{"value":["( "]},{"r":"25","s":[{"s":[{"r":"20","s":[{"r":"19","s":[{"s":[{"value":["Pop",".","diagnosis"]}]}]},{"value":[" ","diag"]}]}]},{"value":["\\n "]},{"r":"24","s":[{"value":["where "]},{"r":"24","s":[{"r":"22","s":[{"r":"21","s":[{"value":["diag"]}]},{"value":["."]},{"r":"22","s":[{"value":["rank"]}]}]},{"r":"23","value":[" ","="," ","1"]}]}]}]},{"value":["\\n )"]}]}]}]}]}]}}],"resultTypeSpecifier":{"type":"ListTypeSpecifier","elementType":{"name":"{http://hl7.org/fhir}Encounter","type":"NamedTypeSpecifier"}},"expression":{"localId":"28","locator":"21:3-24:21","type":"Query","resultTypeSpecifier":{"type":"ListTypeSpecifier","elementType":{"name":"{http://hl7.org/fhir}Encounter","type":"NamedTypeSpecifier"}},"source":[{"localId":"18","locator":"21:3-21:26","alias":"Pop","resultTypeSpecifier":{"type":"ListTypeSpecifier","elementType":{"name":"{http://hl7.org/fhir}Encounter","type":"NamedTypeSpecifier"}},"expression":{"localId":"17","locator":"21:3-21:22","name":"Initial Population","type":"ExpressionRef","resultTypeSpecifier":{"type":"ListTypeSpecifier","elementType":{"name":"{http://hl7.org/fhir}Encounter","type":"NamedTypeSpecifier"}}}}],"relationship":[],"where":{"localId":"27","locator":"22:21-24:21","type":"Null"}}},{"localId":"42","locator":"26:1-29:54","name":"Valid Encounter","context":"Patient","accessLevel":"Public","annotation":[{"type":"Annotation","s":{"r":"42","s":[{"value":["","define ","\\"Valid Encounter\\"",":\\n "]},{"r":"41","s":[{"s":[{"r":"31","s":[{"r":"30","s":[{"r":"30","s":[{"value":["[","\\"Encounter\\"",": "]},{"s":[{"value":["\\"Encounter Inpatient\\""]}]},{"value":["]"]}]}]},{"value":[" ","Encounter"]}]}]},{"value":["\\n "]},{"r":"40","s":[{"value":["where "]},{"r":"40","s":[{"r":"35","s":[{"r":"33","s":[{"r":"32","s":[{"value":["Encounter"]}]},{"value":["."]},{"r":"33","s":[{"value":["period"]}]}]},{"r":"35","value":[" ","during"," "]},{"r":"34","s":[{"value":["\\"Measurement Period\\""]}]}]},{"value":["\\n and "]},{"r":"39","s":[{"r":"37","s":[{"r":"36","s":[{"value":["Encounter"]}]},{"value":["."]},{"r":"37","s":[{"value":["length"]}]}]},{"value":[" ",">="," "]},{"r":"38","s":[{"value":["24 ","hours"]}]}]}]}]}]}]}}],"resultTypeSpecifier":{"type":"ListTypeSpecifier","elementType":{"name":"{http://hl7.org/fhir}Encounter","type":"NamedTypeSpecifier"}},"expression":{"localId":"41","locator":"27:3-29:54","type":"Query","resultTypeSpecifier":{"type":"ListTypeSpecifier","elementType":{"name":"{http://hl7.org/fhir}Encounter","type":"NamedTypeSpecifier"}},"source":[{"localId":"31","locator":"27:3-27:48","alias":"Encounter","resultTypeSpecifier":{"type":"ListTypeSpecifier","elementType":{"name":"{http://hl7.org/fhir}Encounter","type":"NamedTypeSpecifier"}},"expression":{"localId":"30","locator":"27:3-27:38","dataType":"{http://hl7.org/fhir}Encounter","templateId":"http://hl7.org/fhir/us/qicore/StructureDefinition/qicore-encounter","codeProperty":"type","codeComparator":"in","type":"Retrieve","resultTypeSpecifier":{"type":"ListTypeSpecifier","elementType":{"name":"{http://hl7.org/fhir}Encounter","type":"NamedTypeSpecifier"}},"codes":{"locator":"27:17-27:37","resultTypeName":"{urn:hl7-org:elm-types:r1}ValueSet","name":"Encounter Inpatient","preserve":true,"type":"ValueSetRef"}}}],"relationship":[],"where":{"localId":"40","locator":"28:21-29:54","resultTypeName":"{urn:hl7-org:elm-types:r1}Boolean","type":"And","operand":[{"localId":"35","locator":"28:27-28:70","resultTypeName":"{urn:hl7-org:elm-types:r1}Boolean","type":"IncludedIn","operand":[{"localId":"33","locator":"28:27-28:42","path":"period","scope":"Encounter","type":"Property","resultTypeSpecifier":{"type":"IntervalTypeSpecifier","pointType":{"name":"{urn:hl7-org:elm-types:r1}DateTime","type":"NamedTypeSpecifier"}}},{"localId":"34","locator":"28:51-28:70","name":"Measurement Period","type":"ParameterRef","resultTypeSpecifier":{"type":"IntervalTypeSpecifier","pointType":{"name":"{urn:hl7-org:elm-types:r1}DateTime","type":"NamedTypeSpecifier"}}}]},{"localId":"39","locator":"29:27-29:54","resultTypeName":"{urn:hl7-org:elm-types:r1}Boolean","type":"GreaterOrEqual","operand":[{"localId":"37","locator":"29:27-29:42","resultTypeName":"{urn:hl7-org:elm-types:r1}Quantity","name":"ToQuantity","libraryName":"FHIRHelpers","type":"FunctionRef","operand":[{"path":"length","scope":"Encounter","type":"Property"}]},{"localId":"38","locator":"29:47-29:54","resultTypeName":"{urn:hl7-org:elm-types:r1}Quantity","value":24,"unit":"hours","type":"Quantity"}]}]}}},{"localId":"46","locator":"31:1-32:26","resultTypeName":"{urn:hl7-org:elm-types:r1}Integer","name":"Measure Observation","context":"Patient","accessLevel":"Public","type":"FunctionDef","annotation":[{"type":"Annotation","s":{"r":"46","s":[{"value":["","define function ","\\"Measure Observation\\"","(","Enc"," "]},{"r":"43","s":[{"value":["\\"Encounter\\""]}]},{"value":[" ):\\n "]},{"r":"45","s":[{"r":"45","s":[{"value":["Count","("]},{"r":"44","s":[{"value":["\\"Valid Encounter\\""]}]},{"value":[")"]}]}]}]}}],"expression":{"localId":"45","locator":"32:3-32:26","resultTypeName":"{urn:hl7-org:elm-types:r1}Integer","type":"Count","source":{"localId":"44","locator":"32:9-32:25","name":"Valid Encounter","type":"ExpressionRef","resultTypeSpecifier":{"type":"ListTypeSpecifier","elementType":{"name":"{http://hl7.org/fhir}Encounter","type":"NamedTypeSpecifier"}}}},"operand":[{"name":"Enc","operandTypeSpecifier":{"localId":"43","locator":"31:43-31:53","resultTypeName":"{http://hl7.org/fhir}Encounter","name":"{http://hl7.org/fhir}Encounter","type":"NamedTypeSpecifier"}}]}]}},"externalErrors":[]}`;
32 changes: 32 additions & 0 deletions src/api/useMeasureServiceApi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import useServiceConfig from "./useServiceConfig";
import { ServiceConfig } from "./ServiceContext";
import { Measure, Group } from "@madie/madie-models";
import { useOktaTokens } from "@madie/madie-util";
import _ from "lodash";

export class MeasureServiceApi {
constructor(private baseUrl: string, private getAccessToken: () => string) {}
Expand Down Expand Up @@ -178,6 +179,37 @@ export class MeasureServiceApi {
throw new Error(message);
}
}

getReturnTypesForAllCqlDefinitions(elmJson: string): {
[key: string]: string;
} {
if (!elmJson) {
return {};
}

const elm = JSON.parse(elmJson);
const definitions = elm.library?.statements?.def;
if (definitions) {
return definitions.reduce((returnTypes, definition) => {
const returnType = {};
const name = _.camelCase(_.trim(definition.name));
if (definition.resultTypeName) {
returnType[`${name}`] = definition.resultTypeName;
} else if (definition.resultTypeSpecifier) {
returnType[`${name}`] =
definition.resultTypeSpecifier.elementType.name;
} else {
returnType[`${name}`] = undefined;
}
return {
...returnTypes,
...returnType,
};
}, {});
}

return {};
}
}

export default function useMeasureServiceApi(): MeasureServiceApi {
Expand Down

0 comments on commit a0be17f

Please sign in to comment.