-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgdc.class.php
More file actions
521 lines (429 loc) · 13.7 KB
/
gdc.class.php
File metadata and controls
521 lines (429 loc) · 13.7 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
<?php
/*
* gdc.class.php
*
* GoodData PHP Client Library
*
* Copyright (C) 2013 Tomas Jirotka <tomjir@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
require_once( 'httpful/bootstrap.php' );
# Main GDC class.
# Use this class to connect to GoodData platform.
class GDC {
const GDC_REST_SERVER = 'https://secure.gooddata.com';
const GDC_FTP_SERVER = 'secure-di.gooddata.com';
const GDC_COOKIE_AUTH = 'GDCAuthSST';
const GDC_COOKIE_TOKEN = 'GDCAuthTT';
const GDC_API_LOGIN = '/gdc/account/login';
const GDC_API_TOKEN = '/gdc/account/token';
const GDC_API_MD = '/gdc/md';
const GDC_API_ETL = '/gdc/md/<project>/etl/pull2';
const GDC_API_DATASETS = '/gdc/md/<project>/data/sets';
const GDC_API_SLI = '/gdc/md/<project>/ldm/singleloadinterface';
const GDC_API_ID_TO_URI = '/gdc/md/<project>/identifiers';
protected $server;
protected $cookies;
protected $project;
protected $templates;
protected $debug;
public $user;
public function __construct( $server = self::GDC_REST_SERVER ) {
$this->server = $server;
$this->debug = FALSE;
$this->templates = array();
$this->user = array();
$this->cookies = array();
$this->cookies['auth' ] = '';
$this->cookies['token'] = '';
}
######### GD AUTHENTICATION #########
# Authenticate against GDC API, get authentication and security token
# Params: username string, password string
# Return: authentication token string
public function login( $login, $passwd ) {
$r = $this->_post( self::GDC_API_LOGIN, '{"postUserLogin":{"login":"'.$login.'","password":"'.$passwd.'","remember":1}}' );
$this->cookies['auth'] = $this->_extract_cookie( $r, self::GDC_COOKIE_AUTH );
if( $this->cookies['auth'] == '' ) {
throw new Exception( 'Authentication failed' );
}
$this->token();
$this->user['name'] = $login;
$this->user['passwd'] = $passwd;
return $this->cookies['auth'];
}
# Refresh security token
# Params: authentication token string optional
# Returns: security token string
public function token( $token = '' ) {
if( isset( $token ) && $token != '' && $this->cookies['auth'] == '' ) {
$this->cookies['auth'] = $token;
}
$this->cookies['token'] = '';
$r = $this->_get( self::GDC_API_TOKEN );
$this->cookies['token'] = $this->_extract_cookie( $r, self::GDC_COOKIE_TOKEN );
if( $this->cookies['token'] == '' ) {
throw new Exception( 'Getting token failed' );
}
return $this->cookies['token'];
}
######### GD PROJECTS #########
# GET list of projects enabled with user
# Return: array ( id => title )
public function list_projects() {
$r = $this->_get( self::GDC_API_MD );
$p = array();
foreach( $r->body->about->links as $prj ) {
$p[ $prj->identifier ] = $prj->title;
}
return $p;
}
# Set working project
# Param: project identifier string
# Return: string
public function set_project( $project ) {
return ( $this->project = $project );
}
# Get working project
# Return: string
public function get_project() {
return $this->project;
}
######### GD OBJECTS #########
# GET list of project dataset
# Return: array
public function list_datasets() {
$r = $this->_get( self::GDC_API_DATASETS );
$d = array();
foreach( $r->body->dataSetsInfo->sets as $ds ) {
$d[ $ds->meta->identifier ] = $ds;
}
return $d;
}
# GET object definition and meta
# Param: identifier string
# Return: object
public function get_object( $identifier ) {
$r = $this->_post( self::GDC_API_ID_TO_URI, '{"identifierToUri":["'.$identifier.'"]}' );
return $this->_get( $r->body->identifiers[0]->uri )->body;
}
# Get instance of GdcDataset.
# Param: string identifier of dataset
# Return: object GdcDataset
public function get_dataset( $identifier ) {
return new GdcDataset( $this, $identifier );
}
######### HTTP HELPER METHODS #########
# Extract cookie from reponse
# Params: \Httpful\Response, cookie name string
# Return: string
protected function _extract_cookie( $response, $name ) {
$cookie = $response->headers->offsetGet( 'set-cookie' );
$parts = explode( ';', $cookie );
# Important is only the first part
list( $key, $value ) = explode( '=', $parts[0] );
if( $key == $name ) {
return $value;
}
return '';
}
# Send POST request to URI with JSON encoded parameters
# Params: URI string, JSON data string
# Returns: \Httpful\Response
public function _post( $uri, $json = '' ) {
$uri = $this->_bind_uri( $uri );
$this->debug( 'POST ' . $uri . ' <<' . $json . '>>' );
try {
$r = \Httpful\Request::post( $this->server . $uri );
$r = $this->_add_cookie( $r );
$r->addHeader( 'Accept', 'application/json' );
return $r->sendsJson()->body( $json )->send();
} catch( Exception $e ) {
print_r( $e );
return 0;
}
}
# GDC GET request
# Param: URI string
# Returns: \Httpful\Response
public function _get( $uri ) {
$uri = $this->_bind_uri( $uri );
$this->debug( 'GET ' . $uri );
try {
$r = \Httpful\Request::get( $this->server . $uri );
$r = $this->_add_cookie( $r );
$r->addHeader( 'Accept', 'application/json, application/zip' );
return $r->send();
} catch( Exception $e ) {
print_r( $e );
return 0;
}
}
# Set GDC authentication cookie
# Param: \Httpful\Request
# Return: \Httpful\Request
protected function _add_cookie( $r ) {
if( $this->cookies['auth'] != '' ) {
$r->addHeader( 'Cookie', self::GDC_COOKIE_AUTH . '=' . $this->cookies['auth'] );
$this->debug( ' AUTH ' . $this->cookies['auth'] );
}
if( $this->cookies['token'] != '' ) {
$r->addHeader( 'Cookie', self::GDC_COOKIE_TOKEN . '=' . $this->cookies['token'] );
$this->debug( ' TOKN ' . $this->cookies['token'] );
}
return $r;
}
# Substitute placeholders in URI
# Param: URI string
# Return: URI string
protected function _bind_uri( $uri ) {
return str_replace( '<project>', $this->project, $uri );
}
######### DEBUGGING SERVICE #########
# Turn on debugging messages
public function debug_on() {
return $this->debug = TRUE;
}
# Turn on debugging messages
public function debug_off() {
return $this->debug = FALSE;
}
# Print debug message
# Param: string
public function debug( $msg ) {
if( $this->debug ) {
echo "$msg\n";
return TRUE;
} else {
return FALSE;
}
}
# Print warning message
# Param: string
public function warn( $msg ) {
echo "WARN [[$msg]]\n";
return TRUE;
}
######### MISCELANEOUS UTILS #########
# Generate "random" string of given length
# Param: integer
# Return: string
public function _rnd( $length = 6 ) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyz';
$string = '';
for( $p = 0; $p < $length; $p++ ) {
$string .= $characters[ mt_rand( 0, 35 ) ];
}
return $string;
}
} # end of class GDC
# Class to keep dataset data.
# Do not use directly.
class GdcDataset {
protected $gdc;
protected $identifier;
protected $sli; # array( template => tmp.zip.file, info => manifest, csv => csv.columns, uploadfile => upload.zip.filename )
public function __construct( $gdc, $identifier ) {
$this->gdc = $gdc;
$this->identifier = $identifier;
$this->sli = array();
}
# Download and read details from SLI manifest
# Return: boolean
public function read_sli_template() {
$zip = zip_open( $this->get_sli_template() );
while( $entry = zip_read( $zip ) ) {
zip_entry_open( $zip, $entry, 'r' );
$entry_name = zip_entry_name( $entry );
$entry_content = zip_entry_read( $entry, zip_entry_filesize( $entry ) );
if( $entry_name == 'upload_info.json' ) {
$this->sli['info'] = json_decode( $entry_content );
}
if( $entry_name == $this->identifier . '.csv' ) {
$this->sli['csv'] = array_flip( explode( ',', $entry_content ) );
}
zip_entry_close( $entry );
}
zip_close( $zip );
return $this->read_sli_manifest();
}
# Get dataset identifier
# Return: string
public function get_identifier() {
return $this->identifier;
}
# Get number of columns for dataset SLI load
# Return: integer
public function get_num_columns_sli() {
$this->check_read_template();
return count( $this->sli['csv'] );
}
# Get list of CSV column names
# Return: array
public function get_columns_sli() {
$this->check_read_template();
$columns = array_keys( $this->sli['csv'] );
sort( $columns, SORT_STRING );
return $columns;
}
# Set date format constraint to a column
# Params: string column name, string date format optional
# Return: boolean, TRUE on success
public function set_column_date_format( $column, $format = 'yyyy-MM-dd' ) {
$this->check_read_template();
foreach( $this->sli['info']->dataSetSLIManifest->parts as $col ) {
if( $column == $col->columnName ) {
$col->constraints->date = $format;
return TRUE;
}
}
return FALSE;
}
# Set date format constraints to all columns of type day.
# Param: string date format optional
# Return: integer number of rows affected
public function auto_set_date_format( $format = 'yyyy-MM-dd' ) {
$this->check_read_template();
$cnt = 0;
foreach( $this->sli['csv'] as $name => $col ) {
if( preg_match( '/^GDC\.time\.day.*/', $col['type'] ) ) {
$cnt += (int) $this->set_column_date_format( $name, $format );
}
}
return $cnt;
}
# Set dataset SLI mode to INCREMENTAL
# Return: boolean
public function set_sli_incremental() {
$this->check_read_template();
foreach( $this->sli['info']->dataSetSLIManifest->parts as $col ) {
$col->mode = 'INCREMENTAL';
}
return TRUE;
}
# Prepare ZIP file for dataset load
# Params: identifier string, array of arrays (table)
# Return: zip archive filename
public function prepare_load( $data ) {
$this->check_read_template();
$fn = $this->sli['template'] . '.data.zip';
$this->gdc->debug( "FILE $fn" );
$zip = new ZipArchive();
if( $zip->open( $fn, ZipArchive::CREATE ) !== TRUE ) {
throw new Exception( 'Cannot open file ' . $fn );
}
$csv = $this->data_to_csv( $data );
$info = json_encode( $this->sli['info'] );
$zip->addFromString( 'upload_info.json', $info );
$zip->addFromString( $this->identifier . '.csv' , $csv );
$zip->close();
return ( $this->sli['uploadfile'] = $fn );
}
# Run ETL task
# Param: filename of local zip file to upload
# Return: boolean
public function do_etl() {
if( !isset( $this->sli['uploadfile'] ) ) {
throw new Exception( 'Call prepare_load method first' );
}
if( $dir = $this->do_upload() ) {
$this->gdc->_post( GDC::GDC_API_ETL, '{"pullIntegration":"'.$dir.'"}' );
return TRUE;
} else {
return FALSE;
}
}
# Check if the template has been read, and throw an exception if not.
# Return: boolean
protected function check_read_template() {
if( !isset( $this->sli ) ) {
throw new Exception( 'Call read_sli_template method first' );
}
return TRUE;
}
# Download SLI dataset template
# Return: string filename
protected function get_sli_template() {
$r = $this->gdc->_get( GDC::GDC_API_SLI . '/' . $this->identifier . '/template' );
$this->sli['template'] = '/tmp/gdc-' . $this->gdc->get_project() . '-template-'
. $this->identifier . '-' . $this->gdc->_rnd() . '.zip';
$fp = fopen( $this->sli['template'], 'w' );
try {
fwrite( $fp, $r->body );
} catch( Exception $e ) {
fclose( $fp );
return 0;
}
fclose( $fp );
return $this->sli['template'];
}
# Get details on CSV columns
# Return: boolean
protected function read_sli_manifest() {
foreach( $this->sli['info']->dataSetSLIManifest->parts as $column ) {
$obj = array_values( get_object_vars( $this->gdc->get_object( $column->populates[0] ) ) );
$this->sli['csv'][$column->columnName] = array(
'title' => $obj[0]->meta->title,
'uri' => $obj[0]->meta->uri,
'category' => $obj[0]->meta->category,
'identifier' => $obj[0]->meta->identifier,
'type' => ( property_exists( $obj[0]->content, 'type' ) ? $obj[0]->content->type : '' )
);
}
return TRUE;
}
# Stores data into templated CSV dataset
# Param: array of arrays (data table)
# Return: string csv
protected function data_to_csv( $data ) {
$numcols = $this->get_num_columns_sli();
$i = 1; $e = 0;
$csv = implode( ',', $this->get_columns_sli() );
foreach( $data as $row ) {
if( count( $row ) == $numcols ) {
$csv .= "\n" . implode( ',', $row );
} else {
$this->gdc->warn( 'Invalid number of records at row ' . $i . ' (' . implode( ',', $row ) . ') - skipping' );
++$e;
}
++$i;
}
$this->gdc->debug( 'ROWS ' . ( $i - $e - 1 ) . ' added, ' . $e . ' skipped' );
return $csv;
}
# Upload zip to ftp server
# Return: string remote directory name
protected function do_upload() {
try {
$dir = $this->gdc->_rnd();
$this->gdc->debug( 'SFTP upload to ' . $dir . '/upload.zip' );
$ftp = ftp_ssl_connect( GDC::GDC_FTP_SERVER )
or die( 'Ftp server not accessible' );
ftp_login( $ftp, $this->gdc->user['name'], $this->gdc->user['passwd'] )
or die( 'Invalid credentials for ftp login' );
ftp_pasv( $ftp, TRUE );
ftp_mkdir( $ftp, $dir );
ftp_chdir( $ftp, $dir );
ftp_put( $ftp, 'upload.zip', $this->sli['uploadfile'], FTP_BINARY )
or die( 'File upload failed' );
ftp_close( $ftp );
} catch( Exception $e ) {
return FALSE;
}
return $dir;
}
} # end of class GdcDataset
?>