Skip to content

Commit 689a762

Browse files
committed
Merge pull request #17 from yllieth/data_file
[SDK-js] New implementation of data_file
2 parents 7f1c722 + cd748b9 commit 689a762

File tree

6 files changed

+290
-28
lines changed

6 files changed

+290
-28
lines changed

dist/predicsis-jsSDK.js

Lines changed: 144 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ angular
1919
this.setErrorHandler = function(handler) { errorHandler = handler; };
2020

2121
this.$get = function(Restangular, uploadHelper,
22-
Datasets, Dictionaries, Jobs, Modalities, Models, OauthTokens, OauthApplications,
22+
Datafiles, Datasets, Dictionaries, Jobs, Modalities, Models, OauthTokens, OauthApplications,
2323
PreparationRules, Projects, Reports, UserSettings, Sources, Users, Variables) {
2424
var self = this;
2525

@@ -53,6 +53,7 @@ angular
5353
});
5454

5555
return {
56+
Datafiles: Datafiles,
5657
Datasets: Datasets,
5758
Dictionaries: Dictionaries,
5859
Jobs: Jobs,
@@ -81,6 +82,140 @@ angular
8182
};
8283
});
8384

85+
/**
86+
* @ngdoc service
87+
* @name predicsis.jsSDK.models.Datafile
88+
* @requires $q
89+
* @requires Restangular
90+
* @description
91+
*
92+
* <table>
93+
* <tr>
94+
* <td><span class="badge get">get</span> <kbd>/data_files</kbd></td>
95+
* <td><kbd>{@link predicsis.jsSDK.models.Datafiles#methods_all Datafiles.all()}</kbd></td>
96+
* <td></td>
97+
* </tr>
98+
* <tr>
99+
* <td><span class="badge get">get</span> <kbd>/data_files/:id</kbd></td>
100+
* <td><kbd>{@link predicsis.jsSDK.models.Datafiles#methods_get Datafiles.get()}</kbd></td>
101+
* <td></td>
102+
* </tr>
103+
* <tr>
104+
* <td><span class="badge get">get</span> <kbd>/data_files/:id/signed_url</kbd></td>
105+
* <td><kbd>{@link predicsis.jsSDK.models.Datafiles#methods_get_signed_url Datafiles.getSignedUrl()}</kbd></td>
106+
* <td></td>
107+
* </tr>
108+
* <tr>
109+
* <td><span class="badge get">get</span> <kbd>/data_files/:id/signed_url</kbd></td>
110+
* <td><kbd>{@link predicsis.jsSDK.models.Datafiles#methods_download Datafiles.download()}</kbd></td>
111+
* <td></td>
112+
* </tr>
113+
* <tfoot>
114+
* <tr><td colspan="3">Official documentation is available at https://developer.predicsis.com/doc/v1/data_management/data_file/</td></tr>
115+
* </tfoot>
116+
* </table>
117+
*
118+
* Output example of a <kbd>data_file</kbd>:
119+
* <pre>
120+
* {
121+
* data_file: {
122+
* id: "54edf76c6170700001870000",
123+
* filename: "hello.csv",
124+
* type: "S3",
125+
* size: 24,
126+
* signed_url: {
127+
* links: {
128+
* self: "https://api.predicsis.com/data_files/53c7e7b668796493d3010000/signed_url"
129+
* }
130+
* }
131+
* }
132+
* }
133+
* </pre>
134+
*
135+
* Output example of a <kbd>signed_url</kbd>:
136+
* <pre>
137+
* {
138+
* data_file: {
139+
* signed_url: "http://prod.kml-api.s3-us-west-2.amazonaws.com/uploads/5347b31750432d45a5020000/sources/1415101671848/source.csv"
140+
* }
141+
* }
142+
* </pre>
143+
*/
144+
angular
145+
.module('predicsis.jsSDK.models')
146+
.service('Datafiles', function($q, Restangular) {
147+
'use strict';
148+
149+
function dataFile(id) { return Restangular.one('data_files', id); }
150+
function dataFiles() { return Restangular.all('data_files'); }
151+
152+
// -----------------------------------------------------------------------------------------------------------------
153+
154+
/**
155+
* @ngdoc function
156+
* @name all
157+
* @methodOf predicsis.jsSDK.models.Datafiles
158+
* @description Get all (or a list of) data files
159+
* @param {Array} [dataFileIds] List of data files' ids you want to fetch
160+
* @return {Promise} A list of data_files
161+
*/
162+
this.all = function(dataFileIds) {
163+
if(dataFileIds === undefined) {
164+
return dataFiles().getList();
165+
} else {
166+
dataFileIds = dataFileIds || [];
167+
168+
return $q.all(dataFileIds.map(function(id) {
169+
return dataFile(id).get();
170+
}));
171+
}
172+
};
173+
174+
/**
175+
* @ngdoc function
176+
* @name get
177+
* @methodOf predicsis.jsSDK.models.Datafiles
178+
* @description Get a single data_file by its id
179+
* @param {String} dataFileId data_file identifier
180+
* @return {Promise} A single data_file
181+
*/
182+
this.get = function(dataFileId) {
183+
return dataFile(dataFileId).get();
184+
};
185+
186+
/**
187+
* @ngdoc function
188+
* @name getSignedUrl
189+
* @methodOf predicsis.jsSDK.models.Datafiles
190+
* @description Get a signed_url of a data_file
191+
* @param {String} datasetId dataset identifier
192+
* @return {Promise} A signed url you can download raw file with
193+
*/
194+
this.getSignedUrl = function(datasetId) {
195+
return dataFile(datasetId)
196+
.one('signed_url', null).get()
197+
.then(function(url) {
198+
return url.signed_url;
199+
});
200+
};
201+
202+
/**
203+
* @ngdoc function
204+
* @name download
205+
* @methodOf predicsis.jsSDK.models.Datafiles
206+
* @description Convenience method to concretely download the file
207+
* @param {String} datasetId dataset identifier
208+
* @return {Promise} Once the signed url is resolved, we're using <code>window.location.assign</code> to download
209+
* the file without changing page.
210+
*/
211+
this.download = function(datasetId) {
212+
this.getSignedUrl(datasetId).then(function(signedUrl) {
213+
window.location.assign(signedUrl);
214+
});
215+
};
216+
217+
});
218+
84219
/**
85220
* @ngdoc service
86221
* @name predicsis.jsSDK.models.Datasets
@@ -177,13 +312,7 @@ angular
177312
* children_dataset_ids: [],
178313
* dictionary_ids: [],
179314
* generated_dictionaries_ids: [],
180-
* data_file: {
181-
* id: '54904b09776f720001650000',
182-
* filename: 'learning-dataset.csv',
183-
* type: 'S3',
184-
* size: 538296,
185-
* url: S3_URL + '/download/file/from/s3/learning-dataset.csv'
186-
* },
315+
* data_file: '54904b09776f720001650000',
187316
* main_modality: null,
188317
* classifier_id: null,
189318
* dataset_id: null,
@@ -341,7 +470,7 @@ angular
341470
source_ids: [source.id],
342471
data_file: { filename: fileName }
343472
});
344-
})
473+
});
345474
};
346475

347476
/**
@@ -356,18 +485,20 @@ angular
356485
* <li>Idem for learning/testing filenames</li>
357486
* </ul>
358487
*
359-
* @param {String} id Dataset id you want to split (called <em>original dataset</em>)
360-
* @param {String} name Name of the original dataset (used to name its subsets)
361-
* @param {String} filename Name of the original datafile (used to name its subsets's datafile)
362-
* @param {Number} [sampling=70] Examples: If you set <code>sampling</code> to 70, you are going to have:
488+
* @param {String} id Dataset id you want to split (called <em>original dataset</em>)
489+
* @param {String} name Name of the original dataset (used to name its subsets)
490+
* @param {String} [filename=name] Name of the original datafile (used to name its subsets's datafile). If undefined, value of <kbd>name</kbd> parameter is used
491+
* @param {Number} [sampling=70] Examples: If you set <code>sampling</code> to 70, you are going to have:
363492
* <ul>
364493
* <li><code>70%</code> of your original dataset for <b>learning</b></li>
365494
* <li><code>30%</code> of your original dataset for <b>testing</b></li>
366495
* </ul><br/>
367496
* @return {Promise} Subsets
368497
*/
369498
this.split = function(id, name, filename, sampling) {
499+
filename = filename || name;
370500
sampling = sampling || 70;
501+
371502
var learn = {
372503
parent_dataset_id: id,
373504
name: 'learned_' + name,

dist/predicsis-jsSDK.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)