Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Joel/rm billing status #8972

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions api/src/main/java/org/pmiops/workbench/db/model/DbWorkspace.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.pmiops.workbench.db.model.DbFeaturedWorkspace.DbFeaturedCategory;
import org.pmiops.workbench.model.BillingStatus;
import org.pmiops.workbench.model.DisseminateResearchEnum;
import org.pmiops.workbench.model.ResearchOutcomeEnum;
import org.pmiops.workbench.model.SpecificPopulationEnum;
Expand Down Expand Up @@ -84,7 +83,6 @@ public class DbWorkspace {
private Boolean reviewRequested;
private Boolean approved;
private Timestamp timeRequested;
private Short billingStatus = DbStorageEnums.billingStatusToStorage(BillingStatus.ACTIVE);
private String billingAccountName;
private String googleProject;
private boolean adminLocked;
Expand Down Expand Up @@ -638,19 +636,6 @@ public boolean isActive() {
return WorkspaceActiveStatus.ACTIVE.equals(getWorkspaceActiveStatusEnum());
}

@Deprecated(since = "September 2024", forRemoval = true)
@Column(name = "billing_status")
public BillingStatus getBillingStatus() {
return (initialCreditsExhausted || initialCreditsExpired
? BillingStatus.INACTIVE
: BillingStatus.ACTIVE);
}

public DbWorkspace setBillingStatus(BillingStatus billingStatus) {
this.billingStatus = DbStorageEnums.billingStatusToStorage(billingStatus);
return this;
}

@Column(name = "billing_account_name")
public String getBillingAccountName() {
return billingAccountName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.pmiops.workbench.leonardo.LeonardoApiClient;
import org.pmiops.workbench.mail.MailService;
import org.pmiops.workbench.model.BillingStatus;
import org.pmiops.workbench.initialcredits.InitialCreditsExpirationService;
import org.pmiops.workbench.utils.BillingUtils;
import org.pmiops.workbench.utils.CostComparisonUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -485,12 +486,7 @@ private void setAllToUnexhausted(final DbUser user) {
ws ->
BillingUtils.isInitialCredits(
ws.getBillingAccountName(), workbenchConfigProvider.get()))
.forEach(
ws -> {
ws.setInitialCreditsExhausted(false);
ws.setBillingStatus(BillingStatus.ACTIVE);
workspaceDao.save(ws);
});
.forEach(ws -> workspaceDao.save(ws.setInitialCreditsExhausted(false)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -509,12 +509,15 @@ public void updateWorkspaceBillingAccount(DbWorkspace workspace, String newBilli
initialCreditsService.userHasRemainingFreeTierCredits(creator);
workspace.setBillingStatus(
hasInitialCreditsRemaining ? BillingStatus.ACTIVE : BillingStatus.INACTIVE);
freeTierBillingService.userHasRemainingFreeTierCredits(creator);
workspace.setInitialCreditsExhausted(!hasInitialCreditsRemaining);
workspace.setInitialCreditsExpired(initialCreditsService.areUserCreditsExpired(creator));
} else {
// At this point, we can assume that a user provided billing account is open since we
// throw a BadRequestException if a closed one is provided
workspace.setBillingStatus(BillingStatus.ACTIVE);
workspace.setInitialCreditsExpired(
initialCreditsExpirationService.haveCreditsExpired(creator));
}
}

Expand Down Expand Up @@ -616,12 +619,7 @@ public void cloneTanagraFeatureSet(
public void updateInitialCreditsExhaustion(DbUser user, boolean exhausted) {
workspaceDao.findAllByCreator(user).stream()
.filter(ws -> isInitialCredits(ws.getBillingAccountName(), workbenchConfigProvider.get()))
.forEach(
ws -> {
ws.setInitialCreditsExhausted(exhausted);
ws.setBillingStatus(exhausted ? BillingStatus.INACTIVE : BillingStatus.ACTIVE);
workspaceDao.save(ws);
});
.forEach(ws -> workspaceDao.save(ws.setInitialCreditsExhausted(exhausted)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@
FirecloudMapper.class,
})
public interface WorkspaceResourceMapper {
@Mapping(target = "workspaceBillingStatus", ignore = true)
@Mapping(target = "workspaceId", source = "dbWorkspace.workspaceId")
@Mapping(target = "workspaceFirecloudName", source = "dbWorkspace.firecloudName")
@Mapping(target = "workspaceBillingStatus", source = "dbWorkspace.billingStatus")
@Mapping(target = "cdrVersionId", source = "dbWorkspace.cdrVersion")
@Mapping(target = "accessTierShortName", source = "dbWorkspace.cdrVersion.accessTier.shortName")
WorkspaceFields fromWorkspace(DbWorkspace dbWorkspace);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
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.model.ExpiredInitialCreditsEventRequest;
import org.pmiops.workbench.model.WorkspaceActiveStatus;
import org.pmiops.workbench.test.FakeClock;
Expand Down Expand Up @@ -569,10 +568,7 @@ public void handleInitialCreditsExpiry_singleAlert_forExhaustedAndByoBilling() t

// Simulate the user attaching their own billing account to the previously free tier workspace.
workspace = workspaceDao.findDbWorkspaceByWorkspaceId(workspace.getWorkspaceId());
workspaceDao.save(
workspace
.setBillingAccountName(fullBillingAccountName("byo-account"))
.setBillingStatus(BillingStatus.ACTIVE));
workspaceDao.save(workspace.setBillingAccountName(fullBillingAccountName("byo-account")));

cloudTaskInitialCreditsExpiryController.handleInitialCreditsExpiry(
buildExpiredInitialCreditsEventRequest(
Expand All @@ -594,7 +590,6 @@ public void handleInitialCreditsExpiry_singleAlert_forExhaustedAndByoBilling() t
.setWorkspaceNamespace("some other namespace")
.setGoogleProject("other project")
.setBillingAccountName("some other account")
.setBillingStatus(BillingStatus.ACTIVE)
.setInitialCreditsExhausted(false);
workspaceDao.save(userAccountWorkspace);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@
import org.pmiops.workbench.mail.MailService;
import org.pmiops.workbench.model.AnnotationType;
import org.pmiops.workbench.model.ArchivalStatus;
import org.pmiops.workbench.model.BillingStatus;
import org.pmiops.workbench.model.CloneWorkspaceRequest;
import org.pmiops.workbench.model.Cohort;
import org.pmiops.workbench.model.CohortAnnotationDefinition;
Expand Down Expand Up @@ -1173,12 +1172,6 @@ public void testUpdateWorkspace_freeTierBilling_hasCreditsRemaining() {
Workspace workspace = createWorkspace();
workspace = workspacesController.createWorkspace(workspace).getBody();

DbWorkspace dbWorkspace =
workspaceDao.findByWorkspaceNamespaceAndFirecloudNameAndActiveStatus(
workspace.getNamespace(),
workspace.getTerraName(),
DbStorageEnums.workspaceActiveStatusToStorage(WorkspaceActiveStatus.ACTIVE));
dbWorkspace.setBillingStatus(BillingStatus.INACTIVE);
doReturn(true)
.when(mockInitialCreditsService)
.userHasRemainingFreeTierCredits(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import org.pmiops.workbench.leonardo.LeonardoApiClient;
import org.pmiops.workbench.mail.MailService;
import org.pmiops.workbench.model.BillingStatus;
import org.pmiops.workbench.initialcredits.InitialCreditsExpirationService;
import org.pmiops.workbench.model.WorkspaceActiveStatus;
import org.pmiops.workbench.test.FakeClock;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -627,10 +628,7 @@ public void checkFreeTierBillingUsage_singleAlertForExhaustedAndByoBilling() {
// Simulate the user attaching their own billing account to the previously free tier workspace.
TestTransaction.start();
workspace = workspaceDao.findDbWorkspaceByWorkspaceId(workspace.getWorkspaceId());
workspaceDao.save(
workspace
.setBillingAccountName(fullBillingAccountName("byo-account"))
.setBillingStatus(BillingStatus.ACTIVE));
workspaceDao.save(workspace.setBillingAccountName(fullBillingAccountName("byo-account")));

commitTransaction();
}
Expand Down Expand Up @@ -770,8 +768,7 @@ public void test_disableOnlyFreeTierWorkspaces() {
.setCreator(user)
.setWorkspaceNamespace("some other namespace")
.setGoogleProject("other project")
.setBillingAccountName("some other account")
.setBillingStatus(BillingStatus.ACTIVE);
.setBillingAccountName("some other account");
workspaceDao.save(userAccountWorkspace);

commitTransaction();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
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.model.FeaturedWorkspaceCategory;
import org.pmiops.workbench.model.ResearchOutcomeEnum;
import org.pmiops.workbench.model.ResearchPurpose;
Expand Down Expand Up @@ -171,13 +170,14 @@ public void setUp() {
.setReviewRequested(false)
.setApproved(true)
.setTimeRequested(DB_CREATION_TIMESTAMP)
.setBillingStatus(BillingStatus.ACTIVE)
.setBillingAccountName(BILLING_ACCOUNT_NAME)
.setSpecificPopulationsEnum(SPECIFIC_POPULATIONS)
.setResearchOutcomeEnumSet(RESEARCH_OUTCOMES)
.setDisseminateResearchEnumSet(Collections.emptySet())
.setDisseminateResearchOther(DISSEMINATE_FINDINGS_OTHER)
.setGoogleProject(GOOGLE_PROJECT);
.setGoogleProject(GOOGLE_PROJECT)
.setInitialCreditsExpired(false)
.setInitialCreditsExhausted(false);
}

@Test
Expand All @@ -194,6 +194,8 @@ public void testConvertsDbToApiWorkspace() {
assertThat(ws.getCreator()).isEqualTo(CREATOR_EMAIL);
assertThat(ws.getInitialCredits().getExpirationEpochMillis())
.isEqualTo(INITIAL_CREDITS_EXPIRATION_TIMESTAMP.getTime());
assertThat(ws.getInitialCredits().isExhausted()).isFalse();
assertThat(ws.getInitialCredits().isExpired()).isFalse();
assertThat(ws.getGoogleBucketName()).isEqualTo(FIRECLOUD_BUCKET_NAME);
assertThat(ws.getBillingAccountName()).isEqualTo(BILLING_ACCOUNT_NAME);
assertThat(ws.getAccessTierShortName()).isEqualTo(ACCESS_TIER_SHORT_NAME);
Expand Down