-
Notifications
You must be signed in to change notification settings - Fork 2
/
connector.js
220 lines (180 loc) · 6.46 KB
/
connector.js
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
var startDateRegex = /\{dateRange\.startDate\}/gi;
var endDateRegex = /\{dateRange\.endDate\}/gi;
var numDaysRegex = /\{dateRange\.numDays\}/gi;
var ONE_DAY = 24 * 60 * 60 * 1000; //milliseconds
var cachedSchemaFields = null;
//
// Public API
//
function getAuthType() {
return {type: 'NONE'};
}
function getConfig() {
var cc = DataStudioApp.createCommunityConnector();
var config = cc.getConfig();
config.newTextInput()
.setId('endpoint')
.setName('Enter SPARQL Endpoint URL');
config.newTextArea()
.setId('query')
.setName('Enter SPARQL SELECT query');
config.newInfo()
.setText("Only SPARQL SELECT queries with projections are supported (query with * isn\'t supported). " +
"Add a filter by date with placeholders for start and end dates, e.g. " +
"FILTER(?startTime > \"{dateRange.startDate}\"^^xsd:dateTime && ?endTime < \"{dateRange.endDate}\"^^xsd:dateTime)");
config.newTextArea()
.setId('schema')
.setName('Enter the schema of query results');
config.newInfo()
.setText("The schema defines data types, etc. of the projected variables. An example:\n" +
"[{ \"name\":\"location\", \"dataType\":\"STRING\" }, { \"name\":\"price\", \"dataType\":\"NUMBER\" }]. More about schemas: https://developers.google.com/datastudio/connector/semantics")
config.setDateRangeRequired(true);
return config.build();
}
function isAdminUser() {
return true;
}
function getSchema(request) {
console.info('[getSchema] request: %s', request);
_checkEndpointURL(request.configParams.endpoint);
var schema = JSON.parse(request.configParams.schema);
return {schema: schema};
}
function getData(request) {
console.info("[getData] request: %s", request);
var schema = filteredSchema(JSON.parse(request.configParams.schema), request.fields);
if(request.scriptParams && request.scriptParams.sampleExtraction) {
console.log("[getData] sampleExtraction");
return { schema: schema, rows: [] };
} else {
var rows = execute(request.configParams.endpoint, request.configParams.query, request);
console.info("[getData] schema: %s", schema);
console.info("[getData] rows: %s", rows);
return { schema: schema, rows: rows };
}
}
//
// Private API
//
/**
* Checks if the given SPARQL endpoint is accessible and supports JSON as the result format.
* @param {string}
*/
function _checkEndpointURL(url) {
var body = null;
try {
var formData = { 'query': 'SELECT * {?x ?y ?z} LIMIT 1' };
var requestOptions = {
'method': 'post',
'headers': { Accept: 'application/sparql-results+json' },
'contentType': 'application/x-www-form-urlencoded',
'payload': formData
};
var response = UrlFetchApp.fetch(url, requestOptions);
body = JSON.parse(response);
} catch(error) {
logConnectorError(error, 'dysfunctional_endpoint');
throwConnectorError("Failed to communicate with the endpoint. Please, check SPARQL endpoint url.", true);
}
if(!body.head || !body.results) {
throwConnectorError("Failed to handle SPARQL endpoint result format. Please, check that SPARQL endpoint supports JSON as result format.", true);
}
}
function execute(endpoint, query, options) {
try {
var formData = {
'query': prepareQuery(query, options)
};
} catch(e) {
logConnectorError(e, 'prepare_query');
throwConnectorError("Failed to pre-process the query. Please, check your SPARQL query.", true);
}
try {
var requestOptions = {
'method': 'post',
'headers': { Accept: 'application/sparql-results+json' },
'contentType': 'application/x-www-form-urlencoded',
'payload': formData
};
var response = UrlFetchApp.fetch(endpoint, requestOptions);
console.info("[execute] response: %s", response);
} catch(e) {
logConnectorError(e, 'execute_query');
throwConnectorError("Failed to execute the query. Please, check the endpoint URL and the query.", true);
}
try {
var body = JSON.parse(response);
console.info("[execute] body: %s", body);
} catch(e) {
logConnectorError(e, 'parse_json');
throwConnectorError("Failed to parse the query results. Please, check that the SPARQL endpoint supports 'application/sparql-results+json'.", true);
}
try {
if(isQueryResultEmpty(body)) {
return { values: [] };
} else {
return body.results.bindings.map(function(row) {
var values = new Array();
options.fields.forEach(function(rf) {
var cell = row[rf.name];
if(cell) {
values.push(reformatByDatatype(cell.value, cell.datatype));
} else {
values.push(getDefaultValue(cachedSchemaFields[rf.name]));
}
});
return { values: values };
});
}
} catch(e) {
logConnectorError(e, 'postprocess_results');
}
}
function filteredSchema(schema, requestedFields) {
if(!cachedSchemaFields) {
cachedSchemaFields = new Object();
schema.forEach(function(field) {
cachedSchemaFields[field.name] = field;
});
}
return requestedFields.map(function(field) {
return cachedSchemaFields[field.name];
});
}
function prepareQuery(query, options) {
var preparedQuery = query;
if(options.dateRange && options.dateRange.startDate && options.dateRange.endDate) {
var startDate = new Date(options.dateRange.startDate);
var endDate = new Date(options.dateRange.endDate);
if(endDate.getTime() > Date.now()) {
var now = new Date();
now.setDate(now.getDate() - 1);
endDate = now;
}
var numDays = Math.round((endDate.getTime() - startDate.getTime()) / ONE_DAY) + 1;
preparedQuery = preparedQuery
.replace(startDateRegex, options.dateRange.startDate)
.replace(endDateRegex, options.dateRange.endDate)
.replace(numDaysRegex, numDays);
}
if(options.pagination){
if(options.pagination.rowCount) {
preparedQuery += "\nLIMIT " + parseInt(options.pagination.rowCount);
}
if(options.pagination.startRow) {
preparedQuery += "\nOFFSET " + (parseInt(options.pagination.startRow) - 1);
}
}
console.info("[prepareQuery] query: %s", preparedQuery);
return preparedQuery;
}
function isQueryResultEmpty(qr) {
if(qr.results.bindings.length == 0) {
return true;
} else if(qr.results.bindings.length == 1) {
return qr.head.vars.some(function(variable) {
return !qr.results.bindings[0].hasOwnProperty(variable);
});
}
return false;
}