Skip to content

feat: add total users insight #15486

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

Closed
wants to merge 23 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
Fix tests
  • Loading branch information
BrunoQuaresma committed Nov 14, 2024
commit 186a95127dde0dfb97970c69bb8833d76ac89a96
26 changes: 17 additions & 9 deletions coderd/insights_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2380,11 +2380,17 @@ func TestTotalUsersInsight(t *testing.T) {
Username: "user3",
CreatedAt: daysAgo(1),
})
dbgen.User(t, db, database.User{
Email: "user4@coder.com",
Username: "user4",
CreatedAt: daysAgo(0),
})

// When: requesting the accumulated total of users by day
now := time.Now().UTC().Truncate(time.Hour).Add(time.Hour)
res, err := client.TotalUsersInsight(ctx, codersdk.TotalUsersInsightRequest{
StartTime: daysAgo(2),
EndTime: today(),
EndTime: now,
})
require.NoError(t, err)

Expand All @@ -2400,7 +2406,7 @@ func TestTotalUsersInsight(t *testing.T) {
require.Equal(t, res[1].Total, uint64(3))

// Third day
require.Equal(t, res[2].Date, today().Format(time.DateOnly))
require.Equal(t, res[2].Date, daysAgo(0).Format(time.DateOnly))
require.Equal(t, res[2].Total, uint64(4))
})

Expand All @@ -2417,11 +2423,17 @@ func TestTotalUsersInsight(t *testing.T) {
Username: "user1",
CreatedAt: daysAgo(2),
})
dbgen.User(t, db, database.User{
Email: "user2@coder.com",
Username: "user2",
CreatedAt: daysAgo(1),
})

// When: requesting the accumulated total of users for dates with no users created
now := time.Now().UTC().Truncate(time.Hour).Add(time.Hour)
res, err := client.TotalUsersInsight(ctx, codersdk.TotalUsersInsightRequest{
StartTime: daysAgo(2).Truncate(24 * time.Hour),
EndTime: today(),
EndTime: now,
})
require.NoError(t, err)

Expand All @@ -2434,18 +2446,14 @@ func TestTotalUsersInsight(t *testing.T) {

// Second day - no users created so should contain the same total as the previous day
require.Equal(t, res[1].Date, daysAgo(1).Format(time.DateOnly))
require.Equal(t, res[1].Total, uint64(1))
require.Equal(t, res[1].Total, uint64(2))

// Third day
require.Equal(t, res[2].Date, today().Format(time.DateOnly))
require.Equal(t, res[2].Date, daysAgo(0).Format(time.DateOnly))
require.Equal(t, res[2].Total, uint64(2))
})
}

func daysAgo(days int) time.Time {
return time.Now().UTC().AddDate(0, 0, -days).Truncate(24 * time.Hour)
}

func today() time.Time {
return time.Now().UTC().Truncate(time.Hour).Add(time.Hour)
}
Loading