From 030c87e86694de8e6300703cfc2e780c2c107fac Mon Sep 17 00:00:00 2001 From: jayanandagit <54074306+jayanandagit@users.noreply.github.com> Date: Fri, 5 Jun 2020 10:38:28 -0400 Subject: [PATCH] Bug fix: Wrong date printed when lease started and spend calculated are on the same day. (#358) * Bug fix: Wrong date printed when lease started and spend calculated are on the same day. * Revert "Bug fix: Wrong date printed when lease started and spend calculated are on the same day." This reverts commit e294228c7a8d92d3f82e0219c2f640a876144c6b. * Bug fix: Wrong date printed when lease started and spend calculated are on the same day. Co-authored-by: Jaya Nanda --- cmd/lambda/update_lease_status/main_test.go | 18 +++++++++++++++++- cmd/lambda/update_lease_status/spend.go | 18 ++++++++++++++---- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/cmd/lambda/update_lease_status/main_test.go b/cmd/lambda/update_lease_status/main_test.go index 4fd2eb71c..bcb3c9164 100755 --- a/cmd/lambda/update_lease_status/main_test.go +++ b/cmd/lambda/update_lease_status/main_test.go @@ -77,6 +77,7 @@ has exceeded its budget of $100. Actual spend is $150 expectedEmailBodyHTML string expectedEmailBodyText string expectedError string + LeaseStatusModifiedOn int64 } checkBudgetTest := func(test *checkBudgetTestInput) { @@ -97,7 +98,7 @@ has exceeded its budget of $100. Actual spend is $150 BudgetAmount: test.budgetAmount, BudgetCurrency: "USD", BudgetNotificationEmails: []string{"recipA@example.com", "recipB@example.com"}, - LeaseStatusModifiedOn: time.Unix(100, 0).Unix(), + LeaseStatusModifiedOn: map[bool]int64{true: time.Unix(100, 0).Unix(), false: test.LeaseStatusModifiedOn}[test.LeaseStatusModifiedOn == 0], ExpiresOn: time.Now().AddDate(0, 0, +1000).Unix(), //Make sure it expires in the distant future as we aren't testing that }, awsSession: &awsMocks.AwsSession{}, @@ -302,6 +303,21 @@ Actual spend is $76 }) }) + + t.Run("Scenario: Under budget Lease: Lease started and spend calculated the same day", func(t *testing.T) { + checkBudgetTest(&checkBudgetTestInput{ + budgetAmount: 100, + actualSpend: 50, + leaseStatus: db.Active, + // Should not finance lock or reset + shouldTransitionLeaseStatus: false, + shouldSNS: false, + shouldSQSReset: false, + // Should not send notification email + shouldSendEmail: false, + LeaseStatusModifiedOn: time.Now().Unix(), + }) + }) } func Test_isLeaseExpired(t *testing.T) { type args struct { diff --git a/cmd/lambda/update_lease_status/spend.go b/cmd/lambda/update_lease_status/spend.go index c942f1d86..791e7ee7a 100755 --- a/cmd/lambda/update_lease_status/spend.go +++ b/cmd/lambda/update_lease_status/spend.go @@ -74,13 +74,23 @@ func calculateLeaseSpend(input *calculateSpendInput) (float64, error) { // We can look at the `leaseStatusModifiedOn` to know // when the lease status changed from `ResetLock` --> `Active` budgetStartTime := time.Unix(input.lease.LeaseStatusModifiedOn, 0) + currentDate := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.UTC) + budgetStartDate := time.Date(budgetStartTime.Year(), budgetStartTime.Month(), budgetStartTime.Day(), 0, 0, 0, 0, time.UTC) + // budget's `endTime` is set to yesterday budgetEndTime := usageEndTime.AddDate(0, 0, -1) - log.Printf("Retrieving usage for lease %s @ %s for period %s to %s...", - input.lease.PrincipalID, input.lease.AccountID, - budgetStartTime.Format("2006-01-02"), budgetEndTime.Format("2006-01-02"), - ) + if currentDate.Sub(budgetStartDate) <= 0 { + log.Printf("Retrieving usage for lease %s @ %s for period %s to %s...", + input.lease.PrincipalID, input.lease.AccountID, + budgetStartTime.Format("2006-01-02"), usageEndTime.Format("2006-01-02"), + ) + } else { + log.Printf("Retrieving usage for lease %s @ %s for period %s to %s...", + input.lease.PrincipalID, input.lease.AccountID, + budgetStartTime.Format("2006-01-02"), budgetEndTime.Format("2006-01-02"), + ) + } // Query Usage cache DB usageRecords, err := input.usageSvc.GetUsageByDateRange(budgetStartTime, budgetEndTime)