From 491c10e8c57460f2a81bd101cfa8862d2355a661 Mon Sep 17 00:00:00 2001 From: douxu Date: Wed, 1 Jul 2026 14:37:13 +0800 Subject: [PATCH] feat: add component column recommendations for config tokens - add component as a token6 config recommendation constant - read component table column names during startup - bind component config recommendations to component table columns - add full and local autocomplete terms for component column paths --- constants/recommend_keys.go | 2 + database/query_component_columns.go | 28 ++++++++ main.go | 12 ++++ model/component_column_recommend_model.go | 82 +++++++++++++++++++++++ 4 files changed, 124 insertions(+) create mode 100644 database/query_component_columns.go create mode 100644 model/component_column_recommend_model.go diff --git a/constants/recommend_keys.go b/constants/recommend_keys.go index 2fde23f..cef6ff9 100644 --- a/constants/recommend_keys.go +++ b/constants/recommend_keys.go @@ -4,6 +4,8 @@ package constants const ( // DefaultScore define the default score for redissearch suggestion DefaultScore = 1.0 + // ComponentConfigKey define component config token used at token6 + ComponentConfigKey = "component" ) const ( diff --git a/database/query_component_columns.go b/database/query_component_columns.go new file mode 100644 index 0000000..6b7a2eb --- /dev/null +++ b/database/query_component_columns.go @@ -0,0 +1,28 @@ +// Package database define database operation functions +package database + +import ( + "context" + + "modelRT/orm" + + "gorm.io/gorm" +) + +// QueryComponentColumnNames returns all column names from the component table. +func QueryComponentColumnNames(ctx context.Context, db *gorm.DB) ([]string, error) { + columnTypes, err := db.WithContext(ctx).Migrator().ColumnTypes((&orm.Component{}).TableName()) + if err != nil { + return nil, err + } + + columnNames := make([]string, 0, len(columnTypes)) + for _, columnType := range columnTypes { + columnName := columnType.Name() + if columnName == "" { + continue + } + columnNames = append(columnNames, columnName) + } + return columnNames, nil +} diff --git a/main.go b/main.go index 84fa153..875ccc0 100644 --- a/main.go +++ b/main.go @@ -235,6 +235,18 @@ func main() { panic(err) } + componentColumnNames, err := database.QueryComponentColumnNames(ctx, tx) + if err != nil { + logger.Error(ctx, "query component table column names failed", "error", err) + panic(err) + } + + err = model.StoreComponentColumnRecommend(ctx, fullParentPath, isLocalParentPath, componentColumnNames) + if err != nil { + logger.Error(ctx, "store component column recommend content failed", "error", err) + panic(err) + } + allMeasurement, err := database.GetAllMeasurements(ctx, tx) if err != nil { logger.Error(ctx, "load topologic info from postgres failed", "error", err) diff --git a/model/component_column_recommend_model.go b/model/component_column_recommend_model.go new file mode 100644 index 0000000..78e8b8b --- /dev/null +++ b/model/component_column_recommend_model.go @@ -0,0 +1,82 @@ +// Package model define model struct of model runtime service +package model + +import ( + "context" + "fmt" + + "modelRT/constants" + "modelRT/diagram" + + "github.com/RediSearch/redisearch-go/v2/redisearch" +) + +// StoreComponentColumnRecommend binds token6 component config to component table column names. +func StoreComponentColumnRecommend(ctx context.Context, compTagToFullPath map[string]string, isLocalCompTagToFullPath map[string]string, componentColumnNames []string) error { + rdb := diagram.GetRedisClientInstance() + pipe := rdb.Pipeline() + + pipe.SAdd(ctx, constants.RedisAllConfigSetKey, constants.ComponentConfigKey) + + if len(componentColumnNames) == 0 { + _, err := pipe.Exec(ctx) + return err + } + + componentColumnMembers := stringSliceToAny(componentColumnNames) + pipe.SAdd(ctx, constants.RedisAllMeasTagSetKey, componentColumnMembers...) + + sug := make([]redisearch.Suggestion, 0, len(compTagToFullPath)*len(componentColumnNames)*4) + for compTag, fullPath := range compTagToFullPath { + specCompMeasKey := fmt.Sprintf(constants.RedisSpecCompTagMeasSetKey, compTag) + pipe.SAdd(ctx, specCompMeasKey, componentColumnMembers...) + + isLocalFullPath := isLocalCompTagToFullPath[compTag] + fullConfigTerm := fmt.Sprintf("%s.%s", fullPath, constants.ComponentConfigKey) + sug = append(sug, redisearch.Suggestion{ + Term: fullConfigTerm, + Score: constants.DefaultScore, + }) + + if isLocalFullPath != "" { + localConfigTerm := fmt.Sprintf("%s.%s", isLocalFullPath, constants.ComponentConfigKey) + sug = append(sug, redisearch.Suggestion{ + Term: localConfigTerm, + Score: constants.DefaultScore, + }) + } + + for _, columnName := range componentColumnNames { + fullColumnTerm := fmt.Sprintf("%s.%s.%s", fullPath, constants.ComponentConfigKey, columnName) + sug = append(sug, redisearch.Suggestion{ + Term: fullColumnTerm, + Score: constants.DefaultScore, + }) + + if isLocalFullPath == "" { + continue + } + + localColumnTerm := fmt.Sprintf("%s.%s.%s", isLocalFullPath, constants.ComponentConfigKey, columnName) + sug = append(sug, redisearch.Suggestion{ + Term: localColumnTerm, + Score: constants.DefaultScore, + }) + } + } + + if len(sug) > 0 { + ac.AddTerms(sug...) + } + + _, err := pipe.Exec(ctx) + return err +} + +func stringSliceToAny(values []string) []any { + members := make([]any, 0, len(values)) + for _, value := range values { + members = append(members, value) + } + return members +}