UNPKG

@nova-ui/dashboards

Version:

Nova Dashboards is a framework designed to provide feature developers with a common solution for presenting data coming from various sources within a single view, as well as a set of predefined widget visualizations that are 100% configuration-driven and

160 lines 6.66 kB
"use strict"; // © 2022 SolarWinds Worldwide, LLC. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to // deal in the Software without restriction, including without limitation the // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or // sell copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. Object.defineProperty(exports, "__esModule", { value: true }); exports.KpiScaleSyncBroker = void 0; const tslib_1 = require("tslib"); const core_1 = require("@angular/core"); const isArray_1 = tslib_1.__importDefault(require("lodash/isArray")); const rxjs_1 = require("rxjs"); const operators_1 = require("rxjs/operators"); const bits_1 = require("@nova-ui/bits"); const pizzagna_service_1 = require("../../pizzagna/services/pizzagna.service"); const types_1 = require("../../types"); let KpiScaleSyncBroker = class KpiScaleSyncBroker { constructor(eventBus, pizzagnaService) { this.eventBus = eventBus; this.pizzagnaService = pizzagnaService; this.brokers = []; this.builder = new KpiScaleSyncBrokerBuilder(this.brokers); this.destroySubscriptions$ = new rxjs_1.Subject(); this.getMin = (n) => Math.min(...n); this.getMax = (n) => Math.max(...n); this.valuesObject = this.builder.valuesObject; } updateConfiguration(properties) { this.properties = properties; if (this.componentId) { this.createPropertiesAndSubscribeToBrokers(); } } configure() { return this.builder; } getBrokers() { return [...this.brokers]; } subscribeToBrokers() { this.destroy(); this.brokers.forEach((broker) => { broker.in$ .pipe((0, operators_1.filter)((_) => !!broker.in$.observers.length), (0, operators_1.distinctUntilChanged)(), (0, operators_1.tap)((data) => this.configureValueModel(data)), (0, operators_1.tap)((data) => this.processAndEmitSyncedValue(data, broker)), (0, operators_1.takeUntil)(this.destroySubscriptions$)) .subscribe(); }); } setComponent(component, componentId) { this.componentId = componentId; this.updateConfiguration(this.properties); } destroy() { this.destroySubscriptions$.next(); this.destroySubscriptions$.complete(); } processAndEmitSyncedValue(data, broker) { let fn = this.getMin; if (broker?.type) { fn = broker.type === "min" ? this.getMin : this.getMax; } const valuesToCompare = this.valuesObject[broker.id] .filter((item) => item.targetID && item.targetValue) .map((item) => item.targetValue); broker.out$.next({ ...data, targetValue: fn([...valuesToCompare]), }); } configureValueModel(data) { const targetObj = this.valuesObject[data.id].filter((obj) => obj.targetID === data.targetID); targetObj.length ? (targetObj[0].targetValue = data.targetValue) : this.valuesObject[data.id].push({ targetID: data.targetID, targetValue: data.targetValue, }); } createPropertiesAndSubscribeToBrokers() { this.tileNodes = this.pizzagnaService.getComponent(this.componentId).properties?.nodes; const { scaleSyncConfig } = this.properties; if (!this.tileNodes?.length) { return; } if (scaleSyncConfig && (0, isArray_1.default)(scaleSyncConfig)) { // remove brokers that are not in the scaleSyncConfig this.brokers .filter((broker) => !scaleSyncConfig.find((s) => s.id === broker.id)) .forEach(({ id }) => this.builder.removeBroker(id)); // add new brokers from scaleSyncConfig scaleSyncConfig.forEach((s) => this.configure().addBroker({ id: s.id, type: s?.type })); this.subscribeToBrokers(); } this.tileNodes.forEach((node, i) => { const property = { componentId: node, pizzagnaKey: types_1.PizzagnaLayer.Data, propertyPath: [`syncValuesBroker`], }; this.pizzagnaService.setProperty(property, this.getBrokers()); }); } }; exports.KpiScaleSyncBroker = KpiScaleSyncBroker; exports.KpiScaleSyncBroker = KpiScaleSyncBroker = tslib_1.__decorate([ (0, core_1.Injectable)(), tslib_1.__param(0, (0, core_1.Inject)(types_1.PIZZAGNA_EVENT_BUS)), tslib_1.__metadata("design:paramtypes", [bits_1.EventBus, pizzagna_service_1.PizzagnaService]) ], KpiScaleSyncBroker); class KpiScaleSyncBrokerBuilder { constructor(brokers) { this.brokers = brokers; this.valuesObject = {}; } addBroker(broker) { if (this.brokers.find((existingBroker) => existingBroker.id === broker.id)) { // Prevent adding the same broker multiple times return this; } const newBrokerSetting = { id: broker.id, targetID: "", targetValue: 1, }; this.valuesObject[broker.id] = new Array(); this.brokers.push({ ...broker, ...this.getDefaultBrokerObject(newBrokerSetting, newBrokerSetting), }); return this; } removeBroker(brokerId) { const brokerIndex = this.brokers.findIndex((b) => b.id === brokerId); if (brokerIndex !== -1) { this.brokers.splice(brokerIndex, 1); delete this.valuesObject[brokerId]; } } getDefaultBrokerObject(input, output) { return { in$: new rxjs_1.BehaviorSubject(input), out$: new rxjs_1.BehaviorSubject(output), }; } } //# sourceMappingURL=kpi-scale-sync-broker.js.map