6e2c99711cb79206e12fe018f746ea5b74673b20
[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, ViewChild, ViewEncapsulation } 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 { Http, Response, Jsonp, Headers, RequestOptions } from '@angular/http';
22 import { AlarmRuleService } from './alarmRule.service';
23 declare var jQuery: any;
24
25 @Component({
26     selector: 'alarmRule',
27     templateUrl: './alarmRule.component.html'
28 })
29
30 export class AlarmRule implements OnInit {
31     ruleModel: RuleModel;
32     rules: RuleModel[];
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     switch(select: string): void {
49         console.log(select);
50         if (select == "common_enabled") {
51             this.ruleModel.enabled = 1;
52         } else if (select == "common_disabled") {
53             this.ruleModel.enabled = 0;
54         } else {
55
56             this.ruleModel.enabled = null;
57         }
58         this.setActiveText();
59     }
60
61     setActiveText(): void {
62         if (this.ruleModel.enabled == 1) {
63             this.activeText = "common_enabled";
64             this.ruleRequest.enabled = 1;
65         }
66         else if (this.ruleModel.enabled == 0) {
67             this.activeText = "common_disabled";
68             this.ruleRequest.enabled = 0;
69         } else {
70             this.activeText = "option_all";
71             this.ruleRequest.enabled = null;
72         }
73     }
74
75     getRules(): Promise<any> {
76         return this._alarmRuleService
77             .getRules()
78             .then(rules => {
79                 this.rules = rules.correlationRules;
80                 this.totalcount = rules.totalCount;
81             });
82     }
83
84     public searchRules(): void {
85         if (this.ruleModel.enabled == null) {
86             this.ruleRequest.enabled = null;
87         }
88         this.ruleRequest.ruleName = this.ruleModel.ruleName;
89         console.log(this.ruleRequest.enabled, this.ruleRequest.ruleName);
90
91         this._alarmRuleService
92             .searchrules(this.ruleRequest)
93             .then(rules => {
94                 this.rules = rules;
95                 this.totalcount = rules.length;
96             });
97     }
98     public updateRule(rule: RuleModel): void {
99         this.router.navigate(['ruleInfo/', rule.ruleId]);
100     }
101
102     public delete(rule: RuleModel): void {
103         rule.enabled == 1 ? this.deleteActiveRule(rule) : this.deleteModel(rule.ruleId, this._alarmRuleService, this);
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     public deleteActiveRule(rule: RuleModel): void {
123         jQuery('#' + rule.ruleId).popModal({
124             html: jQuery('#deleteActiveRuleContent'),
125             placement: 'leftTop',
126             showCloseBut: false,
127             onDocumentClickClose: true,
128             onOkBut: function () {
129             },
130         });
131     }
132     public deleteModel(ruleId: string, alarm: AlarmRuleService, obj: any): void {
133         jQuery('#' + ruleId).popModal({
134             html: jQuery('#deleteTimingTaskContent'),
135             placement: 'leftTop',
136             showCloseBut: false,
137             onDocumentClickClose: true,
138             onOkBut: function () {
139                 jQuery('#deleteTimingTaskDlg').append(jQuery('#deleteTimingTaskContent'));
140                 alarm.delete(ruleId);
141                 obj.getRules();
142             },
143             onCancelBut: function () {
144             }
145         });
146     }
147
148     public ngOnInit(): void {
149         this.activeText = 'option_all';
150         this.ruleModel = {
151             ruleId: null,
152             ruleName: null,
153             description: '',
154             content: null,
155             createTime: null,
156             creator: null,
157             updateTime: null,
158             modifier: null,
159             enabled: 0,
160             loopControlName: ''
161         };
162         this.ruleRequest = {
163             ruleId: null,
164             ruleName: null,
165             creator: null,
166             modifier: null,
167             enabled: null,
168             loopControlName: ''
169         };
170         this.getRules();
171     }
172 }