83 lines
2.8 KiB
Go
83 lines
2.8 KiB
Go
// Package event define real time data evnet operation functions
|
|
package event
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"modelRT/logger"
|
|
"modelRT/mq"
|
|
)
|
|
|
|
type actionHandler func(ctx context.Context, content string, ops ...EventOption) 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, content string, ops ...EventOption) {
|
|
handler, exists := actionDispatchMap[command]
|
|
if !exists {
|
|
logger.Error(ctx, "unknown action command", "command", command)
|
|
return
|
|
}
|
|
err := handler(ctx, content, ops...)
|
|
if err != nil {
|
|
logger.Error(ctx, "action handler failed", "command", command, "content", content, "error", err)
|
|
return
|
|
}
|
|
logger.Info(ctx, "action handler success", "command", command, "content", content)
|
|
}
|
|
|
|
func handleInfoAction(ctx context.Context, content string, ops ...EventOption) error {
|
|
// 实际执行发送警告、记录日志等操作
|
|
actionParams := content
|
|
// ... logic to send info level event using actionParams ...
|
|
logger.Warn(ctx, "trigger info event", "message", actionParams)
|
|
return nil
|
|
}
|
|
|
|
func handleWarningAction(ctx context.Context, eventName string, ops ...EventOption) error {
|
|
eventRecord, err := NewWarnPlatformSoftRecord(eventName, ops...)
|
|
if err != nil {
|
|
logger.Error(ctx, "failed to create event record", "error", err)
|
|
return err
|
|
}
|
|
mq.MsgChan <- fmt.Sprintf("Generated event record: %+v", eventRecord)
|
|
logger.Info(ctx, "trigger warning event", "event_name", eventName)
|
|
return nil
|
|
}
|
|
|
|
func handleErrorAction(ctx context.Context, content string, ops ...EventOption) error {
|
|
actionParams := content
|
|
eventRecord, err := NewCriticalPlatformSoftRecord("ErrorEvent", WithCondition(map[string]any{"message": actionParams}))
|
|
if err != nil {
|
|
logger.Error(ctx, "failed to create event record", "error", err)
|
|
return err
|
|
}
|
|
mq.MsgChan <- fmt.Sprintf("Generated event record: %+v", eventRecord)
|
|
return nil
|
|
}
|
|
|
|
func handleCriticalAction(ctx context.Context, content string, ops ...EventOption) error {
|
|
// 实际执行发送警告、记录日志等操作
|
|
actionParams := content
|
|
// ... logic to send critical level event using actionParams ...
|
|
logger.Warn(ctx, "trigger critical event", "message", actionParams)
|
|
return nil
|
|
}
|
|
|
|
func handleExceptionAction(ctx context.Context, content string, ops ...EventOption) error {
|
|
// 实际执行发送警告、记录日志等操作
|
|
actionParams := content
|
|
// ... logic to send except level event using actionParams ...
|
|
logger.Warn(ctx, "trigger except event", "message", actionParams)
|
|
return nil
|
|
}
|