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 serviceModelId:string = serviceInstance.modelInfo.modelVersionId;
38 const firstLevelOptions: ILevelNodeInfo[] = _this._objectToTreeService.getFirstLevelOptions();
39 for (let option of firstLevelOptions) {
40 _.forOwn(serviceInstance[option.name], function (instance, modelName) {
41 nodes.push(_this.getNodeInstance(modelName, null, instance, serviceHierarchy, option, serviceModelId));
44 return this.sortElementsByPosition(nodes);
47 /*****************************************************************
48 * should increase number of failed
49 * @param node - the current node
50 ****************************************************************/
51 increaseNumberOfFailed(node) {
52 if (node && node.isFailed) {
53 this.numberOfFailed++;
54 node['errors'] = !_.isNil(node['errors']) ? node['errors'] : {};
55 node['errors']["isFailed"] = true;
56 this._errorMsgService.triggerShowError.next(this._errorMsgService.getRetryErrorObject(this.numberOfFailed));
60 /*****************************************************************
61 * should increase number of existing elements
62 * @param node - the current node
63 ****************************************************************/
64 increaseNumberOfExcitingElements() {
65 this.numberOfElements++;
68 /*****************************************************************
69 * return array of first level node with there child's
73 * @param serviceHierarchy - The service Hierarchy store
75 * @param serviceModelId
77 ****************************************************************/
78 getNodeInstance(modelName: string, parentModel: any, instance: any, serviceHierarchy, option: ILevelNodeInfo, serviceModelId: string, parentType ?: string) {
79 const model = option.getModel(modelName, instance, serviceHierarchy);
81 let optionalNodes = option.createInstanceTreeNode(instance, model, parentModel, modelName, serviceModelId);
82 this.increaseNumberOfFailed(optionalNodes);
83 this.increaseNumberOfExcitingElements();
84 let nodes: any[] = _.isArray(optionalNodes) ? optionalNodes : [optionalNodes];
85 for (let node of nodes) {
86 node = this.addingExtraDataToNode(node, modelName, parentModel, instance, serviceHierarchy, option, parentType);
87 let children = this.addNextInstanceTreeNode(instance, model, option, node, serviceHierarchy, serviceModelId);
88 if (!_.isNil(children) && children.length > 0) {
89 node.children = this.sortElementsByPosition(children);
91 this.updateScalingPolicy(node);
93 return nodes.length === 1 ? nodes[0] : nodes;
96 addingExtraDataToNode(node, modelName: string, parentModel: any, instance: any, serviceHierarchy, option: ILevelNodeInfo, parentType ?: string) {
98 node.trackById = _.isNil(node.trackById) ? DefaultDataGeneratorService.createRandomTrackById() : node['trackById'];
99 node.parentType = !_.isNil(parentType) ? parentType : "";
100 node.updatePoistionFunction = option.updatePosition;
101 node.position = option.getNodePosition(instance, node.dynamicModelName);
102 node.getModel = option.getModel.bind(option);
103 node.getInfo = !_.isNil(option.getInfo) ? option.getInfo.bind(option) : ()=>{};
104 node.componentInfoType = option.componentInfoType;
110 sortElementsByPosition(nodes: any[]): any[] {
111 if (!FeatureFlagsService.getFlagState(Features.FLAG_1911_INSTANTIATION_ORDER_IN_ASYNC_ALACARTE, this.store)) return nodes;
112 return nodes.sort((nodeA, nodeB) => {
113 return nodeA.position - nodeB.position;
117 /*****************************************************************
118 * return next level node with there child's
119 * @param parentInstance
121 * @param levelNodeInfo
123 * @param serviceHierarchy - The service Hierarchy store
124 * @param serviceModelId
125 ****************************************************************/
126 addNextInstanceTreeNode(parentInstance, parentModel, levelNodeInfo: ILevelNodeInfo, parentNode, serviceHierarchy, serviceModelId: string): any[] {
127 if (!_.isNil(levelNodeInfo.childNames)&& levelNodeInfo.childNames.length > 0) {
129 parentNode.children = [];
130 levelNodeInfo.childNames.forEach(function (childName) {
131 if (!_.isNil(parentInstance[childName])) {
132 let parentType = levelNodeInfo.type;
133 let nextLevelNodeInfo = levelNodeInfo.getNextLevelObject.apply(that, [childName]);
134 Object.keys(parentInstance[childName]).map((modelName) => {
135 let nextLevelInstance = parentInstance[childName][modelName];
136 let nodes: any[] | any = that.getNodeInstance(modelName, parentModel, nextLevelInstance, serviceHierarchy, nextLevelNodeInfo, serviceModelId, parentType);
137 if (_.isArray(nodes)) {
138 parentNode.children = parentNode.children.concat(nodes);
140 parentNode.children.push(nodes);
145 return this.sortElementsByPosition(parentNode.children);
147 return !_.isNil(parentNode) ? parentNode.children : null;
151 /************************************************************************************
152 * update instance scaling policy according to instance limit and existing children
154 *********************************************************************************/
155 updateScalingPolicy(node): void {
156 if(_.isNil(node)) return node;
157 node['errors'] = !_.isNil(node['errors']) ? node['errors'] : {};
158 if (!_.isNil(node['limitMembers']) && !_.isNil(node.children)) {
159 let effectiveChildren = (node.children).filter(child => [
160 ServiceInstanceActions.Create,
161 ServiceInstanceActions.None,
162 ServiceInstanceActions.Update
163 ].includes(child.action));
166 if (effectiveChildren.length > node.limitMembers) {
167 node['errors']["scalingError"] = true;
168 this._errorMsgService.triggerShowError.next(this._errorMsgService.getScalingErrorObject());
170 delete node['errors']["scalingError"];