a8a4c074a63c58722331c52a17d08280f86452e6
[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 @Component({
24   selector: 'alarmRule',
25   templateUrl: './alarmRule.component.html',
26   styleUrls: ['./alarmRule.component.css']
27 })
28 export class AlarmRule implements OnInit {
29   ruleModel: RuleModel;
30   rules: any[];
31   queryRule: RuleModel;
32   activeText: string;
33   ruleName: string;
34   enable_on = "enabled";
35   enable_off = "disabled"
36   totalcount: number = 0;
37   model: any;
38   ruleRequest: RuleRequest;
39   solution = 'ANGULAR';
40   selection = 'A';
41   activeStatus = ["option_all", "common_enabled", "common_disabled"];
42
43   constructor(public _alarmRuleService: AlarmRuleService, private modalService: ModalService,
44               private router: Router) {
45   }
46
47   switch(select: string): void {
48     console.log(select);
49     if (select == "common_enabled") {
50       this.ruleModel.enabled = 1;
51     } else if (select == "common_disabled") {
52       this.ruleModel.enabled = 0;
53     } else {
54
55       this.ruleModel.enabled = null;
56     }
57     this.setActiveText();
58   }
59
60   setActiveText(): void {
61     if (this.ruleModel.enabled == 1) {
62       this.activeText = "common_enabled";
63       this.ruleRequest.enabled = 1;
64     }
65     else if (this.ruleModel.enabled == 0) {
66       this.activeText = "common_disabled";
67       this.ruleRequest.enabled = 0;
68     } else {
69       this.activeText = "option_all";
70       this.ruleRequest.enabled = null;
71     }
72   }
73
74   getRules(): void {
75     this._alarmRuleService.getRules().then(rules => {
76       this.rules = rules.correlationRules;
77       this.rules.map(x => x['showModal'] = false);
78       console.log(this.rules);
79       this.totalcount = rules.totalCount;
80     });
81   }
82
83   public searchRules(): void {
84     if (this.ruleModel.enabled == null) {
85       this.ruleRequest.enabled = null;
86     }
87     this.ruleRequest.ruleName = this.ruleModel.ruleName;
88     console.log(this.ruleRequest.enabled, this.ruleRequest.ruleName);
89
90     this._alarmRuleService
91       .searchrules(this.ruleRequest)
92       .then(rules => {
93         this.rules = rules;
94         this.totalcount = rules.length;
95       });
96   }
97
98   public updateRule(rule: RuleModel): void {
99     this.router.navigate(['ruleInfo/', rule.ruleId]);
100   }
101
102   public delete(rule: any): void {
103     rule.showModal = true;
104   }
105
106   public on_off(rule: RuleModel) {
107     rule.enabled == 0 ? rule.enabled = 1 : rule.enabled = 0;
108     this._alarmRuleService
109       .updateRule(rule)
110       .then(res => {
111         rule = res;
112       });
113   }
114
115   public reset(): void {
116     this.ruleModel.ruleName = null;
117     this.activeText = 'option_all';
118     this.ruleModel.enabled = null;
119     this.getRules();
120   }
121
122   deleteRule(ruleId: string): void {
123     this._alarmRuleService.delete(ruleId).then(() => {
124       this.cancelModal(ruleId);
125       this.getRules();
126     }).catch(() => {
127       this.cancelModal(ruleId);
128     })
129   }
130
131   cancelModal(ruleId: string): void {
132     this.rules.find(x => x.ruleId === ruleId).showModal = false;
133   }
134
135   public ngOnInit(): void {
136     this.activeText = 'option_all';
137     this.ruleModel = {
138       ruleId: null,
139       ruleName: null,
140       description: '',
141       content: null,
142       createTime: null,
143       creator: null,
144       updateTime: null,
145       modifier: null,
146       enabled: 0,
147       loopControlName: ''
148     };
149     this.ruleRequest = {
150       ruleId: null,
151       ruleName: null,
152       creator: null,
153       modifier: null,
154       enabled: null,
155       loopControlName: ''
156     };
157     this.getRules();
158   }
159 }