add nspath filter of recommend api

This commit is contained in:
douxu 2025-12-30 16:35:29 +08:00
parent 941d521328
commit 36e196bedd
3 changed files with 79 additions and 4 deletions

View File

@ -152,7 +152,14 @@ func main() {
// panic(err)
// }
err := model.CleanupRecommendRedisCache(ctx)
cacheMap, err := model.GetNSpathToIsLocalMap(ctx, postgresDBClient)
if err != nil {
logger.Error(ctx, "get nspath to is_local map failed", "error", err)
panic(err)
}
model.NSPathToIsLocalMap = cacheMap
err = model.CleanupRecommendRedisCache(ctx)
if err != nil {
logger.Error(ctx, "clean up component measurement and attribute group failed", "error", err)
panic(err)

View File

@ -0,0 +1,41 @@
// Package model define model struct of model runtime service
package model
import (
"context"
"modelRT/logger"
"gorm.io/gorm"
)
var NSPathToIsLocalMap map[string]bool
// ComponentStationRelation define struct to hold component nspath and station is_local fields
type ComponentStationRelation struct {
NSPath string `gorm:"column:nspath"`
IsLocal bool `gorm:"column:is_local"`
}
// GetNSpathToIsLocalMap define func to get component nspath to station is_local map
func GetNSpathToIsLocalMap(ctx context.Context, db *gorm.DB) (map[string]bool, error) {
var results []ComponentStationRelation
nspathMap := make(map[string]bool)
err := db.Table("component").
Select("component.nspath, station.is_local").
Joins("join station on component.station_id = station.id").
Scan(&results).Error
if err != nil {
logger.Error(ctx, "query nspath and is_local relationship failed", "err", err)
return nil, err
}
for _, res := range results {
if res.NSPath != "" {
nspathMap[res.NSPath] = res.IsLocal
}
}
return nspathMap, nil
}

View File

@ -116,13 +116,26 @@ func RedisSearchRecommend(ctx context.Context, input string) map[string]SearchRe
for range 2 {
result := <-fanInChan
if result.Err != nil {
logger.Error(ctx, "return all keys at the special level from redis failed", "query_key", result.RecommendType, "error", result.Err)
logger.Error(ctx, "return all keys at the special level from redis failed", "recommend_type", result.RecommendType, "error", result.Err)
continue
}
if result.RecommendType == constants.CompNSPathRecommendHierarchyType {
filterResults := make([]string, 0, len(result.QueryDatas))
// TODO 增加 nspath 过滤
fmt.Println("process nspath")
for _, queryData := range result.QueryDatas {
var nsPath string
if lastDotIndex := strings.LastIndex(queryData, "."); lastDotIndex == -1 {
nsPath = queryData
} else {
nsPath = queryData[lastDotIndex+1:]
}
if isLocal, ok := NSPathToIsLocalMap[nsPath]; ok && isLocal {
filterResults = append(filterResults, queryData)
}
}
result.QueryDatas = filterResults
}
results[result.RecommendType.String()] = result
}
@ -150,9 +163,23 @@ func RedisSearchRecommend(ctx context.Context, input string) map[string]SearchRe
logger.Error(ctx, "exec redis fuzzy search by key failed", "recommend_type", result.RecommendType, "error", result.Err)
continue
}
if result.RecommendType == constants.CompNSPathRecommendHierarchyType {
filterResults := make([]string, 0, len(result.QueryDatas))
// TODO 增加 nspath 过滤
fmt.Println("process nspath")
for _, queryData := range result.QueryDatas {
var nsPath string
if lastDotIndex := strings.LastIndex(queryData, "."); lastDotIndex == -1 {
nsPath = queryData
} else {
nsPath = queryData[lastDotIndex+1:]
}
if isLocal, ok := NSPathToIsLocalMap[nsPath]; ok && isLocal {
filterResults = append(filterResults, queryData)
}
}
result.QueryDatas = filterResults
}
results[result.RecommendType.String()] = result
}