UNPKG

apisearch

Version:
251 lines (250 loc) 8.16 kB
"use strict"; exports.__esModule = true; exports.ResultAggregation = void 0; var tslib_1 = require("tslib"); var Filter_1 = require("../Query/Filter"); var Counter_1 = require("./Counter"); /** * ResultAggregation class */ var ResultAggregation = /** @class */ (function () { /** * @param name * @param applicationType * @param totalElements * @param activeElements * @param metadata */ function ResultAggregation(name, applicationType, totalElements, activeElements, metadata) { if (metadata === void 0) { metadata = {}; } this.counters = {}; this.highestActiveElement = 0; this.name = name; this.applicationType = applicationType; this.totalElements = totalElements; this.activeElements = {}; this.metadata = metadata; for (var i in activeElements) { var activeElement = activeElements[i]; this.activeElements[activeElement] = activeElement; } } /** * Add counter * * @param name * @param counter */ ResultAggregation.prototype.addCounter = function (name, counter) { if (counter == 0) { return; } var counterInstance = Counter_1.Counter.createByActiveElements(name, counter, Object.keys(this.activeElements)); if (!(counterInstance instanceof Counter_1.Counter)) { return; } if ((this.applicationType & Filter_1.FILTER_MUST_ALL_WITH_LEVELS) && (this.applicationType & ~Filter_1.FILTER_MUST_ALL) && counterInstance.isUsed()) { this.activeElements[counterInstance.getId()] = counterInstance; this.highestActiveElement = Math.max(counterInstance.getLevel(), this.highestActiveElement); return; } this.counters[counterInstance.getId()] = counterInstance; }; /** * Get name * * @return {string} */ ResultAggregation.prototype.getName = function () { return this.name; }; /** * Get counter * * @return {any} */ ResultAggregation.prototype.getCounters = function () { return this.counters; }; /** * */ ResultAggregation.prototype.getMetadata = function () { return this.metadata; }; /** * Return if the aggregation belongs to a filter. * * @return {boolean} */ ResultAggregation.prototype.isFilter = function () { return (this.applicationType & Filter_1.FILTER_MUST_ALL) > 0; }; /** * Aggregation has levels. * * @return {boolean} */ ResultAggregation.prototype.hasLevels = function () { return (this.applicationType & Filter_1.FILTER_MUST_ALL_WITH_LEVELS) > 0; }; /** * Get counter by name * * @param name * * @return {null} */ ResultAggregation.prototype.getCounter = function (name) { return this.counters[name] instanceof Counter_1.Counter ? this.counters[name] : null; }; /** * Get all elements * * @return {{}} */ ResultAggregation.prototype.getAllElements = function () { return tslib_1.__assign(tslib_1.__assign({}, this.activeElements), this.counters); }; /** * Get total elements * * @return {number} */ ResultAggregation.prototype.getTotalElements = function () { return this.totalElements; }; /** * Get active elements * * @return {any} */ ResultAggregation.prototype.getActiveElements = function () { if (Object.keys(this.activeElements).length === 0) { return {}; } if (this.applicationType === Filter_1.FILTER_MUST_ALL_WITH_LEVELS) { var value = null; for (var i in this.activeElements) { var activeElement = this.activeElements[i]; if (!(activeElement instanceof Counter_1.Counter)) { continue; } if (value == null) { value = activeElement; } value = value.getLevel() > activeElement.getLevel() ? value : activeElement; } return value instanceof Counter_1.Counter ? { 0: value } : null; } return this.activeElements; }; /** * Clean results by level and remove all levels higher than the lowest. */ ResultAggregation.prototype.cleanCountersByLevel = function () { for (var i in this.counters) { var counter = this.counters[i]; if (counter.getLevel() !== this.highestActiveElement + 1) { delete this.counters[i]; } } }; /** * Is empty * * @returns {boolean} */ ResultAggregation.prototype.isEmpty = function () { return Object.keys(this.activeElements).length == 0 && Object.keys(this.counters).length == 0; }; /** * To array * * @return {any} */ ResultAggregation.prototype.toArray = function () { var array = { name: this.name, counters: [], active_elements: [], metadata: this.metadata }; for (var i in this.counters) { array.counters.push(this.counters[i].toArray()); } if (this.applicationType !== Filter_1.FILTER_AT_LEAST_ONE) { array.application_type = this.applicationType; } if (this.totalElements > 0) { array.total_elements = this.totalElements; } for (var i in this.activeElements) { var activeElement = this.activeElements[i]; array.active_elements.push(activeElement instanceof Counter_1.Counter ? activeElement.toArray() : activeElement); } if (this.highestActiveElement > 0) { array.highest_active_level = this.highestActiveElement; } if (array.counters.length === 0) { delete array.counters; } if (array.active_elements.length === 0) { delete array.active_elements; } if (Object.keys(array.metadata).length === 0) { delete array.metadata; } return array; }; /** * Create from array * * @param array */ ResultAggregation.createFromArray = function (array) { var activeElements = []; var activeElementsAsArray = array.active_elements; activeElementsAsArray = typeof activeElementsAsArray === typeof [] ? activeElementsAsArray : []; for (var i in activeElementsAsArray) { var activeElementAsArray = activeElementsAsArray[i]; activeElements.push(typeof activeElementAsArray === typeof {} ? Counter_1.Counter.createFromArray(activeElementAsArray) : activeElementAsArray); } var aggregation = new ResultAggregation(array.name, parseInt(array.application_type ? array.application_type : Filter_1.FILTER_AT_LEAST_ONE), parseInt(array.total_elements ? array.total_elements : 0), []); aggregation.activeElements = activeElements; var countersAsArray = typeof array.counters === typeof [] ? array.counters : []; for (var i in countersAsArray) { var counterAsArray = countersAsArray[i]; if (typeof counterAsArray !== "function") { var counter = Counter_1.Counter.createFromArray(counterAsArray); aggregation.counters['_' + counter.getId()] = counter; } } aggregation.highestActiveElement = typeof array.highest_active_level === "number" ? array.highest_active_level : 0; aggregation.metadata = typeof array.metadata === typeof {} ? array.metadata : {}; return aggregation; }; return ResultAggregation; }()); exports.ResultAggregation = ResultAggregation;