4 * Copyright 2016 RIFT.IO Inc
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 import _delay
from 'lodash/delay'
22 import _pick
from 'lodash/pick'
23 import alt
from '../alt'
24 import guid
from '../libraries/guid'
25 import numeral
from 'numeral'
26 import moment
from 'moment'
27 import utils
from '../libraries/utils'
28 import CatalogPackageManagerSource
from '../sources/CatalogPackageManagerSource'
29 import CatalogPackageManagerActions
from '../actions/CatalogPackageManagerActions'
30 import CatalogDataSource
from '../sources/CatalogDataSource'
32 import imgDownload
from '../../../node_modules/open-iconic/svg/cloud-download.svg'
33 import imgOnboard
from '../../../node_modules/open-iconic/svg/cloud-upload.svg'
34 import imgUpdate
from '../../../node_modules/open-iconic/svg/data-transfer-upload.svg'
35 import imgCopy
from '../../../node_modules/open-iconic/svg/layers.svg'
57 message
: 'Requesting catalog package export...',
64 checkStatusDelayInSeconds
: 2,
65 downloadUrlTimeToLiveInMinutes
: 5
68 const exception
= function ignoreException() {};
70 const packagePropertyNames
= Object
.keys(defaults
.downloadPackage
);
72 function getCatalogPackageManagerServerOrigin() {
73 return utils
.getSearchParams(window
.location
).upload_server
+ ':4567';
76 function delayStatusCheck(statusCheckFunction
, operation
) {
77 if (!operation
.checkStatusTimeoutId
) {
78 const delayCallback = function () {
79 delete operation
.checkStatusTimeoutId
;
80 statusCheckFunction(operation
).catch(exception
);
82 operation
.checkStatusTimeoutId
= _delay(delayCallback
, defaults
.checkStatusDelayInSeconds
* 1000);
86 class CatalogPackageManagerStore
{
92 this.registerAsync(CatalogDataSource
);
93 this.registerAsync(CatalogPackageManagerSource
);
94 this.bindAction(CatalogPackageManagerActions
.REMOVE_CATALOG_OPERATION
, this.removeCatalogOperation
);
95 this.bindAction(CatalogPackageManagerActions
.DOWNLOAD_CATALOG_PACKAGE
, this.downloadCatalogPackage
);
96 this.bindAction(CatalogPackageManagerActions
.DOWNLOAD_CATALOG_PACKAGE_STATUS_UPDATED
, this.onDownloadCatalogPackageStatusUpdated
);
97 this.bindAction(CatalogPackageManagerActions
.DOWNLOAD_CATALOG_PACKAGE_ERROR
, this.onDownloadCatalogPackageError
);
98 this.bindAction(CatalogPackageManagerActions
.UPLOAD_CATALOG_PACKAGE
, this.uploadCatalogPackage
);
99 this.bindAction(CatalogPackageManagerActions
.UPLOAD_CATALOG_PACKAGE_STATUS_UPDATED
, this.onUploadCatalogPackageStatusUpdated
);
100 this.bindAction(CatalogPackageManagerActions
.UPLOAD_CATALOG_PACKAGE_ERROR
, this.onUploadCatalogPackageError
);
101 this.bindAction(CatalogPackageManagerActions
.COPY_CATALOG_PACKAGE
, this.copyCatalogPackage
);
102 this.bindAction(CatalogPackageManagerActions
.UPDATE_STATUS
, this.updateOperationStatus
);
105 addOperation(operation
) {
106 const operations
= [operation
].concat(this.operations
);
107 this.setState({operations
});
110 updateOperation(operation
) {
111 const operations
= this.operations
.map(d
=> {
112 if (d
.id
=== operation
.id
) {
113 return Object
.assign({}, d
, operation
);
117 this.setState({operations
});
120 removeCatalogOperation(operation
) {
121 const operations
= this.operations
.filter(d
=> d
.id
!== operation
.id
);
122 this.setState({operations
});
125 copyCatalogPackage(sourcePackage
) {
126 let operationInfo
= Object
.assign({}, defaults
.operation
);
128 operationInfo
.args
.packageType
= sourcePackage
['uiState']['type'].toUpperCase();
129 operationInfo
.args
.id
= sourcePackage
.id
;
130 operationInfo
.args
.name
= sourcePackage
.name
+ ' copy';
132 operationInfo
.id
= guid();
133 operationInfo
.icon
= imgCopy
;
134 operationInfo
.type
= 'copy';
135 operationInfo
.name
= "Creating " + operationInfo
.args
.name
;
136 operationInfo
.message
= "Requesting duplication";
138 this.addOperation(operationInfo
);
139 this.getInstance().requestCatalogPackageCopy(operationInfo
, sourcePackage
);
142 updateOperationStatus(operation
) {
143 console
.debug('package manager operation status update', operation
);
144 this.updateOperation(operation
);
145 if (operation
.pending
) {
146 delayStatusCheck(this.getInstance().requestCatalogPackageCopyStatus
, operation
);
150 uploadCatalogPackage(file
) {
151 file
.id
= file
.id
|| guid();
152 const operation
= _pick(file
, packagePropertyNames
);
153 operation
.icon
= file
.riftAction
=== 'onboard' ? imgOnboard
: imgUpdate
;
154 operation
.type
= 'upload';
155 this.addOperation(operation
);
156 // note DropZone.js handles the async upload so we don't have to invoke any async action creators
159 onUploadCatalogPackageStatusUpdated(response
) {
160 const upload
= updateStatusInfo(response
);
161 this.updateOperation(upload
);
162 console
.log('updating package upload')
163 // if pending with no transaction id - do nothing
164 // bc DropZone.js will notify upload progress
165 if (upload
.pending
&& upload
.transactionId
) {
166 console
.log('checking status')
167 delayStatusCheck(this.getInstance().requestCatalogPackageUploadStatus
, upload
);
168 } else if (upload
.success
) {
169 this.getInstance().loadCatalogs();
170 console
.log('finished uploading to node, requesting status from rest')
174 onUploadCatalogPackageError(response
) {
175 console
.warn('onUploadCatalogPackageError', response
);
176 const operation
= updateStatusInfo(response
);
177 this.updateOperation(operation
);
180 downloadCatalogPackage(data
) {
181 let catalogItems
= data
['selectedItems'] || [];
182 let schema
= data
['selectedFormat'] || 'mano';
183 let grammar
= data
['selectedGrammar'] || 'osm';
185 if (catalogItems
.length
) {
186 const operation
= Object
.assign({}, defaults
.downloadPackage
, {id
: guid()});
187 operation
.name
= catalogItems
[0].name
;
188 operation
.type
= 'download';
189 if (catalogItems
.length
> 1) {
190 operation
.name
+= ' (' + catalogItems
.length
+ ' items)';
192 operation
.ids
= catalogItems
.map(d
=> d
.id
).sort().toString();
193 operation
.catalogItems
= catalogItems
;
194 this.addOperation(operation
);
195 this.getInstance().requestCatalogPackageDownload(operation
, format
, grammar
, schema
).catch(exception
);
199 onDownloadCatalogPackageStatusUpdated(response
) {
200 const download
= updateStatusInfo(response
);
201 this.updateOperation(download
);
202 if (download
.pending
) {
203 delayStatusCheck(this.getInstance().requestCatalogPackageDownloadStatus
, download
);
207 onDownloadCatalogPackageError(response
) {
208 console
.warn('onDownloadCatalogPackageError', response
);
209 const operation
= updateStatusInfo(response
);
210 this.updateOperation(operation
);
215 function calculateUploadProgressMessage(size
= 0, progress
= 0, bytesSent
= 0) {
216 const amount
= parseFloat(progress
) || 0;
217 const loaded
= amount
=== 100 ? size
: size
* amount
/ 100;
219 if (amount
=== 100) {
220 progressText
= numeral(loaded
).format('0.0b') + ' loaded ';
221 } else if (typeof amount
=== 'number' && amount
!= 0) {
222 progressText
= numeral(bytesSent
).format('0.0b') + ' out of ' + numeral(size
).format('0.0b');
224 progressText
= progress
;
229 function updateStatusInfo(response
) {
230 // returns the operation object with the status fields updated based on the server response
236 const responseData
= (response
.data
.output
) ? response
.data
.output
: response
.data
;
237 const operation
= response
.state
;
238 if ( typeof response
.data
.status
!== "number" ) {
239 switch(response
.data
.status
) {
240 case 'upload-progress':
241 statusInfo
.pending
= true;
242 statusInfo
.progress
= parseFloat(responseData
.progress
) || 0;
243 statusInfo
.message
= calculateUploadProgressMessage(operation
.size
, responseData
.progress
, responseData
.bytesSent
);
245 case 'upload-success':
246 statusInfo
.pending
= true;
247 statusInfo
.progress
= 100;
248 statusInfo
.message
= 'Upload completed.';
249 statusInfo
.transactionId
= responseData
['transaction_id'] || responseData
['transaction-id'] || operation
.transactionId
;
252 statusInfo
.error
= true;
253 statusInfo
.message
= responseData
.message
;
255 case 'download-requested':
256 statusInfo
.pending
= true;
257 statusInfo
.progress
= 25;
258 statusInfo
.transactionId
= responseData
['transaction_id'] || responseData
['transaction-id'] || operation
.transactionId
;
261 statusInfo
.pending
= true;
262 statusInfo
.progress
= 50;
263 statusInfo
.message
= responseData
.events
[responseData
.events
.length
- 1].text
;
266 statusInfo
.success
= true;
267 statusInfo
.progress
= 100;
268 statusInfo
.message
= responseData
.events
[responseData
.events
.length
- 1].text
;
269 if (operation
.type
=== 'download') {
270 statusInfo
.urlValidUntil
= moment().add(defaults
.downloadUrlTimeToLiveInMinutes
, 'minutes').toISOString();
271 if (responseData
.filename
) {
272 statusInfo
.url
= getCatalogPackageManagerServerOrigin() + '/api/export/' + responseData
.filename
;
274 statusInfo
.url
= getCatalogPackageManagerServerOrigin() + '/api/export/' + operation
.transactionId
+ '.tar.gz';
279 statusInfo
.error
= true;
280 statusInfo
.message
= responseData
.errors
[0].value
;
283 throw new ReferenceError('a status of "request", "success", "failure", "pending", "upload-completed", "upload-error", "download-requested", "upload-progress", "upload-action" is required');
286 // typically get here due to unexpected development errors (backend exceptions, down/up load server access issues)
287 statusInfo
.error
= true;
288 statusInfo
.message
= responseData
.statusText
|| 'Error';
290 return Object
.assign({}, operation
, statusInfo
);
293 export default alt
.createStore(CatalogPackageManagerStore
, 'CatalogPackageManagerStore');