Fixed ui error
[holmes/rule-management.git] / rulemgt-frontend / src / app / correlation-ruleList / alarmRule.component.ts
1 /*
2  Copyright 2018 ZTE Corporation.
3
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7
8      http://www.apache.org/licenses/LICENSE-2.0
9
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 import {Component, OnInit} from '@angular/core';
17 import {ModalService} from '../correlation-modal/modal.service';
18 import {RuleModel} from './alarmRule';
19 import {RuleRequest} from './ruleRequest';
20 import {Router} from '@angular/router';
21 import {AlarmRuleService} from './alarmRule.service';
22
23 declare var jQuery: any;
24
25 @Component({
26   selector: 'alarmRule',
27   templateUrl: './alarmRule.component.html',
28   styleUrls: ['./alarmRule.component.css']
29 })
30 export class AlarmRule implements OnInit {
31   ruleModel: RuleModel;
32   rules: any[];
33   queryRule: RuleModel;
34   activeText: string;
35   ruleName: string;
36   enable_on = "enabled";
37   enable_off = "disabled"
38   totalcount: number = 0;
39   model: any;
40   ruleRequest: RuleRequest;
41   solution = 'ANGULAR';
42   selection = 'A';
43   activeStatus = ["option_all", "common_enabled", "common_disabled"];
44
45   constructor(public _alarmRuleService: AlarmRuleService, private modalService: ModalService,
46               private router: Router) {
47   }
48
49   switch(select: string): void {
50     console.log(select);
51     if (select == "common_enabled") {
52       this.ruleModel.enabled = 1;
53     } else if (select == "common_disabled") {
54       this.ruleModel.enabled = 0;
55     } else {
56
57       this.ruleModel.enabled = null;
58     }
59     this.setActiveText();
60   }
61
62   setActiveText(): void {
63     if (this.ruleModel.enabled == 1) {
64       this.activeText = "common_enabled";
65       this.ruleRequest.enabled = 1;
66     }
67     else if (this.ruleModel.enabled == 0) {
68       this.activeText = "common_disabled";
69       this.ruleRequest.enabled = 0;
70     } else {
71       this.activeText = "option_all";
72       this.ruleRequest.enabled = null;
73     }
74   }
75
76   getRules(): void {
77     this._alarmRuleService.getRules().then(rules => {
78       this.rules = rules.correlationRules;
79       this.rules.map(x => x['showModal'] = false);
80       console.log(this.rules);
81       this.totalcount = rules.totalCount;
82     });
83   }
84
85   public searchRules(): void {
86     if (this.ruleModel.enabled == null) {
87       this.ruleRequest.enabled = null;
88     }
89     this.ruleRequest.ruleName = this.ruleModel.ruleName;
90     console.log(this.ruleRequest.enabled, this.ruleRequest.ruleName);
91
92     this._alarmRuleService
93       .searchrules(this.ruleRequest)
94       .then(rules => {
95         this.rules = rules;
96         this.totalcount = rules.length;
97       });
98   }
99
100   public updateRule(rule: RuleModel): void {
101     this.router.navigate(['ruleInfo/', rule.ruleId]);
102   }
103
104   public delete(rule: any): void {
105     rule.showModal = true;
106   }
107
108   public on_off(rule: RuleModel) {
109     rule.enabled == 0 ? rule.enabled = 1 : rule.enabled = 0;
110     this._alarmRuleService
111       .updateRule(rule)
112       .then(res => {
113         rule = res;
114       });
115   }
116
117   public reset(): void {
118     this.ruleModel.ruleName = null;
119     this.activeText = 'option_all';
120     this.ruleModel.enabled = null;
121     this.getRules();
122   }
123
124   deleteRule(ruleId: string): void {
125     this._alarmRuleService.delete(ruleId).then(() => {
126       this.cancelModal(ruleId);
127       this.getRules();
128     }).catch(() => {
129       this.cancelModal(ruleId);
130     })
131   }
132
133   cancelModal(ruleId: string): void {
134     this.rules.find(x => x.ruleId === ruleId).showModal = false;
135   }
136
137   public ngOnInit(): void {
138     this.activeText = 'option_all';
139     this.ruleModel = {
140       ruleId: null,
141       ruleName: null,
142       description: '',
143       content: null,
144       createTime: null,
145       creator: null,
146       updateTime: null,
147       modifier: null,
148       enabled: 0,
149       loopControlName: ''
150     };
151     this.ruleRequest = {
152       ruleId: null,
153       ruleName: null,
154       creator: null,
155       modifier: null,
156       enabled: null,
157       loopControlName: ''
158     };
159     this.getRules();
160   }
161 }