Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat(coderd/database): track user status changes over time#16019

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
SasSwart merged 32 commits intomainfromjjs/dau-history-backend
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
32 commits
Select commitHold shift + click to select a range
cd953a3
add user_status_changes table
SasSwartDec 14, 2024
ec16728
add GetUserStatusCountsByDay
SasSwartDec 14, 2024
0d97e82
rename unused variable
SasSwartDec 14, 2024
eb6e249
Test GetUserStatusCountsByDay
SasSwartDec 17, 2024
7b2c259
make gen
SasSwartDec 17, 2024
89177b2
fix dbauthz tests
SasSwartDec 17, 2024
877517f
do the plumbing to get sql, api and frontend talking to one another
SasSwartDec 23, 2024
0b3e0e6
rename migration
SasSwartDec 23, 2024
6462cc2
move aggregation logic for GetUserStatusChanges into the SQL
SasSwartDec 24, 2024
ccd0cdf
use window functions for efficiency
SasSwartDec 24, 2024
12a274f
ensure we use the same time zone as the start_time param
SasSwartDec 24, 2024
fcfd76e
ensure we use the same time zone as the start_time param
SasSwartDec 24, 2024
7c0cade
make gen
SasSwartDec 24, 2024
ecffc8b
update field names and fix tests
SasSwartDec 24, 2024
f3a2ce3
exclude deleted users from the user status graph
SasSwartDec 27, 2024
5067a63
GetUserStatusChanges now passes all querier tests
SasSwartJan 2, 2025
254d436
renumber migrations
SasSwartJan 2, 2025
3e86522
add partial fixture for CI
SasSwartJan 3, 2025
2e49e4c
fix migration numbers
SasSwartJan 3, 2025
ff59729
rename and document sql function
SasSwartJan 3, 2025
b1ad074
make gen
SasSwartJan 3, 2025
de4081f
Remove unwanted comments from the generated interface
SasSwartJan 9, 2025
4de334f
review notes
SasSwartJan 9, 2025
8fca0c5
make gen
SasSwartJan 9, 2025
b06179c
remove frontend changes
SasSwartJan 9, 2025
213b288
rename GetUserStatusCountsOverTime to GetUserStatusCounts
SasSwartJan 9, 2025
9457ac8
fix tests
SasSwartJan 9, 2025
63128a3
Update coderd/database/queries/insights.sql
SasSwartJan 10, 2025
89f0a11
Provide basic durability against multiple deletions
SasSwartJan 13, 2025
89ebab2
Provide basic durability against multiple deletions
SasSwartJan 13, 2025
012f14c
populate the user_deleted_table
SasSwartJan 13, 2025
c2efd97
formatting
SasSwartJan 13, 2025
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
5 changes: 2 additions & 3 deletionsMakefile
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -521,6 +521,7 @@ lint/markdown: node_modules/.installed
# All files generated by the database should be added here, and this can be used
# as a target for jobs that need to run after the database is generated.
DB_GEN_FILES := \
coderd/database/dump.sql \
coderd/database/querier.go \
coderd/database/unique_constraint.go \
coderd/database/dbmem/dbmem.go \
Expand All@@ -540,8 +541,6 @@ GEN_FILES := \
provisionersdk/proto/provisioner.pb.go \
provisionerd/proto/provisionerd.pb.go \
vpn/vpn.pb.go \
coderd/database/dump.sql \
$(DB_GEN_FILES) \
site/src/api/typesGenerated.ts \
coderd/rbac/object_gen.go \
codersdk/rbacresources_gen.go \
Expand All@@ -559,7 +558,7 @@ GEN_FILES := \
coderd/database/pubsub/psmock/psmock.go

# all gen targets should be added here and to gen/mark-fresh
gen: $(GEN_FILES)
gen:gen/db$(GEN_FILES)
.PHONY: gen

gen/db: $(DB_GEN_FILES)
Expand Down
61 changes: 61 additions & 0 deletionscoderd/apidoc/docs.go
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

57 changes: 57 additions & 0 deletionscoderd/apidoc/swagger.json
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

1 change: 1 addition & 0 deletionscoderd/coderd.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1281,6 +1281,7 @@ func New(options *Options) *API {
r.Use(apiKeyMiddleware)
r.Get("/daus", api.deploymentDAUs)
r.Get("/user-activity", api.insightsUserActivity)
r.Get("/user-status-counts", api.insightsUserStatusCounts)
r.Get("/user-latency", api.insightsUserLatency)
r.Get("/templates", api.insightsTemplates)
})
Expand Down
7 changes: 7 additions & 0 deletionscoderd/database/dbauthz/dbauthz.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2421,6 +2421,13 @@ func (q *querier) GetUserNotificationPreferences(ctx context.Context, userID uui
return q.db.GetUserNotificationPreferences(ctx, userID)
}

func (q *querier) GetUserStatusCounts(ctx context.Context, arg database.GetUserStatusCountsParams) ([]database.GetUserStatusCountsRow, error) {
if err := q.authorizeContext(ctx, policy.ActionRead, rbac.ResourceUser); err != nil {
return nil, err
}
return q.db.GetUserStatusCounts(ctx, arg)
}

func (q *querier) GetUserWorkspaceBuildParameters(ctx context.Context, params database.GetUserWorkspaceBuildParametersParams) ([]database.GetUserWorkspaceBuildParametersRow, error) {
u, err := q.db.GetUserByID(ctx, params.OwnerID)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletionscoderd/database/dbauthz/dbauthz_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1708,6 +1708,12 @@ func (s *MethodTestSuite) TestUser() {
rbac.ResourceTemplate.InOrg(orgID), policy.ActionRead,
)
}))
s.Run("GetUserStatusCounts", s.Subtest(func(db database.Store, check *expects) {
check.Args(database.GetUserStatusCountsParams{
StartTime: time.Now().Add(-time.Hour * 24 * 30),
EndTime: time.Now(),
}).Asserts(rbac.ResourceUser, policy.ActionRead)
}))
}

func (s *MethodTestSuite) TestWorkspace() {
Expand Down
56 changes: 56 additions & 0 deletionscoderd/database/dbmem/dbmem.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -88,6 +88,7 @@ func New() database.Store {
customRoles: make([]database.CustomRole, 0),
locks: map[int64]struct{}{},
runtimeConfig: map[string]string{},
userStatusChanges: make([]database.UserStatusChange, 0),
},
}
// Always start with a default org. Matching migration 198.
Expand DownExpand Up@@ -256,6 +257,7 @@ type data struct {
lastLicenseID int32
defaultProxyDisplayName string
defaultProxyIconURL string
userStatusChanges []database.UserStatusChange
}

func tryPercentile(fs []float64, p float64) float64 {
Expand DownExpand Up@@ -5669,6 +5671,42 @@ func (q *FakeQuerier) GetUserNotificationPreferences(_ context.Context, userID u
return out, nil
}

func (q *FakeQuerier) GetUserStatusCounts(_ context.Context, arg database.GetUserStatusCountsParams) ([]database.GetUserStatusCountsRow, error) {
q.mutex.RLock()
defer q.mutex.RUnlock()

err := validateDatabaseType(arg)
if err != nil {
return nil, err
}

result := make([]database.GetUserStatusCountsRow, 0)
for _, change := range q.userStatusChanges {
if change.ChangedAt.Before(arg.StartTime) || change.ChangedAt.After(arg.EndTime) {
continue
}
date := time.Date(change.ChangedAt.Year(), change.ChangedAt.Month(), change.ChangedAt.Day(), 0, 0, 0, 0, time.UTC)
if !slices.ContainsFunc(result, func(r database.GetUserStatusCountsRow) bool {
return r.Status == change.NewStatus && r.Date.Equal(date)
}) {
result = append(result, database.GetUserStatusCountsRow{
Status: change.NewStatus,
Date: date,
Count: 1,
})
} else {
for i, r := range result {
if r.Status == change.NewStatus && r.Date.Equal(date) {
result[i].Count++
break
}
}
}
}

return result, nil
}

func (q *FakeQuerier) GetUserWorkspaceBuildParameters(_ context.Context, params database.GetUserWorkspaceBuildParametersParams) ([]database.GetUserWorkspaceBuildParametersRow, error) {
q.mutex.RLock()
defer q.mutex.RUnlock()
Expand DownExpand Up@@ -8021,6 +8059,12 @@ func (q *FakeQuerier) InsertUser(_ context.Context, arg database.InsertUserParam
sort.Slice(q.users, func(i, j int) bool {
return q.users[i].CreatedAt.Before(q.users[j].CreatedAt)
})

q.userStatusChanges = append(q.userStatusChanges, database.UserStatusChange{
UserID: user.ID,
NewStatus: user.Status,
ChangedAt: user.UpdatedAt,
})
return user, nil
}

Expand DownExpand Up@@ -9062,12 +9106,18 @@ func (q *FakeQuerier) UpdateInactiveUsersToDormant(_ context.Context, params dat
Username: user.Username,
LastSeenAt: user.LastSeenAt,
})
q.userStatusChanges = append(q.userStatusChanges, database.UserStatusChange{
UserID: user.ID,
NewStatus: database.UserStatusDormant,
ChangedAt: params.UpdatedAt,
})
}
}

if len(updated) == 0 {
return nil, sql.ErrNoRows
}

return updated, nil
}

Expand DownExpand Up@@ -9868,6 +9918,12 @@ func (q *FakeQuerier) UpdateUserStatus(_ context.Context, arg database.UpdateUse
user.Status = arg.Status
user.UpdatedAt = arg.UpdatedAt
q.users[index] = user

q.userStatusChanges = append(q.userStatusChanges, database.UserStatusChange{
UserID: user.ID,
NewStatus: user.Status,
ChangedAt: user.UpdatedAt,
})
return user, nil
}
return database.User{}, sql.ErrNoRows
Expand Down
7 changes: 7 additions & 0 deletionscoderd/database/dbmetrics/querymetrics.go
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

15 changes: 15 additions & 0 deletionscoderd/database/dbmock/dbmock.go
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

Loading
Loading

[8]ページ先頭

©2009-2025 Movatter.jp