-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathENTSOGconnector.js
166 lines (139 loc) · 4.58 KB
/
ENTSOGconnector.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
(function() {
// Create the connector object
var myConnector = tableau.makeConnector();
// Define the schema
myConnector.getSchema = function(schemaCallback) {
var cols = [{
id: "id",
dataType: tableau.dataTypeEnum.string
}, {
id: "periodFrom",
alias: "Period start time",
dataType: tableau.dataTypeEnum.string
}, {
id: "periodTo",
alias: "Period end time",
dataType: tableau.dataTypeEnum.string
}, {
id: "countryKey",
alias: "Country abbreviation",
dataType: tableau.dataTypeEnum.string
}, {
id: "countryLabel",
alias: "Country name",
dataType: tableau.dataTypeEnum.string
}, {
id: "adjacentSystemsKey",
alias: "Adjacent system key",
dataType: tableau.dataTypeEnum.string
}, {
id: "adjacentSystemsLabel",
alias: "Adjacent system name",
dataType: tableau.dataTypeEnum.string
}, {
id: "directionKey",
alias: "Direction of transmission",
dataType: tableau.dataTypeEnum.string
}, {
id: "value",
alias: "Value of transmission",
dataType: tableau.dataTypeEnum.float
}, {
id: "unit",
alias: "Tranmission unit",
dataType: tableau.dataTypeEnum.string
}];
var tableSchema = {
id: "EntsogFeed",
alias: "Natural gas transmission",
columns: cols
};
schemaCallback([tableSchema]);
};
// Download the data
myConnector.getData = function(table, doneCallback) {
var dateObj = JSON.parse(tableau.connectionData),
start = dateObj.startDate,
end = dateObj.endDate,
start_point = new Date(start),
end_point = new Date(end),
diff = (end_point - start_point)/86400000,
pointer = new Date(start);
for (var count=0 ; count < diff; count ++) {
start_string = pointer.toISOString().split('T')[0];
pointer.setDate(pointer.getDate() + 1);
end_string = pointer.toISOString().split('T')[0];
api_call = "https://transparency.entsog.eu/api/v1/aggregateddata?&from="+ start_string + "&to=" + end_string + "&periodType=day&timezone=CET&limit=-1"
$.ajax({
url:api_call,
async: false,
success: function(resp) {
var data = resp.aggregateddata,
tableData = [];
for (var i = 0, len = data.length; i < len; i++) {
tableData.push({
"id": data[i].id,
"periodFrom": data[i].periodFrom,
"periodTo": data[i].periodTo,
"countryKey": data[i].countryKey,
"countryLabel": data[i].countryLabel,
"adjacentSystemsKey": data[i].adjacentSystemsKey,
"adjacentSystemsLabel": data[i].adjacentSystemsLabel,
"directionKey": data[i].directionKey,
"value": data[i].value,
"unit": data[i].unit
});
}
tableau.reportProgress("Getting data on " + start_string)
table.appendRows(tableData);
},
});
};
doneCallback();
};
tableau.registerConnector(myConnector);
// Create event listeners for when the user submits the form
$(document).ready(function() {
$("#submitButton").click(function() {
var dateObj = {
startDate: $('#start-date-one').val().trim(),
endDate: $('#end-date-one').val().trim(),
};
// Simple date validation: Call the getDate function on the date object created
function isValidDate(dateStr) {
var d = new Date(dateStr);
return !isNaN(d.getDate());
}
if (isValidDate(dateObj.startDate) && isValidDate(dateObj.endDate)) {
tableau.connectionData =JSON.stringify(dateObj); // Use this variable to pass data to your getSchema and getData functions
tableau.connectionName = "ENTSOG Data Feed"; // This will be the data source name in Tableau
tableau.submit(); // This sends the connector object to Tableau
} else {
$('#errorMsg').html("Enter valid dates. For example, 2015/01/01.");
}
});
});
})();
// var text = "";
// var i = 0;
// a = [1,2,3,4,5,6,7,8,9,10]
// while (i < 10) {
// text += "<br>The number is " + i;
// console.log(a[i])
// i += 2;
// }
// start = '2019/01/01'
// end = '2023/01/08'
// start_point = new Date(start)
// end_point = new Date(end)
// diff = (end_point - start_point)/86400000
// console.log(diff)
// pointer = new Date(start)
// console.log(pointer)
// for (var i=0; i < diff; i++) {
// start_string = pointer.toISOString().split('T')[0];
// pointer.setDate(pointer.getDate() + 1);
// end_string = pointer.toISOString().split('T')[0];
// console.log(start_string, end_string)
// }
// "https://transparency.entsog.eu/api/v1/aggregateddata?&from=2015-01-01&to=2015-01-02&periodType=day&timezone=CET&limit=-1.json"