123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469 |
- 'use strict';
- define(['../../lib/@waca/core-client/js/core-client/ui/core/Events', '../../lib/@waca/core-client/js/core-client/nls/StringResources', '../../lib/@waca/core-client/js/core-client/ui/ProgressToast', '../../lib/@waca/core-client/js/core-client/utils/Deferred', 'underscore'], function (Events, StringResources, ProgressToast, Deferred, _) {
- 'use strict';
- var CONTENT_TYPE = 'application/vnd.ibm.bi.platform.execution+json; charset=UTF-8';
- var APPLICATION_JSON = 'application/json';
- var DatasetExecutionService = Events.extend({
- init: function init() {
- DatasetExecutionService.inherited('init', this, arguments);
- this._backgroundExecutions = {};
-
- this._pingTimeouts = [300, 600, 1000, 1500, 2000, 4000, 6000, 10000];
- this._defaultOptions = {
- showToastWhenDone: true
- };
- },
-
- execute: function execute(options) {
- var _this = this;
- _.defaults(options, this._defaultOptions);
- this._initLogger(options.glassContext);
-
- if (this.isExecuting(options.id)) {
- this.cancel(options, false);
- }
- var timestamp = Date.now();
- options.timestamp = timestamp;
- this._backgroundExecutions[options.id] = {
- 'status': 'executing',
- 'timestamp': timestamp,
- 'isRefresh': options.isRefresh
- };
- if (options.showToastWhenDone) {
- this._createProgressToast(options);
- }
- var data = JSON.stringify({
- 'options': {
- 'delivery': {
- 'save': {
- 'notify': false
- }
- }
- }
- });
- return Promise.resolve().then(function () {
- return options.glassContext.services.ajax.post('v1/datasets/' + options.id + '/executions', {
- 'headers': {
- 'Content-Type': CONTENT_TYPE,
- 'Accept': APPLICATION_JSON
- },
- 'datatype': 'json',
- 'data': data
- }).then(function (result, status, xhr) {
- this._backgroundExecutions[options.id].executionURL = xhr.getResponseHeader('location');
- this._backgroundExecutions[options.id].eventID = result.eventID;
- this._pingForStatus(options);
- }.bind(_this), function (error) {
- _this._rejectDeferredObjects(options, 'failed');
- throw new Error('Execution failed: ' + error.message);
- });
- });
- },
- _initLogger: function _initLogger(glassContext) {
- var _this2 = this;
- if (!this.logger && glassContext) {
- glassContext.getSvc('.Logger').then(function (logger) {
- return _this2.logger = logger;
- });
- }
- },
-
- _createProgressToast: function _createProgressToast(options) {
- var progressToast = new ProgressToast();
- progressToast.show(this._getToastMessage(options));
- progressToast.indefinite(this._getToastMessage(options));
- progressToast.onCancel(function (options) {
- this.cancel(options, true);
- }.bind(this, options));
- progressToast.onHide(function (options) {
- this._hideProgressToast(options.id);
- }.bind(this, options));
- this._backgroundExecutions[options.id].progressToast = progressToast;
- },
-
- _hideProgressToast: function _hideProgressToast(id) {
- var execution = this._backgroundExecutions[id];
- if (execution && execution.progressToast) {
- execution.progressToast.remove(0);
- execution.progressToast = null;
- }
- },
- _showErrorToast: function _showErrorToast(options, errorMessage) {
- var execution = this._backgroundExecutions[options.id];
- if (options.showToastWhenDone) {
- if (execution && execution.progressToast) {
-
-
- var progressToast = execution.progressToast;
- execution.progressToast = null;
- progressToast.fail(this._getToastMessage(options, errorMessage));
- progressToast.hideButton('cancel');
- } else {
- options.glassContext.appController.showToast(this._getToastMessage(options), {
- 'type': 'error'
- });
- }
- }
- },
- _showCancelledRefreshToast: function _showCancelledRefreshToast(options) {
- options.glassContext.appController.showToast(this._getToastMessage(options), {
- 'type': 'info'
- });
- },
-
- _pingForStatus: function _pingForStatus(options) {
- var execution = this._backgroundExecutions[options.id];
-
-
- if (!execution || options.timestamp !== execution.timestamp || !execution.executionURL || execution.status === 'cancelled') {
- return;
- } else if (execution.status === 'failed') {
- this._rejectDeferredObjects(options, 'failed');
- this._cleanupAfterExecutionFinished(options.id);
- return;
- } else if (!this.isExecuting(options.id)) {
- this._processExecutionStatus(execution.status, options);
- return;
- }
- options.glassContext.services.ajax.get(execution.executionURL, {
- 'headers': {
- 'Content-Type': CONTENT_TYPE,
- 'Accept': APPLICATION_JSON
- },
- 'datatype': 'json'
- }).then(function (response) {
- execution.status = response.status;
- this._processExecutionStatus(execution.status, options);
- }.bind(this), function () {
-
- this._rejectDeferredObjects(options, 'statusPingFailed');
- });
- },
-
- _processExecutionStatus: function _processExecutionStatus(status, options) {
- var _this3 = this;
- var execution = this._backgroundExecutions[options.id];
- switch (status) {
- case 'complete':
- case 'succeeded':
-
- if (execution.status === 'cancelled') {
- this._rejectDeferredObjects(options, 'cancelled');
- } else {
- this._resolveDefferedObjects(options.id);
- if (options.showToastWhenDone) {
- options.glassContext.appController.showToast(this._getToastMessage(options));
- }
- this.trigger('loadComplete', { id: options.id });
- }
- this._cleanupAfterExecutionFinished(options.id);
- break;
- case 'cancelled':
- this._rejectDeferredObjects(options, 'cancelled');
- this._cleanupAfterExecutionFinished(options.id);
- break;
- case 'failed':
- this._getErrorMessage(options).then(function (errorMessage) {
- _this3._rejectDeferredObjects(options, 'failed', errorMessage);
- _this3._cleanupAfterExecutionFinished(options.id);
- });
- break;
- case 'executing':
- case 'pending':
- var pingTimeoutIndex = options.pingTimeoutIndex || 0;
- pingTimeoutIndex += 1;
- if (pingTimeoutIndex >= this._pingTimeouts.length) {
- pingTimeoutIndex = this._pingTimeouts.length - 1;
- }
- options.pingTimeoutIndex = pingTimeoutIndex;
- setTimeout(function () {
- this._pingForStatus(options);
- }.bind(this), this._pingTimeouts[pingTimeoutIndex]);
- break;
- default:
- this._cleanupAfterExecutionFinished(options.id);
- console.debug('Unknown status returned by ' + execution.executionURL + '. Status of: ' + status);
- }
- },
-
- _rejectDeferredObjects: function _rejectDeferredObjects(options, status, errorMessage) {
- var execution = this._backgroundExecutions[options.id];
- if (!execution) {
- return;
- }
- execution.status = status;
- if (status === 'failed') {
- this._showErrorToast(options, errorMessage);
- }
-
- if (execution.deferredObjects) {
- execution.deferredObjects.forEach(function (deferred) {
- deferred.reject({
- 'status': status
- });
- });
- }
- },
- _logError: function _logError() {
- if (this.logger) {
- var _logger;
- (_logger = this.logger).error.apply(_logger, arguments);
- }
- },
- _getErrorMessage: function _getErrorMessage(options) {
- var _this4 = this;
- return this._getItemLastHistoryEntry(options).catch(function (error) {
- return _this4._logError('Error trying to read history of asset ' + options.id, error);
- }).then(function (historyEntry) {
- return historyEntry && _this4._getHistoryEntryDetailMessage(options, historyEntry);
- }).catch(function (error) {
- return _this4._logError('Error trying to read history details of asset ' + options.id, error);
- });
- },
- _getItemLastHistoryEntry: function _getItemLastHistoryEntry(options) {
- return options.glassContext.services.ajax.get('v1/objects/' + options.id + '/items?types=history').then(function (result) {
- return result && result.data && _.max(result.data, function (_ref) {
- var modificationTime = _ref.modificationTime;
- return Date.parse(modificationTime);
- });
- });
- },
- _getHistoryEntryDetailMessage: function _getHistoryEntryDetailMessage(options, historyEntry) {
- var historyDetailsUrl = historyEntry._meta && historyEntry._meta.links && historyEntry._meta.links.details && historyEntry._meta.links.details.url;
- return historyDetailsUrl && options.glassContext.services.ajax.get(historyDetailsUrl).then(function (result) {
-
- var message = result && result.data && result.data.messages && result.data.messages.length && result.data.messages[0];
- return message && message.detail;
- });
- },
-
- _resolveDefferedObjects: function _resolveDefferedObjects(id) {
- var execution = this._backgroundExecutions[id];
-
- if (execution.deferredObjects) {
- execution.deferredObjects.forEach(function (deferred) {
- deferred.resolve();
- });
- }
- },
- _cleanupAfterExecutionFinished: function _cleanupAfterExecutionFinished(id) {
- this._hideProgressToast(id);
- this._backgroundExecutions[id] = {
- 'status': this._backgroundExecutions[id].status
- };
- },
-
- cancel: function cancel(options, showCancelToast) {
- this._hideProgressToast(options.id);
- var execution = this._backgroundExecutions[options.id];
- if (!execution || !this.isExecuting(options.id)) {
- return;
- }
- execution.status = 'cancelled';
- if (showCancelToast !== false) {
- this._showCancelledRefreshToast(options);
- }
-
- this._rejectDeferredObjects(options, 'cancelled');
- if (execution.executionURL) {
- options.glassContext.services.ajax['delete'](execution.executionURL, {
- 'headers': {
- 'Content-Type': CONTENT_TYPE,
- 'Accept': APPLICATION_JSON
- },
- 'datatype': 'json'
- });
- }
- },
-
- whenComplete: function whenComplete(id) {
- var deferred = new Deferred();
- var status = this.getStatus(id);
- var execution = this._backgroundExecutions[id];
-
- if (!execution || status === 'complete') {
- deferred.resolve();
- } else if (status === 'failed' || status === 'cancelled') {
-
- deferred.reject({
- 'status': status
- });
- } else {
- if (!execution.deferredObjects) {
- execution.deferredObjects = [];
- }
- execution.deferredObjects.push(deferred);
- }
- return deferred.promise;
- },
-
- getStatus: function getStatus(id) {
- return this._backgroundExecutions[id] ? this._backgroundExecutions[id].status : null;
- },
-
- isExecuting: function isExecuting(id) {
- var status = this.getStatus(id);
- return status === 'pending' || status === 'executing';
- },
- _getToastMessage: function _getToastMessage(options, errorMessage) {
- var execution = this._backgroundExecutions[options.id];
- if (!execution) {
- return '';
- }
- var stringId = '';
- if (execution.isRefresh) {
- switch (execution.status) {
- case 'executing':
- case 'pending':
- stringId = 'datasetRefreshing';
- break;
- case 'complete':
- case 'succeeded':
- stringId = 'datasetFinishedRefreshing';
- break;
- case 'failed':
- stringId = 'datasetRefreshFailed';
- break;
- case 'cancelled':
- stringId = 'datasetRefreshCancelled';
- break;
- }
- } else {
- switch (execution.status) {
- case 'executing':
- case 'pending':
- stringId = 'datasetLoading';
- break;
- case 'complete':
- case 'succeeded':
- stringId = 'datasetFinishedLoading';
- break;
- case 'failed':
- stringId = 'datasetLoadingFailed';
- break;
- case 'cancelled':
- stringId = 'datasetLoadingCancelled';
- break;
- }
- }
- return [StringResources.get(stringId, {
- 'name': options.name
- }), errorMessage].filter(function (string) {
- return !!string;
- }).join('\n');
- }
- });
- return DatasetExecutionService;
- });
|