2024-12-18 16:25:49 +08:00
// Package pool define concurrency call function in ants
package pool
import (
"fmt"
2025-01-22 16:38:46 +08:00
"time"
2024-12-18 16:25:49 +08:00
2025-01-22 16:38:46 +08:00
"modelRT/alert"
2024-12-18 16:25:49 +08:00
"modelRT/config"
2025-01-22 16:38:46 +08:00
"modelRT/constant"
"modelRT/diagram"
2024-12-26 15:03:20 +08:00
"modelRT/logger"
2025-01-22 16:38:46 +08:00
"github.com/panjf2000/ants/v2"
"go.uber.org/zap"
2024-12-18 16:25:49 +08:00
)
2025-01-22 16:38:46 +08:00
// AnchorRealTimePool define anchor param pool of real time data
var AnchorRealTimePool * ants . PoolWithFunc
// AnchorPoolInit define anchor param pool init
func AnchorPoolInit ( concurrentQuantity int ) ( pool * ants . PoolWithFunc , err error ) {
// init anchor param ants pool
AnchorRealTimePool , err = ants . NewPoolWithFunc ( concurrentQuantity , AnchorFunc )
if err != nil {
return nil , err
}
return AnchorRealTimePool , nil
}
2025-01-21 16:35:44 +08:00
// AnchorFunc defines func that process the real time data of component anchor params
2025-01-22 16:38:46 +08:00
var AnchorFunc = func ( poolConfig interface { } ) {
2025-01-21 16:35:44 +08:00
var firstStart bool
2024-12-26 15:03:20 +08:00
logger := logger . GetLoggerInstance ( )
2025-01-22 16:38:46 +08:00
alertManager := alert . GetAlertMangerInstance ( )
2024-12-18 16:25:49 +08:00
2025-01-22 16:38:46 +08:00
anchorChanConfig , ok := poolConfig . ( config . AnchorChanConfig )
2024-12-18 16:25:49 +08:00
if ! ok {
2025-01-21 16:35:44 +08:00
logger . Error ( "conversion component anchor chan type failed" )
2024-12-18 16:25:49 +08:00
return
}
2025-01-22 16:38:46 +08:00
2024-12-20 16:06:42 +08:00
for {
2025-01-21 16:35:44 +08:00
select {
case <- anchorChanConfig . Ctx . Done ( ) :
return
case anchorParaConfig := <- anchorChanConfig . AnchorChan :
if firstStart {
close ( anchorChanConfig . ReadyChan )
firstStart = false
2024-12-26 15:03:20 +08:00
}
2025-01-22 16:38:46 +08:00
componentID := anchorParaConfig . ComponentID
anchorRealTimeDatas := anchorParaConfig . AnchorRealTimeData
2024-12-18 16:25:49 +08:00
2025-01-22 16:38:46 +08:00
for _ , value := range anchorRealTimeDatas {
anchorName , err := diagram . GetAnchorValue ( componentID )
if err != nil {
logger . Error ( "can not get anchor value from map by uuid" , zap . Int64 ( "component_id" , componentID ) , zap . Error ( err ) )
continue
}
2024-12-26 15:03:20 +08:00
2025-01-22 16:38:46 +08:00
if anchorName != anchorParaConfig . AnchorName {
logger . Error ( "anchor name not equal param config anchor value" , zap . String ( "map_anchor_name" , anchorName ) , zap . String ( "param_anchor_name" , anchorParaConfig . AnchorName ) )
continue
}
2024-12-20 16:06:42 +08:00
2025-01-22 16:38:46 +08:00
upperLimitVal := anchorParaConfig . CompareValUpperLimit
lowerlimitVal := anchorParaConfig . CompareValLowerLimit
compareValue := anchorParaConfig . CalculateFunc ( value , anchorParaConfig . CalculateParams ... )
if compareValue > upperLimitVal || compareValue < lowerlimitVal {
message := fmt . Sprintf ( "anchor param %s value out of range, value:%f, upper limit:%f, lower limit:%f" , anchorName ,
compareValue , upperLimitVal , lowerlimitVal )
event := alert . Event {
ComponentID : componentID ,
AnchorName : anchorName ,
Level : constant . InfoAlertLevel ,
Message : message ,
StartTime : time . Now ( ) . Unix ( ) ,
}
alertManager . AddEvent ( event )
}
}
default :
}
}
2024-12-18 16:25:49 +08:00
}