From 945b13524ef6ef685827847c721fb19d1e85bef7 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Wed, 23 Feb 2022 21:48:43 +0100 Subject: [PATCH] Added nightly account value snapshots. --- .../dao/AccountValueSnapshotRepository.java | 12 ++++++++ .../coyotecredit/service/AccountService.java | 29 ++++++++++++++++--- .../coyotecredit/service/ExchangeService.java | 4 +-- 3 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 src/main/java/nl/andrewl/coyotecredit/dao/AccountValueSnapshotRepository.java diff --git a/src/main/java/nl/andrewl/coyotecredit/dao/AccountValueSnapshotRepository.java b/src/main/java/nl/andrewl/coyotecredit/dao/AccountValueSnapshotRepository.java new file mode 100644 index 0000000..d19a38d --- /dev/null +++ b/src/main/java/nl/andrewl/coyotecredit/dao/AccountValueSnapshotRepository.java @@ -0,0 +1,12 @@ +package nl.andrewl.coyotecredit.dao; + +import nl.andrewl.coyotecredit.model.Account; +import nl.andrewl.coyotecredit.model.AccountValueSnapshot; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface AccountValueSnapshotRepository extends JpaRepository { + + void deleteAllByAccount(Account account); +} diff --git a/src/main/java/nl/andrewl/coyotecredit/service/AccountService.java b/src/main/java/nl/andrewl/coyotecredit/service/AccountService.java index 8563a75..1b295b7 100644 --- a/src/main/java/nl/andrewl/coyotecredit/service/AccountService.java +++ b/src/main/java/nl/andrewl/coyotecredit/service/AccountService.java @@ -2,20 +2,23 @@ package nl.andrewl.coyotecredit.service; import lombok.RequiredArgsConstructor; import nl.andrewl.coyotecredit.ctl.dto.*; -import nl.andrewl.coyotecredit.dao.AccountRepository; -import nl.andrewl.coyotecredit.dao.TradeableRepository; -import nl.andrewl.coyotecredit.dao.TransactionRepository; -import nl.andrewl.coyotecredit.dao.TransferRepository; +import nl.andrewl.coyotecredit.dao.*; import nl.andrewl.coyotecredit.model.*; import nl.andrewl.coyotecredit.util.AccountNumberUtils; +import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; import org.springframework.http.HttpStatus; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.MultiValueMap; import org.springframework.web.server.ResponseStatusException; import java.math.BigDecimal; +import java.time.LocalDateTime; +import java.time.ZoneOffset; +import java.util.ArrayList; import java.util.Comparator; import java.util.List; @@ -26,6 +29,7 @@ public class AccountService { private final TransactionRepository transactionRepository; private final TradeableRepository tradeableRepository; private final TransferRepository transferRepository; + private final AccountValueSnapshotRepository valueSnapshotRepository; @Transactional(readOnly = true) public List getTransferData(long accountId, User user) { @@ -169,4 +173,21 @@ public class AccountService { } accountRepository.save(account); } + + @Scheduled(cron = "@midnight") + @Transactional + public void doAccountValueSnapshots() { + Pageable pageable = Pageable.ofSize(10); + LocalDateTime timestamp = LocalDateTime.now(ZoneOffset.UTC); + Page page = accountRepository.findAll(pageable); + while (!page.isEmpty()) { + List snapshots = new ArrayList<>(); + for (var account : page.getContent()) { + snapshots.add(new AccountValueSnapshot(account, timestamp, account.getTotalBalanceUsd())); + } + valueSnapshotRepository.saveAll(snapshots); + pageable = pageable.next(); + page = accountRepository.findAll(pageable); + } + } } diff --git a/src/main/java/nl/andrewl/coyotecredit/service/ExchangeService.java b/src/main/java/nl/andrewl/coyotecredit/service/ExchangeService.java index 0fbcc88..40286c5 100644 --- a/src/main/java/nl/andrewl/coyotecredit/service/ExchangeService.java +++ b/src/main/java/nl/andrewl/coyotecredit/service/ExchangeService.java @@ -10,7 +10,6 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; -import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.server.ResponseStatusException; @@ -31,10 +30,10 @@ public class ExchangeService { private final AccountRepository accountRepository; private final TransactionRepository transactionRepository; private final TradeableRepository tradeableRepository; + private final AccountValueSnapshotRepository accountValueSnapshotRepository; private final UserRepository userRepository; private final ExchangeInvitationRepository invitationRepository; private final JavaMailSender mailSender; - private final PasswordEncoder passwordEncoder; @Value("${coyote-credit.base-url}") private String baseUrl; @@ -122,6 +121,7 @@ public class ExchangeService { if (!userAccount.isAdmin()) { throw new ResponseStatusException(HttpStatus.NOT_FOUND); } + accountValueSnapshotRepository.deleteAllByAccount(account); accountRepository.delete(account); }