diff --git a/main.go b/main.go index ebc952e..e389042 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/model/recommend_islocal_cache.go b/model/recommend_islocal_cache.go new file mode 100644 index 0000000..8bb7cda --- /dev/null +++ b/model/recommend_islocal_cache.go @@ -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 +} diff --git a/model/redis_recommend.go b/model/redis_recommend.go index b771447..7203f3b 100644 --- a/model/redis_recommend.go +++ b/model/redis_recommend.go @@ -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 }