-
Notifications
You must be signed in to change notification settings - Fork 18
/
evaluate-measure.sh
executable file
·148 lines (129 loc) · 3.97 KB
/
evaluate-measure.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env bash
# Usage: ./evaluate-measure.sh -f <query>.cql <server-base>
# Takes a CQL file, creates a Library resource from it, references that from a
# Measure resource and calls $evaluate-measure on it.
library() {
cat <<END
{
"resourceType": "Library",
"status": "active",
"type" : {
"coding" : [
{
"system": "http://terminology.hl7.org/CodeSystem/library-type",
"code" : "logic-library"
}
]
},
"content": [
{
"contentType": "text/cql"
}
]
}
END
}
measure() {
cat <<END
{
"resourceType": "Measure",
"status": "active",
"subjectCodeableConcept": {
"coding": [
{
"system": "http://hl7.org/fhir/resource-types",
"code": "Patient"
}
]
},
"scoring": {
"coding": [
{
"system": "http://terminology.hl7.org/CodeSystem/measure-scoring",
"code": "cohort"
}
]
},
"group": [
{
"population": [
{
"code": {
"coding": [
{
"system": "http://terminology.hl7.org/CodeSystem/measure-population",
"code": "initial-population"
}
]
},
"criteria": {
"language": "text/cql-identifier",
"expression": "InInitialPopulation"
}
}
]
}
]
}
END
}
create-library() {
library | jq -cM ".url = \"urn:uuid:$1\" | .content[0].data = \"$2\""
}
create-measure() {
measure | jq -cM ".url = \"urn:uuid:$1\" | .library[0] = \"urn:uuid:$2\" | .subjectCodeableConcept.coding[0].code = \"$3\""
}
post() {
curl -sH "Content-Type: application/fhir+json" -d @- "$BASE/$1"
}
evaluate-measure() {
curl -s "$BASE/Measure/$1/\$evaluate-measure?periodStart=2000&periodEnd=2030"
}
evaluate-measure-list() {
curl -sd '{"resourceType": "Parameters", "parameter": [{"name": "periodStart", "valueDate": "2000"}, {"name": "periodEnd", "valueDate": "2030"}, {"name": "reportType", "valueCode": "subject-list"}]}' \
-H "Content-Type: application/fhir+json" "$BASE/Measure/$1/\$evaluate-measure"
}
usage()
{
echo "Usage: $0 -f QUERY_FILE [ -t subject-type ] [ -r report-type ] BASE"
echo ""
echo "Example subject-types: Patient, Specimen; default is Patient"
echo "Possible report-types: subject-list, population; default is population"
exit 2
}
unset FILE SUBJECT_TYPE REPORT_TYPE BASE
while getopts 'f:t:r:' c
do
case ${c} in
f) FILE=$OPTARG ;;
t) SUBJECT_TYPE=$OPTARG ;;
r) REPORT_TYPE=$OPTARG ;;
esac
done
shift $((OPTIND-1))
BASE=$1
[[ -z "$FILE" ]] && usage
[[ -z "$SUBJECT_TYPE" ]] && SUBJECT_TYPE="Patient"
[[ -z "$BASE" ]] && usage
SUBJECT_TYPE_LOWER=$(echo $SUBJECT_TYPE | tr '[:upper:]' '[:lower:]')
DATA=$(base64 < "$FILE" | tr -d '\n')
LIBRARY_URI=$(uuidgen | tr '[:upper:]' '[:lower:]')
MEASURE_URI=$(uuidgen | tr '[:upper:]' '[:lower:]')
create-library ${LIBRARY_URI} ${DATA} | post "Library" > /dev/null
MEASURE_ID=$(create-measure ${MEASURE_URI} ${LIBRARY_URI} ${SUBJECT_TYPE} | post "Measure" | jq -r .id | tr -d '\r')
if [ "subject-list" = "$REPORT_TYPE" ]; then
echo "Generating a report including the list of matching ${SUBJECT_TYPE_LOWER}s..."
MEASURE_REPORT=$(evaluate-measure-list ${MEASURE_ID})
COUNT=$(echo $MEASURE_REPORT | jq -r '.group[0].population[0].count' | tr -d '\r')
LIST_REFERENCE=$(echo $MEASURE_REPORT | jq -r '.group[0].population[0].subjectResults.reference' | tr -d '\r')
LIST_ID=$(echo $LIST_REFERENCE | cut -d '/' -f2)
echo "Found $COUNT ${SUBJECT_TYPE_LOWER}s that can be found on List $BASE/$LIST_REFERENCE."
echo ""
echo "Please use the following blazectl command to download the ${SUBJECT_TYPE_LOWER}s:"
echo " blazectl download --server $BASE $SUBJECT_TYPE -q '_list=$LIST_ID' -o ${SUBJECT_TYPE_LOWER}s.ndjson"
else
echo "Generating a population count report..."
MEASURE_REPORT=$(evaluate-measure ${MEASURE_ID})
COUNT=$(echo $MEASURE_REPORT | jq -r '.group[0].population[0].count' | tr -d '\r')
echo "Found $COUNT ${SUBJECT_TYPE_LOWER}s."
fi