This repository has been archived on 2024-04-06. You can view files and clone it, but cannot push or open issues or pull requests.
core-old/internal/service/tribe_snapshot.go
Dawid Wysokiński c6be9a6012
All checks were successful
continuous-integration/drone/push Build is passing
feat: new rest endpoint - /versions/{code}/servers/{key}/tribes/{id}/history (#180)
Reviewed-on: twhelp/core#180
2023-02-08 05:50:54 +00:00

142 lines
3.8 KiB
Go

package service
import (
"context"
"fmt"
"time"
"gitea.dwysokinski.me/twhelp/core/internal/domain"
)
const (
tribeSnapshotMaxLimit = 100
tribeSnapshotSortMaxLen = 2
)
//counterfeiter:generate -o internal/mock/tribe_lister.gen.go . TribeLister
type TribeLister interface {
List(ctx context.Context, params domain.ListTribesParams) ([]domain.Tribe, error)
}
//counterfeiter:generate -o internal/mock/tribe_snapshot_repository.gen.go . TribeSnapshotRepository
type TribeSnapshotRepository interface {
Create(ctx context.Context, params ...domain.CreateTribeSnapshotParams) error
ListCount(ctx context.Context, params domain.ListTribeSnapshotsParams) ([]domain.TribeSnapshot, int64, error)
Delete(ctx context.Context, serverKey string, createdAtLTE time.Time) error
}
type TribeSnapshot struct {
repo TribeSnapshotRepository
tribeSvc TribeLister
}
func NewTribeSnapshot(repo TribeSnapshotRepository, tribeSvc TribeLister) *TribeSnapshot {
return &TribeSnapshot{repo: repo, tribeSvc: tribeSvc}
}
func (t *TribeSnapshot) Create(ctx context.Context, key string, date time.Time) error {
var lastID int64
for {
tribes, err := t.tribeSvc.List(ctx, domain.ListTribesParams{
ServerKeys: []string{key},
IDGT: domain.NullInt64{
Int64: lastID,
Valid: true,
},
Deleted: domain.NullBool{
Valid: true,
Bool: false,
},
Pagination: domain.Pagination{
Limit: tribeMaxLimit,
},
Sort: []domain.TribeSort{
{By: domain.TribeSortByID, Direction: domain.SortDirectionASC},
},
})
if err != nil {
return fmt.Errorf("TribeRepository.List: %w", err)
}
params := make([]domain.CreateTribeSnapshotParams, 0, len(tribes))
for _, tribe := range tribes {
params = append(params, domain.CreateTribeSnapshotParams{
OpponentsDefeated: tribe.OpponentsDefeated,
TribeID: tribe.ID,
ServerKey: tribe.ServerKey,
NumMembers: tribe.NumMembers,
NumVillages: tribe.NumVillages,
Points: tribe.Points,
AllPoints: tribe.AllPoints,
Rank: tribe.Rank,
Dominance: tribe.Dominance,
Date: date,
})
}
if err = t.repo.Create(ctx, params...); err != nil {
return fmt.Errorf("TribeSnapshotRepository.Create: %w", err)
}
if len(tribes) < tribeMaxLimit {
break
}
lastID = tribes[len(tribes)-1].ID
}
return nil
}
func (t *TribeSnapshot) ListCount(ctx context.Context, params domain.ListTribeSnapshotsParams) ([]domain.TribeSnapshot, int64, error) {
if len(params.Sort) == 0 {
params.Sort = []domain.TribeSnapshotSort{
{
By: domain.TribeSnapshotSortByDate,
Direction: domain.SortDirectionASC,
},
{
By: domain.TribeSnapshotSortByID,
Direction: domain.SortDirectionASC,
},
}
}
if params.Pagination.Limit == 0 {
params.Pagination.Limit = tribeSnapshotMaxLimit
}
if len(params.Sort) > tribeSnapshotSortMaxLen {
return nil, 0, domain.ValidationError{
Field: "sort",
Err: domain.MaxLengthError{
Max: tribeSnapshotSortMaxLen,
},
}
}
if err := validatePagination(params.Pagination, tribeSnapshotMaxLimit); err != nil {
return nil, 0, fmt.Errorf("validatePagination: %w", err)
}
snapshots, count, err := t.repo.ListCount(ctx, params)
if err != nil {
return nil, 0, fmt.Errorf("TribeSnapshotRepository.ListCountWithRelations: %w", err)
}
return snapshots, count, nil
}
func (t *TribeSnapshot) CleanUp(ctx context.Context, srv domain.Server) error {
if srv.Special || srv.Open || srv.TribeSnapshotsCreatedAt.After(time.Now().Add(-30*24*time.Hour) /* 30 days */) {
return nil
}
// delete snapshots older than 6 months
if err := t.repo.Delete(ctx, srv.Key, time.Now().Add(-180*24*time.Hour)); err != nil {
return fmt.Errorf("couldn't delete old tribe snapshots: %w", err)
}
return nil
}