QueryModifier.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. /**
  4. * Licensed Materials - Property of IBM
  5. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2019
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. define(['underscore', '../../../lib/@waca/dashboard-common/dist/core/APIFactory', './api/QueryModifierAPI', './DataQueryUtils', '../../../filters/FilterQuerySpec'], function (_, APIFactory, QueryModifierAPI, DataQueryUtils, FilterQuerySpec) {
  9. var QueryModifier = function () {
  10. function QueryModifier(_ref) {
  11. var content = _ref.content,
  12. dashboardAPI = _ref.dashboardAPI;
  13. _classCallCheck(this, QueryModifier);
  14. this.content = content;
  15. this.dashboard = dashboardAPI;
  16. this.eventGroups = this.dashboard.getFeature('EventGroups');
  17. this.globalFilters = this.dashboard.getFeature('GlobalFilters');
  18. this.content.getFeature('DataQueryExecution').registerQueryModifier(this.getAPI());
  19. }
  20. QueryModifier.prototype.getAPI = function getAPI() {
  21. if (!this.api) {
  22. this.api = APIFactory.createAPI(this, [QueryModifierAPI]);
  23. }
  24. return this.api;
  25. };
  26. QueryModifier.prototype.getType = function getType() {
  27. return 'main';
  28. };
  29. QueryModifier.prototype.modifyQuerySpecListForQueryHint = function modifyQuerySpecListForQueryHint(querySpecList) {
  30. var _this = this;
  31. //add more queryHints
  32. querySpecList.forEach(function (querySpec) {
  33. if (querySpec.type === _this.getType()) {
  34. //we only add to main query
  35. _this._addQueryHintToSpec(querySpec.spec);
  36. }
  37. });
  38. return querySpecList;
  39. };
  40. QueryModifier.prototype.modifyQuerySpecList = function modifyQuerySpecList(querySpecList) {
  41. querySpecList = this.modifyQuerySpecListForFilters(querySpecList);
  42. querySpecList = this.modifyQuerySpecListForQueryHint(querySpecList);
  43. return querySpecList;
  44. };
  45. QueryModifier.prototype.modifyQuerySpecListForFilters = function modifyQuerySpecListForFilters(querySpecList) {
  46. var _this2 = this;
  47. var visualization = this.content.getFeature('Visualization');
  48. var filterSpecList = DataQueryUtils.getFilterSpecListByVisualization(this.content, this.dashboard);
  49. var updatedFilterSpecList = DataQueryUtils.convertExcludeEmptyValuesFilters(filterSpecList, visualization.getDataSource());
  50. var filterQuerySpecList = this._getFilterQuerySpecList(updatedFilterSpecList, visualization);
  51. // apply the global filter to each query spec
  52. if (filterQuerySpecList && filterQuerySpecList.length) {
  53. _.each(querySpecList, function (querySpec) {
  54. if (querySpec.type === _this2.getType()) {
  55. var _querySpec$spec$filte;
  56. if (!querySpec.spec.filters) {
  57. querySpec.spec.filters = [];
  58. }
  59. (_querySpec$spec$filte = querySpec.spec.filters).push.apply(_querySpec$spec$filte, filterQuerySpecList);
  60. }
  61. });
  62. }
  63. return querySpecList;
  64. };
  65. /**
  66. * @private
  67. * Get the list of global filter query specifications
  68. */
  69. QueryModifier.prototype._getFilterQuerySpecList = function _getFilterQuerySpecList(filterList, visualization) {
  70. var filterSpec = new FilterQuerySpec(visualization);
  71. filterSpec.addFiltersToSpec(filterList);
  72. return filterSpec.hasFilterSpec() ? filterSpec.getFilterSpec() : [];
  73. };
  74. QueryModifier.prototype._addValueToQueryHints = function _addValueToQueryHints(spec, queryHintKey, queryHintValue) {
  75. if (!spec.queryHints) {
  76. spec.queryHints = {};
  77. }
  78. spec.queryHints[queryHintKey] = queryHintValue;
  79. };
  80. QueryModifier.prototype._addQueryHintToSpec = function _addQueryHintToSpec(spec) {
  81. var preferredModelItems = DataQueryUtils.getRelatedModelItemsForQueryHint(this.content, this.dashboard);
  82. if (preferredModelItems.length) {
  83. this._addValueToQueryHints(spec, 'preferredModelItems', preferredModelItems);
  84. }
  85. var refreshProperty = this.content.getPropertyValue('queryRefresh');
  86. if (!(refreshProperty && refreshProperty.autoRefresh)) {
  87. var localCache = this.dashboard.getCanvas().getPropertyValue('dataCache');
  88. if (localCache === 'yes') {
  89. this._addValueToQueryHints(spec, 'dataCacheExpiry', '3600');
  90. } else if (localCache === 'no') {
  91. this._addValueToQueryHints(spec, 'dataCacheExpiry', '0');
  92. }
  93. }
  94. };
  95. return QueryModifier;
  96. }();
  97. return QueryModifier;
  98. });
  99. //# sourceMappingURL=QueryModifier.js.map