@@ -2,37 +2,77 @@ package event
22
33import (
44 "context"
5+ "encoding/json"
6+ "fmt"
57
68 "github.com/rs/xid"
79
810 "github.com/infraboard/mcube/types/ftime"
911)
1012
11- // 事件主题定义
13+ // 事件主题定义(由事件类型确定)
1214// 1. 资源变更事件 (变更成功和变更失败)
1315// 2. 资源告警事件 ( 更加metric计算触发)
1416
15- // NewEvent 实例
16- func NewEvent ( ) * Event {
17+ // NewOperateEvent 实例
18+ func NewOperateEvent ( header * Header , e * OperateEvent ) * Event {
1719 return & Event {
18- ID : xid .New ().String (),
19- Time : ftime .Now (),
20- Label : make (map [string ]string ),
20+ ID : xid .New ().String (),
21+ Time : ftime .Now (),
22+ Type : OperateEventType ,
23+ Header : header ,
24+ Body : e ,
25+ }
26+ }
27+
28+ // NewDefaultEvent todo
29+ func NewDefaultEvent () * Event {
30+ return & Event {
31+ Header : NewHeader (),
32+ Body : & json.RawMessage {},
2133 }
2234}
2335
2436// Event 事件数据结构
2537type Event struct {
26- ID string `bson:"_id" json:"id"` // 事件ID
27- Time ftime.Time `bson:"time" json:"time"` // 事件发生时间(毫秒)
28- Source string `bson:"source" json:"source"` // 事件来源, 比如cmdb
29- Level Level `bson:"level" json:"level"` // 事件等级
30- Label map [string ]string `bson:"label" json:"label"` // 标签
31- Meta Meta `bson:"meta" json:"meta"` // 事件的元数据
32- Type Type `bson:"type" json:"type"` // 事件类型
33- Body interface {} `bson:"body" json:"body"` // 事件数据
38+ ID string `bson:"_id" json:"id"` // 事件ID
39+ Time ftime.Time `bson:"time" json:"time"` // 事件发生时间(毫秒)
40+ Type Type `bson:"type" json:"type"` // 事件类型
41+ * Header `bson:",inline"` // 事件头
42+ Body interface {} `bson:"body" json:"body"` // 事件数据
43+
44+ ctx context.Context
45+ parsed bool
46+ }
47+
48+ // ParseBody todo
49+ func (e * Event ) ParseBody () error {
50+ if e .parsed {
51+ return nil
52+ }
53+
54+ body , err := e .getBytesBody ()
55+ if err != nil {
56+ return err
57+ }
3458
35- ctx context.Context
59+ switch e .Type {
60+ case OperateEventType :
61+ e .Body , err = ParseOperateEventFromBytes (body )
62+ if err != nil {
63+ return err
64+ }
65+ case AlertEventType :
66+ e .Body , err = ParseAlertEventFromBytes (body )
67+ if err != nil {
68+ return err
69+ }
70+ default :
71+ return fmt .Errorf ("unknown event type: %s" , e .Type )
72+ }
73+
74+ e .parsed = true
75+ return nil
3676}
3777
3878// WithContext 添加上下文
@@ -45,13 +85,61 @@ func (e *Event) Context() context.Context {
4585 return e .ctx
4686}
4787
88+ func (e * Event ) getBytesBody () ([]byte , error ) {
89+ switch v := e .Body .(type ) {
90+ case []byte :
91+ return v , nil
92+ case json.RawMessage :
93+ return v , nil
94+ case * json.RawMessage :
95+ return * v , nil
96+ default :
97+ return nil , fmt .Errorf ("body type is not []byte or json.RawMessage" )
98+ }
99+ }
100+
101+ // NewHeader todo
102+ func NewHeader () * Header {
103+ return & Header {
104+ Label : make (map [string ]string ),
105+ }
106+ }
107+
108+ // Header todo
109+ type Header struct {
110+ Source string `bson:"source" json:"source"` // 事件来源, 比如cmdb
111+ Level Level `bson:"level" json:"level"` // 事件等级
112+ Label map [string ]string `bson:"label" json:"label"` // 标签
113+ Meta Meta `bson:"meta" json:"meta"` // 事件的元数据
114+ }
115+
116+ // ParseOperateEventFromBytes todo
117+ func ParseOperateEventFromBytes (data []byte ) (* OperateEvent , error ) {
118+ oe := & OperateEvent {}
119+ if err := json .Unmarshal (data , oe ); err != nil {
120+ return nil , err
121+ }
122+ return oe , nil
123+ }
124+
48125// OperateEvent 事件具体数据
49126type OperateEvent struct {
50- ResourceType string `bson:"resource_type" json:"resource_type"` // 资源类型,
51- ResourceUUID string `bson:"resource_uuid" json:"resource_uuid"` // 资源UUID
52- ResourceName string `bson:"resource_name" json:"resource_name"` // 资源名称
53- Action string `bson:"action" json:"action"` // 操作
54- Data interface {} `bson:"data" json:"data,omitempty"` // 事件数据
127+ OperateSession string `bson:"operate_session" json:"operate_session"` // 回话ID
128+ OperateUser string `bson:"operate_user" json:"operate_user"` // 操作人
129+ ResourceType string `bson:"resource_type" json:"resource_type"` // 资源类型,
130+ ResourceUUID string `bson:"resource_uuid" json:"resource_uuid"` // 资源UUID
131+ ResourceName string `bson:"resource_name" json:"resource_name"` // 资源名称
132+ Action string `bson:"action" json:"action"` // 操作
133+ Data interface {} `bson:"data" json:"data,omitempty"` // 事件数据
134+ }
135+
136+ // ParseAlertEventFromBytes todo
137+ func ParseAlertEventFromBytes (data []byte ) (* AlertEvent , error ) {
138+ ae := & AlertEvent {}
139+ if err := json .Unmarshal (data , ae ); err != nil {
140+ return nil , err
141+ }
142+ return ae , nil
55143}
56144
57145// AlertEvent 事件具体数据
0 commit comments