190 lines
6.6 KiB
Go
190 lines
6.6 KiB
Go
// Package model define model struct of model runtime service
|
|
package model
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"modelRT/constants"
|
|
"modelRT/diagram"
|
|
"modelRT/logger"
|
|
"modelRT/orm"
|
|
|
|
"github.com/RediSearch/redisearch-go/v2/redisearch"
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
// TraverseMeasurementGroupTables define func to traverse component measurement group tables
|
|
func TraverseMeasurementGroupTables(ctx context.Context, measSet orm.MeasurementSet) (map[string]string, error) {
|
|
rdb := diagram.GetRedisClientInstance()
|
|
pipe := rdb.Pipeline()
|
|
|
|
compTagToFullPath := make(map[string]string)
|
|
|
|
zoneToGridPath := make(map[string]string)
|
|
for gridTag, zoneTags := range measSet.GridToZoneTags {
|
|
for _, zoneTag := range zoneTags {
|
|
zoneToGridPath[zoneTag] = gridTag
|
|
}
|
|
}
|
|
|
|
stationToZonePath := make(map[string]string)
|
|
for zoneTag, stationTags := range measSet.ZoneToStationTags {
|
|
gridTag := zoneToGridPath[zoneTag]
|
|
for _, stationTag := range stationTags {
|
|
stationToZonePath[stationTag] = fmt.Sprintf("%s.%s", gridTag, zoneTag)
|
|
}
|
|
}
|
|
|
|
compNSPathToStationPath := make(map[string]string)
|
|
for stationTag, nsPaths := range measSet.StationToCompNSPaths {
|
|
zonePath := stationToZonePath[stationTag]
|
|
for _, nsPath := range nsPaths {
|
|
compNSPathToStationPath[nsPath] = fmt.Sprintf("%s.%s", zonePath, stationTag)
|
|
}
|
|
}
|
|
|
|
compTagToNSPathPath := make(map[string]string)
|
|
for nsPath, compTags := range measSet.CompNSPathToCompTags {
|
|
stationPath := compNSPathToStationPath[nsPath]
|
|
for _, compTag := range compTags {
|
|
compTagToNSPathPath[compTag] = fmt.Sprintf("%s.%s", stationPath, nsPath)
|
|
}
|
|
}
|
|
|
|
// define a safe SAdd function to avoid errors caused by empty slices
|
|
safeSAdd := func(key string, members []string) {
|
|
if len(members) > 0 {
|
|
pipe.SAdd(ctx, key, members)
|
|
}
|
|
}
|
|
|
|
safeSAdd(constants.RedisAllGridSetKey, measSet.AllGridTags)
|
|
gridSug := make([]redisearch.Suggestion, 0, len(measSet.AllGridTags))
|
|
for _, gridTag := range measSet.AllGridTags {
|
|
gridSug = append(gridSug, redisearch.Suggestion{Term: gridTag, Score: constants.DefaultScore})
|
|
}
|
|
ac.AddTerms(gridSug...)
|
|
|
|
safeSAdd(constants.RedisAllZoneSetKey, measSet.AllZoneTags)
|
|
safeSAdd(constants.RedisAllStationSetKey, measSet.AllStationTags)
|
|
safeSAdd(constants.RedisAllCompNSPathSetKey, measSet.AllCompNSPaths)
|
|
safeSAdd(constants.RedisAllCompTagSetKey, measSet.AllCompTags)
|
|
safeSAdd(constants.RedisAllConfigSetKey, measSet.AllConfigTags)
|
|
safeSAdd(constants.RedisAllMeasTagSetKey, measSet.AllMeasTags)
|
|
|
|
// building the grid -> zones hierarchy
|
|
for gridTag, zoneTags := range measSet.GridToZoneTags {
|
|
sug := make([]redisearch.Suggestion, 0, len(zoneTags))
|
|
for _, zoneTag := range zoneTags {
|
|
term := fmt.Sprintf("%s.%s", gridTag, zoneTag)
|
|
sug = append(sug, redisearch.Suggestion{Term: term, Score: constants.DefaultScore})
|
|
}
|
|
safeSAdd(fmt.Sprintf(constants.RedisSpecGridZoneSetKey, gridTag), zoneTags)
|
|
ac.AddTerms(sug...)
|
|
}
|
|
|
|
// building the zone -> stations hierarchy
|
|
for zoneTag, stationTags := range measSet.ZoneToStationTags {
|
|
sug := make([]redisearch.Suggestion, 0, len(stationTags))
|
|
gridTag, exists := zoneToGridPath[zoneTag]
|
|
if !exists {
|
|
err := fmt.Errorf("zone tag to grid tag mapping not found for zoneTag: %s", zoneTag)
|
|
logger.Error(ctx, "zone tag to grid tag mapping not found", "zoneTag", zoneTag, "error", err)
|
|
return nil, err
|
|
}
|
|
|
|
for _, stationTag := range stationTags {
|
|
term := fmt.Sprintf("%s.%s.%s", gridTag, zoneTag, stationTag)
|
|
sug = append(sug, redisearch.Suggestion{Term: term, Score: constants.DefaultScore})
|
|
}
|
|
|
|
safeSAdd(fmt.Sprintf(constants.RedisSpecZoneStationSetKey, zoneTag), stationTags)
|
|
ac.AddTerms(sug...)
|
|
}
|
|
|
|
// building the station -> component nspaths hierarchy
|
|
for stationTag, compNSPaths := range measSet.StationToCompNSPaths {
|
|
sug := make([]redisearch.Suggestion, 0, len(compNSPaths))
|
|
parentPath, exists := stationToZonePath[stationTag]
|
|
if !exists {
|
|
err := fmt.Errorf("station tag to zone tag mapping not found for stationTag: %s", stationTag)
|
|
logger.Error(ctx, "zone tag to grid tag mapping not found", "stationTag", stationTag, "error", err)
|
|
return nil, err
|
|
}
|
|
|
|
for _, nsPath := range compNSPaths {
|
|
term := fmt.Sprintf("%s.%s.%s", parentPath, stationTag, nsPath)
|
|
sug = append(sug, redisearch.Suggestion{Term: term, Score: constants.DefaultScore})
|
|
}
|
|
safeSAdd(fmt.Sprintf(constants.RedisSpecStationCompNSPATHSetKey, stationTag), compNSPaths)
|
|
ac.AddTerms(sug...)
|
|
}
|
|
|
|
// building the component nspath -> component tags hierarchy
|
|
for compNSPath, compTags := range measSet.CompNSPathToCompTags {
|
|
sug := make([]redisearch.Suggestion, 0, len(compTags))
|
|
parentPath, exists := compNSPathToStationPath[compNSPath]
|
|
if !exists {
|
|
err := fmt.Errorf("component nspath tag to station tag mapping not found for compNSPath: %s", compNSPath)
|
|
logger.Error(ctx, "component nspath tag to station tag mapping not found", "compNSPath", compNSPath, "error", err)
|
|
return nil, err
|
|
}
|
|
|
|
for _, compTag := range compTags {
|
|
fullPath := fmt.Sprintf("%s.%s.%s", parentPath, compNSPath, compTag)
|
|
compTagToFullPath[compTag] = fullPath
|
|
|
|
term := fullPath
|
|
sug = append(sug, redisearch.Suggestion{Term: term, Score: constants.DefaultScore})
|
|
}
|
|
safeSAdd(fmt.Sprintf(constants.RedisSpecCompNSPathCompTagSetKey, compNSPath), compTags)
|
|
ac.AddTerms(sug...)
|
|
}
|
|
|
|
// building the component tag -> measurement tags hierarchy
|
|
for compTag, measTags := range measSet.CompTagToMeasTags {
|
|
sug := make([]redisearch.Suggestion, 0, len(measTags))
|
|
parentPath, exists := compTagToNSPathPath[compTag]
|
|
if !exists {
|
|
err := fmt.Errorf("component tag to component nspath mapping not found for compTag: %s", compTag)
|
|
logger.Error(ctx, "component tag to component nspath mapping not found", "compTag", compTag, "error", err)
|
|
return nil, err
|
|
}
|
|
|
|
for _, measTag := range measTags {
|
|
term := fmt.Sprintf("%s.%s.%s", parentPath, compTag, measTag)
|
|
sug = append(sug, redisearch.Suggestion{Term: term, Score: constants.DefaultScore})
|
|
}
|
|
safeSAdd(fmt.Sprintf(constants.RedisSpecCompTagMeasSetKey, compTag), measTags)
|
|
ac.AddTerms(sug...)
|
|
}
|
|
|
|
var allErrs []error
|
|
cmders, execErr := pipe.Exec(ctx)
|
|
if execErr != nil {
|
|
logger.Error(ctx, "pipeline execution failed", "error", execErr)
|
|
allErrs = append(allErrs, execErr)
|
|
}
|
|
|
|
// check for errors in each subcommand of the pipeline
|
|
for _, cmder := range cmders {
|
|
cmdErr := cmder.Err()
|
|
if cmdErr != nil {
|
|
logger.Error(ctx, "individual redis command failed",
|
|
"command", cmder.Name(),
|
|
"args", cmder.Args(),
|
|
"error", cmdErr)
|
|
allErrs = append(allErrs, cmdErr)
|
|
}
|
|
}
|
|
if len(allErrs) > 0 {
|
|
return nil, errors.Join(allErrs...)
|
|
}
|
|
|
|
return compTagToFullPath, nil
|
|
}
|
|
return nil
|
|
}
|