-
Notifications
You must be signed in to change notification settings - Fork 0
/
npm.js
81 lines (68 loc) · 2.54 KB
/
npm.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
(function() {
// Create the connector object
var myConnector = tableau.makeConnector();
// Define the schema
myConnector.getSchema = function(schemaCallback) {
var cols = [{
id: "name",
alias: "name",
dataType: tableau.dataTypeEnum.string
}, {
id: "downloads",
alias: "downloads",
dataType: tableau.dataTypeEnum.int
}, {
id: "date",
alias: "date",
dataType: tableau.dataTypeEnum.string
}];
var packages = tableau.connectionData.split(";")[0].replace(/\s+/g, '').split(',');
schemas = packages.map(function(name){
return {
id: name,
alias: name,
columns: cols
}
})
schemaCallback(schemas);
};
// Download the data
myConnector.getData = function(table, doneCallback) {
var dates = tableau.connectionData.split(';')[1];
var apiCall = "https://api.npmjs.org/downloads/range/"+dates+"/" + table.tableInfo.id;
tableau.log("dates: " + dates);
tableau.log("api call: " + apiCall);
$.getJSON(apiCall, function(resp) {
tableau.log("resp: " + resp);
var dates = resp.downloads;
table.appendRows(dates.map(function(date){
return {
name: table.tableInfo.id,
date: date.day,
downloads: date.downloads
}
}));
doneCallback();
});
};
tableau.registerConnector(myConnector);
// Create event listeners for when the user submits the form
$(document).ready(function() {
$('.date').datepicker({
todayBtn: true,
format: 'yyyy-mm-dd'
});
$("#submitButton").click(function() {
var packages = $('#packages').val().trim();
var startDate = $('#startDate').val().trim();
var endDate = $('#endDate').val().trim();
if (packages && startDate && endDate) {
tableau.connectionData = packages + ";" + startDate + ":" + endDate; // Use this variable to pass data to your getSchema and getData functions
tableau.connectionName = "NPM Packages"; // This will be the data source name in Tableau
tableau.submit(); // This sends the connector object to Tableau
} else {
alert("Enter a valid date for each date range and packages.");
}
});
});
})();