'use strict'; /* *+------------------------------------------------------------------------+ *| Licensed Materials - Property of IBM *| IBM Cognos Products: Dashboard *| (C) Copyright IBM Corp. 2017, 2020 *| *| US Government Users Restricted Rights - Use, duplication or disclosure *| restricted by GSA ADP Schedule Contract with IBM Corp. *+------------------------------------------------------------------------+ */ define(['./SmartsRecommenderService', './SmartsRecommenderError'], function (BaseService, SmartsError) { 'use strict'; var SMARTS_ROOT = 'v1/smarts'; var CHART_INFO = SMARTS_ROOT + '/visualization-recommender/chartInfo'; var ALTERNATE_VISUALIZATION_URL = SMARTS_ROOT + '/visualization-recommender/recommend'; var RELATED_VISUALIZATION_URL = SMARTS_ROOT + '/insights/related-visualizations'; // This Smarts API will return first vis as the best vis for target fields, and other vis for related fields // of the target fields var RECOMMENDER_RELATED_VISUALIZATION_URL = SMARTS_ROOT + '/explore/related-visualizations'; var RECOMMENDER_COMPARE_VISUALIZATION_URL = SMARTS_ROOT + '/content-recommender/comparison'; var SmartsVisRecommenderService = BaseService.extend({ init: function init(options) { SmartsVisRecommenderService.inherited('init', this, arguments); this.ajaxSvc = options.ajaxSvc; this.logger = options.logger; }, /** * Get result from chartInfo endpoint */ getChartInfo: function getChartInfo(requestParams) { this.logger.debug('getRecommendInfo'); return this._executeRequest(requestParams, CHART_INFO).catch(this._handleError.bind(this)).then(function (_ref) { var data = _ref.data; if (data.smartsStatus !== SmartsError.STATUS.OK) { throw new SmartsError(data.smartsStatus, data.details); } // Recommender may return more than one answer that matches the same slot mappings. // In some cases it may return those mappings plus unmapped slots with the 'mandatorySlotMissing' Flag set. // Give preference to the first complete one as the title source (unless no complete ones are returned). var chartInfo = data; if (data.recommendations) { var completeRecommendations = data.recommendations.filter(function (recommendation) { return !recommendation.mandatorySlotsMissing; }); chartInfo = completeRecommendations && completeRecommendations.length ? completeRecommendations[0] : data.recommendations[0]; } return { title: chartInfo.title, label: chartInfo.label, description: chartInfo.description, mandatorySlotsMissing: chartInfo.mandatorySlotsMissing }; }); }, recommendAlternateVisualizationsOrBindings: function recommendAlternateVisualizationsOrBindings(requestParams) { this.logger.debug('recommendAlternateVisualizationsOrBindings'); return this._executeRequest(requestParams, ALTERNATE_VISUALIZATION_URL).catch(this._handleError.bind(this)).then(this._processResponse.bind(this)); }, recommendRelatedVisualizations: function recommendRelatedVisualizations(requestParams) { var url = RELATED_VISUALIZATION_URL; if (requestParams.includeBestVisForTargetFields) { url = RECOMMENDER_RELATED_VISUALIZATION_URL; } this.logger.debug('recommendRelatedVisualizations'); return this._executeRequest(requestParams, url).catch(this._handleError.bind(this)).then(this._processResponse.bind(this)); }, recommendCompareVisualizations: function recommendCompareVisualizations(requestParams) { var url = RECOMMENDER_COMPARE_VISUALIZATION_URL; this.logger.debug('recommendCompareVisualizations'); return this._executeRequest(requestParams, url).catch(this._handleError.bind(this)).then(this._processCompareResponse.bind(this)); }, _processResponse: function _processResponse(ajaxResponse) { var recommenderResponse = ajaxResponse.data; if (recommenderResponse.smartsStatus === SmartsError.STATUS.OK) { return recommenderResponse.recommendations; } else { throw new SmartsError(recommenderResponse.smartsStatus, recommenderResponse.details); } }, _processCompareResponse: function _processCompareResponse(ajaxResponse) { var recommenderResponse = ajaxResponse.data; if (recommenderResponse.status.code === SmartsError.STATUS.OK) { return recommenderResponse.ircontent; } else { throw new SmartsError(recommenderResponse.smartsStatus, recommenderResponse.details); } }, _handleError: function _handleError(ajaxError) { var responseJSON = ajaxError.jqXHR.responseJSON; this.logger.debug(responseJSON); if (responseJSON) { throw new SmartsError(responseJSON.smartsStatus, responseJSON.details); } else { throw new SmartsError(ajaxError.jqXHR.fail().status, ajaxError.jqXHR.fail().responseText); } } }); return SmartsVisRecommenderService; }); //# sourceMappingURL=SmartsVisRecommenderService.js.map