-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path14_run-r-on-Db2.js
264 lines (204 loc) · 7.88 KB
/
14_run-r-on-Db2.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
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
// demo video: https://raw.githubusercontent.com/spackows/CASCON-2017_Analyzing_chat/master/demo-videos/call-14_run-r-db2.mp4
//
var g_XMLHttpRequest = require( 'xmlhttprequest' ).XMLHttpRequest;
var g_btoa = require( 'btoa' );
var g_readFiles = require( './helper_readFiles.js' );
var g_dashDB_host = ''; // Get this from "Service credentials" section of your Db2 Warehouse on Cloud service in IBM Cloud dashboard
var g_dashDB_r_port = '8443'; // *This is not the same port that you would connect SQL apps to (eg. NOT: 50000 or 50001). This needs to be the port the RStudio web console uses (eg. for me, it was 8443).
var g_dashDB_uid = ''; // Get this from "Service credentials" section of your Db2 Warehouse on Cloud service in IBM Cloud dashboard
var g_dashDB_pwd = ''; // Get this from "Service credentials" section of your Db2 Warehouse on Cloud service in IBM Cloud dashboard
var g_file_name = ''; // <- Enter the name of the file containing output from 13_create-r-script.js
g_file_name = '../sample-output/cluster.R'; // Alternatively, you can just use the sample output
g_readFiles.readRScript( g_file_name, function( read_error, script )
{
if( read_error )
{
console.log( "Reading script file failed. Error:\n" + read_error );
}
else
{
//console.log( JSON.stringify( script, null, 3 ) );
var plot_file_names = getPlotNames( script );
console.log( "Estimated time: " + plot_file_names.length + "+ minutes ..." );
runRScriptRemotely( script ).then( function( groups_details )
{
downloadPlots( plot_file_names );
} ).catch( function( error )
{
console.log( error );
} );
}
} );
function getPlotNames( script )
{
var plot_file_names = [];
var regex = new RegExp( /svg\(\s*\"(.*?)\"/ig );
var res = regex.exec( script );
while( null !== res )
{
plot_file_names.push( res[1] );
res = regex.exec( script );
}
return plot_file_names;
}
function runRScriptRemotely( r_script_contents )
{
// REST API doc:
// https://developer.ibm.com/static/site-id/85/api/db2wh/
//
// curl --user "<user-id>:<pwd>"
// -X POST "https://<host-name>:<port>/dashdb-api/rscript"
// -H "Content-Type: application/json"
// -d "{ \"rScript\" : \"version\" }"
//
console.log( "runRScriptRemotely ..." );
var encoded_script = encodeURIComponent( r_script_contents ).replace( /'/g, "%27" ).replace( /"/g, "%22" );
return new Promise( function( resolve, reject )
{
var url = 'https://' + g_dashDB_host + ':' + g_dashDB_r_port + '/dashdb-api/rscript';
var request = new g_XMLHttpRequest();
request.open( 'POST', url, true );
request.setRequestHeader( 'Content-Type', 'application/json' );
request.setRequestHeader( 'Authorization', 'Basic ' + g_btoa( g_dashDB_uid + ':' + g_dashDB_pwd ) );
request.onreadystatechange = function()
{
if( 4 == request.readyState )
{
console.log( JSON.stringify( request, null, 3 ) );
if( 200 == request.status )
{
var result = JSON.parse( request.responseText ).result;
var rScriptError = result.rScriptError;
var file_name = result.filename;
var rScriptOutput = result.rScriptOutput;
var timestamp = result.timestamp;
console.log( "rScriptError:\n" + rScriptError + "\n" );
console.log( "file_name:\n" + file_name + "\n" );
console.log( "rScriptOutput:\n" + rScriptOutput + "\n" );
console.log( "timestamp:\n" + timestamp + "\n" );
resolve();
}
else
{
reject( "runRScriptRemotely request error.\n" + JSON.stringify( request, null, 3 ) );
}
}
};
request.send( JSON.stringify( { "rScript" : encoded_script } ) );
} );
}
function downloadPlots( plot_file_names )
{
console.log( "downloadPlots:\n" + plot_file_names.join( "\n" ) );
iterateOver( plot_file_names, function( file_name, report )
{
downloadRemoteFile( file_name ).then( deleteRemoteFile ).then( function()
{
report();
} ).catch( function( error )
{
console.log( error );
report();
} );
}, function()
{
console.log( "Downloads done." );
} );
}
function downloadRemoteFile( file_name )
{
// REST API doc:
// https://developer.ibm.com/static/site-id/85/api/db2wh/
//
// curl --user "<user-id>:<pwd>"
// -X GET "https://<host-name>:<port>/dashdb-api/home/<file-name>" > test.svg
//
console.log( "downloadRemoteFile [ " + file_name + " ] ..." );
var local_file_name = file_name;
return new Promise( function( resolve, reject )
{
var url = 'https://' + g_dashDB_host + ':' + g_dashDB_r_port + '/dashdb-api/home/' + file_name;
var request = new g_XMLHttpRequest();
request.open( 'GET', url, true );
request.setRequestHeader( 'Authorization', 'Basic ' + g_btoa( g_dashDB_uid + ':' + g_dashDB_pwd ) );
request.onreadystatechange = function()
{
if( 4 == request.readyState )
{
if( 200 == request.status )
{
var fs = require( 'fs' );
fs.writeFile( local_file_name, request.responseText, function ( write_error )
{
if( null !== write_error )
{
reject( "downloadRemoteFile [ " + file_name + " ] write error.\nFile name: " + local_file_name + "Error:\n" + write_error );
}
else
{
resolve( file_name );
}
} );
}
else
{
reject( "downloadRemoteFile [ " + file_name + " ] request error.\n" + JSON.stringify( request, null, 3 ) );
}
}
};
request.send();
} );
}
function deleteRemoteFile( file_name )
{
// REST API doc:
// https://developer.ibm.com/static/site-id/85/api/db2wh/
//
// curl --user "<user-id>:<pwd>"
// -X DELETE "https://<host-name>:<port>/dashdb-api/home/<file-name>"
//
console.log( "deleteRemoteFile [ " + file_name + " ] ..." );
return new Promise( function( resolve, reject )
{
var url = 'https://' + g_dashDB_host + ':' + g_dashDB_r_port + '/dashdb-api/home/' + file_name;
var request = new g_XMLHttpRequest();
request.open( 'DELETE', url, true );
var btoa = require( 'btoa' );
request.setRequestHeader( 'Authorization', 'Basic ' + btoa( g_dashDB_uid + ':' + g_dashDB_pwd ) );
request.onreadystatechange = function()
{
if( 4 == request.readyState )
{
if( 200 == request.status )
{
resolve();
}
else
{
reject( "deleteRemoteFile [ " + file_name + " ] error. Request:\n" + JSON.stringify( request, null, 3 ) );
}
}
};
request.send();
} );
}
function iterateOver( list, iterator, callback )
{
if( 0 === list.length )
{
callback();
}
var doneCount = 0;
function report()
{
doneCount++;
if( doneCount === list.length )
{
callback();
}
}
list.forEach( function( item, index )
{
iterator( item, report );
} );
}