8000 feat(site): display user status history as an indication of license usage by SasSwart · Pull Request #16020 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

feat(site): display user status history as an indication of license usage #16020

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
Show file tree
Hide file tree
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
exclude deleted users from the user status graph
  • Loading branch information
SasSwart committed Jan 3, 2025
commit 5b8fcac37707b01e86bde19d6325cc640e281b19
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