This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
alert_condition.go
172 lines (156 loc) · 5.77 KB
/
alert_condition.go
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package graylog
import (
"encoding/json"
"errors"
"fmt"
)
type (
// AlertCondition represents an Alert Condition.
// https://docs.graylog.org/en/latest/pages/streams/alerts.html#conditions
AlertCondition struct {
ID string `json:"id,omitempty"`
CreatorUserID string `json:"creator_user_id,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
Title string `json:"title" v-create:"required" v-update:"required"`
// in_grace must be "omitempty". Without "omitempty", it is failed to create an Alert Condition.
// Unable to map property in_grace. Known properties include: title, type, parameters
InGrace bool `json:"in_grace,omitempty"`
Parameters AlertConditionParameters `json:"parameters" v-create:"reqired" v-update:"required"`
}
// AlertConditionParameters represents Alert Condition's parameters.
AlertConditionParameters interface {
AlertConditionType() string
}
// FieldContentAlertConditionParameters represents Field Content Alert Condition's parameters.
FieldContentAlertConditionParameters struct {
Grace int `json:"grace"`
Backlog int `json:"backlog"`
RepeatNotifications bool `json:"repeat_notifications"`
Field string `json:"field,omitempty" v-create:"required"`
Value string `json:"value,omitempty" v-create:"required"`
Query string `json:"query,omitempty"`
}
// FieldAggregationAlertConditionParameters represents Field Aggregation Alert Condition's parameters.
FieldAggregationAlertConditionParameters struct {
Grace int `json:"grace"`
Backlog int `json:"backlog"`
Threshold int `json:"threshold"`
Time int `json:"time" v-create:"required"`
RepeatNotifications bool `json:"repeat_notifications"`
Field string `json:"field,omitempty" v-create:"required"`
Query string `json:"query,omitempty"`
ThresholdType string `json:"threshold_type,omitempty" v-create:"required"`
Type string `json:"type,omitempty" v-create:"required"`
}
// MessageCountAlertConditionParameters represents Field Aggregation Alert Condition's parameters.
MessageCountAlertConditionParameters struct {
Grace int `json:"grace"`
Backlog int `json:"backlog"`
Threshold int `json:"threshold"`
Time int `json:"time"`
RepeatNotifications bool `json:"repeat_notifications"`
Query string `json:"query,omitempty"`
ThresholdType string `json:"threshold_type,omitempty" v-create:"required"`
}
// AlertConditionsBody represents Get Alert Conditions API's response body.
// Basically users don't use this struct, but this struct is public because some sub packages use this struct.
AlertConditionsBody struct {
AlertConditions []AlertCondition `json:"conditions"`
Total int `json:"total"`
}
// GeneralAlertConditionParameters is a general third party's alert condition parameters.
GeneralAlertConditionParameters struct {
Type string
Parameters map[string]interface{}
}
)
// Type returns an alert condition type.
func (cond AlertCondition) Type() string {
if cond.Parameters == nil {
return ""
}
return cond.Parameters.AlertConditionType()
}
// MarshalJSON returns JSON encoding of an alert condition.
func (cond *AlertCondition) MarshalJSON() ([]byte, error) {
if cond == nil {
return []byte("{}"), nil
}
type alias AlertCondition
return json.Marshal(struct {
Type string `json:"type"`
*alias
}{
Type: cond.Type(),
alias: (*alias)(cond),
})
}
// UnmarshalJSON unmarshals JSON into an alert condition.
func (cond *AlertCondition) UnmarshalJSON(b []byte) error {
errMsg := "failed to unmarshal JSON to alert condition"
if cond == nil {
return errors.New(errMsg + ": alert condition is nil")
}
type alias AlertCondition
a := struct {
Type string `json:"type"`
Parameters json.RawMessage `json:"parameters"`
*alias
}{
alias: (*alias)(cond),
}
if err := json.Unmarshal(b, &a); err != nil {
return fmt.Errorf(errMsg+": %w", err)
}
switch a.Type {
case "field_content_value":
p := FieldContentAlertConditionParameters{}
if err := json.Unmarshal(a.Parameters, &p); err != nil {
return fmt.Errorf(errMsg+": %w", err)
}
cond.Parameters = p
return nil
case "field_value":
p := FieldAggregationAlertConditionParameters{}
if err := json.Unmarshal(a.Parameters, &p); err != nil {
return fmt.Errorf(errMsg+": %w", err)
}
cond.Parameters = p
return nil
case "message_count":
p := MessageCountAlertConditionParameters{}
if err := json.Unmarshal(a.Parameters, &p); err != nil {
return fmt.Errorf(errMsg+": %w", err)
}
cond.Parameters = p
return nil
}
p := map[string]interface{}{}
if err := json.Unmarshal(a.Parameters, &p); err != nil {
return fmt.Errorf(errMsg+": %w", err)
}
cond.Parameters = GeneralAlertConditionParameters{
Type: a.Type, Parameters: p,
}
return nil
}
// AlertConditionType returns an alert condition type.
func (p FieldContentAlertConditionParameters) AlertConditionType() string {
return "field_content_value"
}
// AlertConditionType returns an alert condition type.
func (p FieldAggregationAlertConditionParameters) AlertConditionType() string {
return "field_value"
}
// AlertConditionType returns an alert condition type.
func (p MessageCountAlertConditionParameters) AlertConditionType() string {
return "message_count"
}
// AlertConditionType returns an alert condition type.
func (p GeneralAlertConditionParameters) AlertConditionType() string {
return p.Type
}
// MarshalJSON returns JSON encoding of GeneralAlertConditionParameters.
func (p *GeneralAlertConditionParameters) MarshalJSON() ([]byte, error) {
return json.Marshal(p.Parameters)
}