add fullpath completion of component attribute group recommend

This commit is contained in:
douxu 2025-12-26 12:00:00 +08:00
parent 42751c1020
commit 8e4bdfd0e9
5 changed files with 54 additions and 18 deletions

View File

@ -89,6 +89,8 @@ func GetFullMeasurementSet(db *gorm.DB) (*orm.MeasurementSet, error) {
}
}
mSet.AllConfigTags = append(mSet.AllConfigTags, "bay")
var measurements []struct {
orm.Measurement
CompTag string `gorm:"column:comp_tag"`

View File

@ -157,19 +157,21 @@ func main() {
logger.Error(ctx, "generate component measurement group failed", "error", err)
panic(err)
}
err = model.TraverseMeasurementGroupTables(ctx, *measurementSet)
parentPath, err := model.TraverseMeasurementGroupTables(ctx, *measurementSet)
if err != nil {
logger.Error(ctx, "store component measurement group into redis failed", "error", err)
panic(err)
}
fmt.Printf("generate full path mapping result:%v\n", parentPath)
compAttrSet, err := database.GenAllAttributeMap(tx)
if err != nil {
logger.Error(ctx, "generate component attribute group failed", "error", err)
panic(err)
}
err = model.TraverseAttributeGroupTables(ctx, tx, compAttrSet)
err = model.TraverseAttributeGroupTables(ctx, tx, parentPath, compAttrSet)
if err != nil {
logger.Error(ctx, "store component attribute group into redis failed", "error", err)
panic(err)

View File

@ -11,6 +11,8 @@ import (
"modelRT/logger"
"modelRT/orm"
"github.com/RediSearch/redisearch-go/v2/redisearch"
"github.com/redis/go-redis/v9"
"gorm.io/gorm"
)
@ -20,7 +22,7 @@ type columnParam struct {
}
// TraverseAttributeGroupTables define func to traverse component attribute group tables
func TraverseAttributeGroupTables(ctx context.Context, db *gorm.DB, compAttrSet map[string]orm.AttributeSet) error {
func TraverseAttributeGroupTables(ctx context.Context, db *gorm.DB, compTagToFullPath map[string]string, compAttrSet map[string]orm.AttributeSet) error {
var tableNames []string
result := db.Model(&orm.ProjectManager{}).Pluck("name", &tableNames)
@ -92,17 +94,23 @@ func TraverseAttributeGroupTables(ctx context.Context, db *gorm.DB, compAttrSet
}
}
fullPath := compTagToFullPath[attrSet.CompTag]
if fullPath == "" {
err := errors.New("can not find full parent path from mapping by component tag")
logger.Error(ctx, "find full parent path by from mapping by component tag failed", "component_tag", attrSet.CompTag, "error", err)
return err
}
columnParam := columnParam{
AttributeType: attributeType,
AttributeGroup: attributeGroup,
}
go storeAttributeGroup(ctx, attrSet, columnParam)
go storeAttributeGroup(ctx, attrSet, fullPath, columnParam)
}
}
return nil
}
func storeAttributeGroup(ctx context.Context, attributeSet orm.AttributeSet, colParams columnParam) {
func storeAttributeGroup(ctx context.Context, attributeSet orm.AttributeSet, fullPath string, colParams columnParam) {
rdb := diagram.GetRedisClientInstance()
pipe := rdb.Pipeline()
@ -114,9 +122,17 @@ func storeAttributeGroup(ctx context.Context, attributeSet orm.AttributeSet, col
attrNameMembers := make([]string, 0, len(colParams.AttributeGroup))
attrbutesGroups := make([]any, 0, len(colParams.AttributeGroup)*2)
attributeGroupKey := fmt.Sprintf("%s_%s", attributeSet.CompTag, colParams.AttributeType)
sug := make([]redisearch.Suggestion, 0, len(colParams.AttributeGroup))
for attrName, attrValue := range colParams.AttributeGroup {
attrbutesGroups = append(attrbutesGroups, attrName, attrValue)
attrNameMembers = append(attrNameMembers, attrName)
term := fmt.Sprintf("%s.%s", fullPath, attrName)
sug = append(sug, redisearch.Suggestion{
Term: term,
Score: constants.DefaultScore,
})
}
if len(attrbutesGroups) > 0 {
@ -127,7 +143,10 @@ func storeAttributeGroup(ctx context.Context, attributeSet orm.AttributeSet, col
pipe.SAdd(ctx, specCompMeasKey, attrNameMembers)
}
// TODO 增加 suggestion 索引内容
if len(sug) > 0 {
ac.AddTerms(sug...)
}
_, err := pipe.Exec(ctx)
if err != nil {
logger.Error(ctx, "init component attribute group recommend content failed", "error", err)

View File

@ -3,6 +3,7 @@ package model
import (
"context"
"errors"
"fmt"
"modelRT/constants"
@ -15,10 +16,12 @@ import (
)
// TraverseMeasurementGroupTables define func to traverse component measurement group tables
func TraverseMeasurementGroupTables(ctx context.Context, measSet orm.MeasurementSet) error {
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 {
@ -68,7 +71,7 @@ func TraverseMeasurementGroupTables(ctx context.Context, measSet orm.Measurement
safeSAdd(constants.RedisAllStationSetKey, measSet.AllStationTags)
safeSAdd(constants.RedisAllCompNSPathSetKey, measSet.AllCompNSPaths)
safeSAdd(constants.RedisAllCompTagSetKey, measSet.AllCompTags)
safeSAdd(constants.RedisAllConfigSetKey, []string{"bay"})
safeSAdd(constants.RedisAllConfigSetKey, measSet.AllConfigTags)
safeSAdd(constants.RedisAllMeasTagSetKey, measSet.AllMeasTags)
// building the grid -> zones hierarchy
@ -89,7 +92,7 @@ func TraverseMeasurementGroupTables(ctx context.Context, measSet orm.Measurement
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 err
return nil, err
}
for _, stationTag := range stationTags {
@ -108,7 +111,7 @@ func TraverseMeasurementGroupTables(ctx context.Context, measSet orm.Measurement
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 err
return nil, err
}
for _, nsPath := range compNSPaths {
@ -126,11 +129,14 @@ func TraverseMeasurementGroupTables(ctx context.Context, measSet orm.Measurement
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 err
return nil, err
}
for _, compTag := range compTags {
term := fmt.Sprintf("%s.%s.%s", parentPath, compNSPath, compTag)
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)
@ -144,7 +150,7 @@ func TraverseMeasurementGroupTables(ctx context.Context, measSet orm.Measurement
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 err
return nil, err
}
for _, measTag := range measTags {
@ -155,10 +161,11 @@ func TraverseMeasurementGroupTables(ctx context.Context, measSet orm.Measurement
ac.AddTerms(sug...)
}
cmders, err := pipe.Exec(ctx)
if err != nil {
logger.Error(ctx, "pipeline execution failed", "error", err)
return err
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
@ -169,9 +176,14 @@ func TraverseMeasurementGroupTables(ctx context.Context, measSet orm.Measurement
"command", cmder.Name(),
"args", cmder.Args(),
"error", cmdErr)
allErrs = append(allErrs, cmdErr)
}
return cmdErr
}
if len(allErrs) > 0 {
return nil, errors.Join(allErrs...)
}
return compTagToFullPath, nil
}
return nil
}

View File

@ -8,6 +8,7 @@ type MeasurementSet struct {
AllStationTags []string
AllCompNSPaths []string
AllCompTags []string
AllConfigTags []string
AllMeasTags []string
GridToZoneTags map[string][]string // Key: GridTag, Value: ZoneTags