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-08-05 15:20:07 +08:00
"modelRT/constants"
2025-01-22 16:38:46 +08:00
"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"
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
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-06-06 16:41:52 +08:00
logger . Error ( anchorChanConfig . Ctx , "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-08-15 16:25:48 +08:00
componentUUID := anchorParaConfig . ComponentUUID
2025-01-22 16:38:46 +08:00
anchorRealTimeDatas := anchorParaConfig . AnchorRealTimeData
2024-12-18 16:25:49 +08:00
2025-01-22 16:38:46 +08:00
for _ , value := range anchorRealTimeDatas {
2025-08-15 16:25:48 +08:00
anchorName , err := diagram . GetAnchorValue ( componentUUID )
2025-01-22 16:38:46 +08:00
if err != nil {
2025-08-15 16:25:48 +08:00
logger . Error ( anchorChanConfig . Ctx , "can not get anchor value from map by uuid" , "component_uuid" , componentUUID , "error" , err )
2025-01-22 16:38:46 +08:00
continue
}
2024-12-26 15:03:20 +08:00
2025-01-22 16:38:46 +08:00
if anchorName != anchorParaConfig . AnchorName {
2025-06-06 16:41:52 +08:00
logger . Error ( anchorChanConfig . Ctx , "anchor name not equal param config anchor value" , "map_anchor_name" , anchorName , "param_anchor_name" , anchorParaConfig . AnchorName )
2025-01-22 16:38:46 +08:00
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 {
2025-08-15 16:25:48 +08:00
ComponentUUID : componentUUID ,
AnchorName : anchorName ,
Level : constants . InfoAlertLevel ,
Message : message ,
StartTime : time . Now ( ) . Unix ( ) ,
2025-01-22 16:38:46 +08:00
}
alertManager . AddEvent ( event )
}
}
default :
}
}
2024-12-18 16:25:49 +08:00
}