VisKeyDriverExecution.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2018, 2020
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. */
  7. define(['underscore', '../../../../lib/@waca/core-client/js/core-client/ui/core/Class', '../../nls/StringResources'], function (_, Class, resources) {
  8. 'use strict';
  9. var VisKeyDriverExecution = Class.extend({
  10. // Different message types returned by the predict service.
  11. DIAG_MSG_TYPE: 'DIAG_MSG_TYPE',
  12. WARN_MSG_TYPE: 'WARN_MSG_TYPE',
  13. ERR_MSG_TYPE: 'ERR_MSG_TYPE',
  14. // MSG Type to logger title
  15. MSG_TO_LOG_TITLE: {
  16. DIAG_MSG_TYPE: 'FPD Diagnostics',
  17. WARN_MSG_TYPE: 'FPD Warnings',
  18. ERR_MSG_TYPE: 'FPD Errors'
  19. },
  20. /**
  21. * Predict service REST API response status values
  22. **/
  23. RESPONSE_STATUS: {
  24. 'SUCCESS': 'SUCCESS'
  25. },
  26. /**
  27. * Predict service HTTP response status
  28. **/
  29. HTTP_RESPONSE_STATUS: {
  30. SUCCESS: 200,
  31. BAD_REQUEST: 400,
  32. SMARTS_SERVER_ERROR: 500
  33. },
  34. init: function init(options) {
  35. VisKeyDriverExecution.inherited('init', this, arguments);
  36. options = options || {};
  37. this.keyDriverService = options.keyDriverService;
  38. this.logger = options.logger;
  39. },
  40. _getConsoleFuncOutput: function _getConsoleFuncOutput(isError) {
  41. return isError ? console.error : console.warn;
  42. },
  43. /**
  44. * Log FPD service endpoint messages.
  45. * @param {String} messageType - see *_MSG_TYPE, where * is ERR, WARN or DIAG
  46. * @param {Boolean} isError - true if this is an error level set of message, false otherwise
  47. * @param {Array} messageArray - array of messages. This maybe and array of objects with
  48. * captions defined or may just be a string.
  49. */
  50. _logMessagesOfType: function _logMessagesOfType(messageType, isError, messageArray) {
  51. // Only log if we have messages
  52. if (messageArray && messageArray.length > 0) {
  53. // Determine the console and logger methods to use.
  54. var consoleFunc = this._getConsoleFuncOutput(isError); // put it in a method for testing purposes.
  55. var messageFunc = isError ? this.logger.error.bind(this.logger) : this.logger.warn.bind(this.logger);
  56. messageFunc && messageFunc(this.MSG_TO_LOG_TITLE[messageType]);
  57. try {
  58. consoleFunc && consoleFunc(this.MSG_TO_LOG_TITLE[messageType]);
  59. } catch (e) {/*IE11 will fail when you call consoleFunc() when the dev tools are closed.*/}
  60. messageArray.forEach(function (message) {
  61. // warnings and errors are objects with captions, diagnostics are just strings.
  62. var msg = message.caption ? message.caption : message;
  63. messageFunc && messageFunc(msg);
  64. try {
  65. consoleFunc && consoleFunc(msg);
  66. } catch (e) {/*IE11 will fail when you call consoleFunc() when the dev tools are closed.*/}
  67. });
  68. }
  69. },
  70. /**
  71. * @public
  72. * @function execute
  73. * Execute the SmartAnnotation query
  74. * @param {Object} Options
  75. * @returns a promise that resolves with an object with a result, error, and possibleDrivers.
  76. */
  77. execute: function execute(options /*targetId, assertId*/) {
  78. var _this = this;
  79. var querySpec = this._buildQuery(options);
  80. return this.keyDriverService.runQuery({
  81. querySpec: querySpec
  82. }).then(function (_ref) {
  83. var data = _ref.data;
  84. /*
  85. * What? The request is successful? Hurry, return the result while you can!
  86. * Ensure the result is what we expect and determine the possible drivers.
  87. */
  88. var possibleDrivers = [];
  89. var predictStatus = data.predictStatus;
  90. if (predictStatus && predictStatus.state !== 'SUCCESS') {
  91. /*
  92. We had a failure, lets log any reported diagnostics but we will also
  93. just carry on by returning empty possible key drivers. This will show the
  94. user a message and a blank vis. This can be improved upon in future releases.
  95. */
  96. _this._logMessagesOfType(_this.DIAG_MSG_TYPE, false /*isError */, predictStatus.diagnostics);
  97. } else if (data.results && data.results.recommendedFields && data.results.recommendedFields.fields) {
  98. // Success!!!, set the recommended drivers
  99. possibleDrivers = data.results.recommendedFields.fields;
  100. }
  101. /*
  102. Log warnings and errors.
  103. */
  104. _this._logMessagesOfType(_this.WARN_MSG_TYPE, false /*isError */, predictStatus.warnings);
  105. _this._logMessagesOfType(_this.ERR_MSG_TYPE, true /*isError */, predictStatus.errors);
  106. return {
  107. result: 'SUCCESS',
  108. error: null,
  109. possibleDrivers: possibleDrivers,
  110. fpdWarnings: predictStatus.warnings,
  111. fpdErrors: predictStatus.errors
  112. };
  113. }, function (err) {
  114. /*
  115. * Thats more like it, a failed call. Determine the error to return and build
  116. * an empty possibleDriver response.
  117. */
  118. return {
  119. result: 'ERROR',
  120. error: _this._errorResponseHandler(err),
  121. possibleDrivers: [],
  122. fpdWarnings: [],
  123. fpdErrors: []
  124. };
  125. });
  126. },
  127. _errorResponseHandler: function _errorResponseHandler(data) {
  128. var sMsg = void 0;
  129. var sFPDErrorMessage = void 0;
  130. var errorCategory = 'fpd_data_source_category';
  131. /*
  132. *Service Error response should always return a error code
  133. */
  134. var nErrCode = data && data.code ? data.code : 0;
  135. switch (nErrCode) {
  136. case this.HTTP_RESPONSE_STATUS.BAD_REQUEST:
  137. {
  138. sMsg = resources.get('smart_annotation_unauthorized');
  139. sFPDErrorMessage = resources.get('fpd_invalid_data_source');
  140. break;
  141. }
  142. case this.HTTP_RESPONSE_STATUS.SMARTS_SERVER_ERROR:
  143. {
  144. errorCategory = 'predict_error';
  145. sMsg = resources.get('smart_annotation_method');
  146. sFPDErrorMessage = resources.get('fpd_network_issue');
  147. break;
  148. }
  149. default:
  150. {
  151. sMsg = resources.get('smart_annotation_generic_error');
  152. sFPDErrorMessage = resources.get('fpd_invalid_data_source');
  153. }
  154. }
  155. var oErr = new Error();
  156. oErr.message = sMsg;
  157. oErr.code = data.code;
  158. oErr.stack = data.stack;
  159. oErr.caption = sFPDErrorMessage;
  160. oErr.categoryId = errorCategory;
  161. return oErr;
  162. },
  163. /**
  164. * @private
  165. * @function _buildSuggestionQuery
  166. * Build the suggestion query spec
  167. * @returns annotation suggestion query spec
  168. */
  169. _buildQuery: function _buildQuery(options) {
  170. var querySpec = {
  171. moduleId: options.assetId,
  172. targetFieldIDForExpression: options.targetId,
  173. tuningParams: {
  174. allowCategoryTarget: true,
  175. noCorrelationLimit: true
  176. },
  177. shapedModule: options.hasShaping
  178. };
  179. if (options.tempModule) {
  180. querySpec.tempModule = JSON.stringify(options.tempModule);
  181. }
  182. if (options.tempModuleId) {
  183. querySpec.tempModuleId = options.tempModuleId;
  184. }
  185. return querySpec;
  186. }
  187. });
  188. // Enum for the execution targets
  189. VisKeyDriverExecution.TARGETS = {
  190. SUGGESTIONS: 'suggestions',
  191. EXECUTIONS: 'executions'
  192. };
  193. return VisKeyDriverExecution;
  194. });
  195. //# sourceMappingURL=VisKeyDriverExecution.js.map