Skip to content

Commit

Permalink
Bug fix: Wrong date printed when lease started and spend calculated a…
Browse files Browse the repository at this point in the history
…re 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 e294228.

* Bug fix: Wrong date printed when lease started and spend calculated are on the same day.

Co-authored-by: Jaya Nanda <[email protected]>
  • Loading branch information
jayanandagit and jayanandagit authored Jun 5, 2020
1 parent f909c25 commit 030c87e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
18 changes: 17 additions & 1 deletion cmd/lambda/update_lease_status/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -97,7 +98,7 @@ has exceeded its budget of $100. Actual spend is $150
BudgetAmount: test.budgetAmount,
BudgetCurrency: "USD",
BudgetNotificationEmails: []string{"[email protected]", "[email protected]"},
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{},
Expand Down Expand Up @@ -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 {
Expand Down
18 changes: 14 additions & 4 deletions cmd/lambda/update_lease_status/spend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 030c87e

Please sign in to comment.