MSP-6459 update /purge_version endpoint

This commit is contained in:
likhachevam committed 2025-10-07 17:27:58 +03:00
1 parent 99782ef3bb
commit 8ef12f8b11
5 files changed
+52 -29

No files matched your search

+11 -8
View File
@@ -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)
+7 -9
View File
@@ -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
}
+7 -6
View File
@@ -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:
+1 -1
View File
@@ -9,7 +9,7 @@ type IdRequest struct {
}
type PurgeConfigVersionsRequest struct {
ConfigId string `validate:"required"`
ModuleName string `validate:"required"`
KeepVersions int `validate:"required"`
}
+26 -5
View File
@@ -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 {