2 Copyright 2018 ZTE Corporation.
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
16 import { Component, OnInit } from '@angular/core';
17 import { RuleModel } from '../correlation-ruleList/alarmRule';
18 import { ActivatedRoute, Params, Router } from '@angular/router';
19 import { AlarmRuleService } from '../correlation-ruleList/alarmRule.service';
20 import { ModalService } from '../correlation-modal/modal.service';
21 import { NgModel } from '@angular/forms';
27 templateUrl: './ruleInfo.component.html'
29 export class RuleInfo implements OnInit {
33 addBottonStatus: boolean;
35 constructor(private modalService: ModalService,
36 private route: ActivatedRoute,
37 private router: Router,
38 private alarmRuleService: AlarmRuleService) {
69 this.route.params.subscribe((params) => {
71 let id = params['id'];
72 if (typeof (id) == 'string') {
73 if (id.indexOf('&') == -1) {
74 $('#ruleNameInput').attr('disabled', true);
75 $('#saveLabel').hide();
77 this.addBottonStatus = false;
78 $('#saveLabel').hide();
79 $('#checkLabel').hide();
80 $('#updateLabel').hide();
83 let str = id.split('&');
84 this.getRuleInfo(str[0]);
85 } else if (typeof (id) != 'undefined') {
88 $('#updateLabel').hide();
90 if (typeof (this.queryRule.enabled) === 'number') {
91 this.queryRule.enabled = '' + this.queryRule.enabled;
95 this.initImportDiv(this.queryRule);
99 this.queryRule.enabled = $('input:radio:checked').val();
100 if (!this.queryRule.content.startsWith('package ')) {
101 let msg = { title: 'exception_content_error', message: 'exception_package_error' };
102 this.modalService.announceOpenModal(msg);
104 this.alarmRuleService.checkContent(this.replacePkgName(this.queryRule.content))
106 if (res.status == 200) {
107 this.alarmRuleService.updateRule(this.queryRule)
109 if (res.status == 200) {
110 let msg = { title: 'modalTitleUpdate', message: 'message_update_rule_success' };
111 this.modalService.announceOpenModal(msg);
112 this.router.navigate(['alarmRule']);
113 } else if (res.status == 499) {
114 let msg = { title: 'modalTitleUpdate', message: 'message_exception_rule_fail' };
115 this.modalService.announceOpenModal(msg);
117 let msg = { title: 'modalTitleUpdate', message: 'message_other_exception_rule_fail' };
118 this.modalService.announceOpenModal(msg);
122 let msg = { title: 'modalTitleUpdate', message: 'message_other_exception_rule_fail' };
123 this.modalService.announceOpenModal(msg);
126 } else if (res.status == 499) {
127 let msg = { 'title': 'modalTitleCheck', message: 'message_rule_content_repeat_error' };
128 this.modalService.announceOpenModal(msg);
131 let msg = { 'title': 'modalTitleCheck', message: 'message_other_exception_rule_fail' };
132 this.modalService.announceOpenModal(msg);
139 onSubmit(obj: NgModel) {
144 if (!this.queryRule.content || !this.queryRule.content.startsWith('package ')) {
145 let msg = { title: 'exception_content_error', message: 'exception_package_error' };
146 this.modalService.announceOpenModal(msg);
148 this.alarmRuleService.checkContent(this.queryRule.content).then(res => {
149 if (res.status == 200) {
150 this.alarmRuleService.save(this.queryRule).then(res => {
151 if (res.status == 200) {
152 let msg = { title: 'modalTitleDefault', message: 'message_add_rule_success' };
153 this.modalService.announceOpenModal(msg);
154 this.router.navigate(['alarmRule']);
155 } else if (res.status == 499) {
156 let msg = { title: 'modalTitleDefault', message: 'message_rule_name_repeat_error' };
157 this.modalService.announceOpenModal(msg);
159 let msg = { 'title': 'modalTitleCheck', message: 'message_other_exception_rule_fail' };
160 this.modalService.announceOpenModal(msg);
164 let msg = { title: 'modalTitleDefault', message: 'message_rule_name_repeat_error' };
165 this.modalService.announceOpenModal(msg);
168 } else if (res.status == 499) {
169 let msg = { 'title': 'modalTitleCheck', message: 'message_rule_content_repeat_error' };
170 this.modalService.announceOpenModal(msg);
173 let msg = { 'title': 'modalTitleCheck', message: 'message_other_exception_rule_fail' };
174 this.modalService.announceOpenModal(msg);
181 getRuleInfo(id: string): void {
182 this.route.params.switchMap((params: Params) => this.alarmRuleService.search(id))
184 rule[0].enabled = '' + rule[0].enabled;
185 this.queryRule = rule[0]
189 checkContent(judge: string): void {
190 if (!this.queryRule.content || !this.queryRule.content.startsWith('package ')) {
191 let msg = { title: 'exception_content_error', message: 'exception_package_error' };
192 this.modalService.announceOpenModal(msg);
194 this.alarmRuleService.checkContent(this.queryRule.content)
196 if (res.status == 200) {
197 let msg = { 'title': 'modalTitleCheck', message: 'message_checkContent_rule_success' };
198 this.modalService.announceOpenModal(msg);
199 } else if (res.status == 499) {
200 let msg = { 'title': 'modalTitleCheck', message: 'message_rule_content_repeat_error' };
201 this.modalService.announceOpenModal(msg);
204 let msg = { 'title': 'modalTitleCheck', message: 'message_other_exception_rule_fail' };
205 this.modalService.announceOpenModal(msg);
213 // alert(this.queryRule.enabled);
214 this.queryRule.enabled == '0' ? this.queryRule.enabled = '1' : this.queryRule.enabled = '0';
218 initUpload(queryRule: RuleModel): void {
219 $('#fileName').text('');
220 $('#importFailTip').addClass('hide_panel');
221 $('#fileupload').fileupload({
223 dropZone: $('#dropzone'),
225 maxChunkSize: 20000000,
227 add: function (e, data) {
228 let fileName = data.files[0].name;
230 $('#importFailTip').addClass('hide_panel');
231 let suffix = fileName.substring(fileName.lastIndexOf('.'), fileName.length)
233 if (suffix != '.txt') {
234 $('#importFailTip').removeClass('hide_panel');
238 $('#bar').css('width', '0%');
239 $('#persent').text('0%');
240 $('#fileName').text(fileName);
241 $('#fileremove').attr('disabled', false);
242 $('#filesubmit').attr('disabled', false);
243 $('#filesubmit').click(function () {
244 this.file = data.files[0]
245 var reader = new FileReader();
246 reader.readAsText(this.file);
247 reader.onload = function (data) {
248 queryRule.content = this.result;
250 $('#fileremove').click();
251 $('#importDiv').hide();
255 $('#fileremove').click(function () {
256 $('#bar').css('width', '0%');
257 $('#persent').text('0%');
258 $('#fileName').text('');
259 $('#filesubmit').attr('disabled', true);
260 $('#fileremove').attr('disabled', true);
263 done: function (e, data) {
266 fail: function (e, resp) {
268 always: function (e, data) {
269 $('.progress').removeClass('active');
270 $('#bar').css('width', '100%');
271 $('#persent').text('100%');
273 progressall: function (e, data) {
278 initImportDiv(queryRule: RuleModel): void {
279 this.initUpload(queryRule);
280 let importDiv = $('#importDiv');
282 $('#importBtn').click(function (e) {
284 $('[data-toggle="tooltip"]').tooltip();
285 $('#importFailTip').addClass('hide_panel');
287 $(document).one('click', function (e) {
292 $(importDiv).click(function (e) {
295 $('#filesubmit').attr('disabled', true);
296 $('#fileremove').attr('disabled', true);
300 $('#bar').css('width', '0%');
301 $('#persent').text('0%');
302 $(importDiv).fadeIn();
306 replacePkgName (content: string): string {
307 let reg = /(package\s+[\w\d\.]+)(\s+|;).*/;
308 let groups = reg.exec(content);
309 let pkgName = groups[1];
310 if (pkgName == null) {
313 return content.replace(pkgName, pkgName + new Date().getTime());