diff --git a/internal/billing/repository_test.go b/internal/billing/repository_test.go index 4ff4114466..d6a0e17586 100644 --- a/internal/billing/repository_test.go +++ b/internal/billing/repository_test.go @@ -122,14 +122,18 @@ func TestRepository_MonthlyActiveUsers(t *testing.T) { repo := TestRepo(t, conn) activeUsers, err := repo.MonthlyActiveUsers(ctx, WithStartTime(&threeMonthsAgo), WithEndTime(&oneMonthAgo)) assert.NoError(t, err) - // since the end time is exclusive, we should only get one record of active users - // for the month of three months ago - expectedStartTime := time.Date(today.AddDate(0, -3, 0).Year(), today.AddDate(0, -3, 0).Month(), 1, 0, 0, 0, 0, time.UTC) - expectedEndTime := time.Date(today.AddDate(0, -2, 0).Year(), today.AddDate(0, -2, 0).Month(), 1, 0, 0, 0, 0, time.UTC) - require.Len(t, activeUsers, 1) + require.Len(t, activeUsers, 2) + expectedStartTime := time.Date(today.AddDate(0, -2, 0).Year(), today.AddDate(0, -2, 0).Month(), 1, 0, 0, 0, 0, time.UTC) + expectedEndTime := time.Date(today.AddDate(0, -1, 0).Year(), today.AddDate(0, -1, 0).Month(), 1, 0, 0, 0, 0, time.UTC) require.Equal(t, uint64(6), activeUsers[0].ActiveUsersCount) assert.Equal(t, expectedStartTime, activeUsers[0].StartTime) assert.Equal(t, expectedEndTime, activeUsers[0].EndTime) + + expectedStartTime = time.Date(today.AddDate(0, -3, 0).Year(), today.AddDate(0, -3, 0).Month(), 1, 0, 0, 0, 0, time.UTC) + expectedEndTime = time.Date(today.AddDate(0, -2, 0).Year(), today.AddDate(0, -2, 0).Month(), 1, 0, 0, 0, 0, time.UTC) + require.Equal(t, uint64(6), activeUsers[1].ActiveUsersCount) + assert.Equal(t, expectedStartTime, activeUsers[1].StartTime) + assert.Equal(t, expectedEndTime, activeUsers[1].EndTime) }) t.Run("invalid-end-time-without-start-time", func(t *testing.T) { diff --git a/internal/daemon/controller/handlers/billing/billing_service_test.go b/internal/daemon/controller/handlers/billing/billing_service_test.go index f9b41b0c84..07ec26b743 100644 --- a/internal/daemon/controller/handlers/billing/billing_service_test.go +++ b/internal/daemon/controller/handlers/billing/billing_service_test.go @@ -102,6 +102,11 @@ func Test_MonthlyActiveUsers(t *testing.T) { req: &pbs.MonthlyActiveUsersRequest{StartTime: threeMonthsAgo, EndTime: oneMonthAgo}, res: &pbs.MonthlyActiveUsersResponse{ Items: []*pb.ActiveUsers{ + { + Count: 6, + StartTime: timestamppb.New(time.Date(today.Year(), today.Month()-2, 1, 0, 0, 0, 0, time.UTC)), + EndTime: timestamppb.New(time.Date(today.Year(), today.Month()-1, 1, 0, 0, 0, 0, time.UTC)), + }, { Count: 6, StartTime: timestamppb.New(time.Date(today.Year(), today.Month()-3, 1, 0, 0, 0, 0, time.UTC)), diff --git a/internal/db/schema/migrations/oss/postgres/84/08_hcp_user_billing_view.up.sql b/internal/db/schema/migrations/oss/postgres/84/08_hcp_user_billing_view.up.sql index bf8b04016f..656677aed4 100644 --- a/internal/db/schema/migrations/oss/postgres/84/08_hcp_user_billing_view.up.sql +++ b/internal/db/schema/migrations/oss/postgres/84/08_hcp_user_billing_view.up.sql @@ -92,7 +92,7 @@ begin; return query select * from hcp_billing_monthly_active_users_all where start_time >= p_start_time - and end_time < p_end_time; + and end_time <= p_end_time; when p_start_time is not null then return query select * from hcp_billing_monthly_active_users_all @@ -100,7 +100,7 @@ begin; when p_end_time is not null then return query select * from hcp_billing_monthly_active_users_all - where end_time < p_end_time; + where end_time <= p_end_time; else return query select * from hcp_billing_monthly_active_users_all; diff --git a/internal/db/sqltest/tests/hcp/billing/monthly_active_users_all_timezone.sql b/internal/db/sqltest/tests/hcp/billing/monthly_active_users_all_timezone.sql index 96ca000997..c6828a98aa 100644 --- a/internal/db/sqltest/tests/hcp/billing/monthly_active_users_all_timezone.sql +++ b/internal/db/sqltest/tests/hcp/billing/monthly_active_users_all_timezone.sql @@ -144,7 +144,7 @@ begin; select results_eq( $$ select * from hcp_billing_monthly_active_users_all(date_trunc('month', now() - interval '5 month', 'utc'), - date_trunc('month', now() - interval '2 month', 'utc')); + date_trunc('month', now() - interval '3 month', 'utc')); $$, $$ values (date_trunc('month', now() - interval '4 month', 'utc'), date_trunc('month', now() - interval '3 month', 'utc'), 6::bigint), @@ -154,7 +154,7 @@ begin; select results_eq( $$ select * from hcp_billing_monthly_active_users_all(null, - date_trunc('month', now() - interval '2 month', 'utc')); + date_trunc('month', now() - interval '3 month', 'utc')); $$, $$ values (date_trunc('month', now() - interval '4 month', 'utc'), date_trunc('month', now() - interval '3 month', 'utc'), 6::bigint),