Initialize the UI code
[holmes/rule-management.git] / rulemgt / src / main / frontend / src / alarm / app / correlation-ruleList / alarmRule.service.ts
1 /*
2  Copyright 2017 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 { Injectable } from '@angular/core';
17 import { Http, Response, Headers } from '@angular/http';
18 import 'rxjs/add/operator/toPromise';
19 import 'rxjs/add/operator/map';
20 import { RuleModel } from './alarmRule';
21 import { RuleRequest } from './ruleRequest'
22 import { Router } from '@angular/router';
23 import { ModalService } from '../correlation-modal/modal.service';
24
25 @Injectable()
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) { }
30
31     getRules(): Promise<any> {
32         return this.http.get(this.ruleUrl)
33             .toPromise()
34             .then(res => res.json())
35             .catch(this.handleError);
36     }
37
38     private handleError(error: any): Promise<any> {
39         console.error('An error occurred', error);
40         return Promise.reject(error._body || error);
41     }
42
43     search(ruleid: string): Promise<RuleModel> {
44         if (typeof (ruleid) == "string") {
45             let rule = [{
46                 ruleid: null,
47                 rulename: null,
48                 description: null,
49                 content: null,
50                 createtime: null,
51                 creator: null,
52                 updatetime: null,
53                 modifier: null,
54                 enabled: 0,
55             }]
56         }
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})
61             .toPromise()
62             .then(res => res.json().rules as RuleModel)
63             .catch(this.handleError);
64     }
65
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 })
71             .toPromise()
72             .then(res => res.json().rules as RuleModel[])
73             .catch(this.handleError);
74     }
75
76     checkContent(ruleContent: string): Promise<any> {
77         const url = "/api/holmes-engine-mgmt/v1/rule";
78         let data = { content: ruleContent };
79         return this.http
80             .post(url, JSON.stringify(data), { headers: this.headers })
81             .toPromise()
82             .then(res => res)
83             .catch(error => error);
84     }
85
86     updateRule(rule: RuleModel): Promise<any> {
87         let rules = {
88             "ruleid": rule.ruleid,
89             "description": rule.description,
90             "content": rule.content,
91             "enabled": rule.enabled
92         }
93         const url = `${this.ruleUrl}`
94         return this.http
95             .post(url, JSON.stringify(rules), { headers: this.headers })
96             .toPromise()
97             .then(res => res)
98             .catch(error => error)
99     }
100
101     save(rule: RuleModel): Promise<any> {
102         let ruledata = {
103             "description": rule.description,
104             "content": rule.content,
105             "enabled": rule.enabled,
106             "rulename": rule.rulename
107         }
108         return this.http.put(this.ruleUrl, JSON.stringify(ruledata), { headers: this.headers })
109             .toPromise()
110             .then(res => res)
111             .catch(error => error);
112     }
113
114     public delete(ruleid: string): Promise<void> {
115         const url = `${this.ruleUrl}` + '/' + ruleid;
116         return this.http.delete(url, { headers: this.headers })
117             .toPromise()
118             .then(res => {
119                
120             })
121             .catch(this.handleError);
122     }
123 }