1 import {Injectable} from "@angular/core";
2 import {ILevelNodeInfo} from "../models/basic.model.info";
3 import {ObjectToTreeService} from "../objectToTree.service";
4 import {DefaultDataGeneratorService} from "../../../../shared/services/defaultDataServiceGenerator/default.data.generator.service";
5 import * as _ from "lodash";
6 import {ServiceInstanceActions} from "../../../../shared/models/serviceInstanceActions";
7 import {ErrorMsgService} from "../../../../shared/components/error-msg/error-msg.service";
8 import {FeatureFlagsService, Features} from "../../../../shared/services/featureFlag/feature-flags.service";
9 import {NgRedux} from "@angular-redux/store";
10 import {AppState} from "../../../../shared/store/reducers";
13 export class ObjectToInstanceTreeService {
14 constructor(private _objectToTreeService: ObjectToTreeService, private _errorMsgService: ErrorMsgService, private store: NgRedux<AppState>) {
15 this.numberOfFailed = 0;
16 this.numberOfElements = 0;
20 /** store number of failed ******** ONLY IN RETRY MODE ******** **/
21 numberOfFailed: number = 0;
23 /** store number of existing elements **/
24 numberOfElements: number = 0;
26 /*****************************************************************
27 * return array of first level node with there child's
28 * @param serviceInstance - The service instance object from store
29 * @param serviceHierarchy - The service Hierarchy store
30 ****************************************************************/
31 convertServiceInstanceToTreeData(serviceInstance, serviceHierarchy): any[] {
32 this._errorMsgService.triggerClearError.next();
34 this.numberOfFailed = 0;
35 this.numberOfElements = 0;
37 const firstLevelOptions: ILevelNodeInfo[] = _this._objectToTreeService.getFirstLevelOptions();
38 for (let option of firstLevelOptions) {
39 _.forOwn(serviceInstance[option.name], function (instance, modelName) {
40 nodes.push(_this.getNodeInstance(modelName, null, instance, serviceHierarchy, option));
43 return this.sortElementsByPosition(nodes);
46 /*****************************************************************
47 * should increase number of failed
48 * @param node - the current node
49 ****************************************************************/
50 increaseNumberOfFailed(node) {
51 if (node && node.isFailed) {
52 this.numberOfFailed++;
53 node['errors'] = !_.isNil(node['errors']) ? node['errors'] : {};
54 node['errors']["isFailed"] = true;
55 this._errorMsgService.triggerShowError.next(this._errorMsgService.getRetryErrorObject(this.numberOfFailed));
59 /*****************************************************************
60 * should increase number of existing elements
61 * @param node - the current node
62 ****************************************************************/
63 increaseNumberOfExcitingElements() {
64 this.numberOfElements++;
67 /*****************************************************************
68 * return array of first level node with there child's
72 * @param serviceHierarchy - The service Hierarchy store
75 ****************************************************************/
76 getNodeInstance(modelName: string, parentModel: any, instance: any, serviceHierarchy, option: ILevelNodeInfo, parentType ?: string) {
77 const model = option.getModel(modelName, instance, serviceHierarchy);
79 let optionalNodes = option.createInstanceTreeNode(instance, model, parentModel, modelName);
80 this.increaseNumberOfFailed(optionalNodes);
81 this.increaseNumberOfExcitingElements();
82 let nodes: any[] = _.isArray(optionalNodes) ? optionalNodes : [optionalNodes];
83 for (let node of nodes) {
84 node = this.addingExtraDataToNode(node, modelName, parentModel, instance, serviceHierarchy, option, parentType);
85 let children = this.addNextInstanceTreeNode(instance, model, option, node, serviceHierarchy);
86 if (!_.isNil(children) && children.length > 0) {
87 node.children = this.sortElementsByPosition(children);
89 this.updateScalingPolicy(node);
91 return nodes.length === 1 ? nodes[0] : nodes;
94 addingExtraDataToNode(node, modelName: string, parentModel: any, instance: any, serviceHierarchy, option: ILevelNodeInfo, parentType ?: string) {
96 node.trackById = _.isNil(node.trackById) ? DefaultDataGeneratorService.createRandomTrackById() : node['trackById'];
97 node.parentType = !_.isNil(parentType) ? parentType : "";
98 node.updatePoistionFunction = option.updatePosition;
99 node.position = option.getNodePosition(instance, node.dynamicModelName);
100 node.getModel = option.getModel.bind(option);
101 node.getInfo = !_.isNil(option.getInfo) ? option.getInfo.bind(option) : ()=>{};
102 node.componentInfoType = option.componentInfoType;
108 sortElementsByPosition(nodes: any[]): any[] {
109 if (!FeatureFlagsService.getFlagState(Features.DRAG_AND_DROP_OPERATION, this.store)) return nodes;
110 return nodes.sort((nodeA, nodeB) => {
111 return nodeA.position - nodeB.position;
115 /*****************************************************************
116 * return next level node with there child's
117 * @param parentInstance
119 * @param levelNodeInfo
121 * @param serviceHierarchy - The service Hierarchy store
122 ****************************************************************/
123 addNextInstanceTreeNode(parentInstance, parentModel, levelNodeInfo: ILevelNodeInfo, parentNode, serviceHierarchy): any[] {
124 if (!_.isNil(levelNodeInfo.childNames)&& levelNodeInfo.childNames.length > 0) {
126 parentNode.children = [];
127 levelNodeInfo.childNames.forEach(function (childName) {
128 if (!_.isNil(parentInstance[childName])) {
129 let parentType = levelNodeInfo.type;
130 let nextLevelNodeInfo = levelNodeInfo.getNextLevelObject.apply(that, [childName]);
131 Object.keys(parentInstance[childName]).map((modelName) => {
132 let nextLevelInstance = parentInstance[childName][modelName];
133 let nodes: any[] | any = that.getNodeInstance(modelName, parentModel, nextLevelInstance, serviceHierarchy, nextLevelNodeInfo, parentType);
134 if (_.isArray(nodes)) {
135 parentNode.children = parentNode.children.concat(nodes);
137 parentNode.children.push(nodes);
142 return this.sortElementsByPosition(parentNode.children);
144 return !_.isNil(parentNode) ? parentNode.children : null;
148 /************************************************************************************
149 * update instance scaling policy according to instance limit and existing children
151 *********************************************************************************/
152 updateScalingPolicy(node): void {
153 if(_.isNil(node)) return node;
154 node['errors'] = !_.isNil(node['errors']) ? node['errors'] : {};
155 if (!_.isNil(node['limitMembers']) && !_.isNil(node.children)) {
156 let effectiveChildren = (node.children).filter(child => [
157 ServiceInstanceActions.Create,
158 ServiceInstanceActions.None,
159 ServiceInstanceActions.Update
160 ].includes(child.action));
163 if (effectiveChildren.length > node.limitMembers) {
164 node['errors']["scalingError"] = true;
165 this._errorMsgService.triggerShowError.next(this._errorMsgService.getScalingErrorObject());
167 delete node['errors']["scalingError"];