mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
chore: implement database crud for AI seat usage (#22681)
Creates a new table `ai_seat_state` to keep track of when users consume an ai_seat. Once a user consumes an AI seat, they will forever in this table (as it stands today).
This commit is contained in:
@@ -1198,6 +1198,64 @@ func (q *sqlQuerier) UpdateAIBridgeInterceptionEnded(ctx context.Context, arg Up
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getActiveAISeatCount = `-- name: GetActiveAISeatCount :one
|
||||
SELECT
|
||||
COUNT(*)
|
||||
FROM
|
||||
ai_seat_state ais
|
||||
JOIN
|
||||
users u
|
||||
ON
|
||||
ais.user_id = u.id
|
||||
WHERE
|
||||
u.status = 'active'::user_status
|
||||
AND u.deleted = false
|
||||
AND u.is_system = false
|
||||
`
|
||||
|
||||
func (q *sqlQuerier) GetActiveAISeatCount(ctx context.Context) (int64, error) {
|
||||
row := q.db.QueryRowContext(ctx, getActiveAISeatCount)
|
||||
var count int64
|
||||
err := row.Scan(&count)
|
||||
return count, err
|
||||
}
|
||||
|
||||
const upsertAISeatState = `-- name: UpsertAISeatState :exec
|
||||
INSERT INTO ai_seat_state (
|
||||
user_id,
|
||||
first_used_at,
|
||||
last_used_at,
|
||||
last_event_type,
|
||||
last_event_description,
|
||||
updated_at
|
||||
)
|
||||
VALUES
|
||||
($1, $2, $2, $3, $4, $2)
|
||||
ON CONFLICT (user_id) DO UPDATE
|
||||
SET
|
||||
last_used_at = EXCLUDED.last_used_at,
|
||||
last_event_type = EXCLUDED.last_event_type,
|
||||
last_event_description = EXCLUDED.last_event_description,
|
||||
updated_at = EXCLUDED.updated_at
|
||||
`
|
||||
|
||||
type UpsertAISeatStateParams struct {
|
||||
UserID uuid.UUID `db:"user_id" json:"user_id"`
|
||||
FirstUsedAt time.Time `db:"first_used_at" json:"first_used_at"`
|
||||
LastEventType AiSeatUsageReason `db:"last_event_type" json:"last_event_type"`
|
||||
LastEventDescription string `db:"last_event_description" json:"last_event_description"`
|
||||
}
|
||||
|
||||
func (q *sqlQuerier) UpsertAISeatState(ctx context.Context, arg UpsertAISeatStateParams) error {
|
||||
_, err := q.db.ExecContext(ctx, upsertAISeatState,
|
||||
arg.UserID,
|
||||
arg.FirstUsedAt,
|
||||
arg.LastEventType,
|
||||
arg.LastEventDescription,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const deleteAPIKeyByID = `-- name: DeleteAPIKeyByID :exec
|
||||
DELETE FROM
|
||||
api_keys
|
||||
|
||||
Reference in New Issue
Block a user