-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code.gs
305 lines (242 loc) · 6.51 KB
/
Code.gs
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
var DiagnosticReport = "DiagnosticReport";
var Observation = "Observation";
var id = "id";
var report_type = "report_type";
var issued_date = "issued_date";
var conclusion = "conclusion";
var observation_type = "observation_type";
var value = "value";
var unit = "unit";
var ref_country = "reference_country";
var ref_state = "reference_state";
var ref_city = "reference_city";
var cc = DataStudioApp.createCommunityConnector();
function getAuthType() {
var AuthTypes = cc.AuthType;
return cc
.newAuthTypeResponse()
.setAuthType(AuthTypes.NONE)
.build();
}
function getConfig() {
var config = cc.getConfig();
config
.newTextInput()
.setId('url')
.setName(
'FHIR Url'
)
.setHelpText('e.g. http://www.example.com/fhir')
.setAllowOverride(true);
var DiagnosticReportOption = config.newOptionBuilder()
.setLabel(DiagnosticReport)
.setValue(DiagnosticReport);
var ObservationOption = config.newOptionBuilder()
.setLabel(Observation)
.setValue(Observation);
config
.newSelectSingle()
.setId('resource')
.addOption(DiagnosticReportOption)
.addOption(ObservationOption)
.setName(
'Select Resource'
)
.setHelpText('Select a resource on which you want to search.')
.setAllowOverride(true);
config
.newTextInput()
.setId('payload')
.setName(
'POST Payload'
)
.setHelpText('e.g. value1=one&value2=two, for date placeholders you can put like issued=ge#START_DATE#&issued=le#END_DATE#, date format is YYYY-MM-DD')
.setAllowOverride(true);
config
.newTextInput()
.setId('headers')
.setName(
'Request Headers'
)
.setHelpText('e.g. "{"Accept:*/*", "Authorization: Bearer xxxxx"}"')
.setAllowOverride(true);
config.setDateRangeRequired(true);
return config.build();
}
function getFields(resource) {
var fields = cc.getFields();
var types = cc.FieldType;
fields
.newDimension()
.setId(id)
.setName('Id')
.setType(types.TEXT);
if(resource == DiagnosticReport){
fields
.newDimension()
.setId(report_type)
.setName('Report Type')
.setType(types.TEXT);
fields
.newDimension()
.setId(issued_date)
.setName('Issued Date')
.setType(types.TEXT);
fields
.newDimension()
.setId(conclusion)
.setName('Conclusion')
.setType(types.TEXT);
}
else if (resource == Observation){
fields
.newDimension()
.setId(observation_type)
.setName('Observation Type')
.setType(types.TEXT);
fields
.newDimension()
.setId(issued_date)
.setName('Issued Date')
.setType(types.TEXT);
fields
.newDimension()
.setId(value)
.setName('Value')
.setType(types.TEXT);
fields
.newDimension()
.setId(unit)
.setName('Unit')
.setType(types.TEXT);
fields
.newDimension()
.setId(ref_country)
.setName('Country')
.setType(types.TEXT);
fields
.newDimension()
.setId(ref_state)
.setName('State')
.setType(types.TEXT);
fields
.newDimension()
.setId(ref_city)
.setName('City')
.setType(types.TEXT);
}
return fields;
}
function getSchema(request) {
var schema = {schema: getFields(request.configParams.resource).build()};
return schema;
}
function isAdminUser(){
return false;
}
function getData(request) {
try {
var apiResponse = fetchDataFromApi(request);
var objResponse = JSON.parse(apiResponse);
var rows = [];
var schema = [];
var entries = objResponse.hasOwnProperty("entry")? objResponse["entry"] : [];
prepareForDiagnosticReport(request, entries, schema, rows);
var result = {
schema: schema,
rows: rows
};
return result;
}
catch (e) {
cc.newUserError()
.setDebugText('Error fetching data from API. Exception details: ' + e)
.setText(
'The connector has encountered an unrecoverable error. Please try again later, or file an issue if this error persists.'
)
.throwException();
}
}
function prepareForDiagnosticReport(request, entries, schema, rows){
var defaultSchema = getSchema(request).schema;
request.fields.forEach(field => {
schema.push(getItemFromDefaultSchema(defaultSchema, request, field.name));
});
var values;
entries.forEach(entry => {
values = [];
schema.forEach(item => {
includeRowItem(entry, item.name, values);
});
rows.push({values: values});
});
}
function includeRowItem(entry, name, values){
switch(name){
case id:
values.push(entry.resource.id);
break;
case report_type:
values.push(entry.resource.code.text);
break;
case issued_date:
values.push(entry.resource.issued);
break;
case conclusion:
values.push(entry.resource.conclusion);
break;
case observation_type:
if(entry.resource.code.text != null){
values.push(entry.resource.code.text);
}
else{
values.push(entry.resource.code.coding[0].display);
}
break;
case ref_country:
console.log(entry.resource.extension[1].valueString);
values.push(entry.resource.extension[1].valueString);
break;
case ref_state:
values.push(entry.resource.extension[2].valueString);
break;
case ref_city:
values.push(entry.resource.extension[3].valueString);
break;
case value:
if(entry.resource.hasOwnProperty("valueQuantity")){
values.push(entry.resource.valueQuantity.value.toString());
}
else if(entry.resource.hasOwnProperty("valueString")){
values.push(entry.resource.valueString);
}
break;
case unit:
if(entry.resource.hasOwnProperty("valueQuantity")){
values.push(entry.resource.valueQuantity.unit);
}
else if(entry.resource.hasOwnProperty("valueString")){
values.push("N/A");
}
break;
}
}
function getItemFromDefaultSchema(defaultSchema, request, name){
return defaultSchema.find(item => item.name == name);
}
function fetchDataFromApi(request) {
// Make a POST request with a JSON payload
var payload = request.configParams.payload.replace("#START_DATE#", request.dateRange.startDate);
payload = payload.replace("#END_DATE#", request.dateRange.endDate);
var data = payload;
var options = {
'method' : 'POST',
'headers': JSON.parse(request.configParams.headers),
'payload' : data,
'muteHttpExceptions': true
};
var response = UrlFetchApp.fetch(request.configParams.url + '/' + request.configParams.resource + '/_search', options);
// console.log(response.getResponseCode());
// console.log(response.getContentText());
return response;
}