123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486 |
- '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; };
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
- /**
- * Licensed Materials - Property of IBM
- * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2020
- * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
- */
- define(['underscore', '../../../widgets/livewidget/nls/StringResources', '../../../lib/@waca/dashboard-common/dist/core/APIFactory', './api/DataBehindTheVisAPI', '../../../lib/@waca/dashboard-common/dist/utils/ContentUtil', '../../../apiHelpers/SlotAPIHelper', '../../../features/content/dataQueryExecution/DataQueryUtils', '../../../dataSources/nls/StringResources'], function (_, StringResources, APIFactory, DataBehindTheVisAPI, ContentUtil, SlotAPIHelper, DataQueryUtils, DSStringResources) {
- var DataBehindTheVis = function () {
- function DataBehindTheVis(_ref) {
- var content = _ref.content,
- features = _ref.features;
- _classCallCheck(this, DataBehindTheVis);
- this.content = content;
- this.dashboard = features['Dashboard.API'];
- this.csvExport = features['Dashboard.CsvExport'];
- this.queryService = features['Dashboard.QueryService'];
- this.globalFilters = features['Dashboard.GlobalFilters'];
- this.eventGroups = features['Dashboard.EventGroups'];
- this.pageContextService = features['Dashboard.PageContextService'];
- this.visualization = features.Visualization;
- this.queryExec = features.DataQueryExecution;
- }
- DataBehindTheVis.prototype.getAPI = function getAPI() {
- if (!this._api) {
- this._api = APIFactory.createAPI(this, [DataBehindTheVisAPI]);
- }
- return this._api;
- };
- DataBehindTheVis.prototype.export = function _export() {
- var _this = this;
- var doAggregate = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
- return this.getData(doAggregate).then(function (data) {
- return _this.csvExport.export(data.title, [data.headers], data.cells);
- });
- };
- DataBehindTheVis.prototype.destroy = function destroy() {
- this.dashboardApi = null;
- };
- DataBehindTheVis.prototype.getData = function getData() {
- var _this2 = this;
- var doAggregate = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
- return this._queryForData(doAggregate).then(function (result) {
- var data = _this2._filterDataWithSelections(result);
- return _extends({
- title: _this2._getTitle(),
- isAggregated: !!doAggregate
- }, data);
- });
- };
- DataBehindTheVis.prototype._queryForData = function _queryForData() {
- var _this3 = this;
- var doAggregate = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
- var datasource = this.visualization.getDataSource();
- if (datasource) {
- doAggregate = !!doAggregate;
- var extraColumnIds = this._getColumnIdsFromUnprojectedFilters();
- var hasMultiMeasures = this._isMultiMeasureVisualization(this.visualization);
- var hasExtraColumns = !hasMultiMeasures && extraColumnIds && extraColumnIds.length > 0;
- var dataset = this._getQueryDataset();
- var mappingComplete = this.visualization.getSlots().isMappingComplete();
- var execute = hasExtraColumns ? this._getDataFromQuery(dataset, extraColumnIds, doAggregate) : this._getDataFromExecution(dataset, doAggregate, mappingComplete);
- return execute.then(function (result) {
- return _this3._getProcessedHeadersAndCells(result);
- });
- }
- // resolve with empty data
- return Promise.resolve({
- headers: [],
- cells: []
- });
- };
- DataBehindTheVis.prototype._getTitle = function _getTitle() {
- return StringResources.get('visExportFileName');
- };
- /**
- * filtered data down to the vis data selection if there is any
- */
- DataBehindTheVis.prototype._filterDataWithSelections = function _filterDataWithSelections(data) {
- if (data.cells) {
- var filteredDataCells = [];
- var selections = this._getSelections();
- if (selections && selections.length) {
- _.each(data.cells, function (cell) {
- _.each(selections, function (selection) {
- var isSelectionInTheRow = void 0;
- var isTupleSetInCell = function isTupleSetInCell(valueFromTupleSet) {
- return _.some(cell, function (cellValue) {
- // [249167] selections come in decorated as strings from vipr tuples
- // u is usually a string
- return (cellValue.value && cellValue.value.toString()) === valueFromTupleSet.u;
- });
- };
- if (selection.dataPoints) {
- // data points filters
- isSelectionInTheRow = _.some(selection.dataPoints, function (dataPoint) {
- return _.every(dataPoint, function (valueFromTupleSet) {
- return isTupleSetInCell(valueFromTupleSet);
- });
- });
- } else {
- // edge filters
- isSelectionInTheRow = _.some(selection.edges, function (valueFromTupleSet) {
- return isTupleSetInCell(valueFromTupleSet);
- });
- }
- // remove checking if entry already exists when live-widget changes for
- // brushing behaviour for table and indented list has been updated
- if (isSelectionInTheRow && !(filteredDataCells.indexOf(cell) !== -1)) {
- filteredDataCells.push(cell);
- }
- });
- });
- data.cells = filteredDataCells;
- }
- }
- return data;
- };
- /**
- * Get the list of columnIds from unprojected local filters
- * @returns {String[]}
- */
- DataBehindTheVis.prototype._getColumnIdsFromUnprojectedFilters = function _getColumnIdsFromUnprojectedFilters() {
- var _this4 = this;
- var projectedDataItems = this._getVisibleDataItems();
- var columnIds = [];
- var dataSource = this.visualization.getDataSource();
- _.each(this._getFilterInfo(), function (filterEntries) {
- _.each(filterEntries, function (filterEntry) {
- // Get itemId if exists; Otherwise get columnId from filter entry
- // Edge filter entry does not have itemId; it has only columnId that serves the same purpurse as itemId
- // Data point filter has itemId but it does not have columnId.
- // see more _decoratePageContext function in visFilterSupport
- _this4._getUnProjectedItemsFromFilterEntry(_.pluck(projectedDataItems, 'itemId'), filterEntry, columnIds, dataSource);
- });
- });
- return columnIds;
- };
- DataBehindTheVis.prototype._isMultiMeasureVisualization = function _isMultiMeasureVisualization() {
- return !!_.find(this.visualization.getSlots().getMappedSlotList(), function (slot) {
- return SlotAPIHelper.isMultiMeasuresSeriesSlot(slot);
- });
- };
- DataBehindTheVis.prototype._getQueryDataset = function _getQueryDataset() {
- var definition = this.visualization.getDefinition();
- var datasetList = definition.getDatasetList();
- // If there is only 1 data set then use it's id, otherwise, return the default id
- return datasetList && datasetList.length === 1 ? datasetList[0].id : definition.getDefaultDatasetId();
- };
- DataBehindTheVis.prototype._getDataFromQuery = function _getDataFromQuery(dataset, extraColumnIds, doAggregate) {
- var datasource = this.visualization.getDataSource();
- var queryService = this.dashboard.getFeature('QueryService');
- var queryType = doAggregate ? queryService.TYPE.DEFAULT : queryService.TYPE.DETAIL;
- var query = queryService.createQuery(queryType);
- // setup datasource and projected data items
- query.setDataSource(datasource.getId());
- query.addDataItems(this._getDataItems(dataset));
- // append extra data items
- query.addColumns(extraColumnIds);
- // apply all filters
- var filterInfo = DataQueryUtils.removeForecastFilters(this._getFilterInfo());
- var filterList = filterInfo.localFilters;
- filterList.push.apply(filterList, filterInfo.selectionFilters.concat(filterInfo.globalFilters));
- query.addFilters(filterList);
- return query.executeQuery();
- };
- DataBehindTheVis.prototype._getDataFromExecution = function _getDataFromExecution(dataset, doAggregate, mappingComplete) {
- if (doAggregate) {
- var queryResults = this.queryExec.getCurrentQueryResults();
- if (!mappingComplete || _.isEmpty(queryResults)) {
- return Promise.resolve(false);
- }
- // @todo use new query results api
- if (queryResults.getImplType && queryResults.getInterfaceType) {
- return Promise.resolve(queryResults.getResult(dataset));
- }
- if (dataset) {
- return Promise.resolve(queryResults.getQueryResultByDataViewId(dataset).getQueryResult());
- }
- return Promise.resolve(queryResults.getDefaultQueryResult().getQueryResult());
- } else {
- // submit a detail query to obtain the disaggregated result
- var results = this.queryExec.executeQueries('detail');
- //return results.getResult(dataset);
- return results.then(function (_results) {
- return _results.getResult(dataset);
- });
- }
- };
- DataBehindTheVis.prototype._getVisibleDataItems = function _getVisibleDataItems() {
- var dataItems = [];
- var nestList = [];
- var slots = this.visualization.getSlots();
- _.each(slots.getMappedSlotList(), function (slot) {
- _.each(slot.getDataItemList(), function (dataItem, index) {
- if (!SlotAPIHelper.isMultiMeasuresSeriesOrValue(slot, index)) {
- dataItems.push({
- id: dataItem.getId(),
- itemId: dataItem.getColumnId()
- });
- //keep track of the list of dataItems assigned to a slot.
- //If a slot has multiple items and the slot is type stacked,
- //use this list as the nestList of a stacked dataItem (see below)
- nestList.push(dataItem.getId());
- }
- }.bind(this));
- if (nestList.length > 1 && slot.isStacked()) {
- //If the slot is stacked, produce a dataItem whose name matches the id
- //with nested items whose names match the dataItems.
- var datasetIds = slot.getDefinition().getDatasetIdList();
- datasetIds.forEach(function (datasetId) {
- dataItems.push({
- id: slot.getId(),
- nest: nestList,
- layerId: datasetId
- });
- });
- }
- });
- return dataItems;
- };
- DataBehindTheVis.prototype._getUnProjectedItemsFromFilterEntry = function _getUnProjectedItemsFromFilterEntry(projectedDataItemIDs, filterEntry, aUnProjectedItemsFromFilterEntry, dataSource) {
- var filterEntryItemId = filterEntry.itemId || filterEntry.columnId;
- var column = dataSource.getMetadataColumn(filterEntryItemId);
- // Skip model filter columns
- if (!column || column.getObjectType() !== 'Filter') {
- if (filterEntryItemId) {
- if (projectedDataItemIDs.indexOf(filterEntryItemId) === -1 && aUnProjectedItemsFromFilterEntry.indexOf(filterEntryItemId) === -1) {
- aUnProjectedItemsFromFilterEntry.push(filterEntryItemId);
- }
- } else {
- _.each(filterEntry.values, function (valueExpr) {
- this._getUnProjectedItemsFromFilterEntry(projectedDataItemIDs, valueExpr, aUnProjectedItemsFromFilterEntry, dataSource);
- }.bind(this));
- }
- }
- };
- DataBehindTheVis.prototype._getFilterInfo = function _getFilterInfo() {
- var datasource = this.visualization.getDataSource();
- var localFilters = this.visualization.getLocalFilters().getFilterList();
- var type = this.dashboard.getAppConfig('pageContainerType');
- var globalFilters = this.globalFilters.getFilterList({
- origin: 'filter',
- sourceId: datasource.getId(),
- scope: ContentUtil.getPageContent(this.content, type).getId(),
- eventGroupId: this.eventGroups.getGroupId(this.content.getId())
- });
- var selectionFilters = this.globalFilters.getFilterList({
- origin: 'visualization',
- sourceId: datasource.getId(),
- scope: ContentUtil.getPageContent(this.content, type).getId(),
- eventGroupId: this.eventGroups.getGroupId(this.content.getId()),
- eventSourceId: this.content.getId()
- });
- return { localFilters: localFilters, globalFilters: globalFilters, selectionFilters: selectionFilters };
- };
- DataBehindTheVis.prototype._getProcessedHeadersAndCells = function _getProcessedHeadersAndCells(queryResult) {
- var _this5 = this;
- var dataHeaders = [];
- var dataCells = [];
- var usedMetadataColumns = this._getMetadataColumnList();
- if (queryResult) {
- (function () {
- /** when the widget is in not rendered state (eg. missing required slots), removing a slot does not do another query
- * the cached data will have one or more data row(s) extra
- * Hence we filter down to the only used meta data columns */
- var usedIds = _this5._getUsedMetadataColumnsIncluldingFiltersIds(usedMetadataColumns);
- var resultItemList = queryResult.getResultItemList();
- var unusedColumnIndices = [];
- _.each(resultItemList, function (item) {
- _.each(item.getDataItemList(), function (dataItem, index) {
- if (usedIds.indexOf(dataItem.getColumnId()) !== -1) {
- dataHeaders.push(dataItem.getLabel());
- } else {
- unusedColumnIndices.push(index);
- }
- });
- }.bind(_this5));
- var firstRowLevels = [];
- var columnCount = _this5._getColumnCount(resultItemList);
- for (var iRow = 0; iRow < queryResult.getRowCount(); iRow++) {
- var skipRow = false;
- var dataRow = [];
- var _loop = function _loop(iCol) {
- if (unusedColumnIndices.indexOf(iCol) === -1) {
- var value = queryResult.getValue(iRow, iCol);
- if (Array.isArray(value)) {
- dataRow.push.apply(dataRow, value.map(function (dataCellObj) {
- dataCellObj.toString = _this5._getCellValue.bind(_this5, dataCellObj);
- return dataCellObj;
- }));
- if (iRow === 0) {
- // capture the level from the first row including all nested values
- firstRowLevels[iCol] = _.pluck(value, 'ln');
- } else {
- // skip the row if at least one nested value level is deeper than the first row - expanded row
- skipRow = skipRow || _.some(value, function (v, offset) {
- return firstRowLevels[iCol][offset] < v.ln;
- });
- }
- } else {
- value.toString = _this5._getCellValue.bind(_this5, value);
- dataRow.push(value);
- }
- // skip thw row if the value is a summary type
- skipRow = skipRow || value.type === (queryResult.VALUE_TYPE && queryResult.VALUE_TYPE.SUMMARY);
- }
- };
- for (var iCol = 0; iCol < columnCount; iCol++) {
- _loop(iCol);
- }
- if (!skipRow) {
- dataCells.push(dataRow);
- }
- }
- })();
- }
- return {
- headers: dataHeaders,
- cells: dataCells
- };
- };
- /** get an array of used meta data column, filters ids */
- DataBehindTheVis.prototype._getUsedMetadataColumnsIncluldingFiltersIds = function _getUsedMetadataColumnsIncluldingFiltersIds(usedMetadataColumns) {
- var ids = [];
- _.each(usedMetadataColumns, function (dataColumn) {
- if (dataColumn && !dataColumn.isMissing()) {
- ids.push(dataColumn.getId());
- }
- });
- var extraColumnIdsFromNonProjectedFilters = this._getColumnIdsFromUnprojectedFilters();
- ids.push.apply(ids, extraColumnIdsFromNonProjectedFilters);
- // add extra virtual meta data column resulted by multi measure post processing
- var hasMultiMeasures = this._isMultiMeasureVisualization(this.visualization);
- if (hasMultiMeasures) {
- ids.push.apply(ids, ['_multiMeasuresValue', '_multiMeasuresSeries']);
- }
- return ids;
- };
- DataBehindTheVis.prototype._getMetadataColumnList = function _getMetadataColumnList() {
- var slots = this.visualization.getSlots();
- return _.map(slots.getDataItemList(), function (dataItem) {
- return dataItem.getMetadataColumn();
- });
- };
- DataBehindTheVis.prototype._getColumnCount = function _getColumnCount(queryResultItemList) {
- return _.chain(queryResultItemList).map(function (resultItem) {
- var isMeasure = _.every(resultItem.getDataItemList(), function (dataItem) {
- return dataItem.getAggregation() && dataItem.getType() === 'fact';
- });
- // when multiple measures are mapped to a slot, V5 result item will represent the measures as a single set
- var isMultiMeasure = isMeasure && resultItem.getDataItemList().length > resultItem.getValue(0).length;
- return isMultiMeasure ? resultItem.getDataItemList() : resultItem;
- }).flatten().value().length;
- };
- DataBehindTheVis.prototype._getCellValue = function _getCellValue(dataCell) {
- var isMeasure = this._isCellValueMeasure(dataCell);
- var value = isMeasure ? dataCell.value : dataCell.label;
- if (value === null) {
- return DSStringResources.get('nullValueText');
- } else if (isMeasure) {
- // round numbers types to at most 2 digits
- return Math.round(value * 100) / 100;
- } else {
- return value;
- }
- };
- DataBehindTheVis.prototype._isCellValueMeasure = function _isCellValueMeasure(dataCell) {
- return dataCell && _.isUndefined(dataCell.label);
- };
- DataBehindTheVis.prototype._getDataItems = function _getDataItems(dataset) {
- var slots = this.visualization.getSlots();
- if (dataset) {
- var slotMapping = slots.getMappingInfoList();
- var dataItems = [];
- slotMapping.forEach(function (item) {
- if (item.slot.getDefinition().getDatasetIdList().indexOf(dataset) !== -1) {
- dataItems.push(item.dataItem);
- }
- });
- return dataItems;
- } else {
- return slots.getDataItemList();
- }
- };
- DataBehindTheVis.prototype._getSelections = function _getSelections() {
- var selections = [];
- var pageContext = this.pageContextService.getPageContext();
- _.each(pageContext && pageContext.getPageContextItems(this._getItemContext()), function (pageContextItem) {
- if (pageContextItem.getDataPoints() && pageContextItem.getDataPoints().length) {
- selections.push({
- dataPoints: pageContextItem.getDataPoints()
- });
- } else if (pageContextItem.getValues() && pageContextItem.getValues().length) {
- selections.push({
- edges: pageContextItem.getValues()
- });
- }
- });
- return selections;
- };
- DataBehindTheVis.prototype._getItemContext = function _getItemContext() {
- var type = this.dashboard.getAppConfig('pageContainerType');
- if (!this.visualization) {
- return null;
- }
- return {
- origin: 'visualization',
- sourceId: this.visualization.getDataSource() && this.visualization.getDataSource().getId(),
- scope: ContentUtil.getPageContent(this.content, type).getId(),
- eventGroupId: this.eventGroups.getGroupId(this.content.getId()),
- //use the 'strict match option' and event source to only select brushing entries
- //that originate from this visualization.
- eventSourceId: this.content.getId(),
- _strictMatch: true
- };
- };
- return DataBehindTheVis;
- }();
- return DataBehindTheVis;
- });
- //# sourceMappingURL=DataBehindTheVis.js.map
|