fix bug of token4-token7 type attribute recommend api

This commit is contained in:
douxu 2025-12-31 16:52:40 +08:00
parent e74bedd47f
commit fcf4ef3f7d
3 changed files with 33 additions and 14 deletions

View File

@ -170,7 +170,7 @@ func main() {
logger.Error(ctx, "generate component measurement group failed", "error", err) logger.Error(ctx, "generate component measurement group failed", "error", err)
panic(err) panic(err)
} }
parentPath, err := model.TraverseMeasurementGroupTables(ctx, *measurementSet) fullParentPath, isLocalParentPath, err := model.TraverseMeasurementGroupTables(ctx, *measurementSet)
if err != nil { if err != nil {
logger.Error(ctx, "store component measurement group into redis failed", "error", err) logger.Error(ctx, "store component measurement group into redis failed", "error", err)
panic(err) panic(err)
@ -182,7 +182,7 @@ func main() {
panic(err) panic(err)
} }
err = model.TraverseAttributeGroupTables(ctx, tx, parentPath, compAttrSet) err = model.TraverseAttributeGroupTables(ctx, tx, fullParentPath, isLocalParentPath, compAttrSet)
if err != nil { if err != nil {
logger.Error(ctx, "store component attribute group into redis failed", "error", err) logger.Error(ctx, "store component attribute group into redis failed", "error", err)
panic(err) panic(err)

View File

@ -21,7 +21,7 @@ type columnParam struct {
} }
// TraverseAttributeGroupTables define func to traverse component attribute group tables // TraverseAttributeGroupTables define func to traverse component attribute group tables
func TraverseAttributeGroupTables(ctx context.Context, db *gorm.DB, compTagToFullPath map[string]string, compAttrSet map[string]orm.AttributeSet) error { func TraverseAttributeGroupTables(ctx context.Context, db *gorm.DB, compTagToFullPath map[string]string, isLocalCompTagToFullPath map[string]string, compAttrSet map[string]orm.AttributeSet) error {
var tableNames []string var tableNames []string
excludedTables := []string{"component", ""} excludedTables := []string{"component", ""}
@ -92,6 +92,7 @@ func TraverseAttributeGroupTables(ctx context.Context, db *gorm.DB, compTagToFul
} }
fullPath := compTagToFullPath[attrSet.CompTag] fullPath := compTagToFullPath[attrSet.CompTag]
isLocalfullPath := isLocalCompTagToFullPath[attrSet.CompTag]
if fullPath == "" { if fullPath == "" {
err := errors.New("can not find full parent path from mapping by component tag") 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) logger.Error(ctx, "find full parent path by from mapping by component tag failed", "component_tag", attrSet.CompTag, "error", err)
@ -101,13 +102,13 @@ func TraverseAttributeGroupTables(ctx context.Context, db *gorm.DB, compTagToFul
AttributeType: attributeType, AttributeType: attributeType,
AttributeGroup: attributeGroup, AttributeGroup: attributeGroup,
} }
go storeAttributeGroup(ctx, attrSet, fullPath, columnParam) go storeAttributeGroup(ctx, attrSet, fullPath, isLocalfullPath, columnParam)
} }
} }
return nil return nil
} }
func storeAttributeGroup(ctx context.Context, attributeSet orm.AttributeSet, fullPath string, colParams columnParam) { func storeAttributeGroup(ctx context.Context, attributeSet orm.AttributeSet, fullPath string, isLocalFullPath string, colParams columnParam) {
rdb := diagram.GetRedisClientInstance() rdb := diagram.GetRedisClientInstance()
pipe := rdb.Pipeline() pipe := rdb.Pipeline()
@ -120,11 +121,12 @@ func storeAttributeGroup(ctx context.Context, attributeSet orm.AttributeSet, ful
attrbutesGroups := make([]any, 0, len(colParams.AttributeGroup)*2) attrbutesGroups := make([]any, 0, len(colParams.AttributeGroup)*2)
attributeGroupKey := fmt.Sprintf("%s_%s", attributeSet.CompTag, colParams.AttributeType) attributeGroupKey := fmt.Sprintf("%s_%s", attributeSet.CompTag, colParams.AttributeType)
sug := make([]redisearch.Suggestion, 0, len(colParams.AttributeGroup)) sug := make([]redisearch.Suggestion, 0, len(colParams.AttributeGroup)*4)
for attrName, attrValue := range colParams.AttributeGroup { for attrName, attrValue := range colParams.AttributeGroup {
attrbutesGroups = append(attrbutesGroups, attrName, attrValue) attrbutesGroups = append(attrbutesGroups, attrName, attrValue)
attrNameMembers = append(attrNameMembers, attrName) attrNameMembers = append(attrNameMembers, attrName)
// add redis fuzzy search suggestion for token1-token7 type
configTerm := fmt.Sprintf("%s.%s", fullPath, colParams.AttributeType) configTerm := fmt.Sprintf("%s.%s", fullPath, colParams.AttributeType)
sug = append(sug, redisearch.Suggestion{ sug = append(sug, redisearch.Suggestion{
Term: configTerm, Term: configTerm,
@ -136,6 +138,20 @@ func storeAttributeGroup(ctx context.Context, attributeSet orm.AttributeSet, ful
Term: measTerm, Term: measTerm,
Score: constants.DefaultScore, Score: constants.DefaultScore,
}) })
// add redis fuzzy search suggestion for token4-token7 type
configTerm = fmt.Sprintf("%s.%s", isLocalFullPath, colParams.AttributeType)
fmt.Printf("is local full path attribute configTerm:%v\n", configTerm)
sug = append(sug, redisearch.Suggestion{
Term: configTerm,
Score: constants.DefaultScore,
})
measTerm = fmt.Sprintf("%s.%s.%s", isLocalFullPath, colParams.AttributeType, attrName)
sug = append(sug, redisearch.Suggestion{
Term: measTerm,
Score: constants.DefaultScore,
})
} }
if len(attrbutesGroups) > 0 { if len(attrbutesGroups) > 0 {

View File

@ -15,11 +15,12 @@ import (
) )
// TraverseMeasurementGroupTables define func to traverse component measurement group tables // TraverseMeasurementGroupTables define func to traverse component measurement group tables
func TraverseMeasurementGroupTables(ctx context.Context, measSet orm.MeasurementSet) (map[string]string, error) { func TraverseMeasurementGroupTables(ctx context.Context, measSet orm.MeasurementSet) (map[string]string, map[string]string, error) {
rdb := diagram.GetRedisClientInstance() rdb := diagram.GetRedisClientInstance()
pipe := rdb.Pipeline() pipe := rdb.Pipeline()
compTagToFullPath := make(map[string]string) compTagToFullPath := make(map[string]string)
isLocalCompTagToFullPath := make(map[string]string)
zoneToGridPath := make(map[string]string) zoneToGridPath := make(map[string]string)
for gridTag, zoneTags := range measSet.GridToZoneTags { for gridTag, zoneTags := range measSet.GridToZoneTags {
@ -94,7 +95,7 @@ func TraverseMeasurementGroupTables(ctx context.Context, measSet orm.Measurement
if !exists { if !exists {
err := fmt.Errorf("zone tag to grid tag mapping not found for zoneTag: %s", zoneTag) 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) logger.Error(ctx, "zone tag to grid tag mapping not found", "zoneTag", zoneTag, "error", err)
return nil, err return nil, nil, err
} }
for _, stationTag := range stationTags { for _, stationTag := range stationTags {
@ -114,7 +115,7 @@ func TraverseMeasurementGroupTables(ctx context.Context, measSet orm.Measurement
if !exists { if !exists {
err := fmt.Errorf("station tag to zone tag mapping not found for stationTag: %s", stationTag) 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) logger.Error(ctx, "zone tag to grid tag mapping not found", "stationTag", stationTag, "error", err)
return nil, err return nil, nil, err
} }
for _, nsPath := range compNSPaths { for _, nsPath := range compNSPaths {
@ -135,12 +136,14 @@ func TraverseMeasurementGroupTables(ctx context.Context, measSet orm.Measurement
if !exists { if !exists {
err := fmt.Errorf("component nspath tag to station tag mapping not found for compNSPath: %s", compNSPath) 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) logger.Error(ctx, "component nspath tag to station tag mapping not found", "compNSPath", compNSPath, "error", err)
return nil, err return nil, nil, err
} }
for _, compTag := range compTags { for _, compTag := range compTags {
fullPath := fmt.Sprintf("%s.%s.%s", parentPath, compNSPath, compTag) fullPath := fmt.Sprintf("%s.%s.%s", parentPath, compNSPath, compTag)
compTagToFullPath[compTag] = fullPath compTagToFullPath[compTag] = fullPath
fullPath = fmt.Sprintf("%s.%s", compNSPath, compTag)
isLocalCompTagToFullPath[compTag] = fullPath
// add redis fuzzy search suggestion for token1-token7 type // add redis fuzzy search suggestion for token1-token7 type
term := fullPath term := fullPath
@ -160,14 +163,14 @@ func TraverseMeasurementGroupTables(ctx context.Context, measSet orm.Measurement
if !exists { if !exists {
err := fmt.Errorf("component tag to component nspath mapping not found for compTag: %s", compTag) 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) logger.Error(ctx, "component tag to component nspath mapping not found", "compTag", compTag, "error", err)
return nil, err return nil, nil, err
} }
isLocalParentPath, exists := isLocalCompTagToNSPathPath[compTag] isLocalParentPath, exists := isLocalCompTagToNSPathPath[compTag]
if !exists { if !exists {
err := fmt.Errorf("component tag to component nspath is local mapping not found for compTag: %s", compTag) err := fmt.Errorf("component tag to component nspath is local mapping not found for compTag: %s", compTag)
logger.Error(ctx, "component tag to component nspath is local mapping not found", "compTag", compTag, "error", err) logger.Error(ctx, "component tag to component nspath is local mapping not found", "compTag", compTag, "error", err)
return nil, err return nil, nil, err
} }
for _, measTag := range measTags { for _, measTag := range measTags {
@ -207,8 +210,8 @@ func TraverseMeasurementGroupTables(ctx context.Context, measSet orm.Measurement
} }
} }
if len(allErrs) > 0 { if len(allErrs) > 0 {
return nil, errors.Join(allErrs...) return nil, nil, errors.Join(allErrs...)
} }
return compTagToFullPath, nil return compTagToFullPath, isLocalCompTagToFullPath, nil
} }