From 9d04468d268fa5df057695c832f2f30a468b89d7 Mon Sep 17 00:00:00 2001 From: BrunoQuaresma Date: Mon, 24 Jul 2023 18:54:30 +0000 Subject: [PATCH 1/6] Add avatar url into user latency insihgts --- coderd/apidoc/docs.go | 3 +++ coderd/apidoc/swagger.json | 3 +++ coderd/database/queries.sql.go | 13 ++++++++----- coderd/database/queries/insights.sql | 1 + coderd/insights.go | 1 + codersdk/insights.go | 1 + docs/api/insights.md | 1 + docs/api/schemas.md | 4 ++++ site/src/api/api.ts | 10 ++++++++++ site/src/api/typesGenerated.ts | 1 + 10 files changed, 33 insertions(+), 5 deletions(-) diff --git a/coderd/apidoc/docs.go b/coderd/apidoc/docs.go index 8c765a3e9b2b5..18d3594dfe6fd 100644 --- a/coderd/apidoc/docs.go +++ b/coderd/apidoc/docs.go @@ -9916,6 +9916,9 @@ const docTemplate = `{ "codersdk.UserLatency": { "type": "object", "properties": { + "avatar_url": { + "type": "string" + }, "latency_ms": { "$ref": "#/definitions/codersdk.ConnectionLatency" }, diff --git a/coderd/apidoc/swagger.json b/coderd/apidoc/swagger.json index 2dc5fedea55ea..7e5ffd0f7676e 100644 --- a/coderd/apidoc/swagger.json +++ b/coderd/apidoc/swagger.json @@ -8966,6 +8966,9 @@ "codersdk.UserLatency": { "type": "object", "properties": { + "avatar_url": { + "type": "string" + }, "latency_ms": { "$ref": "#/definitions/codersdk.ConnectionLatency" }, diff --git a/coderd/database/queries.sql.go b/coderd/database/queries.sql.go index 120edc4eb949c..2371d98d726d7 100644 --- a/coderd/database/queries.sql.go +++ b/coderd/database/queries.sql.go @@ -1545,6 +1545,7 @@ const getUserLatencyInsights = `-- name: GetUserLatencyInsights :many SELECT workspace_agent_stats.user_id, users.username, + users.avatar_url, array_agg(DISTINCT template_id)::uuid[] AS template_ids, coalesce((PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY connection_median_latency_ms)), -1)::FLOAT AS workspace_connection_latency_50, coalesce((PERCENTILE_CONT(0.95) WITHIN GROUP (ORDER BY connection_median_latency_ms)), -1)::FLOAT AS workspace_connection_latency_95 @@ -1567,11 +1568,12 @@ type GetUserLatencyInsightsParams struct { } type GetUserLatencyInsightsRow struct { - UserID uuid.UUID `db:"user_id" json:"user_id"` - Username string `db:"username" json:"username"` - TemplateIDs []uuid.UUID `db:"template_ids" json:"template_ids"` - WorkspaceConnectionLatency50 float64 `db:"workspace_connection_latency_50" json:"workspace_connection_latency_50"` - WorkspaceConnectionLatency95 float64 `db:"workspace_connection_latency_95" json:"workspace_connection_latency_95"` + UserID uuid.UUID `db:"user_id" json:"user_id"` + Username string `db:"username" json:"username"` + AvatarURL sql.NullString `db:"avatar_url" json:"avatar_url"` + TemplateIDs []uuid.UUID `db:"template_ids" json:"template_ids"` + WorkspaceConnectionLatency50 float64 `db:"workspace_connection_latency_50" json:"workspace_connection_latency_50"` + WorkspaceConnectionLatency95 float64 `db:"workspace_connection_latency_95" json:"workspace_connection_latency_95"` } // GetUserLatencyInsights returns the median and 95th percentile connection @@ -1590,6 +1592,7 @@ func (q *sqlQuerier) GetUserLatencyInsights(ctx context.Context, arg GetUserLate if err := rows.Scan( &i.UserID, &i.Username, + &i.AvatarURL, pq.Array(&i.TemplateIDs), &i.WorkspaceConnectionLatency50, &i.WorkspaceConnectionLatency95, diff --git a/coderd/database/queries/insights.sql b/coderd/database/queries/insights.sql index e611ab209ff68..90e9ad1e79c26 100644 --- a/coderd/database/queries/insights.sql +++ b/coderd/database/queries/insights.sql @@ -6,6 +6,7 @@ SELECT workspace_agent_stats.user_id, users.username, + users.avatar_url, array_agg(DISTINCT template_id)::uuid[] AS template_ids, coalesce((PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY connection_median_latency_ms)), -1)::FLOAT AS workspace_connection_latency_50, coalesce((PERCENTILE_CONT(0.95) WITHIN GROUP (ORDER BY connection_median_latency_ms)), -1)::FLOAT AS workspace_connection_latency_95 diff --git a/coderd/insights.go b/coderd/insights.go index 3da60a13bfe84..f5f5b4ab2fd19 100644 --- a/coderd/insights.go +++ b/coderd/insights.go @@ -117,6 +117,7 @@ func (api *API) insightsUserLatency(rw http.ResponseWriter, r *http.Request) { TemplateIDs: row.TemplateIDs, UserID: row.UserID, Username: row.Username, + AvatarURL: row.AvatarURL.String, LatencyMS: codersdk.ConnectionLatency{ P50: row.WorkspaceConnectionLatency50, P95: row.WorkspaceConnectionLatency95, diff --git a/codersdk/insights.go b/codersdk/insights.go index fb1c582c686c8..3c606920b1aac 100644 --- a/codersdk/insights.go +++ b/codersdk/insights.go @@ -44,6 +44,7 @@ type UserLatency struct { TemplateIDs []uuid.UUID `json:"template_ids" format:"uuid"` UserID uuid.UUID `json:"user_id" format:"uuid"` Username string `json:"username"` + AvatarURL string `json:"avatar_url"` LatencyMS ConnectionLatency `json:"latency_ms"` } diff --git a/docs/api/insights.md b/docs/api/insights.md index a802916fa579c..961f6fd25c4b9 100644 --- a/docs/api/insights.md +++ b/docs/api/insights.md @@ -117,6 +117,7 @@ curl -X GET http://coder-server:8080/api/v2/insights/user-latency \ "template_ids": ["497f6eca-6276-4993-bfeb-53cbbbba6f08"], "users": [ { + "avatar_url": "string", "latency_ms": { "p50": 31.312, "p95": 119.832 diff --git a/docs/api/schemas.md b/docs/api/schemas.md index a8cee1c174fce..c634ba9f2b65c 100644 --- a/docs/api/schemas.md +++ b/docs/api/schemas.md @@ -4879,6 +4879,7 @@ If the schedule is empty, the user will be updated to use the default schedule.| ```json { + "avatar_url": "string", "latency_ms": { "p50": 31.312, "p95": 119.832 @@ -4893,6 +4894,7 @@ If the schedule is empty, the user will be updated to use the default schedule.| | Name | Type | Required | Restrictions | Description | | -------------- | -------------------------------------------------------- | -------- | ------------ | ----------- | +| `avatar_url` | string | false | | | | `latency_ms` | [codersdk.ConnectionLatency](#codersdkconnectionlatency) | false | | | | `template_ids` | array of string | false | | | | `user_id` | string | false | | | @@ -4907,6 +4909,7 @@ If the schedule is empty, the user will be updated to use the default schedule.| "template_ids": ["497f6eca-6276-4993-bfeb-53cbbbba6f08"], "users": [ { + "avatar_url": "string", "latency_ms": { "p50": 31.312, "p95": 119.832 @@ -4938,6 +4941,7 @@ If the schedule is empty, the user will be updated to use the default schedule.| "template_ids": ["497f6eca-6276-4993-bfeb-53cbbbba6f08"], "users": [ { + "avatar_url": "string", "latency_ms": { "p50": 31.312, "p95": 119.832 diff --git a/site/src/api/api.ts b/site/src/api/api.ts index ba412e39e8764..f40781afbda2f 100644 --- a/site/src/api/api.ts +++ b/site/src/api/api.ts @@ -1368,3 +1368,13 @@ export const getWorkspaceParameters = async (workspace: TypesGen.Workspace) => { buildParameters, } } + +export const getInsightsUserLatency = async (filters: { + start_time: string + end_time: string + template_ids: string +}): Promise => { + const params = new URLSearchParams(filters) + const response = await axios.get(`/api/v2/insights/user-latency?${params}`) + return response.data +} diff --git a/site/src/api/typesGenerated.ts b/site/src/api/typesGenerated.ts index 4bc840801994e..5714b39fef7ba 100644 --- a/site/src/api/typesGenerated.ts +++ b/site/src/api/typesGenerated.ts @@ -1176,6 +1176,7 @@ export interface UserLatency { readonly template_ids: string[] readonly user_id: string readonly username: string + readonly avatar_url: string readonly latency_ms: ConnectionLatency } From 3eb00c70eb7047e2384306080764b12038137b95 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Tue, 25 Jul 2023 10:11:06 +0200 Subject: [PATCH 2/6] WIP --- coderd/database/dbfake/dbfake.go | 1 + coderd/database/models.go | 4 ++-- coderd/database/queries.sql.go | 4 ++-- codersdk/insights.go | 2 +- site/src/api/api.ts | 10 ---------- 5 files changed, 6 insertions(+), 15 deletions(-) diff --git a/coderd/database/dbfake/dbfake.go b/coderd/database/dbfake/dbfake.go index 7bfaa6aa29246..bb9b7235939b0 100644 --- a/coderd/database/dbfake/dbfake.go +++ b/coderd/database/dbfake/dbfake.go @@ -2369,6 +2369,7 @@ func (q *FakeQuerier) GetUserLatencyInsights(_ context.Context, arg database.Get row := database.GetUserLatencyInsightsRow{ UserID: userID, Username: user.Username, + AvatarURL: user.AvatarURL, TemplateIDs: templateIDs, WorkspaceConnectionLatency50: tryPercentile(latencies, 50), WorkspaceConnectionLatency95: tryPercentile(latencies, 95), diff --git a/coderd/database/models.go b/coderd/database/models.go index 80787f57b7642..30d1af9b899ad 100644 --- a/coderd/database/models.go +++ b/coderd/database/models.go @@ -1,6 +1,6 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.19.1 +// sqlc v1.19.0 package database @@ -13,7 +13,7 @@ import ( "github.com/google/uuid" "github.com/lib/pq" - "github.com/sqlc-dev/pqtype" + "github.com/tabbed/pqtype" ) type APIKeyScope string diff --git a/coderd/database/queries.sql.go b/coderd/database/queries.sql.go index 2371d98d726d7..9384988c3b507 100644 --- a/coderd/database/queries.sql.go +++ b/coderd/database/queries.sql.go @@ -1,6 +1,6 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.19.1 +// sqlc v1.19.0 package database @@ -12,7 +12,7 @@ import ( "github.com/google/uuid" "github.com/lib/pq" - "github.com/sqlc-dev/pqtype" + "github.com/tabbed/pqtype" ) const deleteAPIKeyByID = `-- name: DeleteAPIKeyByID :exec diff --git a/codersdk/insights.go b/codersdk/insights.go index 3c606920b1aac..5c806218b0a91 100644 --- a/codersdk/insights.go +++ b/codersdk/insights.go @@ -44,7 +44,7 @@ type UserLatency struct { TemplateIDs []uuid.UUID `json:"template_ids" format:"uuid"` UserID uuid.UUID `json:"user_id" format:"uuid"` Username string `json:"username"` - AvatarURL string `json:"avatar_url"` + AvatarURL string `json:"avatar_url,omitempty"` LatencyMS ConnectionLatency `json:"latency_ms"` } diff --git a/site/src/api/api.ts b/site/src/api/api.ts index f40781afbda2f..ba412e39e8764 100644 --- a/site/src/api/api.ts +++ b/site/src/api/api.ts @@ -1368,13 +1368,3 @@ export const getWorkspaceParameters = async (workspace: TypesGen.Workspace) => { buildParameters, } } - -export const getInsightsUserLatency = async (filters: { - start_time: string - end_time: string - template_ids: string -}): Promise => { - const params = new URLSearchParams(filters) - const response = await axios.get(`/api/v2/insights/user-latency?${params}`) - return response.data -} From 837561fa6f5fc2e119cf861829c0a0ef0b223d87 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Tue, 25 Jul 2023 08:13:03 +0000 Subject: [PATCH 3/6] make gen --- coderd/database/models.go | 4 ++-- coderd/database/queries.sql.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/coderd/database/models.go b/coderd/database/models.go index 30d1af9b899ad..80787f57b7642 100644 --- a/coderd/database/models.go +++ b/coderd/database/models.go @@ -1,6 +1,6 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.19.0 +// sqlc v1.19.1 package database @@ -13,7 +13,7 @@ import ( "github.com/google/uuid" "github.com/lib/pq" - "github.com/tabbed/pqtype" + "github.com/sqlc-dev/pqtype" ) type APIKeyScope string diff --git a/coderd/database/queries.sql.go b/coderd/database/queries.sql.go index 9384988c3b507..2371d98d726d7 100644 --- a/coderd/database/queries.sql.go +++ b/coderd/database/queries.sql.go @@ -1,6 +1,6 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.19.0 +// sqlc v1.19.1 package database @@ -12,7 +12,7 @@ import ( "github.com/google/uuid" "github.com/lib/pq" - "github.com/tabbed/pqtype" + "github.com/sqlc-dev/pqtype" ) const deleteAPIKeyByID = `-- name: DeleteAPIKeyByID :exec From 4d1244a4c96418327c175971eea20edaa6bde7d8 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Tue, 25 Jul 2023 08:29:41 +0000 Subject: [PATCH 4/6] make gen --- site/src/api/typesGenerated.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/site/src/api/typesGenerated.ts b/site/src/api/typesGenerated.ts index 5714b39fef7ba..4fc6215d38fe6 100644 --- a/site/src/api/typesGenerated.ts +++ b/site/src/api/typesGenerated.ts @@ -1176,7 +1176,7 @@ export interface UserLatency { readonly template_ids: string[] readonly user_id: string readonly username: string - readonly avatar_url: string + readonly avatar_url?: string readonly latency_ms: ConnectionLatency } From 4d25414a2af1298e9abcb4c9d45d59b990aaea54 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Tue, 25 Jul 2023 10:36:41 +0200 Subject: [PATCH 5/6] fix: uri --- coderd/apidoc/docs.go | 3 ++- coderd/apidoc/swagger.json | 3 ++- codersdk/insights.go | 2 +- docs/api/insights.md | 2 +- docs/api/schemas.md | 6 +++--- site/src/api/typesGenerated.ts | 2 +- 6 files changed, 10 insertions(+), 8 deletions(-) diff --git a/coderd/apidoc/docs.go b/coderd/apidoc/docs.go index 18d3594dfe6fd..a07c9a8fc4b97 100644 --- a/coderd/apidoc/docs.go +++ b/coderd/apidoc/docs.go @@ -9917,7 +9917,8 @@ const docTemplate = `{ "type": "object", "properties": { "avatar_url": { - "type": "string" + "type": "string", + "format": "uri" }, "latency_ms": { "$ref": "#/definitions/codersdk.ConnectionLatency" diff --git a/coderd/apidoc/swagger.json b/coderd/apidoc/swagger.json index 7e5ffd0f7676e..47fbbca0234bb 100644 --- a/coderd/apidoc/swagger.json +++ b/coderd/apidoc/swagger.json @@ -8967,7 +8967,8 @@ "type": "object", "properties": { "avatar_url": { - "type": "string" + "type": "string", + "format": "uri" }, "latency_ms": { "$ref": "#/definitions/codersdk.ConnectionLatency" diff --git a/codersdk/insights.go b/codersdk/insights.go index 5c806218b0a91..f746e5c1243d8 100644 --- a/codersdk/insights.go +++ b/codersdk/insights.go @@ -44,7 +44,7 @@ type UserLatency struct { TemplateIDs []uuid.UUID `json:"template_ids" format:"uuid"` UserID uuid.UUID `json:"user_id" format:"uuid"` Username string `json:"username"` - AvatarURL string `json:"avatar_url,omitempty"` + AvatarURL string `json:"avatar_url" format:"uri"` LatencyMS ConnectionLatency `json:"latency_ms"` } diff --git a/docs/api/insights.md b/docs/api/insights.md index 961f6fd25c4b9..f3ceaffea13db 100644 --- a/docs/api/insights.md +++ b/docs/api/insights.md @@ -117,7 +117,7 @@ curl -X GET http://coder-server:8080/api/v2/insights/user-latency \ "template_ids": ["497f6eca-6276-4993-bfeb-53cbbbba6f08"], "users": [ { - "avatar_url": "string", + "avatar_url": "http://example.com", "latency_ms": { "p50": 31.312, "p95": 119.832 diff --git a/docs/api/schemas.md b/docs/api/schemas.md index c634ba9f2b65c..a5958e7e92a26 100644 --- a/docs/api/schemas.md +++ b/docs/api/schemas.md @@ -4879,7 +4879,7 @@ If the schedule is empty, the user will be updated to use the default schedule.| ```json { - "avatar_url": "string", + "avatar_url": "http://example.com", "latency_ms": { "p50": 31.312, "p95": 119.832 @@ -4909,7 +4909,7 @@ If the schedule is empty, the user will be updated to use the default schedule.| "template_ids": ["497f6eca-6276-4993-bfeb-53cbbbba6f08"], "users": [ { - "avatar_url": "string", + "avatar_url": "http://example.com", "latency_ms": { "p50": 31.312, "p95": 119.832 @@ -4941,7 +4941,7 @@ If the schedule is empty, the user will be updated to use the default schedule.| "template_ids": ["497f6eca-6276-4993-bfeb-53cbbbba6f08"], "users": [ { - "avatar_url": "string", + "avatar_url": "http://example.com", "latency_ms": { "p50": 31.312, "p95": 119.832 diff --git a/site/src/api/typesGenerated.ts b/site/src/api/typesGenerated.ts index 4fc6215d38fe6..5714b39fef7ba 100644 --- a/site/src/api/typesGenerated.ts +++ b/site/src/api/typesGenerated.ts @@ -1176,7 +1176,7 @@ export interface UserLatency { readonly template_ids: string[] readonly user_id: string readonly username: string - readonly avatar_url?: string + readonly avatar_url: string readonly latency_ms: ConnectionLatency } From c6e82dafcddc6d5e327e69b247e09cc930619575 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Tue, 25 Jul 2023 11:34:31 +0200 Subject: [PATCH 6/6] fix: GROUP BY --- coderd/database/queries.sql.go | 2 +- coderd/database/queries/insights.sql | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/coderd/database/queries.sql.go b/coderd/database/queries.sql.go index 2371d98d726d7..920f488b1e915 100644 --- a/coderd/database/queries.sql.go +++ b/coderd/database/queries.sql.go @@ -1557,7 +1557,7 @@ WHERE AND workspace_agent_stats.connection_median_latency_ms > 0 AND workspace_agent_stats.connection_count > 0 AND CASE WHEN COALESCE(array_length($3::uuid[], 1), 0) > 0 THEN template_id = ANY($3::uuid[]) ELSE TRUE END -GROUP BY workspace_agent_stats.user_id, users.username +GROUP BY workspace_agent_stats.user_id, users.username, users.avatar_url ORDER BY user_id ASC ` diff --git a/coderd/database/queries/insights.sql b/coderd/database/queries/insights.sql index 90e9ad1e79c26..d24128fc31f48 100644 --- a/coderd/database/queries/insights.sql +++ b/coderd/database/queries/insights.sql @@ -18,7 +18,7 @@ WHERE AND workspace_agent_stats.connection_median_latency_ms > 0 AND workspace_agent_stats.connection_count > 0 AND CASE WHEN COALESCE(array_length(@template_ids::uuid[], 1), 0) > 0 THEN template_id = ANY(@template_ids::uuid[]) ELSE TRUE END -GROUP BY workspace_agent_stats.user_id, users.username +GROUP BY workspace_agent_stats.user_id, users.username, users.avatar_url ORDER BY user_id ASC; -- name: GetTemplateInsights :one