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
);
127 operationInfo
.name
= "Duplication of " + sourcePackage
.name
;
128 operationInfo
.id
= guid();
129 operationInfo
.icon
= imgCopy
;
130 operationInfo
.type
= 'copy';
131 operationInfo
.message
= 'Requesting package duplication.';
132 operationInfo
.args
.packageType
= sourcePackage
['uiState']['type'].toUpperCase();
133 operationInfo
.args
.id
= sourcePackage
.id
;
134 operationInfo
.args
.name
= sourcePackage
.name
+ ' copy';
136 this.addOperation(operationInfo
);
137 this.getInstance().requestCatalogPackageCopy(operationInfo
, sourcePackage
);
140 updateOperationStatus(operation
) {
141 console
.debug('package manager operation status update', operation
);
142 this.updateOperation(operation
);
143 if (operation
.pending
) {
144 delayStatusCheck(this.getInstance().requestCatalogPackageCopyStatus
, operation
);
148 uploadCatalogPackage(file
) {
149 file
.id
= file
.id
|| guid();
150 const operation
= _pick(file
, packagePropertyNames
);
151 operation
.icon
= file
.riftAction
=== 'onboard' ? imgOnboard
: imgUpdate
;
152 operation
.type
= 'upload';
153 this.addOperation(operation
);
154 // note DropZone.js handles the async upload so we don't have to invoke any async action creators
157 onUploadCatalogPackageStatusUpdated(response
) {
158 const upload
= updateStatusInfo(response
);
159 this.updateOperation(upload
);
160 console
.log('updating package upload')
161 // if pending with no transaction id - do nothing
162 // bc DropZone.js will notify upload progress
163 if (upload
.pending
&& upload
.transactionId
) {
164 console
.log('checking status')
165 delayStatusCheck(this.getInstance().requestCatalogPackageUploadStatus
, upload
);
166 } else if (upload
.success
) {
167 this.getInstance().loadCatalogs();
168 console
.log('finished uploading to node, requesting status from rest')
172 onUploadCatalogPackageError(response
) {
173 console
.warn('onUploadCatalogPackageError', response
);
174 const operation
= updateStatusInfo(response
);
175 this.updateOperation(operation
);
178 downloadCatalogPackage(data
) {
179 let catalogItems
= data
['selectedItems'] || [];
180 let schema
= data
['selectedFormat'] || 'mano';
181 let grammar
= data
['selectedGrammar'] || 'osm';
183 if (catalogItems
.length
) {
184 const operation
= Object
.assign({}, defaults
.downloadPackage
, {id
: guid()});
185 operation
.name
= catalogItems
[0].name
;
186 operation
.type
= 'download';
187 if (catalogItems
.length
> 1) {
188 operation
.name
+= ' (' + catalogItems
.length
+ ' items)';
190 operation
.ids
= catalogItems
.map(d
=> d
.id
).sort().toString();
191 operation
.catalogItems
= catalogItems
;
192 this.addOperation(operation
);
193 this.getInstance().requestCatalogPackageDownload(operation
, format
, grammar
, schema
).catch(exception
);
197 onDownloadCatalogPackageStatusUpdated(response
) {
198 const download
= updateStatusInfo(response
);
199 this.updateOperation(download
);
200 if (download
.pending
) {
201 delayStatusCheck(this.getInstance().requestCatalogPackageDownloadStatus
, download
);
205 onDownloadCatalogPackageError(response
) {
206 console
.warn('onDownloadCatalogPackageError', response
);
207 const operation
= updateStatusInfo(response
);
208 this.updateOperation(operation
);
213 function calculateUploadProgressMessage(size
= 0, progress
= 0, bytesSent
= 0) {
214 const amount
= parseFloat(progress
) || 0;
215 const loaded
= amount
=== 100 ? size
: size
* amount
/ 100;
217 if (amount
=== 100) {
218 progressText
= numeral(loaded
).format('0.0b') + ' loaded ';
219 } else if (typeof amount
=== 'number' && amount
!= 0) {
220 progressText
= numeral(bytesSent
).format('0.0b') + ' out of ' + numeral(size
).format('0.0b');
222 progressText
= progress
;
227 function updateStatusInfo(response
) {
228 // returns the operation object with the status fields updated based on the server response
234 const responseData
= (response
.data
.output
) ? response
.data
.output
: response
.data
;
235 const operation
= response
.state
;
236 if ( typeof response
.data
.status
!== "number" ) {
237 switch(response
.data
.status
) {
238 case 'upload-progress':
239 statusInfo
.pending
= true;
240 statusInfo
.progress
= parseFloat(responseData
.progress
) || 0;
241 statusInfo
.message
= calculateUploadProgressMessage(operation
.size
, responseData
.progress
, responseData
.bytesSent
);
243 case 'upload-success':
244 statusInfo
.pending
= true;
245 statusInfo
.progress
= 100;
246 statusInfo
.message
= 'Upload completed.';
247 statusInfo
.transactionId
= responseData
['transaction_id'] || responseData
['transaction-id'] || operation
.transactionId
;
250 statusInfo
.error
= true;
251 statusInfo
.message
= responseData
.message
;
253 case 'download-requested':
254 statusInfo
.pending
= true;
255 statusInfo
.progress
= 25;
256 statusInfo
.transactionId
= responseData
['transaction_id'] || responseData
['transaction-id'] || operation
.transactionId
;
259 statusInfo
.pending
= true;
260 statusInfo
.progress
= 50;
261 statusInfo
.message
= responseData
.events
[responseData
.events
.length
- 1].text
;
264 statusInfo
.success
= true;
265 statusInfo
.progress
= 100;
266 statusInfo
.message
= responseData
.events
[responseData
.events
.length
- 1].text
;
267 if (operation
.type
=== 'download') {
268 statusInfo
.urlValidUntil
= moment().add(defaults
.downloadUrlTimeToLiveInMinutes
, 'minutes').toISOString();
269 if (responseData
.filename
) {
270 statusInfo
.url
= getCatalogPackageManagerServerOrigin() + '/api/export/' + responseData
.filename
;
272 statusInfo
.url
= getCatalogPackageManagerServerOrigin() + '/api/export/' + operation
.transactionId
+ '.tar.gz';
277 statusInfo
.error
= true;
278 statusInfo
.message
= responseData
.errors
[0].value
;
281 throw new ReferenceError('a status of "request", "success", "failure", "pending", "upload-completed", "upload-error", "download-requested", "upload-progress", "upload-action" is required');
284 // typically get here due to unexpected development errors (backend exceptions, down/up load server access issues)
285 statusInfo
.error
= true;
286 statusInfo
.message
= responseData
.statusText
|| 'Error';
288 return Object
.assign({}, operation
, statusInfo
);
291 export default alt
.createStore(CatalogPackageManagerStore
, 'CatalogPackageManagerStore');