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

RE: https://github.com/coder/coder/issues/15740,
https://github.com/coder/coder/issues/15297

In order to add a graph to the coder frontend to show user status over
time as an indicator of license usage, this PR adds the following:

* a new `api.insightsUserStatusCountsOverTime` endpoint to the API
* which calls a new `GetUserStatusCountsOverTime` query from postgres
* which relies on two new tables `user_status_changes` and
`user_deleted`
* which are populated by a new trigger and function that tracks updates
to the users table

The chart itself will be added in a subsequent PR

---------

Co-authored-by: Mathias Fredriksson <mafredri@gmail.com>
This commit is contained in:
Sas Swart
2025-01-13 13:08:16 +02:00
committed by GitHub
co-authored by Mathias Fredriksson
parent 73d8dde6ed
commit 4543b21b7c
25 changed files with 1456 additions and 3 deletions
+50
View File
@@ -260,3 +260,53 @@ curl -X GET http://coder-server:8080/api/v2/insights/user-latency?start_time=201
| 200 | [OK](https://tools.ietf.org/html/rfc7231#section-6.3.1) | OK | [codersdk.UserLatencyInsightsResponse](schemas.md#codersdkuserlatencyinsightsresponse) |
To perform this operation, you must be authenticated. [Learn more](authentication.md).
## Get insights about user status counts
### Code samples
```shell
# Example request using curl
curl -X GET http://coder-server:8080/api/v2/insights/user-status-counts?tz_offset=0 \
-H 'Accept: application/json' \
-H 'Coder-Session-Token: API_KEY'
```
`GET /insights/user-status-counts`
### Parameters
| Name | In | Type | Required | Description |
|-------------|-------|---------|----------|----------------------------|
| `tz_offset` | query | integer | true | Time-zone offset (e.g. -2) |
### Example responses
> 200 Response
```json
{
"status_counts": {
"property1": [
{
"count": 10,
"date": "2019-08-24T14:15:22Z"
}
],
"property2": [
{
"count": 10,
"date": "2019-08-24T14:15:22Z"
}
]
}
}
```
### Responses
| Status | Meaning | Description | Schema |
|--------|---------------------------------------------------------|-------------|----------------------------------------------------------------------------------------|
| 200 | [OK](https://tools.ietf.org/html/rfc7231#section-6.3.1) | OK | [codersdk.GetUserStatusCountsResponse](schemas.md#codersdkgetuserstatuscountsresponse) |
To perform this operation, you must be authenticated. [Learn more](authentication.md).
+44
View File
@@ -3000,6 +3000,34 @@ Git clone makes use of this by parsing the URL from: 'Username for "https://gith
|-------|--------|----------|--------------|-------------|
| `key` | string | false | | |
## codersdk.GetUserStatusCountsResponse
```json
{
"status_counts": {
"property1": [
{
"count": 10,
"date": "2019-08-24T14:15:22Z"
}
],
"property2": [
{
"count": 10,
"date": "2019-08-24T14:15:22Z"
}
]
}
}
```
### Properties
| Name | Type | Required | Restrictions | Description |
|--------------------|---------------------------------------------------------------------------|----------|--------------|-------------|
| `status_counts` | object | false | | |
| » `[any property]` | array of [codersdk.UserStatusChangeCount](#codersdkuserstatuschangecount) | false | | |
## codersdk.GetUsersResponse
```json
@@ -6724,6 +6752,22 @@ If the schedule is empty, the user will be updated to use the default schedule.|
| `dormant` |
| `suspended` |
## codersdk.UserStatusChangeCount
```json
{
"count": 10,
"date": "2019-08-24T14:15:22Z"
}
```
### Properties
| Name | Type | Required | Restrictions | Description |
|---------|---------|----------|--------------|-------------|
| `count` | integer | false | | |
| `date` | string | false | | |
## codersdk.ValidateUserPasswordRequest
```json