2025-04-17 18:01:00: merge [main:0e6b5dec5225670f20a213993b54e1c0fcd44c63];
This commit is contained in:
1 parent
22baffabfb
commit
18205b2e66
40 files changed
+483
-161
No files matched your search
+77
-10
@@ -6,6 +6,9 @@ import (
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/txix-open/go-cmp/cmp"
|
||||
"github.com/txix-open/isp-kit/json"
|
||||
"github.com/txix-open/isp-kit/log"
|
||||
"github.com/xeipuuv/gojsonschema"
|
||||
"isp-config-service/domain"
|
||||
"isp-config-service/entity"
|
||||
@@ -20,6 +23,7 @@ type ConfigRepo interface {
|
||||
Insert(ctx context.Context, cfg entity.Config) error
|
||||
UpdateByVersion(ctx context.Context, cfg entity.Config) (bool, error)
|
||||
SetActive(ctx context.Context, configId string, active xtypes.Bool) error
|
||||
UpdateConfigName(ctx context.Context, req domain.UpdateConfigNameRequest) (bool, error)
|
||||
}
|
||||
|
||||
type EventRepo interface {
|
||||
@@ -42,6 +46,7 @@ type Config struct {
|
||||
eventRepo EventRepo
|
||||
configHistoryService ConfigHistoryService
|
||||
variableService VariableService
|
||||
logger log.Logger
|
||||
}
|
||||
|
||||
func NewConfig(
|
||||
@@ -51,6 +56,7 @@ func NewConfig(
|
||||
eventRepo EventRepo,
|
||||
configHistoryService ConfigHistoryService,
|
||||
variableService VariableService,
|
||||
logger log.Logger,
|
||||
) Config {
|
||||
return Config{
|
||||
configRepo: configRepo,
|
||||
@@ -59,6 +65,7 @@ func NewConfig(
|
||||
eventRepo: eventRepo,
|
||||
configHistoryService: configHistoryService,
|
||||
variableService: variableService,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,7 +121,7 @@ func (c Config) CreateUpdateConfig(
|
||||
ctx context.Context,
|
||||
adminId int,
|
||||
req domain.CreateUpdateConfigRequest,
|
||||
) (*domain.Config, error) {
|
||||
) (*domain.CreateUpdateConfigResponse, error) {
|
||||
if !req.Unsafe {
|
||||
err := c.validateConfigUpdate(ctx, req.ModuleId, req.Data)
|
||||
if err != nil {
|
||||
@@ -123,7 +130,9 @@ func (c Config) CreateUpdateConfig(
|
||||
}
|
||||
|
||||
if req.Id == "" {
|
||||
return c.insertNewConfig(ctx, adminId, req)
|
||||
newConfig, err := c.insertNewConfig(ctx, adminId, req)
|
||||
return &domain.CreateUpdateConfigResponse{
|
||||
Config: newConfig, IsChanged: true}, err
|
||||
}
|
||||
|
||||
variableExtractionResult, err := c.extractAndCheckVariables(ctx, req.Id, req.Data)
|
||||
@@ -139,6 +148,15 @@ func (c Config) CreateUpdateConfig(
|
||||
return nil, entity.ErrConfigNotFound
|
||||
}
|
||||
|
||||
isMatch, err := isEqualConfigs(oldConfig, req)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "compare data configs")
|
||||
}
|
||||
if isMatch {
|
||||
c.logger.Info(ctx, "no changes in config; skip update")
|
||||
return &domain.CreateUpdateConfigResponse{Config: configToDto(*oldConfig, nil), IsChanged: false}, nil
|
||||
}
|
||||
|
||||
config := entity.Config{
|
||||
Id: req.Id,
|
||||
Name: req.Name,
|
||||
@@ -158,7 +176,6 @@ func (c Config) CreateUpdateConfig(
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "change config history")
|
||||
}
|
||||
|
||||
err = c.variableService.SaveVariableLinks(ctx, config.Id, variableExtractionResult.VariableLinks)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "save variable links")
|
||||
@@ -179,8 +196,8 @@ func (c Config) CreateUpdateConfig(
|
||||
Version: req.Version + 1,
|
||||
Active: oldConfig.Active,
|
||||
}
|
||||
result := configToDto(newConfig, nil)
|
||||
return &result, nil
|
||||
return &domain.CreateUpdateConfigResponse{
|
||||
Config: configToDto(newConfig, nil), IsChanged: true}, nil
|
||||
}
|
||||
|
||||
func (c Config) GetConfigById(ctx context.Context, configId string) (*domain.Config, error) {
|
||||
@@ -240,7 +257,37 @@ func (c Config) DeleteConfig(ctx context.Context, configId string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c Config) insertNewConfig(ctx context.Context, adminId int, req domain.CreateUpdateConfigRequest) (*domain.Config, error) {
|
||||
func (c Config) UpdateConfigName(ctx context.Context, req domain.UpdateConfigNameRequest) error {
|
||||
exist, err := c.configRepo.UpdateConfigName(ctx, req)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "update config name")
|
||||
}
|
||||
if !exist {
|
||||
return entity.ErrConfigNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c Config) SyncConfig(ctx context.Context, moduleName string) error {
|
||||
modules, err := c.moduleRepo.GetByNames(ctx, []string{moduleName})
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "get config by module name")
|
||||
}
|
||||
if len(modules) == 0 {
|
||||
return entity.ErrModuleNotFound
|
||||
}
|
||||
|
||||
moduleId := modules[0].Id
|
||||
err = c.emitChangeActiveConfigEvent(ctx, moduleId)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "emit change active config event")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c Config) insertNewConfig(ctx context.Context, adminId int, req domain.CreateUpdateConfigRequest) (domain.Config, error) {
|
||||
config := entity.Config{
|
||||
Id: uuid.NewString(),
|
||||
Name: req.Name,
|
||||
@@ -252,20 +299,20 @@ func (c Config) insertNewConfig(ctx context.Context, adminId int, req domain.Cre
|
||||
}
|
||||
err := c.configRepo.Insert(ctx, config)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "insert new config")
|
||||
return domain.Config{}, errors.WithMessage(err, "insert new config")
|
||||
}
|
||||
|
||||
extractResult, err := c.extractAndCheckVariables(ctx, config.Id, config.Data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return domain.Config{}, err
|
||||
}
|
||||
err = c.variableService.SaveVariableLinks(ctx, config.Id, extractResult.VariableLinks)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "save variable links")
|
||||
return domain.Config{}, errors.WithMessage(err, "save variable links")
|
||||
}
|
||||
|
||||
result := configToDto(config, nil)
|
||||
return &result, nil
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (c Config) extractAndCheckVariables(ctx context.Context, configId string, input []byte) (*domain.VariableExtractionResult, error) {
|
||||
@@ -349,3 +396,23 @@ func validateConfig(config []byte, schema []byte) (map[string]string, error) {
|
||||
}
|
||||
return details, nil
|
||||
}
|
||||
|
||||
func isEqualConfigs(oldConfig *entity.Config, reqConfig domain.CreateUpdateConfigRequest) (bool, error) {
|
||||
if oldConfig.Name != reqConfig.Name {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
oldDataMap := make(map[string]any)
|
||||
err := json.Unmarshal(oldConfig.Data, &oldDataMap)
|
||||
if err != nil {
|
||||
return false, errors.WithMessage(err, "unmarshal actual config data")
|
||||
}
|
||||
|
||||
reqDataMap := make(map[string]any)
|
||||
err = json.Unmarshal(reqConfig.Data, &reqDataMap)
|
||||
if err != nil {
|
||||
return false, errors.WithMessage(err, "unmarshal request config data")
|
||||
}
|
||||
|
||||
return cmp.Equal(oldDataMap, reqDataMap), nil
|
||||
}
|
||||
@@ -3,9 +3,10 @@ package api
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/txix-open/isp-kit/log"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"isp-config-service/domain"
|
||||
@@ -20,18 +21,21 @@ type ConfigHistoryRepo interface {
|
||||
}
|
||||
|
||||
type ConfigHistory struct {
|
||||
configRepo ConfigRepo
|
||||
repo ConfigHistoryRepo
|
||||
keepVersions int
|
||||
logger log.Logger
|
||||
}
|
||||
|
||||
func NewConfigHistory(
|
||||
configRepo ConfigRepo,
|
||||
repo ConfigHistoryRepo,
|
||||
keepVersions int,
|
||||
logger log.Logger,
|
||||
) ConfigHistory {
|
||||
return ConfigHistory{
|
||||
repo: repo,
|
||||
configRepo: configRepo,
|
||||
keepVersions: keepVersions,
|
||||
logger: logger,
|
||||
}
|
||||
@@ -42,8 +46,23 @@ func (s ConfigHistory) GetAllVersions(ctx context.Context, configId string) ([]d
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get config versions")
|
||||
}
|
||||
actualCfg, err := s.configRepo.GetById(ctx, configId)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get actual config by id")
|
||||
}
|
||||
if actualCfg == nil {
|
||||
return nil, entity.ErrConfigNotFound
|
||||
}
|
||||
|
||||
result := make([]domain.ConfigVersion, 0, len(versions)+1)
|
||||
result = append(result, domain.ConfigVersion{
|
||||
Id: uuid.NewString(),
|
||||
ConfigId: actualCfg.Id,
|
||||
ConfigVersion: actualCfg.Version,
|
||||
Data: actualCfg.Data,
|
||||
CreatedAt: time.Time(actualCfg.UpdatedAt),
|
||||
})
|
||||
|
||||
result := make([]domain.ConfigVersion, 0, len(versions))
|
||||
for _, version := range versions {
|
||||
result = append(result, domain.ConfigVersion{
|
||||
Id: version.Id,
|
||||
@@ -67,11 +86,12 @@ func (s ConfigHistory) Delete(ctx context.Context, id string) error {
|
||||
|
||||
func (s ConfigHistory) OnUpdateConfig(ctx context.Context, oldConfig entity.Config) error {
|
||||
history := entity.ConfigHistory{
|
||||
Id: uuid.NewString(),
|
||||
ConfigId: oldConfig.Id,
|
||||
Data: oldConfig.Data,
|
||||
Version: oldConfig.Version,
|
||||
AdminId: oldConfig.AdminId,
|
||||
Id: uuid.NewString(),
|
||||
ConfigId: oldConfig.Id,
|
||||
Data: oldConfig.Data,
|
||||
Version: oldConfig.Version,
|
||||
AdminId: oldConfig.AdminId,
|
||||
CreatedAt: oldConfig.UpdatedAt,
|
||||
}
|
||||
err := s.repo.Insert(ctx, history)
|
||||
if err != nil {
|
||||
|
||||
Reference in new issue
Block a user