Added nightly account value snapshots.
This commit is contained in:
		
							parent
							
								
									f2f1df5b42
								
							
						
					
					
						commit
						945b13524e
					
				| 
						 | 
					@ -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<AccountValueSnapshot, Long> {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						void deleteAllByAccount(Account account);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -2,20 +2,23 @@ package nl.andrewl.coyotecredit.service;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import lombok.RequiredArgsConstructor;
 | 
					import lombok.RequiredArgsConstructor;
 | 
				
			||||||
import nl.andrewl.coyotecredit.ctl.dto.*;
 | 
					import nl.andrewl.coyotecredit.ctl.dto.*;
 | 
				
			||||||
import nl.andrewl.coyotecredit.dao.AccountRepository;
 | 
					import nl.andrewl.coyotecredit.dao.*;
 | 
				
			||||||
import nl.andrewl.coyotecredit.dao.TradeableRepository;
 | 
					 | 
				
			||||||
import nl.andrewl.coyotecredit.dao.TransactionRepository;
 | 
					 | 
				
			||||||
import nl.andrewl.coyotecredit.dao.TransferRepository;
 | 
					 | 
				
			||||||
import nl.andrewl.coyotecredit.model.*;
 | 
					import nl.andrewl.coyotecredit.model.*;
 | 
				
			||||||
import nl.andrewl.coyotecredit.util.AccountNumberUtils;
 | 
					import nl.andrewl.coyotecredit.util.AccountNumberUtils;
 | 
				
			||||||
 | 
					import org.springframework.data.domain.Page;
 | 
				
			||||||
import org.springframework.data.domain.PageRequest;
 | 
					import org.springframework.data.domain.PageRequest;
 | 
				
			||||||
 | 
					import org.springframework.data.domain.Pageable;
 | 
				
			||||||
import org.springframework.http.HttpStatus;
 | 
					import org.springframework.http.HttpStatus;
 | 
				
			||||||
 | 
					import org.springframework.scheduling.annotation.Scheduled;
 | 
				
			||||||
import org.springframework.stereotype.Service;
 | 
					import org.springframework.stereotype.Service;
 | 
				
			||||||
import org.springframework.transaction.annotation.Transactional;
 | 
					import org.springframework.transaction.annotation.Transactional;
 | 
				
			||||||
import org.springframework.util.MultiValueMap;
 | 
					import org.springframework.util.MultiValueMap;
 | 
				
			||||||
import org.springframework.web.server.ResponseStatusException;
 | 
					import org.springframework.web.server.ResponseStatusException;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.math.BigDecimal;
 | 
					import java.math.BigDecimal;
 | 
				
			||||||
 | 
					import java.time.LocalDateTime;
 | 
				
			||||||
 | 
					import java.time.ZoneOffset;
 | 
				
			||||||
 | 
					import java.util.ArrayList;
 | 
				
			||||||
import java.util.Comparator;
 | 
					import java.util.Comparator;
 | 
				
			||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -26,6 +29,7 @@ public class AccountService {
 | 
				
			||||||
	private final TransactionRepository transactionRepository;
 | 
						private final TransactionRepository transactionRepository;
 | 
				
			||||||
	private final TradeableRepository tradeableRepository;
 | 
						private final TradeableRepository tradeableRepository;
 | 
				
			||||||
	private final TransferRepository transferRepository;
 | 
						private final TransferRepository transferRepository;
 | 
				
			||||||
 | 
						private final AccountValueSnapshotRepository valueSnapshotRepository;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	@Transactional(readOnly = true)
 | 
						@Transactional(readOnly = true)
 | 
				
			||||||
	public List<BalanceData> getTransferData(long accountId, User user) {
 | 
						public List<BalanceData> getTransferData(long accountId, User user) {
 | 
				
			||||||
| 
						 | 
					@ -169,4 +173,21 @@ public class AccountService {
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		accountRepository.save(account);
 | 
							accountRepository.save(account);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						@Scheduled(cron = "@midnight")
 | 
				
			||||||
 | 
						@Transactional
 | 
				
			||||||
 | 
						public void doAccountValueSnapshots() {
 | 
				
			||||||
 | 
							Pageable pageable = Pageable.ofSize(10);
 | 
				
			||||||
 | 
							LocalDateTime timestamp = LocalDateTime.now(ZoneOffset.UTC);
 | 
				
			||||||
 | 
							Page<Account> page = accountRepository.findAll(pageable);
 | 
				
			||||||
 | 
							while (!page.isEmpty()) {
 | 
				
			||||||
 | 
								List<AccountValueSnapshot> 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);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -10,7 +10,6 @@ import org.springframework.beans.factory.annotation.Value;
 | 
				
			||||||
import org.springframework.http.HttpStatus;
 | 
					import org.springframework.http.HttpStatus;
 | 
				
			||||||
import org.springframework.mail.javamail.JavaMailSender;
 | 
					import org.springframework.mail.javamail.JavaMailSender;
 | 
				
			||||||
import org.springframework.mail.javamail.MimeMessageHelper;
 | 
					import org.springframework.mail.javamail.MimeMessageHelper;
 | 
				
			||||||
import org.springframework.security.crypto.password.PasswordEncoder;
 | 
					 | 
				
			||||||
import org.springframework.stereotype.Service;
 | 
					import org.springframework.stereotype.Service;
 | 
				
			||||||
import org.springframework.transaction.annotation.Transactional;
 | 
					import org.springframework.transaction.annotation.Transactional;
 | 
				
			||||||
import org.springframework.web.server.ResponseStatusException;
 | 
					import org.springframework.web.server.ResponseStatusException;
 | 
				
			||||||
| 
						 | 
					@ -31,10 +30,10 @@ public class ExchangeService {
 | 
				
			||||||
	private final AccountRepository accountRepository;
 | 
						private final AccountRepository accountRepository;
 | 
				
			||||||
	private final TransactionRepository transactionRepository;
 | 
						private final TransactionRepository transactionRepository;
 | 
				
			||||||
	private final TradeableRepository tradeableRepository;
 | 
						private final TradeableRepository tradeableRepository;
 | 
				
			||||||
 | 
						private final AccountValueSnapshotRepository accountValueSnapshotRepository;
 | 
				
			||||||
	private final UserRepository userRepository;
 | 
						private final UserRepository userRepository;
 | 
				
			||||||
	private final ExchangeInvitationRepository invitationRepository;
 | 
						private final ExchangeInvitationRepository invitationRepository;
 | 
				
			||||||
	private final JavaMailSender mailSender;
 | 
						private final JavaMailSender mailSender;
 | 
				
			||||||
	private final PasswordEncoder passwordEncoder;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	@Value("${coyote-credit.base-url}")
 | 
						@Value("${coyote-credit.base-url}")
 | 
				
			||||||
	private String baseUrl;
 | 
						private String baseUrl;
 | 
				
			||||||
| 
						 | 
					@ -122,6 +121,7 @@ public class ExchangeService {
 | 
				
			||||||
		if (!userAccount.isAdmin()) {
 | 
							if (!userAccount.isAdmin()) {
 | 
				
			||||||
			throw new ResponseStatusException(HttpStatus.NOT_FOUND);
 | 
								throw new ResponseStatusException(HttpStatus.NOT_FOUND);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
							accountValueSnapshotRepository.deleteAllByAccount(account);
 | 
				
			||||||
		accountRepository.delete(account);
 | 
							accountRepository.delete(account);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue