Skip to content

Commit

Permalink
Framework for handling warning email.
Browse files Browse the repository at this point in the history
  • Loading branch information
evrii committed Oct 29, 2024
1 parent 16836c5 commit 2fc7cf4
Showing 1 changed file with 52 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.pmiops.workbench.exceptions.WorkbenchException;
import org.pmiops.workbench.institution.InstitutionService;
import org.pmiops.workbench.leonardo.LeonardoApiClient;
import org.pmiops.workbench.mail.MailService;
import org.pmiops.workbench.model.BillingStatus;
import org.pmiops.workbench.utils.BillingUtils;
import org.slf4j.LoggerFactory;
Expand All @@ -33,6 +34,7 @@ public class InitialCreditsExpirationServiceImpl implements InitialCreditsExpira
private final Provider<WorkbenchConfig> workbenchConfigProvider;
private final LeonardoApiClient leonardoApiClient;
private final InstitutionService institutionService;
private final MailService mailService;

@Autowired
public InitialCreditsExpirationServiceImpl(
Expand All @@ -41,13 +43,14 @@ public InitialCreditsExpirationServiceImpl(
Clock clock,
Provider<WorkbenchConfig> workbenchConfigProvider,
LeonardoApiClient leonardoApiClient,
InstitutionService institutionService) {
InstitutionService institutionService, MailService mailService) {
this.userDao = userDao;
this.clock = clock;
this.workspaceDao = workspaceDao;
this.workbenchConfigProvider = workbenchConfigProvider;
this.leonardoApiClient = leonardoApiClient;
this.institutionService = institutionService;
this.mailService = mailService;
}

@Override
Expand All @@ -73,6 +76,21 @@ public boolean haveCreditsExpired(DbUser user) {
.orElse(false);
}

// Returns true if the user's credits are expiring within the initialCreditsExpirationWarningDays.
private boolean areCreditsExpiringSoon(DbUser user) {
Timestamp now = new Timestamp(clock.instant().toEpochMilli());
long initialCreditsExpirationWarningDays =
workbenchConfigProvider.get().billing.initialCreditsExpirationWarningDays;
return getCreditsExpiration(user)
.map(
expirationTime ->
now.after(
new Timestamp(
expirationTime.getTime()
- TimeUnit.DAYS.toMillis(initialCreditsExpirationWarningDays))))
.orElse(false);
}

@Override
public DbUserInitialCreditsExpiration createInitialCreditsExpiration(DbUser user) {
long initialCreditsValidityPeriodDays =
Expand Down Expand Up @@ -124,30 +142,40 @@ private void checkExpiration(DbUser user) {
DbUserInitialCreditsExpiration userInitialCreditsExpiration =
user.getUserInitialCreditsExpiration();
if (null != userInitialCreditsExpiration
&& haveCreditsExpired(user)
&& null == userInitialCreditsExpiration.getExpirationCleanupTime()) {
logger.info(
"Initial credits expired for user {}. Expiration time: {}",
user.getUsername(),
userInitialCreditsExpiration.getExpirationTime());

workspaceDao.findAllByCreator(user).stream()
.filter(
ws ->
BillingUtils.isInitialCredits(
ws.getBillingAccountName(), workbenchConfigProvider.get()))
.filter(DbWorkspace::isActive)
.filter(ws -> !ws.isInitialCreditsExpired())
.forEach(
ws -> {
ws.setInitialCreditsExpired(true);
ws.setBillingStatus(BillingStatus.INACTIVE);
workspaceDao.save(ws);
deleteAppsAndRuntimesInWorkspace(ws);
});

userInitialCreditsExpiration.setExpirationCleanupTime(Timestamp.from(Instant.now()));
userDao.save(user);
if (haveCreditsExpired(user)) {
logger.info(
"Initial credits expired for user {}. Expiration time: {}",
user.getUsername(),
userInitialCreditsExpiration.getExpirationTime());

workspaceDao.findAllByCreator(user).stream()
.filter(
ws ->
BillingUtils.isInitialCredits(
ws.getBillingAccountName(), workbenchConfigProvider.get()))
.filter(DbWorkspace::isActive)
.filter(ws -> !ws.isInitialCreditsExpired())
.forEach(
ws -> {
ws.setInitialCreditsExpired(true);
ws.setBillingStatus(BillingStatus.INACTIVE);
workspaceDao.save(ws);
deleteAppsAndRuntimesInWorkspace(ws);
});

userInitialCreditsExpiration.setExpirationCleanupTime(Timestamp.from(Instant.now()));
userDao.save(user);
} else if (areCreditsExpiringSoon(user)
&& null == userInitialCreditsExpiration.getApproachingExpirationNotificationTime()) {
logger.info(
"Initial credits expiring soon for user {}. Expiration time: {}",
user.getUsername(),
userInitialCreditsExpiration.getExpirationTime());
//Send email notification to user
userInitialCreditsExpiration.setApproachingExpirationNotificationTime(Timestamp.from(Instant.now()));
userDao.save(user);
}
}
}

Expand Down

0 comments on commit 2fc7cf4

Please sign in to comment.