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 { Injectable } from '@angular/core';
17 import { Http, Response, Headers } from '@angular/http';
20 import { RuleModel } from './alarmRule';
21 import { RuleRequest } from './ruleRequest';
22 import { Router } from '@angular/router';
23 import { ModalService } from '../correlation-modal/modal.service';
26 export class AlarmRuleService {
27 private ruleUrl = "/api/holmes-rule-mgmt/v1/rule";
28 private headers = new Headers({ 'Content-Type': 'application/json' });
29 constructor(private http: Http, private modalService: ModalService, private router: Router) { }
31 getRules(): Promise<any> {
32 return this.http.get(this.ruleUrl)
34 .then(res => res.json())
35 .catch(this.handleError);
38 private handleError(error: any): Promise<any> {
39 console.error('An error occurred', error);
40 return Promise.reject(error._body || error);
43 search(ruleId: string): Promise<RuleModel> {
44 if (typeof (ruleId) == "string") {
57 let data = { 'ruleId': ruleId };
58 var queryrequest = JSON.stringify(data);
59 const url = `${this.ruleUrl}?queryrequest=${queryrequest}`;
60 return this.http.get(url, {headers:this.headers})
62 .then(res => res.json().correlationRules as RuleModel[])
63 .catch(this.handleError);
66 searchrules(rule: RuleRequest): Promise<RuleModel[]> {
67 let data = { ruleName: rule.ruleName, enabled: rule.enabled }
68 console.log(JSON.stringify(data));
69 const url = `${this.ruleUrl}?queryrequest=${JSON.stringify(data)}`
70 return this.http.get(url, { body: data, headers: this.headers })
72 .then(res => res.json().correlationRules as RuleModel[])
73 .catch(this.handleError);
76 checkContent(ruleContent: string): Promise<any> {
77 const url = "/api/holmes-engine-mgmt/v1/rule";
78 let data = { content: ruleContent };
80 .post(url, JSON.stringify(data), { headers: this.headers })
83 .catch(error => error);
86 updateRule(rule: RuleModel): Promise<any> {
88 "ruleId": rule.ruleId,
89 "description": rule.description,
90 "content": rule.content,
91 "enabled": rule.enabled,
92 "loopControlName": rule.loopControlName
94 const url = `${this.ruleUrl}`
96 .post(url, JSON.stringify(rules), { headers: this.headers })
99 .catch(error => error)
102 save(rule: RuleModel): Promise<any> {
104 "description": rule.description,
105 "content": rule.content,
106 "enabled": rule.enabled,
107 "ruleName": rule.ruleName,
108 "loopControlName": rule.loopControlName
110 return this.http.put(this.ruleUrl, JSON.stringify(ruledata), { headers: this.headers })
113 .catch(error => error);
116 public delete(ruleId: string): Promise<void> {
117 const url = `${this.ruleUrl}` + '/' + ruleId;
118 return this.http.delete(url, { headers: this.headers })
123 .catch(this.handleError);