8000 feat(coderd/database): track user status changes over time by SasSwart · Pull Request #16019 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 32 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
cd953a3
add user_status_changes table
SasSwart Dec 14, 2024
ec16728
add GetUserStatusCountsByDay
SasSwart Dec 14, 2024
0d97e82
rename unused variable
SasSwart Dec 14, 2024
eb6e249
Test GetUserStatusCountsByDay
SasSwart Dec 17, 2024
7b2c259
make gen
SasSwart Dec 17, 2024
89177b2
fix dbauthz tests
SasSwart Dec 17, 2024
877517f
do the plumbing to get sql, api and frontend talking to one another
SasSwart Dec 23, 2024
0b3e0e6
rename migration
SasSwart Dec 23, 2024
6462cc2
move aggregation logic for GetUserStatusChanges into the SQL
SasSwart Dec 24, 2024
ccd0cdf
use window functions for efficiency
SasSwart Dec 24, 2024
12a274f
ensure we use the same time zone as the start_time param
SasSwart Dec 24, 2024
fcfd76e
ensure we use the same time zone as the start_time param
SasSwart Dec 24, 2024
7c0cade
make gen
SasSwart Dec 24, 2024
ecffc8b
update field names and fix tests
SasSwart Dec 24, 2024
f3a2ce3
exclude deleted users from the user status graph
SasSwart Dec 27, 2024
5067a63
GetUserStatusChanges now passes all querier tests
SasSwart Jan 2, 2025
254d436
renumber migrations
SasSwart Jan 2, 2025
3e86522
add partial fixture for CI
SasSwart Jan 3, 2025
2e49e4c
fix migration numbers
SasSwart Jan 3, 2025
ff59729
rename and document sql function
SasSwart Jan 3, 2025
b1ad074
make gen
SasSwart Jan 3, 2025
de4081f
Remove unwanted comments from the generated interface
SasSwart Jan 9, 2025
4de334f
review notes
SasSwart Jan 9, 2025
8fca0c5
make gen
SasSwart Jan 9, 2025
b06179c
remove frontend changes
SasSwart Jan 9, 2025
213b288
rename GetUserStatusCountsOverTime to GetUserStatusCounts
SasSwart Jan 9, 2025
9457ac8
fix tests
SasSwart Jan 9, 2025
63128a3
Update coderd/database/queries/insights.sql
SasSwart Jan 10, 2025
89f0a11
Provide basic durability against multiple deletions
SasSwart Jan 13, 2025
89ebab2
Provide basic durability against multiple deletions
SasSwart Jan 13, 2025
012f14c
populate the user_deleted_table
SasSwart Jan 13, 2025
c2efd97
formatting
SasSwart Jan 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
Prev Previous commit
Next Next commit
exclude deleted users from the user status graph
  • Loading branch information
SasSwart committed Jan 9, 2025
commit f3a2ce34d5fd209decc70792ae3f10d09a65eb53
27 changes: 27 additions & 0 deletions coderd/database/dump.sql

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

1 change: 1 addition & 0 deletions coderd/database/foreign_key_constraint.go

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

Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
-- Drop the trigger first
DROP TRIGGER IF EXISTS user_status_change_trigger ON users;

-- Drop the trigger function
DROP FUNCTION IF EXISTS record_user_status_change();

-- Drop the indexes
DROP INDEX IF EXISTS idx_user_status_changes_changed_at;
DROP INDEX IF EXISTS idx_user_deleted_deleted_at;

-- Drop the table
DROP TABLE IF EXISTS user_status_changes;
DROP TABLE IF EXISTS user_deleted;
21 changes: 21 additions & 0 deletions coderd/database/migrations/000280_user_status_changes.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ SELECT
FROM users
WHERE NOT deleted;

CREATE TABLE user_deleted (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
user_id uuid NOT NULL REFERENCES users(id),
deleted_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP
);

COMMENT ON TABLE user_deleted IS 'Tracks when users were deleted';

CREATE INDEX idx_user_deleted_deleted_at ON user_deleted(deleted_at);

CREATE OR REPLACE FUNCTION record_user_status_change() RETURNS trigger AS $$
BEGIN
IF TG_OP = 'INSERT' OR OLD.status IS DISTINCT FROM NEW.status THEN
Expand All @@ -34,6 +44,17 @@ BEGIN
NEW.updated_at
);
END IF;

IF OLD.deleted = FALSE AND NEW.deleted = TRUE THEN
INSERT INTO user_deleted (
user_id,
deleted_at
) VALUES (
NEW.id,
NEW.updated_at
);
END IF;

RETURN NEW;
END;
$$ LANGUAGE plpgsql;
Expand Down
7 changes: 7 additions & 0 deletions coderd/database/models.go

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

8 changes: 7 additions & 1 deletion coderd/database/queries.sql.go

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

8 changes: 7 additions & 1 deletion coderd/database/queries/insights.sql
Original file line number Diff line number Diff line change
Expand Up @@ -812,14 +812,20 @@ daily_counts AS (
d.date,
asc1.new_status,
-- For each date and status, count users whose most recent status change
-- (up to that date) matches this status
-- (up to that date) matches this status AND who weren't deleted by that date
COUNT(*) FILTER (
WHERE asc1.changed_at = (
SELECT MAX(changed_at)
FROM all_status_changes asc2
WHERE asc2.user_id = asc1.user_id
AND asc2.changed_at <= d.date
)
AND NOT EXISTS (
SELECT 1
FROM user_deleted ud
WHERE ud.user_id = asc1.user_id
AND ud.deleted_at <= d.date
)
)::bigint AS count
FROM dates d
CROSS JOIN all_status_changes asc1
Expand Down
1 change: 1 addition & 0 deletions coderd/database/unique_constraint.go

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

0