@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
172 lines • 10.7 kB
JavaScript
"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.TimeseriesSeriesConverterService = void 0;
const tslib_1 = require("tslib");
const core_1 = require("@angular/core");
const get_1 = tslib_1.__importDefault(require("lodash/get"));
const isEmpty_1 = tslib_1.__importDefault(require("lodash/isEmpty"));
const isEqual_1 = tslib_1.__importDefault(require("lodash/isEqual"));
const isUndefined_1 = tslib_1.__importDefault(require("lodash/isUndefined"));
// eslint-disable-next-line import/no-deprecated
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");
const types_2 = require("../../../types");
const preview_service_1 = require("../../preview.service");
const base_converter_1 = require("../base-converter");
let TimeseriesSeriesConverterService = class TimeseriesSeriesConverterService extends base_converter_1.BaseConverter {
constructor(eventBus, previewService, pizzagnaService) {
super(eventBus, previewService, pizzagnaService);
this.indicatorDataKey = "indicatorData";
this.previewSeriesPath = `providers.${types_1.WellKnownProviders.Adapter}.properties.series`;
this.formSeriesComponentPath = `${types_1.PizzagnaLayer.Data}.series.properties`;
this.dataSourceSeries$ = new rxjs_1.Subject();
this.selectedSeriesIds$ = new rxjs_1.Subject();
this.shouldReadForm = false;
this.subscribeToAvailableDataFieldsChange();
this.eventBus.subscribeUntil(types_2.DATA_SOURCE_CHANGE, this.destroy$, (v) => {
const nodesPath = `${types_1.PizzagnaLayer.Structure}.series.properties.nodes`;
const nodes = (0, get_1.default)(this.pizzagnaService.pizzagna, nodesPath);
this.pizzagnaService.removeComponents(nodes);
let updatedPizzagna = this.pizzagnaService.pizzagna;
updatedPizzagna = (0, bits_1.immutableSet)(updatedPizzagna, nodesPath, []);
updatedPizzagna = (0, bits_1.immutableSet)(updatedPizzagna, `${this.formSeriesComponentPath}.series`, []);
this.updateFormPizzagna(updatedPizzagna);
});
this.eventBus.subscribeUntil(types_2.DATA_SOURCE_OUTPUT, this.destroy$, (event) => {
// Because typing is lenient for the data source output, the event may or may not contain an IDataSourceOutput
// with a result property; the payload may actually be the result itself, so both possibilities are accommodated.
const { series } = (0, isUndefined_1.default)(event.payload.result)
? event.payload
: event.payload.result || {};
if (series) {
this.dataSourceSeries$.next(series);
let updatedPizzagna = this.pizzagnaService.pizzagna;
updatedPizzagna = (0, bits_1.immutableSet)(updatedPizzagna, `${this.formSeriesComponentPath}.allSeries`, series);
const seriesTemplate = this.pizzagnaService.pizzagna[types_1.PizzagnaLayer.Structure][this.component.componentId].properties?.template;
const indicatorDataIndex = seriesTemplate.findIndex((val) => val.id === this.indicatorDataKey);
const indicatorDataPath = `${types_1.PizzagnaLayer.Structure}.series.properties.template[${indicatorDataIndex}].properties.allSeries`;
updatedPizzagna = (0, bits_1.immutableSet)(updatedPizzagna, indicatorDataPath, series);
const previewSeries = (0, get_1.default)(this.getPreview(), `chart.${this.previewSeriesPath}`, []);
updatedPizzagna = previewSeries.reduce((res, s) => (0, bits_1.immutableSet)(res, `${types_1.PizzagnaLayer.Data}.${s.id}/${this.indicatorDataKey}.properties.allSeries`, series), updatedPizzagna);
this.updateFormPizzagna(updatedPizzagna);
// updating preview series if no series set in editor. this occurs when changing DS, for example.
// in DS is changing all the series are removed and then all series from DS should be set.
// that's why updating preview series.
const editorSeriesPath = `${this.formSeriesComponentPath}.series`;
const editorSeries = (0, get_1.default)(this.pizzagnaService.pizzagna, editorSeriesPath);
if ((0, isEmpty_1.default)(editorSeries)) {
this.shouldReadForm = false;
const seriesForPreview = series.map((s) => ({
id: s.id,
selectedSeriesId: s.id,
label: s.name,
}));
const updatedPreview = (0, bits_1.immutableSet)(this.getPreview(), `chart.${this.previewSeriesPath}`, seriesForPreview);
this.updatePreview(updatedPreview);
this.buildForm();
setTimeout(() => {
this.updateAvailableSeries([]);
this.shouldReadForm = true;
});
}
}
});
}
buildForm() {
const preview = this.getPreview();
const series = (0, get_1.default)(preview, `chart.${this.previewSeriesPath}`, []);
const seriesToSet = series.map((s) => ({
id: s.id,
selectedSeriesId: s.selectedSeriesId,
}));
const seriesIds = series.map((s) => s.id);
this.pizzagnaService.createComponentsFromTemplate("series", seriesIds);
const editorPizzagna = this.pizzagnaService.pizzagna;
const pizzagnaWithSeries = (0, bits_1.immutableSet)(editorPizzagna, `${this.formSeriesComponentPath}.series`, seriesToSet);
this.updateFormPizzagna(pizzagnaWithSeries);
this.selectedSeriesIds$.next(seriesToSet.map((s) => s.selectedSeriesId));
setTimeout(() => (this.shouldReadForm = true));
}
toPreview(form) {
form.valueChanges
.pipe((0, operators_1.takeUntil)(this.destroy$))
.subscribe((formSeries) => {
if (!this.shouldReadForm) {
return;
}
const preview = this.getPreview();
const { chart } = preview;
const seriesFromPreview = (0, get_1.default)(chart, this.previewSeriesPath, []);
const series = formSeries.map((s) => ({
id: s.id,
selectedSeriesId: s.selectedSeriesId,
...seriesFromPreview.find((previewSeries) => previewSeries.id === s.id),
}));
const updatedPreview = (0, bits_1.immutableSet)(preview, `chart.${this.previewSeriesPath}`, series);
this.updatePreview(updatedPreview);
const selectedSeriesIds = formSeries
.map((s) => (0, get_1.default)(s, `properties[${s.id}/indicatorData].id`))
.filter((v) => !(0, isEmpty_1.default)(v));
this.selectedSeriesIds$.next(selectedSeriesIds);
});
}
ngOnDestroy() {
this.dataSourceSeries$.complete();
this.selectedSeriesIds$.complete();
super.ngOnDestroy();
}
updateAvailableSeries(availableSeries) {
if (!this.pizzagnaService.pizzagna) {
return;
}
let updatedPizzagna = this.pizzagnaService.pizzagna;
// configuration component
updatedPizzagna = (0, bits_1.immutableSet)(updatedPizzagna, `${this.formSeriesComponentPath}.availableSeries`, availableSeries);
// template
const seriesTemplate = this.pizzagnaService.pizzagna[types_1.PizzagnaLayer.Structure][this.component.componentId].properties?.template;
const indicatorDataIndex = seriesTemplate.findIndex((val) => val.id === this.indicatorDataKey);
const indicatorDataPath = `${types_1.PizzagnaLayer.Structure}.series.properties.template[${indicatorDataIndex}].properties.availableSeries`;
updatedPizzagna = (0, bits_1.immutableSet)(updatedPizzagna, indicatorDataPath, availableSeries);
// already created forms
const seriesIds = (0, get_1.default)(this.pizzagnaService.pizzagna, `${types_1.PizzagnaLayer.Structure}.series.properties.nodes`);
updatedPizzagna = seriesIds.reduce((res, id) => (0, bits_1.immutableSet)(res, `${types_1.PizzagnaLayer.Data}.${id}/${this.indicatorDataKey}.properties.availableSeries`, availableSeries), updatedPizzagna);
this.updateFormPizzagna(updatedPizzagna);
}
subscribeToAvailableDataFieldsChange() {
// eslint-disable-next-line import/no-deprecated
(0, rxjs_1.combineLatest)([this.dataSourceSeries$, this.selectedSeriesIds$])
.pipe((0, operators_1.takeUntil)(this.destroy$), (0, operators_1.map)(([dsSeries, usedSeriesIds]) => dsSeries.filter((ds) => !usedSeriesIds.find((id) => id === ds.id))), (0, operators_1.distinctUntilChanged)(isEqual_1.default))
.subscribe(this.updateAvailableSeries.bind(this));
}
};
exports.TimeseriesSeriesConverterService = TimeseriesSeriesConverterService;
exports.TimeseriesSeriesConverterService = TimeseriesSeriesConverterService = 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,
preview_service_1.PreviewService,
pizzagna_service_1.PizzagnaService])
], TimeseriesSeriesConverterService);
//# sourceMappingURL=timeseries-series-converter.service.js.map