init event struct with option mode

This commit is contained in:
douxu 2026-02-24 17:08:48 +08:00
parent 56b9999d6b
commit 6c9da6fcd4
4 changed files with 124 additions and 21 deletions

View File

@ -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"
)

39
alert/event_options.go Normal file
View File

@ -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
}
}

68
alert/gen_event.go Normal file
View File

@ -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...)
}

View File

@ -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
)