summaryrefslogtreecommitdiffstats
path: root/rulemgt-frontend/src/app/correlation-ruleList/alarmRule.service.ts
blob: d5580414e7f4bf5d8e38b7411f40a20b8bb872a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 Copyright 2018 ZTE Corporation.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
*/
import { Injectable } from '@angular/core';
import { Http, Response, Headers } from '@angular/http';


import { RuleModel } from './alarmRule';
import { RuleRequest } from './ruleRequest';
import { Router } from '@angular/router';
import { ModalService } from '../correlation-modal/modal.service';

@Injectable()
export class AlarmRuleService {
    private ruleUrl = "/api/holmes-rule-mgmt/v1/rule";
    private headers = new Headers({ 'Content-Type': 'application/json' });
    constructor(private http: Http, private modalService: ModalService, private router: Router) { }

    getRules(): Promise<any> {
        return this.http.get(this.ruleUrl)
            .toPromise()
            .then(res => res.json())
            .catch(this.handleError);
    }

    private handleError(error: any): Promise<any> {
        console.error('An error occurred', error);
        return Promise.reject(error._body || error);
    }

    search(ruleId: string): Promise<RuleModel> {
        if (typeof (ruleId) == "string") {
            let rule = [{
                ruleId: null,
                ruleName: null,
                description: null,
                content: null,
                createtime: null,
                creator: null,
                updatetime: null,
                modifier: null,
                enabled: 0,
            }]
        }
        let data = { 'ruleId': ruleId };
        var queryrequest = JSON.stringify(data);
        const url = `${this.ruleUrl}?queryrequest=${queryrequest}`;
        return this.http.get(url, {headers:this.headers})
            .toPromise()
            .then(res => res.json().rules as RuleModel)
            .catch(this.handleError);
    }

    searchrules(rule: RuleRequest): Promise<RuleModel[]> {
        let data = { ruleName: rule.ruleName, enabled: rule.enabled }
        console.log(JSON.stringify(data));
        const url = `${this.ruleUrl}?queryrequest=${JSON.stringify(data)}`
        return this.http.get(url, { body: data, headers: this.headers })
            .toPromise()
            .then(res => res.json().rules as RuleModel[])
            .catch(this.handleError);
    }

    checkContent(ruleContent: string): Promise<any> {
        const url = "/api/holmes-engine-mgmt/v1/rule";
        let data = { content: ruleContent };
        return this.http
            .post(url, JSON.stringify(data), { headers: this.headers })
            .toPromise()
            .then(res => res)
            .catch(error => error);
    }

    updateRule(rule: RuleModel): Promise<any> {
        let rules = {
            "ruleId": rule.ruleId,
            "description": rule.description,
            "content": rule.content,
            "enabled": rule.enabled
        }
        const url = `${this.ruleUrl}`
        return this.http
            .post(url, JSON.stringify(rules), { headers: this.headers })
            .toPromise()
            .then(res => res)
            .catch(error => error)
    }

    save(rule: RuleModel): Promise<any> {
        let ruledata = {
            "description": rule.description,
            "content": rule.content,
            "enabled": rule.enabled,
            "ruleName": rule.ruleName
        }
        return this.http.put(this.ruleUrl, JSON.stringify(ruledata), { headers: this.headers })
            .toPromise()
            .then(res => res)
            .catch(error => error);
    }

    public delete(ruleId: string): Promise<void> {
        const url = `${this.ruleUrl}` + '/' + ruleId;
        return this.http.delete(url, { headers: this.headers })
            .toPromise()
            .then(res => {
               
            })
            .catch(this.handleError);
    }
}