aboutsummaryrefslogtreecommitdiffstats
path: root/vid-webpack-master/src/app/drawingBoard/service-planning/objectsToTree/objectToInstanceTree/objectToInstanceTree.service.ts
blob: 1fc439ea3e800412a171dc569db3c7dc3edfcbbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import {Injectable} from "@angular/core";
import {ILevelNodeInfo} from "../models/basic.model.info";
import {ObjectToTreeService} from "../objectToTree.service";
import {DefaultDataGeneratorService} from "../../../../shared/services/defaultDataServiceGenerator/default.data.generator.service";
import * as _ from "lodash";
import {ServiceInstanceActions} from "../../../../shared/models/serviceInstanceActions";
import {ErrorMsgService} from "../../../../shared/components/error-msg/error-msg.service";
import {FeatureFlagsService, Features} from "../../../../shared/services/featureFlag/feature-flags.service";
import {NgRedux} from "@angular-redux/store";
import {AppState} from "../../../../shared/store/reducers";

@Injectable()
export class ObjectToInstanceTreeService {
  constructor(private _objectToTreeService: ObjectToTreeService, private _errorMsgService: ErrorMsgService, private store: NgRedux<AppState>) {
    this.numberOfFailed = 0;
    this.numberOfElements = 0;

  }

  /** store number of failed ********  ONLY IN RETRY MODE  ******** **/
  numberOfFailed: number = 0;

  /** store number of existing elements **/
  numberOfElements: number = 0;

  /*****************************************************************
   * return array of first level node with there child's
   * @param serviceInstance - The service instance object from store
   * @param serviceHierarchy - The service Hierarchy store
   ****************************************************************/
  convertServiceInstanceToTreeData(serviceInstance, serviceHierarchy): any[] {
    this._errorMsgService.triggerClearError.next();
    let nodes = [];
    this.numberOfFailed = 0;
    this.numberOfElements = 0;
    let _this = this;
    const firstLevelOptions: ILevelNodeInfo[] = _this._objectToTreeService.getFirstLevelOptions();
    for (let option of firstLevelOptions) {
      _.forOwn(serviceInstance[option.name], function (instance, modelName) {
        nodes.push(_this.getNodeInstance(modelName, null, instance, serviceHierarchy, option));
      });
    }
    return this.sortElementsByPosition(nodes);
  }

  /*****************************************************************
   * should increase number of failed
   * @param node - the current node
   ****************************************************************/
  increaseNumberOfFailed(node) {
    if (node.isFailed) {
      this.numberOfFailed++;
      node['errors'] = !_.isNil(node['errors']) ? node['errors'] : {};
      node['errors']["isFailed"] = true;
      this._errorMsgService.triggerShowError.next(this._errorMsgService.getRetryErrorObject(this.numberOfFailed));
    }
  }

  /*****************************************************************
   * should increase number of existing elements
   * @param node - the current node
   ****************************************************************/
  increaseNumberOfExcitingElements() {
    this.numberOfElements++;
  }

  /*****************************************************************
   * return array of first level node with there child's
   * @param modelName
   * @param parentModel
   * @param instance
   * @param serviceHierarchy - The service Hierarchy store
   * @param option
   * @param parentType
   ****************************************************************/
  getNodeInstance(modelName: string, parentModel: any, instance: any, serviceHierarchy, option: ILevelNodeInfo, parentType ?: string) {
    const model = option.getModel(modelName, instance, serviceHierarchy);

    let optionalNodes = option.createInstanceTreeNode(instance, model, parentModel, modelName);
    this.increaseNumberOfFailed(optionalNodes);
    this.increaseNumberOfExcitingElements();
    let nodes: any[] = _.isArray(optionalNodes) ? optionalNodes : [optionalNodes];
    for (let node of nodes) {
      node = this.addingExtraDataToNode(node, modelName, parentModel, instance, serviceHierarchy, option, parentType);
      let children = this.addNextInstanceTreeNode(instance, model, option, node, serviceHierarchy);
      if (!_.isNil(children) && children.length > 0) {
        node.children = this.sortElementsByPosition(children);
      }
      this.updateScalingPolicy(node);
    }
    return nodes.length === 1 ? nodes[0] : nodes;
  }

  addingExtraDataToNode(node, modelName: string, parentModel: any, instance: any, serviceHierarchy, option: ILevelNodeInfo, parentType ?: string) {
    node.trackById = _.isNil(node.trackById) ? DefaultDataGeneratorService.createRandomTrackById() : node['trackById'];
    node.parentType = !_.isNil(parentType) ? parentType : "";
    node.updatePoistionFunction = option.updatePosition;
    node.position = option.getNodePosition(instance, node.dynamicModelName);
    node.onSelectedNode = option.onSelectedNode;
    return node;
  }

  sortElementsByPosition(nodes: any[]): any[] {
    if (!FeatureFlagsService.getFlagState(Features.DRAG_AND_DROP_OPERATION, this.store)) return nodes;
    return nodes.sort((nodeA, nodeB) => {
      return nodeA.position - nodeB.position;
    });
  }

  /*****************************************************************
   * return next level node with there child's
   * @param parentInstance
   * @param parentModel
   * @param levelNodeInfo
   * @param parentNode
   * @param serviceHierarchy - The service Hierarchy store
   ****************************************************************/
  addNextInstanceTreeNode(parentInstance, parentModel, levelNodeInfo: ILevelNodeInfo, parentNode, serviceHierarchy): any[] {
    if (!_.isNil(levelNodeInfo.childName)) {
      parentNode.children = [];
      if (!_.isNil(parentInstance[levelNodeInfo.childName])) {
        let parentType = levelNodeInfo.type;
        let nextLevelNodeInfo = levelNodeInfo.getNextLevelObject.apply(this);
        Object.keys(parentInstance[levelNodeInfo.childName]).map((modelName) => {
          let nextLevelInstance = parentInstance[levelNodeInfo.childName][modelName];
          let nodes: any[] | any = this.getNodeInstance(modelName, parentModel, nextLevelInstance, serviceHierarchy, nextLevelNodeInfo, parentType);
          if (_.isArray(nodes)) {
            parentNode.children = parentNode.children.concat(nodes);
          } else {
            parentNode.children.push(nodes);
          }
        });
        return this.sortElementsByPosition(parentNode.children);
      }
    }
    return parentNode.children;
  }


  /************************************************************************************
   * update instance scaling policy according to instance limit and existing children
   * @param node
   *********************************************************************************/
  updateScalingPolicy(node): void {
    node['errors'] = !_.isNil(node['errors']) ? node['errors'] : {};
    if (!_.isNil(node['limitMembers']) && !_.isNil(node.children)) {
      let effectiveChildren = (node.children).filter(child => [
        ServiceInstanceActions.Create,
        ServiceInstanceActions.None,
        ServiceInstanceActions.Update
      ].includes(child.action));


      if (effectiveChildren.length > node.limitMembers) {
        node['errors']["scalingError"] = true;
        this._errorMsgService.triggerShowError.next(this._errorMsgService.getScalingErrorObject());
      } else {
        delete node['errors']["scalingError"];
      }

    }
  }
}