83 lines
2.9 KiB
Go
83 lines
2.9 KiB
Go
// Package event define real time data evnet operation functions
|
|
package event
|
|
|
|
import (
|
|
"context"
|
|
|
|
"modelRT/common"
|
|
"modelRT/logger"
|
|
)
|
|
|
|
type actionHandler func(ctx context.Context, content string, ops ...EventOption) (*EventRecord, error)
|
|
|
|
// actionDispatchMap define variable to store all action handler into map
|
|
var actionDispatchMap = map[string]actionHandler{
|
|
"info": handleInfoAction,
|
|
"warning": handleWarningAction,
|
|
"error": handleErrorAction,
|
|
"critical": handleCriticalAction,
|
|
"exception": handleExceptionAction,
|
|
}
|
|
|
|
// TriggerEventAction define func to trigger event by action in compute config
|
|
func TriggerEventAction(ctx context.Context, command string, eventName string, ops ...EventOption) (*EventRecord, error) {
|
|
handler, exists := actionDispatchMap[command]
|
|
if !exists {
|
|
logger.Error(ctx, "unknown action command", "command", command)
|
|
return nil, common.ErrUnknowEventActionCommand
|
|
}
|
|
|
|
eventRecord, err := handler(ctx, eventName, ops...)
|
|
if err != nil {
|
|
logger.Error(ctx, "action event handler failed", "error", err)
|
|
return nil, common.ErrExecEventActionFailed
|
|
}
|
|
return eventRecord, nil
|
|
}
|
|
|
|
func handleInfoAction(ctx context.Context, eventName string, ops ...EventOption) (*EventRecord, error) {
|
|
logger.Info(ctx, "trigger info event", "event_name", eventName)
|
|
eventRecord, err := NewGeneralPlatformSoftRecord(eventName, ops...)
|
|
if err != nil {
|
|
logger.Error(ctx, "generate info event record failed", "error", err)
|
|
return nil, err
|
|
}
|
|
return eventRecord, nil
|
|
}
|
|
|
|
func handleWarningAction(ctx context.Context, eventName string, ops ...EventOption) (*EventRecord, error) {
|
|
logger.Info(ctx, "trigger warning event", "event_name", eventName)
|
|
eventRecord, err := NewWarnPlatformSoftRecord(eventName, ops...)
|
|
if err != nil {
|
|
logger.Error(ctx, "generate warning event record failed", "error", err)
|
|
return nil, err
|
|
}
|
|
return eventRecord, nil
|
|
}
|
|
|
|
func handleErrorAction(ctx context.Context, eventName string, ops ...EventOption) (*EventRecord, error) {
|
|
logger.Info(ctx, "trigger error event", "event_name", eventName)
|
|
eventRecord, err := NewCriticalPlatformSoftRecord(eventName, ops...)
|
|
if err != nil {
|
|
logger.Error(ctx, "generate error event record failed", "error", err)
|
|
return nil, err
|
|
}
|
|
return eventRecord, nil
|
|
}
|
|
|
|
func handleCriticalAction(ctx context.Context, content string, ops ...EventOption) (*EventRecord, error) {
|
|
// 实际执行发送警告、记录日志等操作
|
|
actionParams := content
|
|
// ... logic to send critical level event using actionParams ...
|
|
logger.Warn(ctx, "trigger critical event", "message", actionParams)
|
|
return nil, nil
|
|
}
|
|
|
|
func handleExceptionAction(ctx context.Context, content string, ops ...EventOption) (*EventRecord, error) {
|
|
// 实际执行发送警告、记录日志等操作
|
|
actionParams := content
|
|
// ... logic to send except level event using actionParams ...
|
|
logger.Warn(ctx, "trigger except event", "message", actionParams)
|
|
return nil, nil
|
|
}
|