-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv-converter.sh
More file actions
executable file
·168 lines (146 loc) · 6.01 KB
/
csv-converter.sh
File metadata and controls
executable file
·168 lines (146 loc) · 6.01 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/bin/bash -e
usage() {
echo "Usage: $0 <measure-report> <output-dir>"
echo " $0 <fhir-url> <output-dir> [-u <user> -p <password>]"
echo " $0 <fhir-url> <output-dir> [-i <issuer-url> -c <client-id> -s <client-secret>]"
exit 1
}
if [[ $# -lt 2 ]]; then
usage
fi
report=""
output_dir=""
url=""
user=""
password=""
issuer_url=""
client_id=""
client_secret=""
read_http_args() {
url="$1"
shift
while getopts ":u:p:i:c:s:" opt; do
case $opt in
u)
user="$OPTARG"
;;
p)
password="$OPTARG"
;;
i)
issuer_url="$OPTARG"
;;
c)
client_id="$OPTARG"
;;
s)
client_secret="$OPTARG"
;;
*)
echo "Unknown option: -$OPTARG"
usage
;;
esac
done
}
http_get_report() {
auth="$1"
url="$2"
if [ "$auth" == "no-auth" ]; then
response=$(curl -s "$url" \
-H "Content-Type: application/fhir+json")
echo "$response"
elif [ "$auth" == "basic-auth" ]; then
fhir_user="$3"
fhir_password="$4"
response=$(curl -s "$url" \
-H "Content-Type: application/fhir+json" \
-u "$fhir_user:$fhir_password")
echo "$response"
else
fhir_oauth_issuer_url="$3"
fhir_oauth_client_id="$4"
fhir_oauth_client_secret="$5"
oauth_response=$(curl -s -X POST "$fhir_oauth_issuer_url" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=client_credentials" \
-d "client_id=$fhir_oauth_client_id" \
-d "client_secret=$fhir_oauth_client_secret")
fhir_report_bearer_token=$(echo "$oauth_response" | jq -r '.access_token')
response=$(curl -s "$url" \
-H "Content-Type: application/fhir+json" \
-H "Authorization: Bearer $fhir_report_bearer_token")
echo "$response"
fi
}
if [[ ! -d $2 ]]; then
echo "Second arg is not a directory."
usage
else
output_dir="$2"
fi
if [[ "$1" == http?(s)://* ]]; then
if [[ $# == 2 ]]; then
echo "Using FHIR URL without authentication."
report="$(http_get_report "no-auth" "$1")"
else
read_http_args "$1" "${@:3}"
if [[ -n "$user" && -n "$password" ]]; then
echo "Using FHIR URL with user/password authentication."
report="$(http_get_report "basic-auth" "$url" "$user" "$password")"
elif [[ -n "$issuer_url" && -n "$client_id" && -n "$client_secret" ]]; then
echo "Using FHIR URL with OAuth2 client credentials."
http_get_report "oauth" "$url" "$issuer_url" "$client_id" "$client_secret"
report="$(http_get_report "oauth" "$url" "$issuer_url" "$client_id" "$client_secret")"
else
echo "Error: Missing required arguments for FHIR URL authentication."
usage
fi
fi
elif [[ -f $1 ]]; then
echo "Reading MeasureReport from file."
report=$(cat "$1")
else
echo "First argument is neither a file or directory."
usage
exit 1
fi
resource_type=$(echo "$report" | jq -r '.resourceType')
if [[ "$resource_type" != "MeasureReport" ]]; then
echo "Invalid input: File or url contains resource of type '$resource_type' but must be 'MeasureReport'"
usage
fi
echo "$report" | jq -c '.group[]' | while IFS= read -r group; do
echo "$group" | jq -c '.stratifier[]' | while IFS= read -r stratifier; do
if ! echo "$stratifier" | jq -e 'has("stratum")' > /dev/null; then
continue
fi
strat_codes=$(echo "$stratifier" | jq --raw-output 'if has("code") then (.code[0].coding[0].code) else (.stratum[0].component | map(.code.coding[0].code) | join("-")) end')
filename="${output_dir}/${strat_codes}.csv"
result=$(echo "$stratifier" | jq --raw-output '
(.) as $stratifier
| .stratum[0]
| if has("component") then
([$stratifier.stratum[0] | .component | sort_by(.code.coding[0].code)
| map(["system", .code.coding[0].code, "display"])]| flatten + ["count"] as $header
| $stratifier.stratum[0]
| if has("measureScore") then $header + ["unique count"] else $header end),
($stratifier.stratum[] as $stratum | $stratum.component | sort_by(.code.coding[0].code)
| map([.value.coding[0].system, .value.coding[0].code, .value.coding[0].display])
| flatten as $row
| $stratum.population[] | select(.code.coding[0].code == "initial-population") | .count as $count
| $row + [$count] as $row
| $stratum
| if has("measureScore") then $row + [.measureScore.value] else $row end)
else
(["system", "code", "display", "count"] as $header
| if has("measureScore") then $header + ["unique count"] else $header end),
( $stratifier.stratum[] as $stratum
| $stratum.population[] | select(.code.coding[0].code == "initial-population") | .count as $count
| [$stratum.value.coding[0].system, $stratum.value.coding[0].code, $stratum.value.coding[0].display, $count] as $row
| $stratum | if has("measureScore") then $row + [.measureScore.value] else $row end)
end
| @csv')
echo "${result}" > "$filename"
done
done