-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalert.go
221 lines (170 loc) · 5.25 KB
/
alert.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//go:generate go run tools/withoutctx.go
package redash
import (
"context"
"fmt"
"time"
"github.com/winebarrel/redash-go/v2/internal/util"
)
type Alert struct {
CreatedAt time.Time `json:"created_at"`
ID int `json:"id"`
LastTriggeredAt time.Time `json:"last_triggered_at"`
Name string `json:"name"`
Options AlertOptions `json:"options"`
Query Query `json:"query"`
Rearm int `json:"rearm"`
State string `json:"state"`
UpdatedAt time.Time `json:"updated_at"`
User User `json:"user"`
}
type AlertOptions struct {
Column string `json:"column"`
Op string `json:"op"`
Value int `json:"value"`
CustomSubject string `json:"custom_subject"`
CustomBody string `json:"custom_body"`
// Deprecated: for backward compatibility
Template string `json:"template"`
Muted bool `json:"muted"`
}
type AlertSubscription struct {
AlertID int `json:"alert_id"`
Destination *Destination `json:"destination"`
ID int `json:"id"`
User User `json:"user"`
}
func (client *Client) ListAlerts(ctx context.Context) ([]Alert, error) {
res, close, err := client.Get(ctx, "api/alerts", nil)
defer close()
if err != nil {
return nil, err
}
alerts := []Alert{}
if err := util.UnmarshalBody(res, &alerts); err != nil {
return nil, err
}
return alerts, nil
}
func (client *Client) GetAlert(ctx context.Context, id int) (*Alert, error) {
res, close, err := client.Get(ctx, fmt.Sprintf("api/alerts/%d", id), nil)
defer close()
if err != nil {
return nil, err
}
alert := &Alert{}
if err := util.UnmarshalBody(res, &alert); err != nil {
return nil, err
}
return alert, nil
}
type CreateAlertInput struct {
Name string `json:"name"`
Options CreateAlertOptions `json:"options"`
QueryId int `json:"query_id"`
Rearm int `json:"rearm,omitempty"`
}
type CreateAlertOptions struct {
Column string `json:"column"`
Op string `json:"op"`
Value int `json:"value"`
CustomSubject string `json:"custom_subject,omitempty"`
CustomBody string `json:"custom_body,omitempty"`
// Deprecated: for backward compatibility
Template string `json:"template,omitempty"`
}
func (client *Client) CreateAlert(ctx context.Context, input *CreateAlertInput) (*Alert, error) {
res, close, err := client.Post(ctx, "api/alerts", input)
defer close()
if err != nil {
return nil, err
}
alert := &Alert{}
if err := util.UnmarshalBody(res, &alert); err != nil {
return nil, err
}
return alert, nil
}
type UpdateAlertInput struct {
Name string `json:"name,omitempty"`
Options *UpdateAlertOptions `json:"options,omitempty"`
QueryId int `json:"query_id,omitempty"`
Rearm int `json:"rearm"`
}
type UpdateAlertOptions struct {
Column string `json:"column"`
Value int `json:"value"`
Op string `json:"op"`
CustomSubject string `json:"custom_subject,omitempty"`
CustomBody string `json:"custom_body,omitempty"`
// Deprecated: for backward compatibility
Template string `json:"template,omitempty"`
}
func (client *Client) UpdateAlert(ctx context.Context, id int, input *UpdateAlertInput) (*Alert, error) {
res, close, err := client.Post(ctx, fmt.Sprintf("api/alerts/%d", id), input)
defer close()
if err != nil {
return nil, err
}
alert := &Alert{}
if err := util.UnmarshalBody(res, &alert); err != nil {
return nil, err
}
return alert, nil
}
func (client *Client) DeleteAlert(ctx context.Context, id int) error {
_, close, err := client.Delete(ctx, fmt.Sprintf("api/alerts/%d", id))
defer close()
if err != nil {
return err
}
return nil
}
func (client *Client) ListAlertSubscriptions(ctx context.Context, id int) ([]AlertSubscription, error) {
res, close, err := client.Get(ctx, fmt.Sprintf("api/alerts/%d/subscriptions", id), nil)
defer close()
if err != nil {
return nil, err
}
subs := []AlertSubscription{}
if err := util.UnmarshalBody(res, &subs); err != nil {
return nil, err
}
return subs, nil
}
func (client *Client) AddAlertSubscription(ctx context.Context, id int, destinationId int) (*AlertSubscription, error) {
res, close, err := client.Post(ctx, fmt.Sprintf("api/alerts/%d/subscriptions", id), map[string]int{"destination_id": destinationId})
defer close()
if err != nil {
return nil, err
}
sub := &AlertSubscription{}
if err := util.UnmarshalBody(res, &sub); err != nil {
return nil, err
}
return sub, nil
}
func (client *Client) RemoveAlertSubscription(ctx context.Context, id int, subscriptionId int) error {
_, close, err := client.Delete(ctx, fmt.Sprintf("api/alerts/%d/subscriptions/%d", id, subscriptionId))
defer close()
if err != nil {
return err
}
return nil
}
func (client *Client) MuteAlert(ctx context.Context, id int) error {
_, close, err := client.Post(ctx, fmt.Sprintf("api/alerts/%d/mute", id), nil)
defer close()
if err != nil {
return err
}
return nil
}
func (client *Client) UnmuteAlert(ctx context.Context, id int) error {
_, close, err := client.Delete(ctx, fmt.Sprintf("api/alerts/%d/mute", id))
defer close()
if err != nil {
return err
}
return nil
}