DataQueryExecution.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. 'use strict';
  2. var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. /**
  5. * Licensed Materials - Property of IBM
  6. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2019, 2020
  7. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  8. */
  9. define(['../../../lib/@waca/dashboard-common/dist/core/APIFactory', './postProcess/QueryPostProcessHelper', './api/DataQueryExecutionAPI', './api/InternalDataQueryExecutionAPI', './api/QueryResultsAPI', './QueryResults', './DataQueryResult', '../../../widgets/livewidget/nls/StringResources', '../../dashboard/prompts/PromptsState', 'underscore'], function (APIFactory, QueryPostProcessHelper, DataQueryExecutionAPI, InternalDataQueryExecutionAPI, QueryResultsAPI, QueryResults, DataQueryResult, StringResources, PromptsState, _) {
  10. var _class, _temp;
  11. var DataQueryExecution = (_temp = _class = function () {
  12. function DataQueryExecution(params) {
  13. _classCallCheck(this, DataQueryExecution);
  14. this.MAIN = 'main';
  15. this.content = params.content;
  16. this.dashboard = params.dashboardAPI;
  17. this.logger = this.dashboard.getGlassCoreSvc('.Logger');
  18. this._queryProviders = {};
  19. this._queryModifiers = {};
  20. this._queryDefinitionProviders = {};
  21. this._queryDefinitionModifiers = {};
  22. this._queryPostProcessors = [];
  23. this._queryResults = null;
  24. this._rawResults = {};
  25. this._cachedDefinitionList = null;
  26. this._requestOptions = {};
  27. // Query key and value pair with the Key constructed from query spec and last modified time stamp, and the value
  28. // used to uniquely identify the query result data when render viz.
  29. this._queryKeyValuePair = {};
  30. this._promptsState = new PromptsState();
  31. }
  32. DataQueryExecution.prototype.getAPI = function getAPI(type) {
  33. if (type === 'internal') {
  34. if (!this.internalAPI) {
  35. this.internalAPI = APIFactory.createAPI(this, [DataQueryExecutionAPI, InternalDataQueryExecutionAPI]);
  36. }
  37. return this.internalAPI;
  38. } else {
  39. if (!this.api) {
  40. this.api = APIFactory.createAPI(this, [DataQueryExecutionAPI]);
  41. }
  42. return this.api;
  43. }
  44. };
  45. DataQueryExecution.prototype.destroy = function destroy() {
  46. this.content = null;
  47. this.dashboard = null;
  48. this.logger = null;
  49. this._queryProviders = null;
  50. this._queryModifiers = null;
  51. this._queryDefinitionProviders = null;
  52. this._queryDefinitionModifiers = null;
  53. this._rawResults = null;
  54. this._cachedDefinitionList = null;
  55. this.api = null;
  56. if (this._queryResults) {
  57. this._queryResults.destroy();
  58. this._queryResults = null;
  59. }
  60. this._promptsState.destroy();
  61. this._promptsState = null;
  62. };
  63. DataQueryExecution.prototype.addRequestOptions = function addRequestOptions() {
  64. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  65. this._requestOptions = _extends({}, this._requestOptions, options);
  66. };
  67. DataQueryExecution.prototype.removeRequestOptions = function removeRequestOptions() {
  68. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  69. var key = void 0;
  70. for (var i = 0; i < options.length; i++) {
  71. key = options[i];
  72. if (this._requestOptions[key]) {
  73. delete this._requestOptions[key];
  74. }
  75. }
  76. };
  77. DataQueryExecution.prototype.resetRequestOptions = function resetRequestOptions() {
  78. this._requestOptions = {};
  79. };
  80. DataQueryExecution.prototype._isValidOption = function _isValidOption(name) {
  81. return this._requestOptionWhiteList && this._requestOptionWhiteList.indexOf(name) !== -1;
  82. };
  83. DataQueryExecution.prototype.registerQueryProvider = function registerQueryProvider(provider) {
  84. if (provider && provider.getQuerySpecList) {
  85. var type = provider.getType();
  86. if (!this._queryProviders[type]) {
  87. this._queryProviders[type] = [];
  88. }
  89. this._queryProviders[type].push(provider);
  90. }
  91. };
  92. DataQueryExecution.prototype.registerQueryDefinitionProvider = function registerQueryDefinitionProvider(provider) {
  93. if (!provider.getQueryDefinitionList) {
  94. throw new Error('Invalid QueryDefinitionProvider');
  95. }
  96. var type = provider.getType();
  97. if (!this._queryDefinitionProviders[type]) {
  98. this._queryDefinitionProviders[type] = [];
  99. }
  100. this._queryDefinitionProviders[type].push(provider);
  101. };
  102. DataQueryExecution.prototype.getCurrentQueryResults = function getCurrentQueryResults() {
  103. var results = this._queryResults && this._queryResults.getAPI();
  104. if (!results) {
  105. // @todo temporary solution to obtain the query results when the query
  106. // was not executed by the new DataQueryExecution
  107. var renderSequence = this.content.getFeature('RenderSequence.internal');
  108. results = renderSequence.getCurrentRenderContext('data').getData('data');
  109. }
  110. return results;
  111. };
  112. DataQueryExecution.prototype.getRawResult = function getRawResult() {
  113. var id = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'data';
  114. return this._rawResults && this._rawResults[id];
  115. };
  116. /**
  117. * Add a spec with max min value as min max query results to the current query results
  118. * @param {object} spec - includes data items and min max info
  119. * Note - a falsy spec will remove the existing min max query results
  120. * eg.
  121. * {
  122. * reveuneavg: {
  123. * min: 20,
  124. * max: 100,
  125. * columnId: 'revenue',
  126. * layerId: 'data'
  127. * },
  128. * quantitysum: {
  129. * min: -100,
  130. * max: 80,
  131. * columnId: 'quantity',
  132. * layerId: 'data.region'
  133. * }
  134. * }
  135. */
  136. DataQueryExecution.prototype.setMinMaxQueryResults = function setMinMaxQueryResults(spec) {
  137. var _this = this;
  138. if (spec) {
  139. var layersData = {};
  140. _.each(spec, function (dataItemValue, dataItemKey) {
  141. var layerId = dataItemValue.layerId;
  142. var dataItem = {
  143. itemClass: {
  144. h: [{
  145. u: dataItemValue.columnId,
  146. aggregate: dataItemValue.aggregate
  147. }],
  148. id: dataItemKey
  149. }
  150. };
  151. if (layersData[layerId]) {
  152. layersData[layerId].pt.push({ v: dataItemValue.min }, { v: dataItemValue.max });
  153. layersData[layerId].dataItems.push(dataItem, dataItem);
  154. } else {
  155. layersData[layerId] = {
  156. pt: [{ v: dataItemValue.min }, { v: dataItemValue.max }],
  157. dataItems: [dataItem, dataItem]
  158. };
  159. }
  160. });
  161. // creates a min max query result per layer
  162. _.each(layersData, function (data, layerId) {
  163. var mockData = {
  164. dataItems: data.dataItems,
  165. data: [{ pt: data.pt }]
  166. };
  167. var queryResult = new DataQueryResult(mockData, _this._getVisualization().getSlots());
  168. _this._queryResults.addResult(queryResult, layerId, QueryResultsAPI.QUERY_RESULT_TYPE.MINMAX);
  169. });
  170. } else {
  171. // removes min max queries from all of the layers
  172. var layerIds = this._queryResults.getQueryResultIdList();
  173. _.each(layerIds, function (layerId) {
  174. _this._queryResults.removeResult(layerId, QueryResultsAPI.QUERY_RESULT_TYPE.MINMAX);
  175. });
  176. }
  177. return this._queryResults.getAPI();
  178. };
  179. DataQueryExecution.prototype.registerQueryModifier = function registerQueryModifier(modifier) {
  180. var _this2 = this;
  181. if (modifier && modifier.modifyQuerySpecList) {
  182. var type = modifier.getType();
  183. if (!this._queryModifiers[type]) {
  184. this._queryModifiers[type] = [];
  185. }
  186. this._queryModifiers[type].push(modifier);
  187. return {
  188. remove: function remove() {
  189. return _this2._queryModifiers[type].splice(_this2._queryModifiers[type].indexOf(modifier), 1);
  190. }
  191. };
  192. }
  193. };
  194. DataQueryExecution.prototype.registerQueryDefinitionModifier = function registerQueryDefinitionModifier(modifier) {
  195. var _this3 = this;
  196. if (!modifier.modifyQueryDefinitionList) {
  197. throw new Error('Invalid QueryDefinitionModifier');
  198. }
  199. var type = modifier.getType();
  200. if (!this._queryDefinitionModifiers[type]) {
  201. this._queryDefinitionModifiers[type] = [];
  202. }
  203. this._queryDefinitionModifiers[type].push(modifier);
  204. return {
  205. remove: function remove() {
  206. return _this3._queryDefinitionModifiers[type].splice(_this3._queryDefinitionModifiers[type].indexOf(modifier), 1);
  207. }
  208. };
  209. };
  210. DataQueryExecution.prototype.registerQueryPostProcessor = function registerQueryPostProcessor(postProcessor) {
  211. var _this4 = this;
  212. if (postProcessor) {
  213. this._queryPostProcessors.push(postProcessor);
  214. return {
  215. remove: function remove() {
  216. return _this4._queryPostProcessors.splice(_this4._queryPostProcessors.indexOf(postProcessor), 1);
  217. }
  218. };
  219. }
  220. };
  221. DataQueryExecution.prototype.executeQueries = function executeQueries() {
  222. var _this5 = this;
  223. var type = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.MAIN;
  224. if (this._skipDataQuery()) {
  225. return this._buildEmptyData();
  226. }
  227. return this._getQueryDefinitionList(type).then(function (queryDefinitions) {
  228. // Once the definition list is consumed for query execution
  229. // make sure to clear the definition cache
  230. _this5._setDefinitionListCache();
  231. //Favour using QueryService2 and its constructs if they support this query
  232. _this5._currentV2Specs = null;
  233. if (queryDefinitions && queryDefinitions.length) {
  234. return _this5._executeQueries(queryDefinitions).then(function (resultList) {
  235. _this5.resetRequestOptions();
  236. _this5._resetPromptState();
  237. return resultList;
  238. });
  239. }
  240. //Otherwise fall back to original QueryService
  241. var querySpecList = _this5._getQuerySpecList(type);
  242. if (querySpecList) {
  243. return _this5._doExecute(querySpecList, type).then(function (resultList) {
  244. _this5.resetRequestOptions();
  245. _this5._resetPromptState();
  246. return resultList;
  247. });
  248. } else {
  249. return Promise.reject();
  250. }
  251. });
  252. };
  253. DataQueryExecution.prototype._resetPromptState = function _resetPromptState() {
  254. if (this._promptsState.hasInProgressPrompts()) {
  255. this._promptsState.reset();
  256. } else {
  257. // Clean saved prompts
  258. this._getVisualization().getSavedPrompts().reset();
  259. }
  260. };
  261. DataQueryExecution.prototype._getQuerySpecList = function _getQuerySpecList() {
  262. var type = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.MAIN;
  263. var queryProviderList = this._queryProviders[type];
  264. var queryModifierList = this._queryModifiers[type];
  265. if (queryProviderList && queryProviderList.length > 0) {
  266. var querySpecList = [];
  267. for (var j = 0; j < queryProviderList.length; j++) {
  268. var provider = queryProviderList[j];
  269. var specList = provider.getQuerySpecList();
  270. for (var k = 0; k < specList.length; k++) {
  271. var specEntry = specList[k];
  272. querySpecList.push(specEntry);
  273. }
  274. }
  275. if (queryModifierList && queryModifierList.length > 0) {
  276. for (var i = 0; i < queryModifierList.length; i++) {
  277. var modifier = queryModifierList[i];
  278. querySpecList = modifier.modifyQuerySpecList(querySpecList);
  279. }
  280. }
  281. return querySpecList;
  282. } else {
  283. this.logger.error('Required query queryProvider is not registered');
  284. }
  285. };
  286. DataQueryExecution.prototype._getQueryDefinitionList = function _getQueryDefinitionList(type) {
  287. var _this6 = this;
  288. // use the cached list if available
  289. var cachedList = this._getDefinitionListCache();
  290. if (cachedList) {
  291. return Promise.resolve(cachedList);
  292. }
  293. var allProviders = this._queryDefinitionProviders[type] || [];
  294. var providers = allProviders.filter(function (provider) {
  295. return provider.supports(_this6.content);
  296. });
  297. if (providers.length) {
  298. var modifiers = this._queryDefinitionModifiers[type] || [];
  299. return Promise.all(providers.map(function (provider) {
  300. return provider.getQueryDefinitionList();
  301. })).then(function (definitionLists) {
  302. var definitionList = _.flatten(definitionLists);
  303. modifiers.forEach(function (modifier) {
  304. return definitionList = modifier.modifyQueryDefinitionList(definitionList);
  305. });
  306. return _this6._setDefinitionListCache(definitionList);
  307. });
  308. }
  309. return Promise.resolve(null);
  310. };
  311. DataQueryExecution.prototype._setDefinitionListCache = function _setDefinitionListCache() {
  312. var definitionList = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
  313. this._cachedDefinitionList = definitionList;
  314. return this._cachedDefinitionList;
  315. };
  316. DataQueryExecution.prototype._getDefinitionListCache = function _getDefinitionListCache() {
  317. return this._cachedDefinitionList;
  318. };
  319. DataQueryExecution.prototype._doExecute = function _doExecute(queryList, providerType) {
  320. var _this7 = this;
  321. var slots = this._getVisualization().getSlots();
  322. var internalQueryService = this.dashboard.getFeature('QueryService.internal');
  323. this.currentSpecList = [];
  324. var requestOptions = this._getRequestOptions();
  325. for (var i = 0; i < queryList.length; i++) {
  326. var query = queryList[i];
  327. this.currentSpecList.push(JSON.stringify(query.spec));
  328. query.result = internalQueryService.executeQuery(query.dataSourceId, query.spec, this.constructor.name, requestOptions);
  329. }
  330. var resultPromises = _.pluck(queryList, 'result');
  331. return Promise.all(resultPromises).then(function (rawResultList) {
  332. // save the raw result
  333. _this7._rawResults = {};
  334. _this7._buildQueryTag(queryList, rawResultList);
  335. var queryResults = new QueryResults(_this7._queryKeyValuePair.value);
  336. if (providerType === _this7.MAIN) {
  337. // cache the main query results
  338. _this7._queryResults = queryResults;
  339. }
  340. var _loop = function _loop(index) {
  341. var rawResult = rawResultList[index];
  342. var queryId = queryList[index].id;
  343. var queryType = queryList[index].type;
  344. // Should be able to just pass mapped data items, but due to DSS defect 274285, temporarily pass slots to build result.
  345. var resultInstance = new DataQueryResult(rawResult.data, slots);
  346. if ((providerType === _this7.MAIN || providerType === 'detail') && queryType === QueryResultsAPI.QUERY_RESULT_TYPE.MAIN) {
  347. if (providerType === _this7.MAIN) {
  348. // Make the query spec and raw response available as properties in the query result data object
  349. resultInstance.setPropertyValue('QuerySpec.internal', JSON.stringify(queryList[index].spec));
  350. // Ideally we should access the raw data from the ajax response as raw string,
  351. // but currentlt we don't have access to it because we query using modelling which we should mve away from
  352. resultInstance.setPropertyValue('RawData.internal', JSON.stringify(rawResult.data));
  353. resultInstance.setPropertyValue('RequestTime.internal', rawResult.requestTime || null);
  354. if (rawResult.data.meta && rawResult.data.meta.messages) {
  355. _this7._setQueryFeedbackResult(rawResult.data.meta.messages, resultInstance);
  356. }
  357. // Keep the raw data
  358. _this7._rawResults[queryId] = rawResult.data;
  359. }
  360. var queryDefinition = {
  361. getId: function getId() {
  362. return queryId;
  363. }
  364. };
  365. // Post process query result data
  366. _this7._postProcess(rawResult.data, queryDefinition);
  367. }
  368. queryResults.addResult(resultInstance, queryId, queryType);
  369. };
  370. for (var index = 0; index < rawResultList.length; index++) {
  371. _loop(index);
  372. }
  373. return queryResults.getAPI();
  374. }).catch(function (jqXHR) {
  375. var dataSource = _this7._getVisualization().getDataSource();
  376. return _this7._handleQueryError({ dataSource: dataSource, error: jqXHR }).then(function (promptInfoList) {
  377. if (promptInfoList && promptInfoList.length) {
  378. // Rerun queries after resolve the prompts
  379. queryList.forEach(function (query) {
  380. query.spec.parameterValues = promptInfoList;
  381. });
  382. return _this7._doExecute(queryList, providerType);
  383. }
  384. });
  385. });
  386. };
  387. DataQueryExecution.prototype._setQueryFeedbackResult = function _setQueryFeedbackResult(messages, resultInstance) {
  388. for (var i = 0; i < messages.length; i++) {
  389. var message = void 0;
  390. message = messages[i];
  391. var queryLanguagePropName = void 0;
  392. if (message[DataQueryExecution.QUERY_LANGUAGE_PROP_NAMES.QUERY_LANGUAGE]) {
  393. queryLanguagePropName = DataQueryExecution.QUERY_LANGUAGE_PROP_NAMES.QUERY_LANGUAGE;
  394. } else if (message[DataQueryExecution.QUERY_LANGUAGE_PROP_NAMES.TYPE]) {
  395. queryLanguagePropName = DataQueryExecution.QUERY_LANGUAGE_PROP_NAMES.TYPE;
  396. } else {
  397. return;
  398. }
  399. var queryLanguage = message[queryLanguagePropName];
  400. if (DataQueryExecution.COGNOS_QUERY_LANGUAGE.indexOf(queryLanguage) !== -1) {
  401. resultInstance.setPropertyValue('QueryFeedback.cognosSQL', message.message);
  402. } else if (DataQueryExecution.NATIVE_QUERY_LANGUAGE.indexOf(queryLanguage) !== -1) {
  403. resultInstance.setPropertyValue('QueryFeedback.nativeSQL', message.message);
  404. } else if (DataQueryExecution.MDX_QUERY_LANGUAGE.indexOf(queryLanguage) !== -1) {
  405. resultInstance.setPropertyValue('QueryFeedback.MDX', message.message);
  406. }
  407. }
  408. };
  409. DataQueryExecution.prototype._getRequestOptions = function _getRequestOptions() {
  410. return this._requestOptions;
  411. };
  412. DataQueryExecution.prototype._handleQueryError = function _handleQueryError(_ref) {
  413. var _this8 = this;
  414. var dataSource = _ref.dataSource,
  415. error = _ref.error;
  416. var prompts = this.dashboard.getFeature('Prompts');
  417. var isPromptFault = prompts.isPromptFault(error);
  418. var promptSpecList = isPromptFault && prompts.getPromptSpecList(error);
  419. if (isPromptFault && !this._promptsState.hasInValidState(promptSpecList)) {
  420. this._promptsState.updatePromptsState(promptSpecList);
  421. return prompts.openPromptView(promptSpecList, this._getVisualization()).then(function (responseList) {
  422. return _this8._resolvePromptValues(responseList);
  423. }).catch(function (error) {
  424. return Promise.reject(error);
  425. });
  426. } else {
  427. // TODO: We should eventually use 'getName' when it's implemented (it will be synchronous)
  428. // instead of using 'getLocalizedName' (which is async)
  429. return dataSource.getLocalizedName().then(function (name) {
  430. return Promise.reject(_this8._queryFailed(name, error));
  431. }).catch(function (error) {
  432. return Promise.reject(error);
  433. });
  434. }
  435. };
  436. DataQueryExecution.prototype._executeQueries = function _executeQueries(definitions) {
  437. var _this9 = this;
  438. var queryService = this.dashboard.getFeature('QueryService2');
  439. var query = queryService.createQuery(definitions);
  440. query.registerFaultHandler(this._handleQueryError.bind(this));
  441. query.registerPostProcessor(this._postProcess.bind(this));
  442. // Use this object to cache query results prior to post-processing and saving with the result.
  443. var rawDataInternalByQueryId = {};
  444. query.registerSaveRawDataHandler(function (rawDataResult, queryId) {
  445. //Save an object that contains the queryfeedback metadata (if any) and the raw result to save to the RawData.internal property.
  446. //NOTE: Its important to save string (or cloned) raw results here since post-processors can modify the output.
  447. rawDataInternalByQueryId[queryId] = { meta: rawDataResult.meta, result: JSON.stringify(rawDataResult) };
  448. });
  449. this._currentV2Specs = JSON.stringify(query.getSpecList());
  450. var requestOptions = this._getRequestOptions();
  451. return query.execute(undefined, requestOptions).then(function (queryResultList) {
  452. _this9._buildQueryDefinitionTag(query, queryResultList);
  453. var queryResults = new QueryResults(_this9._queryKeyValuePair.value, query.getWarningList());
  454. queryResultList.forEach(function (queryResult, index) {
  455. var queryDefinition = definitions[index];
  456. var queryId = queryDefinition.getId();
  457. var queryType = queryDefinition.getType();
  458. var rawResult = rawDataInternalByQueryId[queryId];
  459. queryResult.setPropertyValue('QuerySpec.internal', _this9._currentV2Specs);
  460. if (rawResult) {
  461. queryResult.setPropertyValue('RawData.internal', rawResult.result);
  462. if (rawResult.meta && rawResult.meta.messages) {
  463. _this9._setQueryFeedbackResult(rawResult.meta.messages, queryResult);
  464. }
  465. }
  466. queryResults.addResult(queryResult, queryId, queryType);
  467. });
  468. return queryResults.getAPI();
  469. });
  470. };
  471. DataQueryExecution.prototype._skipDataQuery = function _skipDataQuery() {
  472. var definition = this._getVisualization().getDefinition();
  473. return !!definition.getProperty('noDataQuery');
  474. };
  475. DataQueryExecution.prototype._buildEmptyData = function _buildEmptyData() {
  476. var slots = this._getVisualization().getSlots();
  477. var mappedInfo = slots.getMappingInfoList();
  478. var dataItemList = _.pluck(mappedInfo, 'dataItem');
  479. var emptyData = {
  480. dataItems: _.map(dataItemList, function (dataItem) {
  481. var emptyDataItem = {
  482. itemClass: {
  483. h: [{
  484. u: dataItem.getColumnId(),
  485. d: dataItem.getLabel()
  486. }],
  487. id: dataItem.getId()
  488. }
  489. };
  490. // ordinal dataitems requires an aggregation
  491. // while categorical dataitems requires an empty items array
  492. var aggregate = dataItem.getAggregation();
  493. if (!dataItem.hasDefaultAggregation() && aggregate) {
  494. emptyDataItem.itemClass.h[0].aggregate = aggregate;
  495. } else {
  496. emptyDataItem.items = [];
  497. }
  498. return emptyDataItem;
  499. }),
  500. data: []
  501. };
  502. var queryResult = new DataQueryResult(emptyData, slots);
  503. var queryResults = new QueryResults();
  504. queryResults.addResult(queryResult);
  505. return Promise.resolve(queryResults.getAPI());
  506. };
  507. DataQueryExecution.prototype._resolvePromptValues = function _resolvePromptValues(responseList) {
  508. var aPromptInfo = [];
  509. var queryProperties = ['name', 'values', 'dataType', 'capabilities', 'modelFilterItem'];
  510. _.each(responseList, function (promptResponse) {
  511. var promptResult = _.pick(promptResponse, queryProperties);
  512. aPromptInfo.push(promptResult);
  513. });
  514. this._resetSavedPrompts(aPromptInfo);
  515. // Prepare query spec values based on prompt dataType
  516. var promptValueSpecs = JSON.parse(JSON.stringify(aPromptInfo));
  517. _.each(promptValueSpecs, function (promptInfo) {
  518. // MUN based dataType expect a useValue while all other types expect a displayValue for a parameter.
  519. if (!promptInfo.capabilities.optional) {
  520. // Check whether saved old spec
  521. // TODO may need upgrade for saved spec.
  522. var oldSpec = promptInfo.values[0].u;
  523. var valueType = void 0;
  524. if (oldSpec) {
  525. valueType = promptInfo.dataType === 'memberUniqueName' || promptInfo.dataType === 'hierarchyUniqueName' ? 'u' : 'd';
  526. } else {
  527. valueType = promptInfo.dataType === 'memberUniqueName' || promptInfo.dataType === 'hierarchyUniqueName' ? 'value' : 'label';
  528. }
  529. promptInfo.values = _.pluck(promptInfo.values, valueType);
  530. }
  531. });
  532. return promptValueSpecs;
  533. };
  534. DataQueryExecution.prototype._resetSavedPrompts = function _resetSavedPrompts(currPromptSpecs) {
  535. var savedPrompts = this._getVisualization().getSavedPrompts();
  536. var newSpecs = null;
  537. if (currPromptSpecs && currPromptSpecs.length) {
  538. newSpecs = {};
  539. currPromptSpecs.forEach(function (currPromptSpec) {
  540. newSpecs[currPromptSpec.name] = currPromptSpec;
  541. });
  542. }
  543. // Reset to null if prompts are all deleted or reset with new values.
  544. savedPrompts.reset(newSpecs);
  545. };
  546. DataQueryExecution.prototype._postProcess = function _postProcess(rawDataResult) {
  547. var queryDefinition = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
  548. // In the future, to preserve the order of the post processors, a possible way is to define an array of
  549. // other post processor dependencies while register to query execution.
  550. this._invokeRegisteredPostProcessors(rawDataResult);
  551. QueryPostProcessHelper.postProcessMeasuresAsSeries(rawDataResult, this._getVisualization(), queryDefinition);
  552. //There are only 2 values for suppression 'rowsAndColumns' or 'none'
  553. var suppressionEnabled = this.content.getPropertyValue('suppression') !== 'none';
  554. QueryPostProcessHelper.postProcessMultiEdgeMeasures(rawDataResult, queryDefinition, suppressionEnabled);
  555. QueryPostProcessHelper.postProcessAggregatedSort(rawDataResult, this._getVisualization());
  556. QueryPostProcessHelper.postProcessCustomSort(rawDataResult, this._getVisualization());
  557. QueryPostProcessHelper.postProcessOlapProperties(rawDataResult, this._getVisualization());
  558. };
  559. DataQueryExecution.prototype._invokeRegisteredPostProcessors = function _invokeRegisteredPostProcessors(rawDataResult) {
  560. this._queryPostProcessors.forEach(function (processor) {
  561. return processor.process(rawDataResult);
  562. });
  563. };
  564. DataQueryExecution.prototype._buildGenericQueryTag = function _buildGenericQueryTag(specList, resultList) {
  565. var fullKey = '';
  566. var INIT_TAG_ID = 100;
  567. var isLiveData = false;
  568. specList.forEach(function (spec, index) {
  569. if (spec) {
  570. fullKey = fullKey.concat(JSON.stringify(spec));
  571. }
  572. var result = resultList[index];
  573. if (result.ETag) {
  574. fullKey = fullKey.concat(result.ETag);
  575. } else {
  576. isLiveData = true;
  577. }
  578. });
  579. if (_.isEmpty(this._queryKeyValuePair)) {
  580. this._queryKeyValuePair.key = fullKey;
  581. this._queryKeyValuePair.value = INIT_TAG_ID;
  582. } else if (this._queryKeyValuePair.key !== fullKey || isLiveData) {
  583. // Update the key value pair
  584. this._queryKeyValuePair.key = fullKey;
  585. this._queryKeyValuePair.value++;
  586. }
  587. };
  588. DataQueryExecution.prototype._buildQueryTag = function _buildQueryTag(queryList, resultList) {
  589. this._buildGenericQueryTag(queryList.map(function (_ref2) {
  590. var spec = _ref2.spec;
  591. return spec;
  592. }), resultList);
  593. };
  594. DataQueryExecution.prototype._buildQueryDefinitionTag = function _buildQueryDefinitionTag(query, resultList) {
  595. this._buildGenericQueryTag(query.getSpecList(), resultList);
  596. };
  597. DataQueryExecution.prototype._queryFailed = function _queryFailed(sourceName, failure) {
  598. var hasUnavailableMetadataColumns = false;
  599. if (failure.message === 'promptingIsDisabled') {
  600. // this failure will be handled in Explore
  601. return failure;
  602. }
  603. var msg = 'dwErrorRunningQuery';
  604. var code = void 0;
  605. if (failure.reason === 'staleRequest') {
  606. msg = 'dwErrorStaleRequest';
  607. } else if (failure.reason === 'geoQueryFail') {
  608. msg = 'dwErrorGeoData';
  609. } else if (failure.reason === 'cancelPromptSignon') {
  610. msg = 'dwPromptSignonCancelWarning';
  611. } else if (failure.reason === 'unSupportedPromptType') {
  612. msg = failure.reason;
  613. } else if (failure.responseJSON && failure.responseJSON.errors) {
  614. var oErr = failure.responseJSON.errors[0];
  615. code = oErr && oErr.code;
  616. if (code === 'DSS-GEN-0002') {
  617. hasUnavailableMetadataColumns = true;
  618. } else if (code === 'DSS-GEN-0001') {
  619. msg = StringResources.get('errorSourceNotFound', { sourceName: sourceName });
  620. } else if (code === 'XQE-PLN-0226') {
  621. msg = StringResources.get('errorActionNotSupported', { sourceName: sourceName });
  622. } else if (code === 'XQE-PLN-0214') {
  623. msg = StringResources.get('errorMeasureNotSupportedOnMultiEdges');
  624. } else if (code === 'XQE-MDX-0020') {
  625. msg = StringResources.get('errorExceedAllowableValue');
  626. } else if (oErr && oErr.message) {
  627. msg = oErr.message;
  628. }
  629. }
  630. if (failure.reason !== 'staleRequest') {
  631. var state = this.content.getFeature('state.internal');
  632. state.setError(Object.assign(new Error(), { msg: msg, code: code }));
  633. }
  634. var param = {
  635. 'datasetName': sourceName
  636. };
  637. return Object.assign(new Error(), {
  638. msg: msg,
  639. param: param,
  640. hasUnavailableMetadataColumns: hasUnavailableMetadataColumns,
  641. errorInfo: failure
  642. });
  643. };
  644. DataQueryExecution.prototype.queryChanged = function queryChanged() {
  645. // complete mapping is required for a complete query
  646. // attempt to create the definition list without a complete mapping
  647. // will result the definition cache to become stale
  648. if (!this._getVisualization().getSlots().isMappingComplete()) {
  649. return Promise.resolve(false);
  650. }
  651. return this._queryChanged();
  652. };
  653. DataQueryExecution.prototype._queryChanged = function _queryChanged() {
  654. var _this10 = this;
  655. // this._getQueryDefinitionList always returns a promise (even for v1)
  656. return this._getQueryDefinitionList(this.MAIN).then(function (queryDefinitions) {
  657. var queryChanged = false;
  658. if (queryDefinitions && queryDefinitions.length) {
  659. var queryService = _this10.dashboard.getFeature('QueryService2');
  660. var query = queryService.createQuery(queryDefinitions, 'v2');
  661. var querySpec = query && query.getSpecList();
  662. queryChanged = JSON.stringify(querySpec) !== _this10._currentV2Specs;
  663. if (!queryChanged) {
  664. _this10._setDefinitionListCache();
  665. }
  666. } else {
  667. //Otherwise fall back to original QueryService
  668. var querySpecList = _this10._getQuerySpecList();
  669. if (!querySpecList) {
  670. // Should not happen
  671. _this10.logger.error('Could not build query spec');
  672. }
  673. if (!_this10._queryResults) {
  674. queryChanged = true;
  675. } else {
  676. queryChanged = _.some(_this10.currentSpecList, function (currentSpec, idx) {
  677. var querySpec = querySpecList[idx] && querySpecList[idx].spec;
  678. return JSON.stringify(querySpec) !== currentSpec;
  679. });
  680. }
  681. }
  682. return queryChanged;
  683. });
  684. };
  685. DataQueryExecution.prototype._getVisualization = function _getVisualization() {
  686. if (!this._visualization) {
  687. this._visualization = this.content.getFeature('Visualization.internal');
  688. }
  689. return this._visualization;
  690. };
  691. return DataQueryExecution;
  692. }(), _class.QUERY_LANGUAGE_PROP_NAMES = { TYPE: 'type', QUERY_LANGUAGE: 'queryLanguage' }, _class.COGNOS_QUERY_LANGUAGE = ['cognosSQL'], _class.NATIVE_QUERY_LANGUAGE = ['SQL', 'nativeSQL'], _class.MDX_QUERY_LANGUAGE = ['MDX'], _temp);
  693. return DataQueryExecution;
  694. });
  695. //# sourceMappingURL=DataQueryExecution.js.map