-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathflows.go
251 lines (213 loc) · 9.22 KB
/
flows.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
* Copyright 2023 Pius Alfred <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the “Software”), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package flow
// https://developers.facebook.com/docs/whatsapp/flows/reference/flowswebhooks
import (
"context"
"fmt"
"net/http"
"github.com/piusalfred/whatsapp/webhooks"
)
const (
StatusDraft = "DRAFT"
StatusPublished = "PUBLISHED"
StatusDeprecated = "DEPRECATED"
StatusBlocked = "BLOCKED"
StatusThrottled = "THROTTLED"
EventFlowStatusChange = "FLOW_STATUS_CHANGE"
EventEndpointErrorRate = "ENDPOINT_ERROR_RATE"
EventEndpointLatency = "ENDPOINT_LATENCY"
EventEndpointAvailability = "ENDPOINT_AVAILABILITY"
EventClientErrorRate = "CLIENT_ERROR_RATE"
)
var _ NotificationHandler = (*Handlers)(nil)
type Handlers struct {
FlowStatusChangeHandler EventHandler[StatusChangeDetails]
ClientErrorRateHandler EventHandler[ClientErrorRateDetails]
EndpointErrorRateHandler EventHandler[EndpointErrorRateDetails]
EndpointLatencyHandler EventHandler[EndpointLatencyDetails]
EndpointAvailabilityHandler EventHandler[EndpointAvailabilityDetails]
}
func (handlers *Handlers) HandleNotification(ctx context.Context, notification *Notification) *webhooks.Response {
if err := handlers.dispatchNotification(ctx, notification); err != nil {
return &webhooks.Response{StatusCode: http.StatusInternalServerError}
}
return &webhooks.Response{StatusCode: http.StatusOK}
}
func (handlers *Handlers) dispatchNotification(ctx context.Context, notification *Notification) error {
for _, entry := range notification.Entry {
for _, change := range entry.Changes {
notificationCtx := &NotificationContext{
NotificationObject: notification.Object,
EntryID: entry.ID,
EntryTime: entry.Time,
ChangeField: change.Field,
EventName: change.Value.Event,
EventMessage: change.Value.Message,
FlowID: change.Value.FlowID,
}
switch change.Value.Event {
case EventFlowStatusChange:
details := &StatusChangeDetails{
OldStatus: change.Value.OldStatus,
NewStatus: change.Value.NewStatus,
}
if err := handlers.FlowStatusChangeHandler.HandleEvent(ctx, notificationCtx, details); err != nil {
return fmt.Errorf("handle flow status change event: %w", err)
}
return nil
case EventClientErrorRate:
details := &ClientErrorRateDetails{
ErrorRate: change.Value.ErrorRate,
Threshold: change.Value.Threshold,
AlertState: change.Value.AlertState,
Errors: change.Value.Errors,
}
if err := handlers.ClientErrorRateHandler.HandleEvent(ctx, notificationCtx, details); err != nil {
return fmt.Errorf("handle client error rate event: %w", err)
}
return nil
case EventEndpointErrorRate:
details := &EndpointErrorRateDetails{
ErrorRate: change.Value.ErrorRate,
Threshold: change.Value.Threshold,
AlertState: change.Value.AlertState,
Errors: change.Value.Errors,
}
if err := handlers.EndpointErrorRateHandler.HandleEvent(ctx, notificationCtx, details); err != nil {
return fmt.Errorf("handle endpoint error rate event: %w", err)
}
return nil
case EventEndpointLatency:
details := &EndpointLatencyDetails{
P50Latency: change.Value.P50Latency,
P90Latency: change.Value.P90Latency,
RequestsCount: change.Value.RequestsCount,
Threshold: change.Value.Threshold,
AlertState: change.Value.AlertState,
}
if err := handlers.EndpointLatencyHandler.HandleEvent(ctx, notificationCtx, details); err != nil {
return fmt.Errorf("handle endpoint latency event: %w", err)
}
return nil
case EventEndpointAvailability:
details := &EndpointAvailabilityDetails{
Availability: change.Value.Availability,
Threshold: change.Value.Threshold,
AlertState: change.Value.AlertState,
}
if err := handlers.EndpointAvailabilityHandler.HandleEvent(ctx, notificationCtx, details); err != nil {
return fmt.Errorf("handle endpoint availability event: %w", err)
}
return nil
}
}
}
return nil
}
type (
NotificationHandler webhooks.NotificationHandler[Notification]
NotificationHandlerFunc webhooks.NotificationHandlerFunc[Notification]
)
func (e NotificationHandlerFunc) HandleNotification(ctx context.Context,
notification *Notification,
) *webhooks.Response {
return e(ctx, notification)
}
type (
EventHandler[T any] interface {
HandleEvent(ctx context.Context, ntx *NotificationContext, notification *T) error
}
EventHandlerFunc[T any] func(ctx context.Context, ntx *NotificationContext, notification *T) error
)
func (fn EventHandlerFunc[T]) HandleEvent(ctx context.Context, ntx *NotificationContext, notification *T) error {
return fn(ctx, ntx, notification)
}
type (
NotificationContext struct {
NotificationObject string // Corresponds to the 'object' field
EntryID string // Corresponds to the 'id' field in Entry
EntryTime int64 // Corresponds to the 'time' field in Entry
ChangeField string // Corresponds to the 'field' in Changes
EventName string // Corresponds to 'event' field in Value
EventMessage string // Corresponds to 'message' field in Value
FlowID string // Corresponds to 'flow_id' field in Value
}
StatusChangeDetails struct {
OldStatus string `json:"old_status,omitempty"`
NewStatus string `json:"new_status,omitempty"`
}
ClientErrorRateDetails struct {
ErrorRate float64 `json:"error_rate,omitempty"`
Threshold int `json:"threshold,omitempty"`
AlertState string `json:"alert_state,omitempty"`
Errors []ErrorInfo `json:"errors,omitempty"`
}
EndpointErrorRateDetails struct {
ErrorRate float64 `json:"error_rate,omitempty"`
Threshold int `json:"threshold,omitempty"`
AlertState string `json:"alert_state,omitempty"`
Errors []ErrorInfo `json:"errors,omitempty"`
}
EndpointLatencyDetails struct {
P50Latency int `json:"p50_latency,omitempty"`
P90Latency int `json:"p90_latency,omitempty"`
RequestsCount int `json:"requests_count,omitempty"`
Threshold int `json:"threshold,omitempty"`
AlertState string `json:"alert_state,omitempty"`
}
EndpointAvailabilityDetails struct {
Availability int `json:"availability,omitempty"`
Threshold int `json:"threshold,omitempty"`
AlertState string `json:"alert_state,omitempty"`
}
Notification struct {
Object string `json:"object"`
Entry []*Entry `json:"entry"`
}
Entry struct {
ID string `json:"id"`
Time int64 `json:"time"`
Changes []*Changes `json:"changes"`
}
Changes struct {
Value *Value `json:"value"`
Field string `json:"field"`
}
Value struct {
Event string `json:"event"` // Event type, e.g., "CLIENT_ERROR_RATE"
Message string `json:"message"` // Descriptive message of the event
FlowID string `json:"flow_id"` // ID of the flow
OldStatus string `json:"old_status,omitempty"` // Previous status of the flow (optional)
NewStatus string `json:"new_status,omitempty"` // New status of the flow (optional)
ErrorRate float64 `json:"error_rate,omitempty"` // Overall error rate for the alert (optional)
Threshold int `json:"threshold,omitempty"` // Alert threshold that was reached or recovered from
AlertState string `json:"alert_state,omitempty"` // Status of the alert, e.g., "ACTIVATED" or "DEACTIVATED"
Errors []ErrorInfo `json:"errors,omitempty"` // List of errors describing the alert (optional)
P50Latency int `json:"p50_latency,omitempty"` // P50 latency of the endpoint requests (optional)
P90Latency int `json:"p90_latency,omitempty"` // P90 latency of the endpoint requests (optional)
RequestsCount int `json:"requests_count,omitempty"` // Number of requests used to calculate metric (optional)
Availability int `json:"availability"`
}
ErrorInfo struct {
ErrorType string `json:"error_type,omitempty"`
ErrorRate float64 `json:"error_rate,omitempty"`
ErrorCount int64 `json:"error_count,omitempty"`
}
)