123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589 |
- 'use strict';
- 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; };
- /*
- *+------------------------------------------------------------------------+
- *| Licensed Materials - Property of IBM
- *| IBM Cognos Products: Dashboard
- *| (C) Copyright IBM Corp. 2018, 2020
- *|
- *| US Government Users Restricted Rights - Use, duplication or disclosure
- *| restricted by GSA ADP Schedule Contract with IBM Corp.
- *+------------------------------------------------------------------------+
- */
- define(['underscore', '../../lib/@waca/core-client/js/core-client/ui/core/Class', './IRUtils', '../../lib/@waca/dashboard-common/dist/core/UniqueHashIdBuilder', '../../filters/FilterSpecHelper'], function (_, Class, IRUtils, UniqueHashIdBuilder, FilterSpecHelper) {
- 'use strict';
- var _cntr = 0;
- var SLOT_TYPE_MAP = {
- 'fact': 'ordinal',
- 'attribute': 'category'
- };
- var SmartsCompareCardRecommenderHelper = Class.extend({
- // constructor
- init: function init(columnIds, requestOptions) {
- this.columnIds = columnIds;
- this.requestOptions = requestOptions;
- this.id = 'id_' + ++_cntr;
- SmartsCompareCardRecommenderHelper.inherited('init', this, arguments);
- },
- getRequestParameters: function getRequestParameters(bPartialTempModule) {
- var columnIds = this.columnIds,
- requestOptions = this.requestOptions;
- var assetId = requestOptions.assetId,
- numResults = requestOptions.numResults,
- sourceType = requestOptions.sourceType,
- module = requestOptions.module;
- var columns = this._getColumnsInfo(columnIds);
- var _columnIds = columns.map(function (column) {
- return column.id;
- }).reduce(function (acc, columnIds) {
- return acc.concat(columnIds);
- }, []);
- var tempModuleJSON = bPartialTempModule ? module.copyPartialMoserModuleJSON(_columnIds) : module.getTemporarySessionModuleJSON();
- var widgetSpec = requestOptions.widgetSpec;
- var visualization = requestOptions.visualization;
- var filters = this._toIRFilters(visualization, widgetSpec);
- var irContent = {
- id: this.id,
- topicId: '',
- combinations: [{
- columnCombinations: this._getColumnCombinationsForIR(visualization),
- filters: filters
- }]
- };
- var requestParameters = {
- sourceModuleId: assetId,
- sourceModule: JSON.stringify(tempModuleJSON),
- sourceType: sourceType,
- includedColumns: [],
- excludedColumns: [],
- shapedModule: !!module.getShapingId(), //module has shaping
- irContents: [irContent],
- maxCardsPerIR: numResults || 10
- };
- return module.saveTemporaryModule().then(function (shapingModuleId) {
- if (assetId !== shapingModuleId) {
- requestParameters.tempModuleId = shapingModuleId;
- }
- return requestParameters;
- });
- },
- _restoreHiddenCustomGroupCols: function _restoreHiddenCustomGroupCols(irContent) {
- var replacements = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
- if (replacements.length === 0) {
- return;
- }
- Object.keys(irContent).forEach(function (key) {
- irContent[key].forEach(function (irContentItem) {
- (irContentItem.combinations || []).forEach(function (combination) {
- (combination.columnCombinations || []).forEach(function (columnCombination, i) {
- var replacement = replacements.find(function (item) {
- return item.index === i;
- });
- if (replacement && replacement.to === columnCombination.id) {
- columnCombination.id = replacement.from;
- }
- });
- });
- });
- });
- },
- _replaceHiddenCustomGroupCols: function _replaceHiddenCustomGroupCols(columns, options) {
- var _this = this;
- var visualization = options.visualization,
- moserDataSources = options.moserDataSources;
- var sourceId = visualization.getDataSource().getId();
- if (moserDataSources && moserDataSources.isConsumerGroupColumn) {
- columns.forEach(function (columnEntry, i) {
- var itemId = columnEntry.itemId;
- if (moserDataSources.isConsumerGroupColumn(sourceId, itemId)) {
- var customGroup = moserDataSources.getCustomGroupData(sourceId, itemId);
- if (customGroup && customGroup.basedOnMoserObjectId) {
- var from = columns[i].itemId;
- var to = customGroup.basedOnMoserObjectId;
- columns[i].itemId = to;
- if (!_this.requestOptions.hiddenCustomGroupColumnReplacements) {
- _this.requestOptions.hiddenCustomGroupColumnReplacements = [];
- }
- _this.requestOptions.hiddenCustomGroupColumnReplacements.push({
- index: i,
- from: from,
- to: to
- });
- }
- }
- });
- }
- },
- _getColumnCombinationsForIR: function _getColumnCombinationsForIR(visualization) {
- var _columnCombinations = this._getColumnCombinations(visualization);
- this._replaceHiddenCustomGroupCols(_columnCombinations, {
- visualization: visualization,
- moserDataSources: this.requestOptions.moserDataSources
- });
- var colComboForIR = _columnCombinations.map(function (entry) {
- var irEntry = {
- id: entry.itemId
- };
- if (entry.aggregation) {
- irEntry.aggregation = entry.aggregation;
- }
- if (entry.autoGrouping) {
- irEntry.autoGrouping = entry.autoGrouping;
- }
- return irEntry;
- });
- return colComboForIR;
- },
- _getColumnCombinations: function _getColumnCombinations(visualization) {
- var combinations = [];
- var slots = visualization.getSlots().getMappedSlotList();
- slots.forEach(function (slotAPI) {
- var dataItems = slotAPI.getDataItemList(true /* ignoreDefaultDataItemMapping */);
- dataItems.forEach(function (dataItem) {
- var slotAggType = dataItem.getAggregation();
- var id = dataItem.getId();
- var itemId = dataItem.getColumnId();
- var dataType = dataItem.getMetadataColumn().getType();
- var label = dataItem.getLabel();
- var finalSlotType = SLOT_TYPE_MAP[dataItem.getType()];
- if (itemId !== '_multiMeasuresSeries') {
- if (slotAggType && finalSlotType === 'ordinal') {
- combinations.push({
- id: id,
- itemId: itemId,
- dataType: dataType,
- finalSlotType: finalSlotType,
- label: label,
- aggregation: IRUtils.IRAggregateType(slotAggType) //TODO this is IR aggregation type, could keep it dashboard and transform to IR where used
- });
- } else {
- var autoGrouping = void 0;
- if (dataItem.getBinning()) {
- autoGrouping = {
- size: dataItem.getBinning().bins
- };
- }
- combinations.push({
- id: id,
- itemId: itemId,
- dataType: dataType,
- finalSlotType: finalSlotType,
- label: label,
- autoGrouping: autoGrouping
- });
- }
- }
- });
- });
- return combinations;
- },
- transformCompareRecommendationResponse: function transformCompareRecommendationResponse(module, widgetSpec, irContent) {
- //TODO , requestOptions.module, requestOptions.widgetSpec
- var requestOptions = this.requestOptions;
- var postSpec = this._fromIRContent(requestOptions, widgetSpec, irContent);
- return postSpec;
- },
- _getColumnsInfo: function _getColumnsInfo(columnIds) {
- return columnIds.map(function (columnId) {
- return { id: [columnId] };
- });
- },
- _toIRFilters: function _toIRFilters(visualization, widgetSpec) {
- var filters = this._getFilters(visualization);
- var topBottom = this._getTopBottom(widgetSpec);
- var filtersForRequest = [];
- IRUtils.localFilters2IRFilters(filters, filtersForRequest, visualization);
- IRUtils.topBottomToIRFilters(topBottom, filtersForRequest);
- return filtersForRequest;
- },
- //TODO: this file is getting big, breakup/move utility functions
- _getFilters: function _getFilters(visualization) {
- var _this2 = this;
- //NOT ideal, have to use local filter spec
- var localFilters = visualization.getLocalFilters().getFilterList();
- if (!localFilters) {
- localFilters = [];
- }
- var leafFilters = [];
- // We need to convert all the filters to leaf level filters (ones with columnId),
- // beacuse some cases, the localfilter is a datapoint filter, which is a compound filter (no columnId)
- localFilters.forEach(function (localFilter) {
- return _this2._collectLeafFilterEntries(localFilter, leafFilters);
- });
- return JSON.parse(JSON.stringify(leafFilters));
- },
- /**
- * Given a filter, collect its leaf level filters
- * @param {Object} filter the filter spec
- * @param {Array} leafFilterEntries an array used to collect the leaf level filters (ones with columnId)
- */
- _collectLeafFilterEntries: function _collectLeafFilterEntries(filter, leafFilterEntries) {
- var _this3 = this;
- if (FilterSpecHelper.isCompound(filter)) {
- filter.values.forEach(function (value) {
- if (value) {
- _this3._collectLeafFilterEntries(value, leafFilterEntries);
- }
- });
- } else {
- leafFilterEntries.push(filter);
- }
- },
- _getTopBottom: function _getTopBottom(orgWidgetSpec) {
- //NOT ideal, have to read spec
- var topBottom = [];
- var widgetSpec = JSON.parse(JSON.stringify(orgWidgetSpec));
- var dataViews = widgetSpec.data.dataViews || [];
- dataViews.forEach(function (viewItem) {
- var dataItems = viewItem.dataItems || [];
- dataItems.forEach(function (item) {
- var selectionItems = item.selection || [];
- selectionItems.forEach(function (selection) {
- if (selection && selection.topBottom) {
- topBottom.push({
- id: item.id,
- itemId: item.itemId,
- selection: selection
- });
- }
- });
- });
- });
- return topBottom;
- },
- /*
- sample IR response
- {
- "smartsStatus" : "OK",
- "ircontent" : {
- "id" : {
- "id" : "id",
- "topicId" : "Movies.Budget",
- "combinations" : [ {
- "columnCombinations" : [ "Movies.Genre", "Movies.Income", "Movies.Gross" ],
- "filters" : [ {
- "id" : "Movies.Budget",
- "filterId" : "0",
- "lowValue" : "5",
- "highValue" : "10",
- "invertFlag" : false,
- "type" : "NUMERIC",
- "numericOperator" : "LESS_THAN"
- }, {
- "id" : "Movies.Genre",
- "filterId" : "0",
- "topBottomType" : "TOP",
- "method" : "PERCENTAGE",
- "byColumn" : "Movies.Budget",
- "domainSize" : 10,
- "type" : "TOP_BOTTOM"
- }, {
- "id" : "Movies.Budget",
- "filterId" : "0",
- "label" : "Include",
- "values" : [ {
- "uniqueValue" : "Movies.Studio.Columbia",
- "displayValue" : "Columbia"
- } ],
- "type" : "INCLUSION_EXCLUSION",
- "include" : true
- } ],
- "score" : 0.0
- } ],
- "score" : 0.0
- }
- }
- }
- */
- _fromIRContent: function _fromIRContent(requestOptions, widgetSpec, irContent) {
- var _this4 = this;
- var visualization = requestOptions.visualization;
- var candidateWidgetSpecs = [];
- var currentColumnCombinations = this._getColumnCombinations(visualization);
- this._restoreHiddenCustomGroupCols(irContent, requestOptions.hiddenCustomGroupColumnReplacements);
- if (irContent && irContent[this.id]) {
- irContent[this.id].forEach(function (irContentItem) {
- var widgetSpecs = _this4._processIRContent(irContentItem, widgetSpec, currentColumnCombinations, irContentItem.details, visualization);
- candidateWidgetSpecs.push.apply(candidateWidgetSpecs, widgetSpecs);
- });
- }
- return candidateWidgetSpecs;
- },
- _processIRContent: function _processIRContent(irContent, widgetSpec, currentColumnCombinations, reason, visualization) {
- var _this5 = this;
- var combinations = irContent.combinations;
- var candidateWidgetSpecs = [];
- var details = {
- reason: reason
- };
- combinations.forEach(function (irCandidate) {
- var filters = {
- local: [],
- topBottom: []
- };
- var irFilters = irCandidate.filters || [];
- irFilters.forEach(function (irFilter) {
- if (irFilter.type === 'TOP_BOTTOM') {
- var filter = IRUtils.IRFilters2TopBottom(irFilter, widgetSpec, currentColumnCombinations, details, visualization);
- if (filter) {
- filters.topBottom.push(filter);
- }
- } else {
- var _filter = IRUtils.IRFilters2LocalFilter(irFilter, widgetSpec, currentColumnCombinations, details, visualization);
- if (_filter) {
- filters.local.push(_filter);
- }
- }
- });
- widgetSpec.titleHtml = undefined;
- widgetSpec.name = undefined;
- var dataItemIdsMap = {};
- var candidateWidgetSpec = _this5._transform2WidgetSpec(widgetSpec, currentColumnCombinations, irCandidate.columnCombinations, filters, details, visualization, dataItemIdsMap);
- if (candidateWidgetSpec) {
- candidateWidgetSpecs.push({
- spec: candidateWidgetSpec,
- details: details,
- score: irCandidate.score
- });
- }
- });
- return candidateWidgetSpecs;
- },
- _transform2WidgetSpec: function _transform2WidgetSpec(widgetSpec, currentColumnCombinations, columnCombinations, filters, details, visualization, dataItemIdsMap) {
- var spec = JSON.parse(JSON.stringify(widgetSpec));
- this._assimilateTopBottom(spec, filters.topBottom, currentColumnCombinations, dataItemIdsMap);
- this._assimilateFilters(spec, filters.local);
- this._assimilateComparableMeasures(spec, columnCombinations, currentColumnCombinations, details, visualization, dataItemIdsMap);
- return spec;
- },
- _replaceSlotId: function _replaceSlotId(spec, oldId, newId) {
- spec.slotmapping.slots.forEach(function (item, index) {
- var dataItemIndex = item.dataItems.indexOf(oldId);
- if (dataItemIndex !== -1) {
- spec.slotmapping.slots[index].dataItems[dataItemIndex] = newId;
- }
- });
- },
- _assimilateTopBottom: function _assimilateTopBottom(spec, topBottomItems, currentColumnCombinations, dataItemIdsMap) {
- var _this6 = this;
- topBottomItems.forEach(function (item) {
- _this6._assimilateTopBottomItem(spec, item, currentColumnCombinations, dataItemIdsMap);
- });
- },
- _findMatchingColumnEntry: function _findMatchingColumnEntry(topBottomSpec, currentColumnCombinations) {
- var currentColEntry = void 0;
- currentColumnCombinations.some(function (entry) {
- if (entry.id === topBottomSpec.id) {
- currentColEntry = entry;
- return true;
- }
- });
- if (!currentColEntry) {
- var isAppropriateSlotForTopBottom = function isAppropriateSlotForTopBottom(entry, topBottomSpec) {
- if (topBottomSpec.selection[0].context) {
- //is contextual, should apply on a category
- if (entry.finalSlotType !== 'ordinal') {
- return true;
- } else {
- return false;
- }
- }
- return true;
- };
- if (currentColumnCombinations.length === 1) {
- currentColEntry = currentColumnCombinations[0].itemId === topBottomSpec.itemId ? currentColumnCombinations[0] : undefined;
- } else {
- currentColumnCombinations.some(function (entry) {
- if (entry.itemId === topBottomSpec.itemId && isAppropriateSlotForTopBottom(entry, topBottomSpec)) {
- currentColEntry = entry;
- return true;
- }
- });
- }
- }
- return currentColEntry;
- },
- _assimilateTopBottomItem: function _assimilateTopBottomItem(spec, topBottomSpec, currentColumnCombinations, dataItemIdsMap) {
- var currentColEntry = this._findMatchingColumnEntry(topBottomSpec, currentColumnCombinations);
- if (!currentColEntry) {
- return;
- }
- var dataItemRef = IRUtils.findDataItem(spec, function (dataItem) {
- return dataItem.id === currentColEntry.id;
- });
- if (!dataItemRef) {
- // unable to find proper data item
- return;
- }
- var originalDataItem = spec.data.dataViews[dataItemRef.viewIndex].dataItems[dataItemRef.viewItemIndex];
- var oldId = originalDataItem.id;
- var newId = UniqueHashIdBuilder.createUniqueHashId(originalDataItem.itemId, dataItemIdsMap);
- dataItemIdsMap[newId] = newId;
- spec.data.dataViews[dataItemRef.viewIndex].dataItems[dataItemRef.viewItemIndex] = _extends({}, topBottomSpec, {
- id: newId
- });
- this._replaceSlotId(spec, oldId, newId);
- },
- _assimilateFilters: function _assimilateFilters(spec, localFilters) {
- if (localFilters.length === 0 && spec.localFilters === undefined) {
- return;
- }
- spec.localFilters = localFilters;
- },
- _assimilateComparableMeasures: function _assimilateComparableMeasures(spec, columnCombinations, currentColumnCombinations, details, visualization, dataItemIdsMap) {
- var _columnCombinations = columnCombinations.map(function (col) {
- return col.id;
- });
- var combo = currentColumnCombinations.map(function (item) {
- return item.itemId;
- });
- var _$difference = _.difference(combo, _columnCombinations),
- from = _$difference[0];
- var _$difference2 = _.difference(_columnCombinations, combo),
- to = _$difference2[0];
- if (!from && !!to) {
- from = combo[0]; //all columns in currentColumnCombinations are the same and is included in the IR _columnCombinations
- }
- var id = void 0;
- currentColumnCombinations.forEach(function (curr) {
- if (curr.itemId === from && curr.finalSlotType != 'category' && !id) {
- id = curr.id;
- }
- });
- if (!id) {
- currentColumnCombinations.forEach(function (curr) {
- if (curr.itemId === from && !id) {
- id = curr.id;
- }
- });
- }
- if (details.reason === 'REPLACE_COMPARABLE_MEASURE') {
- details.type = 'REPLACE_COMPARABLE_MEASURE';
- details.from = IRUtils.getColumnInfo(currentColumnCombinations, visualization, from);
- details.to = IRUtils.getColumnInfo(currentColumnCombinations, visualization, to);
- }
- this._assimilateComparableMeasure(spec, from, to, id, dataItemIdsMap);
- },
- _assimilateComparableMeasure: function _assimilateComparableMeasure(spec, from, to, id, dataItemIdsMap) {
- // - update data items with the new top bottom spec + replace old id with new
- var dataItemRef = IRUtils.findDataItem(spec, function (dataItem) {
- return dataItem.itemId === from && dataItem.id === id;
- });
- if (!dataItemRef) {
- return;
- }
- var originalDataItem = spec.data.dataViews[dataItemRef.viewIndex].dataItems[dataItemRef.viewItemIndex];
- var oldId = originalDataItem.id;
- var newId = UniqueHashIdBuilder.createUniqueHashId(originalDataItem.itemId, dataItemIdsMap);
- dataItemIdsMap[newId] = newId;
- var toParts = to.split('.');
- var itemLabel = toParts[toParts.length - 1];
- spec.data.dataViews[dataItemRef.viewIndex].dataItems[dataItemRef.viewItemIndex] = _extends({}, originalDataItem, {
- itemId: to,
- itemLabel: itemLabel,
- id: newId
- });
- this._replaceSlotId(spec, oldId, newId);
- }
- });
- return SmartsCompareCardRecommenderHelper;
- });
- //# sourceMappingURL=SmartsCompareCardRecommenderHelper.js.map
|