optimize real time data computing api
This commit is contained in:
parent
329b4827f8
commit
10b91abee9
|
|
@ -71,6 +71,20 @@ func generateMixedData(highMin, lowMin, highBase, lowBase, baseValue, normalBase
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func generateNormalData(baseValue, normalBase float64) []float64 {
|
||||||
|
totalLength = 500
|
||||||
|
seed := time.Now().UnixNano()
|
||||||
|
source := rand.NewSource(seed)
|
||||||
|
r := rand.New(source)
|
||||||
|
|
||||||
|
data := make([]float64, totalLength)
|
||||||
|
for i := 0; i < totalLength; i++ {
|
||||||
|
change := normalBase - r.Float64()*normalBase*2
|
||||||
|
data[i] = baseValue + change
|
||||||
|
}
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
rootCtx := context.Background()
|
rootCtx := context.Background()
|
||||||
|
|
||||||
|
|
@ -103,63 +117,86 @@ func main() {
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
for key, measInfo := range measInfos {
|
for key, measInfo := range measInfos {
|
||||||
var highMin, highBase float64
|
randomType := selectRandomType()
|
||||||
var lowMin, lowBase float64
|
var datas []float64
|
||||||
var normalBase float64
|
if randomType {
|
||||||
|
// 生成正常数据
|
||||||
|
log.Printf("key:%s generate normal data type is %v\n", key, randomType)
|
||||||
|
baseValue := measInfo.BaseValue
|
||||||
|
changes := measInfo.Changes
|
||||||
|
normalBase := changes[0]
|
||||||
|
noramlMin := baseValue - normalBase
|
||||||
|
normalMax := baseValue + normalBase
|
||||||
|
datas = generateNormalData(baseValue, normalBase)
|
||||||
|
allTrue := true
|
||||||
|
|
||||||
// TODO 生成一次测试数据
|
for i := 0; i < totalLength-1; i++ {
|
||||||
changes := measInfo.Changes
|
value := datas[i]
|
||||||
baseValue := measInfo.BaseValue
|
// log.Printf("index:%d, value:%.2f\n", i, value)
|
||||||
if len(changes) == 2 {
|
if value < noramlMin && value > normalMax {
|
||||||
highMin = baseValue + changes[0]
|
|
||||||
lowMin = baseValue + changes[1]
|
|
||||||
highBase = changes[0]
|
|
||||||
lowBase = changes[1]
|
|
||||||
normalBase = changes[0]
|
|
||||||
} else {
|
|
||||||
randomIndex := selectRandomInt()
|
|
||||||
highMin = baseValue + changes[randomIndex]
|
|
||||||
lowMin = baseValue + changes[randomIndex+1]
|
|
||||||
highBase = changes[randomIndex]
|
|
||||||
lowBase = changes[randomIndex+1]
|
|
||||||
normalBase = changes[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
datas := generateMixedData(highMin, lowMin, highBase, lowBase, baseValue, normalBase)
|
|
||||||
// log.Printf("key:%s\n datas:%v\n", key, datas)
|
|
||||||
|
|
||||||
allHigh := true
|
|
||||||
for i := highStart; i < highEnd; i++ {
|
|
||||||
if datas[i] <= highMin {
|
|
||||||
allHigh = false
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
log.Printf("\n// 验证结果 (高值段在 %d-%d): 所有值是否 > %.2f? %t\n", highStart, highEnd-1, highMin, allHigh)
|
|
||||||
|
|
||||||
allLow := true
|
|
||||||
for i := lowStart; i < lowEnd; i++ {
|
|
||||||
if datas[i] >= lowMin {
|
|
||||||
allLow = false
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
log.Printf("// 验证结果 (低值段在 %d-%d): 所有值是否 < %.2f? %t\n", lowStart, lowEnd-1, lowMin, allLow)
|
|
||||||
|
|
||||||
allTrue := true
|
|
||||||
for i := 0; i < totalLength-1; i++ {
|
|
||||||
value := datas[i]
|
|
||||||
if i < highStart || (i >= highEnd && i < lowStart) || i >= lowEnd {
|
|
||||||
log.Printf("index:%d, value:%.2f\n", i, value)
|
|
||||||
if value >= highMin && value <= lowMin {
|
|
||||||
allTrue = false
|
allTrue = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
log.Printf("// 验证结果: 所有值是否 >= %.2f或 <= %.2f %t\n", noramlMin, normalMax, allTrue)
|
||||||
|
} else {
|
||||||
|
// 生成异常数据
|
||||||
|
log.Printf("key:%s generate abnormal data type is %v\n", key, randomType)
|
||||||
|
var highMin, highBase float64
|
||||||
|
var lowMin, lowBase float64
|
||||||
|
var normalBase float64
|
||||||
|
|
||||||
|
// TODO 生成一次测试数据
|
||||||
|
changes := measInfo.Changes
|
||||||
|
baseValue := measInfo.BaseValue
|
||||||
|
if len(changes) == 2 {
|
||||||
|
highMin = baseValue + changes[0]
|
||||||
|
lowMin = baseValue + changes[1]
|
||||||
|
highBase = changes[0]
|
||||||
|
lowBase = changes[1]
|
||||||
|
normalBase = changes[0]
|
||||||
|
} else {
|
||||||
|
randomIndex := selectRandomInt()
|
||||||
|
highMin = baseValue + changes[randomIndex]
|
||||||
|
lowMin = baseValue + changes[randomIndex+1]
|
||||||
|
highBase = changes[randomIndex]
|
||||||
|
lowBase = changes[randomIndex+1]
|
||||||
|
normalBase = changes[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
datas = generateMixedData(highMin, lowMin, highBase, lowBase, baseValue, normalBase)
|
||||||
|
// log.Printf("key:%s\n datas:%v\n", key, datas)
|
||||||
|
|
||||||
|
allHigh := true
|
||||||
|
for i := highStart; i < highEnd; i++ {
|
||||||
|
if datas[i] <= highMin {
|
||||||
|
allHigh = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log.Printf("// 验证结果 (高值段在 %d-%d): 所有值是否 > %.2f? %t\n", highStart, highEnd-1, highMin, allHigh)
|
||||||
|
|
||||||
|
allLow := true
|
||||||
|
for i := lowStart; i < lowEnd; i++ {
|
||||||
|
if datas[i] >= lowMin {
|
||||||
|
allLow = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log.Printf("// 验证结果 (低值段在 %d-%d): 所有值是否 < %.2f? %t\n", lowStart, lowEnd-1, lowMin, allLow)
|
||||||
|
|
||||||
|
allTrue := true
|
||||||
|
for i := 0; i < totalLength-1; i++ {
|
||||||
|
value := datas[i]
|
||||||
|
if i < highStart || (i >= highEnd && i < lowStart) || i >= lowEnd {
|
||||||
|
// log.Printf("index:%d, value:%.2f\n", i, value)
|
||||||
|
if value >= highMin && value <= lowMin {
|
||||||
|
allTrue = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log.Printf("// 验证结果 (正常段在 %d-%d): 所有值是否 <= %.2f或>= %.2f %t\n", 0, totalLength-1, highMin, lowMin, allTrue)
|
||||||
}
|
}
|
||||||
log.Printf("// 验证结果 (正常段在 %d-%d): 所有值是否 <= %.2f或>= %.2f %t\n", 0, totalLength-1, highMin, lowMin, allTrue)
|
|
||||||
|
|
||||||
log.Printf("启动数据写入程序, Redis Key: %s, 基准值: %.4f, 变化范围: %+v\n", key, measInfo.BaseValue, measInfo.Changes)
|
log.Printf("启动数据写入程序, Redis Key: %s, 基准值: %.4f, 变化范围: %+v\n", key, measInfo.BaseValue, measInfo.Changes)
|
||||||
|
|
||||||
pipe := globalRedisClient.Pipeline()
|
pipe := globalRedisClient.Pipeline()
|
||||||
redisZs := make([]redis.Z, 0, totalLength)
|
redisZs := make([]redis.Z, 0, totalLength)
|
||||||
currentTime := time.Now().UnixNano()
|
currentTime := time.Now().UnixNano()
|
||||||
|
|
@ -179,3 +216,9 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func selectRandomType() bool {
|
||||||
|
options := []int{0, 2}
|
||||||
|
randomValue := rand.Intn(len(options))
|
||||||
|
return randomValue != 0
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
// Package util provide some utility fun
|
||||||
package util
|
package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
@ -7,6 +8,7 @@ import (
|
||||||
"github.com/redis/go-redis/v9"
|
"github.com/redis/go-redis/v9"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// InitRedisClient define func to initialize and return a redis client
|
||||||
func InitRedisClient(redisAddr string) *redis.Client {
|
func InitRedisClient(redisAddr string) *redis.Client {
|
||||||
rdb := redis.NewClient(&redis.Options{
|
rdb := redis.NewClient(&redis.Options{
|
||||||
Addr: redisAddr,
|
Addr: redisAddr,
|
||||||
|
|
|
||||||
|
|
@ -126,7 +126,7 @@ func analyzeTEDataLogic(ctx context.Context, conf *ComputeConfig, thresholds teE
|
||||||
}
|
}
|
||||||
|
|
||||||
if eventTriggered {
|
if eventTriggered {
|
||||||
command, content := genTEEventCommandAndContent(conf.Action)
|
command, content := genTEEventCommandAndContent(ctx, conf.Action)
|
||||||
// TODO 考虑 content 是否可以为空,先期不允许
|
// TODO 考虑 content 是否可以为空,先期不允许
|
||||||
if command == "" || content == "" {
|
if command == "" || content == "" {
|
||||||
logger.Error(ctx, "generate telemetry evnet command or content failed", "action", conf.Action, "command", command, "content", content)
|
logger.Error(ctx, "generate telemetry evnet command or content failed", "action", conf.Action, "command", command, "content", content)
|
||||||
|
|
@ -137,25 +137,29 @@ func analyzeTEDataLogic(ctx context.Context, conf *ComputeConfig, thresholds teE
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func genTEEventCommandAndContent(action map[string]any) (command string, content string) {
|
func genTEEventCommandAndContent(ctx context.Context, action map[string]any) (command string, content string) {
|
||||||
cmdValue, exist := action["command"]
|
cmdValue, exist := action["command"]
|
||||||
if !exist {
|
if !exist {
|
||||||
|
logger.Error(ctx, "can not find command variable into action map", "action", action)
|
||||||
return "", ""
|
return "", ""
|
||||||
}
|
}
|
||||||
|
|
||||||
commandStr, ok := cmdValue.(string)
|
commandStr, ok := cmdValue.(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
logger.Error(ctx, "convert command to string type failed", "command", cmdValue, "type", fmt.Sprintf("%T", cmdValue))
|
||||||
return "", ""
|
return "", ""
|
||||||
}
|
}
|
||||||
command = commandStr
|
command = commandStr
|
||||||
|
|
||||||
paramsValue, exist := action["parametes"]
|
paramsValue, exist := action["parameters"]
|
||||||
if !exist {
|
if !exist {
|
||||||
|
logger.Error(ctx, "can not find parameters variable into action map", "action", action)
|
||||||
return command, ""
|
return command, ""
|
||||||
}
|
}
|
||||||
|
|
||||||
parameterSlice, ok := paramsValue.([]string)
|
parameterSlice, ok := paramsValue.([]any)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
logger.Error(ctx, "convert parameters to []any type failed", "parameters", paramsValue, "type", fmt.Sprintf("%T", paramsValue))
|
||||||
return command, ""
|
return command, ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -164,7 +168,12 @@ func genTEEventCommandAndContent(action map[string]any) (command string, content
|
||||||
if i > 0 {
|
if i > 0 {
|
||||||
builder.WriteString(",")
|
builder.WriteString(",")
|
||||||
}
|
}
|
||||||
builder.WriteString(parameter)
|
parameterStr, ok := parameter.(string)
|
||||||
|
if !ok {
|
||||||
|
logger.Warn(ctx, "parameter type is incorrect, skip this parameter", "parameter", parameter, "type", fmt.Sprintf("%T", parameter))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
builder.WriteString(parameterStr)
|
||||||
}
|
}
|
||||||
|
|
||||||
return command, builder.String()
|
return command, builder.String()
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,7 @@ func handleWarningAction(ctx context.Context, content string) error {
|
||||||
// 实际执行发送警告、记录日志等操作
|
// 实际执行发送警告、记录日志等操作
|
||||||
actionParams := content
|
actionParams := content
|
||||||
// ... logic to send warning level event using actionParams ...
|
// ... logic to send warning level event using actionParams ...
|
||||||
logger.Warn(ctx, "trigger Warning event", "message", actionParams)
|
logger.Warn(ctx, "trigger warning event", "message", actionParams)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue