From 8ef12f8b1157c9bde7ce625fcff717088cba5320 Mon Sep 17 00:00:00 2001 From: likhachevam Date: Tue, 7 Oct 2025 17:27:58 +0300 Subject: [PATCH] MSP-6459 update /purge_version endpoint --- assembly/locator.go | 19 +++++++++++-------- controller/api/config_history.go | 16 +++++++--------- docs/swagger.yaml | 13 +++++++------ domain/request.go | 2 +- service/api/config_history.go | 31 ++++++++++++++++++++++++++----- 5 files changed, 52 insertions(+), 29 deletions(-) diff --git a/assembly/locator.go b/assembly/locator.go index f6705c0..cb46ad2 100644 --- a/assembly/locator.go +++ b/assembly/locator.go @@ -7,17 +7,13 @@ import ( "net/url" "time" - httpEndpoint "github.com/txix-open/isp-kit/http/endpoint" - "github.com/txix-open/isp-kit/http/endpoint/httplog" "isp-config-service/conf" "isp-config-service/service/module/backend" "isp-config-service/service/variable" - "github.com/txix-open/etp/v4" - "github.com/txix-open/isp-kit/grpc" - "github.com/txix-open/isp-kit/grpc/endpoint" - "github.com/txix-open/isp-kit/log" - "github.com/txix-open/isp-kit/worker" + httpEndpoint "github.com/txix-open/isp-kit/http/endpoint" + "github.com/txix-open/isp-kit/http/endpoint/httplog" + "isp-config-service/controller" "isp-config-service/controller/api" "isp-config-service/repository" @@ -28,6 +24,12 @@ import ( "isp-config-service/service/module" "isp-config-service/service/rqlite/db" "isp-config-service/service/subscription" + + "github.com/txix-open/etp/v4" + "github.com/txix-open/isp-kit/grpc" + "github.com/txix-open/isp-kit/grpc/endpoint" + "github.com/txix-open/isp-kit/log" + "github.com/txix-open/isp-kit/worker" ) const ( @@ -135,7 +137,8 @@ func (l Locator) Config() *Config { ) moduleController := controller.NewModule(moduleService, l.logger) - configHistoryApiService := apisvs.NewConfigHistory(configRepo, configHistoryRepo, l.cfg.Local.KeepConfigVersions, l.logger) + configHistoryApiService := apisvs.NewConfigHistory(configRepo, moduleRepo, configHistoryRepo, + l.cfg.Local.KeepConfigVersions, l.logger) configHistoryController := api.NewConfigHistory(configHistoryApiService) moduleApiService := apisvs.NewModule(moduleRepo, backendRepo, configSchemaRepo) diff --git a/controller/api/config_history.go b/controller/api/config_history.go index 18b6791..e7b586f 100644 --- a/controller/api/config_history.go +++ b/controller/api/config_history.go @@ -13,7 +13,7 @@ import ( type ConfigHistoryService interface { GetAllVersions(ctx context.Context, configId string) ([]domain.ConfigVersion, error) Delete(ctx context.Context, id string) error - Purge(ctx context.Context, configId string, keepVersions int) (int, error) + Purge(ctx context.Context, moduleName string, keepVersions int) (map[string]int, error) } type ConfigHistory struct { @@ -75,22 +75,20 @@ func (c ConfigHistory) DeleteConfigVersion(ctx context.Context, req domain.IdReq } // PurgeConfigVersions -// @Summary Метод удаления всех версий конфигурации, за исключением n последних -// @Description Возвращает количество удаленных версий +// @Summary Метод удаления старых версий всех конфигураций модуля, оставляет keepVersions последних версий каждой конфигурации +// @Description Возвращает количество удаленных версий каждой конфигурации // @Tags Конфигурация // @Accept json // @Produce json -// @Param body body domain.PurgeConfigVersionsRequest true "id конфигурации, количество оставленных версий конфигурации" +// @Param body body domain.PurgeConfigVersionsRequest true "Название модуля, количество оставленных версий конфигурации" // @Success 200 {object} domain.DeleteResponse // @Failure 400 {object} apierrors.Error "не указан массив идентификаторов" // @Failure 500 {object} apierrors.Error // @Router /config/purge_versions [POST] -func (c ConfigHistory) PurgeConfigVersions(ctx context.Context, req domain.PurgeConfigVersionsRequest) (*domain.DeleteResponse, error) { - deleted, err := c.service.Purge(ctx, req.ConfigId, req.KeepVersions) +func (c ConfigHistory) PurgeConfigVersions(ctx context.Context, req domain.PurgeConfigVersionsRequest) (map[string]int, error) { + result, err := c.service.Purge(ctx, req.ModuleName, req.KeepVersions) if err != nil { return nil, apierrors.NewInternalServiceError(err) } - return &domain.DeleteResponse{ - Deleted: deleted, - }, nil + return result, nil } diff --git a/docs/swagger.yaml b/docs/swagger.yaml index df06809..c4981ee 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -210,13 +210,13 @@ definitions: type: object domain.PurgeConfigVersionsRequest: properties: - configId: - type: string keepVersions: type: integer + moduleName: + type: string required: - - configId - keepVersions + - moduleName type: object domain.SyncConfigRequest: properties: @@ -573,9 +573,9 @@ paths: post: consumes: - application/json - description: Возвращает количество удаленных версий + description: Возвращает количество удаленных версий каждой конфигурации parameters: - - description: id конфигурации, количество оставленных версий конфигурации + - description: Название модуля, количество оставленных версий конфигурации in: body name: body required: true @@ -596,7 +596,8 @@ paths: description: Internal Server Error schema: $ref: '#/definitions/github_com_txix-open_isp-kit_grpc_apierrors.Error' - summary: Метод удаления всех версий конфигурации, за исключением n последних + summary: Метод удаления старых версий всех конфигураций модуля, оставляет keepVersions + последних версий каждой конфигурации tags: - Конфигурация /config/sync_config: diff --git a/domain/request.go b/domain/request.go index 7b9166d..53889d5 100644 --- a/domain/request.go +++ b/domain/request.go @@ -9,7 +9,7 @@ type IdRequest struct { } type PurgeConfigVersionsRequest struct { - ConfigId string `validate:"required"` + ModuleName string `validate:"required"` KeepVersions int `validate:"required"` } diff --git a/service/api/config_history.go b/service/api/config_history.go index dad97bc..49fa1b0 100644 --- a/service/api/config_history.go +++ b/service/api/config_history.go @@ -23,6 +23,7 @@ type ConfigHistoryRepo interface { type ConfigHistory struct { configRepo ConfigRepo + moduleRepo ModuleRepo repo ConfigHistoryRepo keepVersions int logger log.Logger @@ -30,12 +31,14 @@ type ConfigHistory struct { func NewConfigHistory( configRepo ConfigRepo, + moduleRepo ModuleRepo, repo ConfigHistoryRepo, keepVersions int, logger log.Logger, ) ConfigHistory { return ConfigHistory{ repo: repo, + moduleRepo: moduleRepo, configRepo: configRepo, keepVersions: keepVersions, logger: logger, @@ -85,14 +88,32 @@ func (s ConfigHistory) Delete(ctx context.Context, id string) error { return nil } -func (s ConfigHistory) Purge(ctx context.Context, configId string, keepVersions int) (int, error) { - deletedCount, err := s.repo.DeleteOld(ctx, configId, keepVersions) +func (s ConfigHistory) Purge(ctx context.Context, moduleName string, keepVersions int) (map[string]int, error) { + modules, err := s.moduleRepo.GetByNames(ctx, []string{moduleName}) if err != nil { - return 0, errors.WithMessage(err, "delete old config versions") + return nil, errors.WithMessage(err, "get config by module name") + } + if len(modules) == 0 { + return nil, entity.ErrModuleNotFound } - s.logger.Debug(ctx, fmt.Sprintf("delete '%d' old config versions", deletedCount)) - return deletedCount, nil + configs, err := s.configRepo.GetByModuleId(ctx, modules[0].Id) + if err != nil { + return nil, errors.WithMessage(err, "get configs by module id") + } + + result := make(map[string]int) + for _, config := range configs { + deletedCount, err := s.repo.DeleteOld(ctx, config.Id, keepVersions) + if err != nil { + return nil, errors.WithMessage(err, "delete old config") + } + s.logger.Debug(ctx, fmt.Sprintf("delete '%d' old version of config '%s'", deletedCount, config.Id)) + + result[config.Id] = deletedCount + } + + return result, nil } func (s ConfigHistory) OnUpdateConfig(ctx context.Context, oldConfig entity.Config) error {