-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.go
68 lines (57 loc) · 1.33 KB
/
event.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
package noaalert
import (
"encoding/json"
"github.com/rotationalio/go-ensign"
api "github.com/rotationalio/go-ensign/api/v1beta1"
mimetype "github.com/rotationalio/go-ensign/mimetype/v1beta1"
)
type AlertEvent struct {
CorrelationID string
RequestID string
ServerID string
LastModified string
Expires string
Data []byte
parsed map[string]interface{}
}
var Mimetype = mimetype.ApplicationJSON
var AlertType = &api.Type{
Name: "Alert",
MajorVersion: 1,
MinorVersion: 0,
PatchVersion: 0,
}
func (a *AlertEvent) Event() *ensign.Event {
meta := make(ensign.Metadata)
meta["correlation_id"] = a.CorrelationID
meta["request_id"] = a.RequestID
meta["server_id"] = a.ServerID
meta["last_modified"] = a.LastModified
meta["expires"] = a.Expires
return &ensign.Event{
Metadata: meta,
Data: a.Data,
Type: AlertType,
Mimetype: Mimetype,
}
}
func (a *AlertEvent) Headline() (_ string, err error) {
if err = a.parse(); err != nil {
return "", err
}
props, ok := a.parsed["properties"].(map[string]interface{})
if !ok {
return "", ErrNoProperties
}
headline, ok := props["headline"].(string)
if !ok {
return "", ErrNoHeadline
}
return headline, nil
}
func (a *AlertEvent) parse() error {
if a.parsed == nil {
return json.Unmarshal(a.Data, &a.parsed)
}
return nil
}