From 6c9da6fcd4c475d3243cd427ef1cd8f3926863ad Mon Sep 17 00:00:00 2001 From: douxu Date: Tue, 24 Feb 2026 17:08:48 +0800 Subject: [PATCH] init event struct with option mode --- alert/event.go | 23 ++------------ alert/event_options.go | 39 ++++++++++++++++++++++++ alert/gen_event.go | 68 ++++++++++++++++++++++++++++++++++++++++++ constants/event.go | 15 ++++++++++ 4 files changed, 124 insertions(+), 21 deletions(-) create mode 100644 alert/event_options.go create mode 100644 alert/gen_event.go diff --git a/alert/event.go b/alert/event.go index f580489..ca2ea81 100644 --- a/alert/event.go +++ b/alert/event.go @@ -4,7 +4,7 @@ package alert // EventRecord define struct for CIM event record type EventRecord struct { // 事件名称 - Event string `json:"event"` + EventName string `json:"event"` // 事件唯一标识符 EventUUID string `json:"event_uuid"` // 事件类型 @@ -28,7 +28,7 @@ type EventRecord struct { // 操作历史记录 (CIM ActivityRecord) Operations []OperationRecord `json:"operations"` // 子站告警原始数据 (CIM Alarm 数据) - Alarm map[string]any `json:"alarm,omitempty"` + Origin map[string]any `json:"origin,omitempty"` } // OperationRecord 描述对事件的操作记录,如确认(acknowledgment)等 @@ -37,22 +37,3 @@ type OperationRecord struct { Op string `json:"op"` // 操作人/操作账号标识 TS int64 `json:"ts"` // 操作发生的毫秒时间戳 } - -// 定义事件类型常量,便于逻辑判断 -const ( - TypeGeneralHard = 0 - TypeGeneralPlatformSoft = 1 - TypeGeneralApplicationSoft = 2 - TypeWarnHard = 3 - TypeWarnPlatformSoft = 4 - TypeWarnApplicationSoft = 5 - TypeCriticalHard = 6 - TypeCriticalPlatformSoft = 7 - TypeCriticalApplicationSoft = 8 -) - -const ( - FromStation = "station" - FromPlatform = "platform" - FromMSA = "msa" -) diff --git a/alert/event_options.go b/alert/event_options.go new file mode 100644 index 0000000..e114222 --- /dev/null +++ b/alert/event_options.go @@ -0,0 +1,39 @@ +// Package alert define alert event struct of modelRT project +package alert + +// EventOption 定义选项函数的类型 +type EventOption func(*EventRecord) + +// WithCondition 设置事件场景描述 +func WithCondition(cond map[string]any) EventOption { + return func(e *EventRecord) { + if cond != nil { + e.Condition = cond + } + } +} + +// WithSubscriptions 设置订阅信息 +func WithSubscriptions(subs []any) EventOption { + return func(e *EventRecord) { + if subs != nil { + e.AttachedSubscriptions = subs + } + } +} + +// WithOperations 设置操作记录 +func WithOperations(ops []OperationRecord) EventOption { + return func(e *EventRecord) { + if ops != nil { + e.Operations = ops + } + } +} + +// WithCategory 设置可选分类 +func WithCategory(cat string) EventOption { + return func(e *EventRecord) { + e.Category = cat + } +} diff --git a/alert/gen_event.go b/alert/gen_event.go new file mode 100644 index 0000000..d8b467e --- /dev/null +++ b/alert/gen_event.go @@ -0,0 +1,68 @@ +// Package alert define alert event struct of modelRT project +package alert + +import ( + "fmt" + "time" + + "modelRT/constants" + + "github.com/gofrs/uuid" +) + +// NewPlatformEventRecord define func to create a new platform event record with common fields initialized +func NewPlatformEventRecord(eventType int, priority int, eventName string, opts ...EventOption) (*EventRecord, error) { + u, err := uuid.NewV4() + if err != nil { + return nil, fmt.Errorf("failed to generate UUID: %w", err) + } + + record := &EventRecord{ + EventName: eventName, + EventUUID: u.String(), + Type: eventType, + Priority: priority, + Status: 1, + From: constants.EventFromPlatform, + Timestamp: time.Now().UnixNano() / int64(time.Millisecond), + Condition: make(map[string]any), + AttachedSubscriptions: make([]any, 0), + Operations: make([]OperationRecord, 0), + } + + for _, opt := range opts { + opt(record) + } + + return record, nil +} + +// NewGeneralPlatformSoftRecord define func to create a new general platform software event record +func NewGeneralPlatformSoftRecord(name string, opts ...EventOption) (*EventRecord, error) { + return NewPlatformEventRecord(int(constants.EventGeneralPlatformSoft), 0, name, opts...) +} + +// NewGeneralApplicationSoftRecord define func to create a new general application software event record +func NewGeneralApplicationSoftRecord(name string, opts ...EventOption) (*EventRecord, error) { + return NewPlatformEventRecord(int(constants.EventGeneralApplicationSoft), 0, name, opts...) +} + +// NewWarnPlatformSoftRecord define func to create a new warning platform software event record +func NewWarnPlatformSoftRecord(name string, opts ...EventOption) (*EventRecord, error) { + return NewPlatformEventRecord(int(constants.EventWarnPlatformSoft), 3, name, opts...) +} + +// NewWarnApplicationSoftRecord define func to create a new warning application software event record +func NewWarnApplicationSoftRecord(name string, opts ...EventOption) (*EventRecord, error) { + return NewPlatformEventRecord(int(constants.EventWarnApplicationSoft), 3, name, opts...) +} + +// NewCriticalPlatformSoftRecord define func to create a new critical platform software event record +func NewCriticalPlatformSoftRecord(name string, opts ...EventOption) (*EventRecord, error) { + return NewPlatformEventRecord(int(constants.EventCriticalPlatformSoft), 6, name, opts...) +} + +// NewCriticalApplicationSoftRecord define func to create a new critical application software event record +func NewCriticalApplicationSoftRecord(name string, opts ...EventOption) (*EventRecord, error) { + return NewPlatformEventRecord(int(constants.EventCriticalApplicationSoft), 6, name, opts...) +} diff --git a/constants/event.go b/constants/event.go index e3c3560..c9bc90d 100644 --- a/constants/event.go +++ b/constants/event.go @@ -48,3 +48,18 @@ const ( // EventFromOthers define event from others type EventFromOthers = "others" ) + +const ( + // EventStatusHappended define status for event record when event just happened, no data attached yet + EventStatusHappended = iota + // EventStatusDataAttached define status for event record when event just happened, data attached already + EventStatusDataAttached + // EventStatusReported define status for event record when event reported to CIM, no matter it's successful or failed + EventStatusReported + // EventStatusConfirmed define status for event record when event confirmed by CIM, no matter it's successful or failed + EventStatusConfirmed + // EventStatusPersisted define status for event record when event persisted in database, no matter it's successful or failed + EventStatusPersisted + // EventStatusClosed define status for event record when event closed, no matter it's successful or failed + EventStatusClosed +)