-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdwPrototype.js
153 lines (132 loc) · 5.2 KB
/
dwPrototype.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
(function() {
// Create the connector object
var myConnector = tableau.makeConnector();
// Define the schema
myConnector.getSchema = function(schemaCallback) {
// [TODO] API call should be to dataset schema - currently just querying table and using metadata (which is also wrong)
var datasetCreds = JSON.parse(tableau.connectionData);
var createTable = function (tableName) {
return new Promise(function(resolve, reject) {
var queryTable = tableName;
var tableApiCall = getApiCall(datasetCreds, queryTable);
return $.getJSON(tableApiCall, function(resp) {
var metadata = resp.metadata,
columnIds = resp.head.vars,
dataset_cols = [];
var i = 0;
for (i = 0, len = metadata.length; i < len; i++) {
var name = metadata[i].name;
var idName = name.replace(/[^A-Z0-9]/ig, "");
var dataType = "http://www.w3.org/2001/XMLSchema#string"; //setting default to string
if(resp.results.bindings[0].hasOwnProperty(columnIds[i])) {
dataType = resp.results.bindings[0][columnIds[i]].datatype;
}
dataset_cols.push({
"id": columnIds[i],
"alias": metadata[i].name,
"dataType": getDatatype(dataType)
}); //push close
} // for close
var datasetTable = {
id: queryTable,
alias: queryTable,
columns: dataset_cols
};
resolve(datasetTable)
});
})
} // createTable end
getTables(datasetCreds).then(function(tables) {
Promise.all(
tables.map(function(tableName) {
return createTable(tableName)
})
).then(function(results) {
schemaCallback(results);
}).catch(function(error) {
console.error('oh no', error)
})
})
}; // myConnector.getSchema end
function getTables (datasetCreds) {
return new Promise(function(resolve, reject) {
var queryTable = "Tables";
$.getJSON(getApiCall(datasetCreds, queryTable), function(resp) {
var datasetTablesResults = resp.results.bindings,
datasetTables = [];
for (var i = 0, dtLen = datasetTablesResults.length; i < dtLen; i++) {
datasetTables[i] = datasetTablesResults[i].v_1.value;
}
resolve(datasetTables)
});
})
}
function getApiCall(datasetCreds, queryTable) {
var apiCall = "http://localhost:8889/query.data.world/sql/" + datasetCreds.dataset + "?authentication=Bearer+" + datasetCreds.apiToken + "&query=SELECT%20*%20FROM%20%60" + queryTable + "%60";
return apiCall;
};
// [TODO] switch case for correct dataType - metadata only uses string field, so needs to be updated
function getDatatype(datatype) {
var tDatatype = null;
switch (datatype) {
case "http://www.w3.org/2001/XMLSchema#string":
tDatatype = tableau.dataTypeEnum.string;
break;
case "http://www.w3.org/2001/XMLSchema#integer":
tDatatype = tableau.dataTypeEnum.int;
break;
case "http://www.w3.org/2001/XMLSchema#decimal":
tDatatype = tableau.dataTypeEnum.float;
break;
case "http://www.w3.org/2001/XMLSchema#boolean":
tDatatype = tableau.dataTypeEnum.bool;
break;
case "http://www.w3.org/2001/XMLSchema#date":
tDatatype = tableau.dataTypeEnum.date;
break;
case "http://www.w3.org/2001/XMLSchema#dateTime":
tDatatype = tableau.dataTypeEnum.datetime;
break;
default:
tDatatype = tableau.dataTypeEnum.string;
}
return tDatatype;
};
// Download the data
myConnector.getData = function(table, doneCallback) {
// [TODO] API call should be to dataset schema - currently just querying table and using metadata (which is also wrong)
var datasetCreds = JSON.parse(tableau.connectionData);
var filesApiCall = getApiCall(datasetCreds, table.tableInfo.id);
$.getJSON(filesApiCall, function(resp) {
var results = resp.results.bindings,
columnIds = resp.head.vars,
tableData = [];
var i = 0;
for (i = 0, resLen = results.length; i < resLen; i++) {
var jsonData = {};
for(var j = 0, colLen = columnIds.length; j < colLen; j++) {
var id = columnIds[j];
if (results[i][id]) {
jsonData[id] = results[i][columnIds[j]].value;
}
}
tableData.push(jsonData);
}
table.appendRows(tableData);
doneCallback();
});
};
tableau.registerConnector(myConnector);
// Create event listeners for when the user submits the form
$(document).ready(function() {
$("#submitButton").click(function() {
var datasetCreds = {
dataset: $('#dwDataset').val().trim(),
apiToken: $('#apiToken').val().trim()
};
tableau.connectionData = JSON.stringify(datasetCreds);
tableau.connectionName = "data.world connector";
tableau.submit();
});
});
})();