2025-11-03 17:35:03 +08:00
|
|
|
// Package handler provides HTTP handlers for various endpoints.
|
|
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
import (
|
2025-11-04 17:12:15 +08:00
|
|
|
"context"
|
2025-11-03 17:35:03 +08:00
|
|
|
"fmt"
|
2025-11-11 17:37:06 +08:00
|
|
|
"maps"
|
2025-11-03 17:35:03 +08:00
|
|
|
"net/http"
|
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
|
|
"modelRT/constants"
|
2025-11-05 18:20:54 +08:00
|
|
|
"modelRT/database"
|
2025-11-03 17:35:03 +08:00
|
|
|
"modelRT/logger"
|
|
|
|
|
"modelRT/network"
|
2025-11-10 17:32:18 +08:00
|
|
|
"modelRT/orm"
|
2025-11-03 17:35:03 +08:00
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"github.com/gofrs/uuid"
|
2025-11-05 18:20:54 +08:00
|
|
|
"gorm.io/gorm"
|
2025-11-03 17:35:03 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var globalMonitorState *SharedMonitorState
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
globalMonitorState = NewSharedMonitorState()
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-08 17:11:07 +08:00
|
|
|
// RealTimeSubHandler define real time data subscriptions process API
|
|
|
|
|
// @Summary 开始或结束订阅实时数据
|
2025-11-03 17:35:03 +08:00
|
|
|
// @Description 根据用户输入的组件token,从 modelRT 服务中开始或结束对于实时数据的监控
|
|
|
|
|
// @Tags RealTime Component
|
|
|
|
|
// @Accept json
|
|
|
|
|
// @Produce json
|
2025-11-06 17:22:14 +08:00
|
|
|
// @Param request body network.MeasurementRecommendRequest true "查询输入参数,例如 'trans' 或 'transformfeeder1_220.'"
|
|
|
|
|
// @Success 200 {object} network.SuccessResponse{payload=network.RealTimeMonitorPayload} "订阅实时数据结果列表"
|
|
|
|
|
//
|
|
|
|
|
// @Example 200 {
|
|
|
|
|
// "code": 200,
|
|
|
|
|
// "msg": "success",
|
|
|
|
|
// "payload": {
|
|
|
|
|
// "targets": [
|
|
|
|
|
// {
|
|
|
|
|
// "id": "grid1.zone1.station1.ns1.tag1.transformfeeder1_220.I_A_rms",
|
|
|
|
|
// "code": "1001",
|
|
|
|
|
// "msg": "subscription success"
|
|
|
|
|
// },
|
|
|
|
|
// {
|
|
|
|
|
// "id": "grid1.zone1.station1.ns1.tag1.transformfeeder1_220.I_B_rms",
|
|
|
|
|
// "code": "1002",
|
|
|
|
|
// "msg": "subscription failed"
|
|
|
|
|
// }
|
|
|
|
|
// ]
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// @Failure 400 {object} network.FailureResponse{payload=network.RealTimeMonitorPayload} "订阅实时数据结果列表"
|
|
|
|
|
//
|
|
|
|
|
// @Example 400 {
|
|
|
|
|
// "code": 400,
|
|
|
|
|
// "msg": "failed to get recommend data from redis",
|
|
|
|
|
// "payload": {
|
|
|
|
|
// "targets": [
|
|
|
|
|
// {
|
|
|
|
|
// "id": "grid1.zone1.station1.ns1.tag1.transformfeeder1_220.I_A_rms",
|
|
|
|
|
// "code": "1002",
|
|
|
|
|
// "msg": "subscription failed"
|
|
|
|
|
// },
|
|
|
|
|
// {
|
|
|
|
|
// "id": "grid1.zone1.station1.ns1.tag1.transformfeeder1_220.I_B_rms",
|
|
|
|
|
// "code": "1002",
|
|
|
|
|
// "msg": "subscription failed"
|
|
|
|
|
// }
|
|
|
|
|
// ]
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//
|
2025-11-08 17:11:07 +08:00
|
|
|
// @Router /monitors/data/subscriptions [post]
|
|
|
|
|
func RealTimeSubHandler(c *gin.Context) {
|
2025-11-06 17:22:14 +08:00
|
|
|
var request network.RealTimeSubRequest
|
2025-11-03 17:35:03 +08:00
|
|
|
var monitorAction string
|
2025-11-11 17:37:06 +08:00
|
|
|
var clientID string
|
2025-11-03 17:35:03 +08:00
|
|
|
|
|
|
|
|
if err := c.ShouldBindJSON(&request); err != nil {
|
|
|
|
|
logger.Error(c, "failed to unmarshal real time query request", "error", err)
|
|
|
|
|
c.JSON(http.StatusOK, network.FailureResponse{
|
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
|
Msg: err.Error(),
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-08 17:11:07 +08:00
|
|
|
if request.Action == constants.MonitorStartAction && request.ClientID == "" {
|
2025-11-03 17:35:03 +08:00
|
|
|
monitorAction = request.Action
|
|
|
|
|
id, err := uuid.NewV4()
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Error(c, "failed to generate monitor id", "error", err)
|
|
|
|
|
c.JSON(http.StatusOK, network.FailureResponse{
|
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
|
Msg: err.Error(),
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-11-11 17:37:06 +08:00
|
|
|
clientID = id.String()
|
2025-11-08 17:11:07 +08:00
|
|
|
} else if request.Action == constants.MonitorStartAction && request.ClientID != "" {
|
2025-11-03 17:35:03 +08:00
|
|
|
monitorAction = constants.MonitorAppendAction
|
2025-11-11 17:37:06 +08:00
|
|
|
clientID = request.ClientID
|
2025-11-08 17:11:07 +08:00
|
|
|
} else if request.Action == constants.MonitorStopAction && request.ClientID != "" {
|
2025-11-03 17:35:03 +08:00
|
|
|
monitorAction = request.Action
|
2025-11-11 17:37:06 +08:00
|
|
|
clientID = request.ClientID
|
2025-11-03 17:35:03 +08:00
|
|
|
}
|
|
|
|
|
|
2025-11-05 18:20:54 +08:00
|
|
|
pgClient := database.GetPostgresDBClient()
|
|
|
|
|
// open transaction
|
|
|
|
|
tx := pgClient.Begin()
|
|
|
|
|
defer tx.Commit()
|
|
|
|
|
|
2025-11-03 17:35:03 +08:00
|
|
|
switch monitorAction {
|
|
|
|
|
case constants.MonitorStartAction:
|
2025-11-11 17:37:06 +08:00
|
|
|
results, err := globalMonitorState.CreateConfig(c, tx, clientID, request.Components)
|
2025-11-04 17:12:15 +08:00
|
|
|
if err != nil {
|
|
|
|
|
logger.Error(c, "create real time data monitor config failed", "error", err)
|
|
|
|
|
c.JSON(http.StatusOK, network.FailureResponse{
|
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
|
Msg: err.Error(),
|
2025-11-12 17:34:18 +08:00
|
|
|
Payload: network.RealTimeSubPayload{
|
2025-11-11 17:37:06 +08:00
|
|
|
ClientID: clientID,
|
2025-11-06 17:22:14 +08:00
|
|
|
TargetResults: results,
|
|
|
|
|
},
|
2025-11-04 17:12:15 +08:00
|
|
|
})
|
|
|
|
|
return
|
2025-11-03 17:35:03 +08:00
|
|
|
}
|
2025-11-04 17:12:15 +08:00
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, network.SuccessResponse{
|
|
|
|
|
Code: http.StatusOK,
|
|
|
|
|
Msg: "success",
|
2025-11-12 17:34:18 +08:00
|
|
|
Payload: network.RealTimeSubPayload{
|
2025-11-11 17:37:06 +08:00
|
|
|
ClientID: clientID,
|
2025-11-05 18:20:54 +08:00
|
|
|
TargetResults: results,
|
2025-11-04 17:12:15 +08:00
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
return
|
2025-11-03 17:35:03 +08:00
|
|
|
case constants.MonitorStopAction:
|
2025-11-11 17:37:06 +08:00
|
|
|
results, err := globalMonitorState.RemoveTargets(c, clientID, request.Components)
|
2025-11-06 17:22:14 +08:00
|
|
|
if err != nil {
|
|
|
|
|
logger.Error(c, "remove target to real time data monitor config failed", "error", err)
|
|
|
|
|
c.JSON(http.StatusOK, network.FailureResponse{
|
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
|
Msg: err.Error(),
|
2025-11-12 17:34:18 +08:00
|
|
|
Payload: network.RealTimeSubPayload{
|
2025-11-11 17:37:06 +08:00
|
|
|
ClientID: clientID,
|
2025-11-06 17:22:14 +08:00
|
|
|
TargetResults: results,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, network.SuccessResponse{
|
|
|
|
|
Code: http.StatusOK,
|
|
|
|
|
Msg: "success",
|
2025-11-12 17:34:18 +08:00
|
|
|
Payload: network.RealTimeSubPayload{
|
2025-11-11 17:37:06 +08:00
|
|
|
ClientID: clientID,
|
2025-11-06 17:22:14 +08:00
|
|
|
TargetResults: results,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
return
|
2025-11-03 17:35:03 +08:00
|
|
|
case constants.MonitorAppendAction:
|
2025-11-11 17:37:06 +08:00
|
|
|
results, err := globalMonitorState.AppendTargets(c, tx, clientID, request.Components)
|
2025-11-04 17:12:15 +08:00
|
|
|
if err != nil {
|
|
|
|
|
logger.Error(c, "append target to real time data monitor config failed", "error", err)
|
|
|
|
|
c.JSON(http.StatusOK, network.FailureResponse{
|
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
|
Msg: err.Error(),
|
2025-11-12 17:34:18 +08:00
|
|
|
Payload: network.RealTimeSubPayload{
|
2025-11-11 17:37:06 +08:00
|
|
|
ClientID: clientID,
|
2025-11-06 17:22:14 +08:00
|
|
|
TargetResults: results,
|
|
|
|
|
},
|
2025-11-04 17:12:15 +08:00
|
|
|
})
|
|
|
|
|
return
|
2025-11-03 17:35:03 +08:00
|
|
|
}
|
2025-11-06 17:22:14 +08:00
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, network.SuccessResponse{
|
|
|
|
|
Code: http.StatusOK,
|
|
|
|
|
Msg: "success",
|
2025-11-12 17:34:18 +08:00
|
|
|
Payload: network.RealTimeSubPayload{
|
2025-11-11 17:37:06 +08:00
|
|
|
ClientID: clientID,
|
2025-11-06 17:22:14 +08:00
|
|
|
TargetResults: results,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
return
|
2025-11-03 17:35:03 +08:00
|
|
|
default:
|
2025-11-06 17:22:14 +08:00
|
|
|
err := fmt.Errorf("%w: request action is %s", constants.ErrUnsupportedAction, request.Action)
|
2025-11-03 17:35:03 +08:00
|
|
|
logger.Error(c, "unsupported action of real time data monitor request", "error", err)
|
2025-11-06 17:22:14 +08:00
|
|
|
requestTargetsCount := processRealTimeRequestCount(request.Components)
|
|
|
|
|
results := processRealTimeRequestTargets(request.Components, requestTargetsCount, err)
|
2025-11-03 17:35:03 +08:00
|
|
|
c.JSON(http.StatusOK, network.FailureResponse{
|
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
|
Msg: err.Error(),
|
2025-11-12 17:34:18 +08:00
|
|
|
Payload: network.RealTimeSubPayload{
|
2025-11-11 17:37:06 +08:00
|
|
|
ClientID: clientID,
|
2025-11-06 17:22:14 +08:00
|
|
|
TargetResults: results,
|
|
|
|
|
},
|
2025-11-03 17:35:03 +08:00
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RealTimeMonitorComponent define struct of real time monitor component
|
|
|
|
|
type RealTimeMonitorComponent struct {
|
2025-11-06 17:22:14 +08:00
|
|
|
targets []string
|
2025-11-10 17:32:18 +08:00
|
|
|
targetParam map[string]*orm.Measurement
|
2025-11-03 17:35:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RealTimeMonitorConfig define struct of real time monitor config
|
|
|
|
|
type RealTimeMonitorConfig struct {
|
2025-11-10 17:32:18 +08:00
|
|
|
noticeChan chan *transportTargets
|
2025-11-11 17:37:06 +08:00
|
|
|
mutex sync.RWMutex
|
2025-11-03 17:35:03 +08:00
|
|
|
components map[string]*RealTimeMonitorComponent
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SharedMonitorState define struct of shared monitor state with mutex
|
|
|
|
|
type SharedMonitorState struct {
|
2025-11-11 17:37:06 +08:00
|
|
|
monitorMap map[string]*RealTimeMonitorConfig
|
|
|
|
|
globalMutex sync.RWMutex
|
2025-11-03 17:35:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewSharedMonitorState define function to create new SharedMonitorState
|
|
|
|
|
func NewSharedMonitorState() *SharedMonitorState {
|
|
|
|
|
return &SharedMonitorState{
|
|
|
|
|
monitorMap: make(map[string]*RealTimeMonitorConfig),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-11 17:37:06 +08:00
|
|
|
// processAndValidateTargets define func to perform all database I/O operations in a lock-free state (eg,ParseDataIdentifierToken)
|
|
|
|
|
func processAndValidateTargets(ctx context.Context, tx *gorm.DB, components []network.RealTimeComponentItem, allReqTargetNum int) (
|
|
|
|
|
[]network.TargetResult,
|
|
|
|
|
map[string]*RealTimeMonitorComponent,
|
|
|
|
|
[]string,
|
|
|
|
|
) {
|
|
|
|
|
targetProcessResults := make([]network.TargetResult, 0, allReqTargetNum)
|
|
|
|
|
newComponentsMap := make(map[string]*RealTimeMonitorComponent)
|
|
|
|
|
successfulTargets := make([]string, 0, allReqTargetNum)
|
2025-11-04 17:12:15 +08:00
|
|
|
|
2025-11-05 18:20:54 +08:00
|
|
|
for _, componentItem := range components {
|
|
|
|
|
interval := componentItem.Interval
|
2025-11-11 17:37:06 +08:00
|
|
|
|
2025-11-05 18:20:54 +08:00
|
|
|
for _, target := range componentItem.Targets {
|
|
|
|
|
var targetResult network.TargetResult
|
|
|
|
|
targetResult.ID = target
|
2025-11-06 17:22:14 +08:00
|
|
|
targetModel, err := database.ParseDataIdentifierToken(ctx, tx, target)
|
2025-11-05 18:20:54 +08:00
|
|
|
if err != nil {
|
|
|
|
|
logger.Error(ctx, "parse data indentity token failed", "error", err, "identity_token", target)
|
|
|
|
|
targetResult.Code = constants.SubFailedCode
|
|
|
|
|
targetResult.Msg = fmt.Sprintf("%s: %s", constants.SubFailedMsg, err.Error())
|
|
|
|
|
targetProcessResults = append(targetProcessResults, targetResult)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
targetResult.Code = constants.SubSuccessCode
|
|
|
|
|
targetResult.Msg = constants.SubSuccessMsg
|
2025-11-06 17:22:14 +08:00
|
|
|
targetProcessResults = append(targetProcessResults, targetResult)
|
2025-11-11 17:37:06 +08:00
|
|
|
successfulTargets = append(successfulTargets, target)
|
|
|
|
|
|
|
|
|
|
if _, ok := newComponentsMap[interval]; !ok {
|
|
|
|
|
newComponentsMap[interval] = &RealTimeMonitorComponent{
|
|
|
|
|
targets: make([]string, 0, len(componentItem.Targets)),
|
|
|
|
|
targetParam: make(map[string]*orm.Measurement),
|
2025-11-05 18:20:54 +08:00
|
|
|
}
|
|
|
|
|
}
|
2025-11-11 17:37:06 +08:00
|
|
|
|
|
|
|
|
comp := newComponentsMap[interval]
|
|
|
|
|
comp.targets = append(comp.targets, target)
|
|
|
|
|
comp.targetParam[target] = targetModel.GetMeasurementInfo()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return targetProcessResults, newComponentsMap, successfulTargets
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// mergeComponents define func to merge newComponentsMap into existingComponentsMap
|
|
|
|
|
func mergeComponents(existingComponents map[string]*RealTimeMonitorComponent, newComponents map[string]*RealTimeMonitorComponent) {
|
|
|
|
|
for interval, newComp := range newComponents {
|
|
|
|
|
if existingComp, ok := existingComponents[interval]; ok {
|
|
|
|
|
existingComp.targets = append(existingComp.targets, newComp.targets...)
|
|
|
|
|
maps.Copy(existingComp.targetParam, newComp.targetParam)
|
|
|
|
|
} else {
|
|
|
|
|
existingComponents[interval] = newComp
|
2025-11-04 17:12:15 +08:00
|
|
|
}
|
|
|
|
|
}
|
2025-11-11 17:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CreateConfig define function to create config in SharedMonitorState
|
|
|
|
|
func (s *SharedMonitorState) CreateConfig(ctx context.Context, tx *gorm.DB, clientID string, components []network.RealTimeComponentItem) ([]network.TargetResult, error) {
|
|
|
|
|
requestTargetsCount := processRealTimeRequestCount(components)
|
|
|
|
|
targetProcessResults, newComponentsMap, _ := processAndValidateTargets(ctx, tx, components, requestTargetsCount)
|
|
|
|
|
|
|
|
|
|
s.globalMutex.Lock()
|
|
|
|
|
if _, exist := s.monitorMap[clientID]; exist {
|
|
|
|
|
s.globalMutex.Unlock()
|
|
|
|
|
err := fmt.Errorf("clientID %s already exists. use AppendTargets to modify existing config", clientID)
|
|
|
|
|
logger.Error(ctx, "clientID already exists. use AppendTargets to modify existing config", "error", err)
|
|
|
|
|
return targetProcessResults, err
|
|
|
|
|
}
|
2025-11-04 17:12:15 +08:00
|
|
|
|
2025-11-11 17:37:06 +08:00
|
|
|
config := &RealTimeMonitorConfig{
|
|
|
|
|
noticeChan: make(chan *transportTargets),
|
|
|
|
|
components: newComponentsMap, // 直接使用预构建的 Map
|
|
|
|
|
}
|
|
|
|
|
s.monitorMap[clientID] = config
|
|
|
|
|
s.globalMutex.Unlock()
|
2025-11-05 18:20:54 +08:00
|
|
|
return targetProcessResults, nil
|
2025-11-03 17:35:03 +08:00
|
|
|
}
|
|
|
|
|
|
2025-11-04 17:12:15 +08:00
|
|
|
// AppendTargets define function to append targets in SharedMonitorState
|
2025-11-11 17:37:06 +08:00
|
|
|
func (s *SharedMonitorState) AppendTargets(ctx context.Context, tx *gorm.DB, clientID string, components []network.RealTimeComponentItem) ([]network.TargetResult, error) {
|
2025-11-06 17:22:14 +08:00
|
|
|
requestTargetsCount := processRealTimeRequestCount(components)
|
|
|
|
|
targetProcessResults := make([]network.TargetResult, 0, requestTargetsCount)
|
|
|
|
|
|
2025-11-11 17:37:06 +08:00
|
|
|
s.globalMutex.RLock()
|
|
|
|
|
config, exist := s.monitorMap[clientID]
|
2025-11-04 17:12:15 +08:00
|
|
|
if !exist {
|
2025-11-11 17:37:06 +08:00
|
|
|
s.globalMutex.RUnlock()
|
|
|
|
|
err := fmt.Errorf("clientID %s not found. use CreateConfig to start a new config", clientID)
|
|
|
|
|
logger.Error(ctx, "clientID not found. use CreateConfig to start a new config", "error", err)
|
2025-11-06 17:22:14 +08:00
|
|
|
return processRealTimeRequestTargets(components, requestTargetsCount, err), err
|
2025-11-04 17:12:15 +08:00
|
|
|
}
|
2025-11-11 17:37:06 +08:00
|
|
|
s.globalMutex.RUnlock()
|
|
|
|
|
|
|
|
|
|
targetProcessResults, newComponentsMap, successfulTargets := processAndValidateTargets(ctx, tx, components, requestTargetsCount)
|
|
|
|
|
|
|
|
|
|
config.mutex.Lock()
|
|
|
|
|
mergeComponents(config.components, newComponentsMap)
|
|
|
|
|
config.mutex.Unlock()
|
|
|
|
|
|
|
|
|
|
if len(successfulTargets) > 0 {
|
|
|
|
|
transportTargets := &transportTargets{
|
|
|
|
|
OperationType: constants.OpAppend,
|
|
|
|
|
Targets: successfulTargets,
|
|
|
|
|
}
|
|
|
|
|
config.noticeChan <- transportTargets
|
|
|
|
|
}
|
2025-11-04 17:12:15 +08:00
|
|
|
|
2025-11-10 17:32:18 +08:00
|
|
|
transportTargets := &transportTargets{
|
|
|
|
|
OperationType: constants.OpAppend,
|
|
|
|
|
Targets: make([]string, requestTargetsCount),
|
|
|
|
|
}
|
2025-11-11 17:37:06 +08:00
|
|
|
config.mutex.Lock()
|
2025-11-06 17:22:14 +08:00
|
|
|
for _, componentItem := range components {
|
|
|
|
|
interval := componentItem.Interval
|
|
|
|
|
for _, target := range componentItem.Targets {
|
|
|
|
|
var targetResult network.TargetResult
|
|
|
|
|
targetResult.ID = target
|
|
|
|
|
|
|
|
|
|
targetModel, err := database.ParseDataIdentifierToken(ctx, tx, target)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Error(ctx, "parse data indentity token failed", "error", err, "identity_token", target)
|
|
|
|
|
targetResult.Code = constants.SubFailedCode
|
|
|
|
|
targetResult.Msg = fmt.Sprintf("%s: %s", constants.SubFailedMsg, err.Error())
|
|
|
|
|
targetProcessResults = append(targetProcessResults, targetResult)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
targetResult.Code = constants.SubSuccessCode
|
|
|
|
|
targetResult.Msg = constants.SubSuccessMsg
|
|
|
|
|
targetProcessResults = append(targetProcessResults, targetResult)
|
|
|
|
|
if comp, ok := config.components[interval]; !ok {
|
|
|
|
|
targets := make([]string, 0, len(componentItem.Targets))
|
2025-11-10 17:32:18 +08:00
|
|
|
targetParam := make(map[string]*orm.Measurement)
|
|
|
|
|
targetParam[target] = targetModel.GetMeasurementInfo()
|
2025-11-06 17:22:14 +08:00
|
|
|
config.components[interval] = &RealTimeMonitorComponent{
|
|
|
|
|
targets: append(targets, target),
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
comp.targets = append(comp.targets, target)
|
2025-11-10 17:32:18 +08:00
|
|
|
comp.targetParam[target] = targetModel.GetMeasurementInfo()
|
2025-11-04 17:12:15 +08:00
|
|
|
}
|
2025-11-10 17:32:18 +08:00
|
|
|
transportTargets.Targets = append(transportTargets.Targets, target)
|
2025-11-04 17:12:15 +08:00
|
|
|
}
|
|
|
|
|
}
|
2025-11-11 17:37:06 +08:00
|
|
|
config.mutex.Unlock()
|
2025-11-10 17:32:18 +08:00
|
|
|
|
|
|
|
|
config.noticeChan <- transportTargets
|
2025-11-06 17:22:14 +08:00
|
|
|
return targetProcessResults, nil
|
2025-11-04 17:12:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UpsertTargets define function to upsert targets in SharedMonitorState
|
2025-11-11 17:37:06 +08:00
|
|
|
func (s *SharedMonitorState) UpsertTargets(ctx context.Context, tx *gorm.DB, clientID string, components []network.RealTimeComponentItem) ([]network.TargetResult, error) {
|
|
|
|
|
requestTargetsCount := processRealTimeRequestCount(components)
|
|
|
|
|
targetProcessResults, newComponentsMap, successfulTargets := processAndValidateTargets(ctx, tx, components, requestTargetsCount)
|
|
|
|
|
|
|
|
|
|
s.globalMutex.RLock()
|
|
|
|
|
config, exist := s.monitorMap[clientID]
|
|
|
|
|
s.globalMutex.RUnlock()
|
|
|
|
|
|
|
|
|
|
var opType constants.TargetOperationType
|
|
|
|
|
if exist {
|
|
|
|
|
opType = constants.OpUpdate
|
|
|
|
|
config.mutex.Lock()
|
|
|
|
|
mergeComponents(config.components, newComponentsMap)
|
|
|
|
|
config.mutex.Unlock()
|
|
|
|
|
} else {
|
|
|
|
|
opType = constants.OpAppend
|
|
|
|
|
s.globalMutex.Lock()
|
|
|
|
|
if config, exist = s.monitorMap[clientID]; !exist {
|
|
|
|
|
config = &RealTimeMonitorConfig{
|
|
|
|
|
noticeChan: make(chan *transportTargets),
|
|
|
|
|
components: newComponentsMap,
|
2025-11-06 17:22:14 +08:00
|
|
|
}
|
2025-11-11 17:37:06 +08:00
|
|
|
s.monitorMap[clientID] = config
|
|
|
|
|
} else {
|
|
|
|
|
s.globalMutex.Unlock()
|
|
|
|
|
config.mutex.Lock()
|
|
|
|
|
mergeComponents(config.components, newComponentsMap)
|
|
|
|
|
config.mutex.Unlock()
|
2025-11-04 17:12:15 +08:00
|
|
|
}
|
2025-11-11 17:37:06 +08:00
|
|
|
s.globalMutex.Unlock()
|
2025-11-04 17:12:15 +08:00
|
|
|
}
|
|
|
|
|
|
2025-11-11 17:37:06 +08:00
|
|
|
if len(successfulTargets) > 0 {
|
|
|
|
|
transportTargets := &transportTargets{
|
|
|
|
|
OperationType: opType,
|
|
|
|
|
Targets: successfulTargets,
|
2025-11-04 17:12:15 +08:00
|
|
|
}
|
2025-11-11 17:37:06 +08:00
|
|
|
config.noticeChan <- transportTargets
|
2025-11-04 17:12:15 +08:00
|
|
|
}
|
2025-11-06 17:22:14 +08:00
|
|
|
return targetProcessResults, nil
|
2025-11-04 17:12:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RemoveTargets define function to remove targets in SharedMonitorState
|
2025-11-11 17:37:06 +08:00
|
|
|
func (s *SharedMonitorState) RemoveTargets(ctx context.Context, clientID string, components []network.RealTimeComponentItem) ([]network.TargetResult, error) {
|
2025-11-06 17:22:14 +08:00
|
|
|
requestTargetsCount := processRealTimeRequestCount(components)
|
|
|
|
|
targetProcessResults := make([]network.TargetResult, 0, requestTargetsCount)
|
|
|
|
|
|
2025-11-11 17:37:06 +08:00
|
|
|
s.globalMutex.RLock()
|
|
|
|
|
config, exist := s.monitorMap[clientID]
|
2025-11-04 17:12:15 +08:00
|
|
|
if !exist {
|
2025-11-11 17:37:06 +08:00
|
|
|
s.globalMutex.RUnlock()
|
|
|
|
|
err := fmt.Errorf("clientID %s not found", clientID)
|
|
|
|
|
logger.Error(ctx, "clientID not found in remove targets operation", "error", err)
|
2025-11-06 17:22:14 +08:00
|
|
|
return processRealTimeRequestTargets(components, requestTargetsCount, err), err
|
2025-11-04 17:12:15 +08:00
|
|
|
}
|
2025-11-11 17:37:06 +08:00
|
|
|
s.globalMutex.RUnlock()
|
2025-11-04 17:12:15 +08:00
|
|
|
|
2025-11-11 17:37:06 +08:00
|
|
|
var shouldRemoveClient bool
|
2025-11-06 17:22:14 +08:00
|
|
|
// components is the list of items to be removed passed in the request
|
2025-11-10 17:32:18 +08:00
|
|
|
transportTargets := &transportTargets{
|
|
|
|
|
OperationType: constants.OpRemove,
|
2025-11-11 17:37:06 +08:00
|
|
|
Targets: make([]string, 0, requestTargetsCount),
|
2025-11-10 17:32:18 +08:00
|
|
|
}
|
2025-11-11 17:37:06 +08:00
|
|
|
config.mutex.Lock()
|
2025-11-04 17:12:15 +08:00
|
|
|
for _, compent := range components {
|
|
|
|
|
interval := compent.Interval
|
2025-11-06 17:22:14 +08:00
|
|
|
// comp is the locally running listener configuration
|
2025-11-04 17:12:15 +08:00
|
|
|
comp, compExist := config.components[interval]
|
|
|
|
|
if !compExist {
|
2025-11-11 17:37:06 +08:00
|
|
|
logger.Error(ctx, fmt.Sprintf("component with interval %s not found under clientID %s", interval, clientID), "clientID", clientID, "interval", interval)
|
|
|
|
|
for _, target := range compent.Targets {
|
2025-11-06 17:22:14 +08:00
|
|
|
targetResult := network.TargetResult{
|
|
|
|
|
ID: target,
|
|
|
|
|
Code: constants.CancelSubFailedCode,
|
|
|
|
|
Msg: constants.CancelSubFailedMsg,
|
|
|
|
|
}
|
|
|
|
|
targetProcessResults = append(targetProcessResults, targetResult)
|
|
|
|
|
}
|
2025-11-04 17:12:15 +08:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
targetsToRemoveMap := make(map[string]struct{})
|
|
|
|
|
for _, target := range compent.Targets {
|
|
|
|
|
targetsToRemoveMap[target] = struct{}{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var newTargets []string
|
|
|
|
|
for _, existingTarget := range comp.targets {
|
|
|
|
|
if _, found := targetsToRemoveMap[existingTarget]; !found {
|
|
|
|
|
newTargets = append(newTargets, existingTarget)
|
2025-11-06 17:22:14 +08:00
|
|
|
} else {
|
2025-11-10 17:32:18 +08:00
|
|
|
transportTargets.Targets = append(transportTargets.Targets, existingTarget)
|
2025-11-06 17:22:14 +08:00
|
|
|
targetResult := network.TargetResult{
|
|
|
|
|
ID: existingTarget,
|
|
|
|
|
Code: constants.CancelSubSuccessCode,
|
|
|
|
|
Msg: constants.CancelSubSuccessMsg,
|
|
|
|
|
}
|
|
|
|
|
targetProcessResults = append(targetProcessResults, targetResult)
|
|
|
|
|
delete(targetsToRemoveMap, existingTarget)
|
|
|
|
|
delete(comp.targetParam, existingTarget)
|
2025-11-04 17:12:15 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
comp.targets = newTargets
|
|
|
|
|
|
|
|
|
|
if len(comp.targets) == 0 {
|
|
|
|
|
delete(config.components, interval)
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-06 17:22:14 +08:00
|
|
|
if len(config.components) == 0 {
|
2025-11-11 17:37:06 +08:00
|
|
|
shouldRemoveClient = true
|
2025-11-06 17:22:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(targetsToRemoveMap) > 0 {
|
2025-11-11 17:37:06 +08:00
|
|
|
err := fmt.Errorf("target remove were not found under clientID %s and interval %s", clientID, interval)
|
2025-11-06 17:22:14 +08:00
|
|
|
for target := range targetsToRemoveMap {
|
|
|
|
|
targetResult := network.TargetResult{
|
|
|
|
|
ID: target,
|
|
|
|
|
Code: constants.CancelSubFailedCode,
|
|
|
|
|
Msg: fmt.Sprintf("%s: %s", constants.SubFailedMsg, err.Error()),
|
|
|
|
|
}
|
|
|
|
|
targetProcessResults = append(targetProcessResults, targetResult)
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-04 17:12:15 +08:00
|
|
|
}
|
2025-11-11 17:37:06 +08:00
|
|
|
config.mutex.Unlock()
|
2025-11-10 17:32:18 +08:00
|
|
|
// pass the removed subscription configuration to the notice channel
|
|
|
|
|
config.noticeChan <- transportTargets
|
2025-11-11 17:37:06 +08:00
|
|
|
|
|
|
|
|
if shouldRemoveClient {
|
|
|
|
|
s.globalMutex.Lock()
|
|
|
|
|
if currentConfig, exist := s.monitorMap[clientID]; exist && len(currentConfig.components) == 0 {
|
|
|
|
|
delete(s.monitorMap, clientID)
|
|
|
|
|
}
|
|
|
|
|
s.globalMutex.Unlock()
|
|
|
|
|
}
|
2025-11-06 17:22:14 +08:00
|
|
|
return targetProcessResults, nil
|
2025-11-04 17:12:15 +08:00
|
|
|
}
|
2025-11-05 18:20:54 +08:00
|
|
|
|
2025-11-11 17:37:06 +08:00
|
|
|
// Get define function to get subscriptions config from SharedMonitorState
|
|
|
|
|
func (s *SharedMonitorState) Get(clientID string) (*RealTimeMonitorConfig, bool) {
|
|
|
|
|
s.globalMutex.RLock()
|
|
|
|
|
defer s.globalMutex.RUnlock()
|
|
|
|
|
|
|
|
|
|
config, ok := s.monitorMap[clientID]
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, false
|
|
|
|
|
}
|
|
|
|
|
return config, true
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-10 17:32:18 +08:00
|
|
|
// TODO 增加一个update 函数用来更新 interval
|
|
|
|
|
|
2025-11-05 18:20:54 +08:00
|
|
|
func processRealTimeRequestCount(components []network.RealTimeComponentItem) int {
|
|
|
|
|
totalTargetsCount := 0
|
|
|
|
|
for _, compItem := range components {
|
|
|
|
|
totalTargetsCount += len(compItem.Targets)
|
|
|
|
|
}
|
|
|
|
|
return totalTargetsCount
|
|
|
|
|
}
|
2025-11-06 17:22:14 +08:00
|
|
|
|
|
|
|
|
func processRealTimeRequestTargets(components []network.RealTimeComponentItem, targetCount int, err error) []network.TargetResult {
|
|
|
|
|
targetProcessResults := make([]network.TargetResult, 0, targetCount)
|
|
|
|
|
for _, componentItem := range components {
|
|
|
|
|
for _, target := range componentItem.Targets {
|
|
|
|
|
var targetResult network.TargetResult
|
|
|
|
|
targetResult.ID = target
|
|
|
|
|
targetResult.Code = constants.SubFailedCode
|
|
|
|
|
targetResult.Msg = fmt.Sprintf("%s: %s", constants.SubFailedMsg, err.Error())
|
|
|
|
|
targetProcessResults = append(targetProcessResults, targetResult)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return targetProcessResults
|
|
|
|
|
}
|
2025-11-10 17:32:18 +08:00
|
|
|
|
|
|
|
|
// transportTargets define struct to transport update or remove target to real
|
|
|
|
|
// time pull api
|
|
|
|
|
type transportTargets struct {
|
|
|
|
|
OperationType constants.TargetOperationType
|
|
|
|
|
Targets []string
|
|
|
|
|
}
|