97 lines
2.6 KiB
Go
97 lines
2.6 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"gitea.dwysokinski.me/twhelp/core/internal/domain"
|
|
)
|
|
|
|
//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
|
|
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) 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
|
|
}
|