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
This commit is contained in:
douxu 2026-07-01 14:37:13 +08:00
parent 66870c7008
commit 491c10e8c5
4 changed files with 124 additions and 0 deletions

View File

@ -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 (

View File

@ -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
}

12
main.go
View File

@ -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)

View File

@ -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
}