@@ -4300,27 +4300,44 @@ func (q *FakeQuerier) GetUserLatencyInsights(_ context.Context, arg database.Get
4300
4300
q .mutex .RLock ()
4301
4301
defer q .mutex .RUnlock ()
4302
4302
4303
+ /*
4304
+ SELECT
4305
+ tus.user_id,
4306
+ u.username,
4307
+ u.avatar_url,
4308
+ array_agg(DISTINCT tus.template_id)::uuid[] AS template_ids,
4309
+ COALESCE((PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY tus.median_latency_ms)), -1)::float AS workspace_connection_latency_50,
4310
+ COALESCE((PERCENTILE_CONT(0.95) WITHIN GROUP (ORDER BY tus.median_latency_ms)), -1)::float AS workspace_connection_latency_95
4311
+ FROM
4312
+ template_usage_stats tus
4313
+ JOIN
4314
+ users u
4315
+ ON
4316
+ u.id = tus.user_id
4317
+ WHERE
4318
+ tus.start_time >= @start_time::timestamptz
4319
+ AND tus.end_time <= @end_time::timestamptz
4320
+ AND CASE WHEN COALESCE(array_length(@template_ids::uuid[], 1), 0) > 0 THEN tus.template_id = ANY(@template_ids::uuid[]) ELSE TRUE END
4321
+ GROUP BY
4322
+ tus.user_id, u.username, u.avatar_url
4323
+ ORDER BY
4324
+ tus.user_id ASC;
4325
+ */
4326
+
4303
4327
latenciesByUserID := make (map [uuid.UUID ][]float64 )
4304
- seenTemplatesByUserID := make (map [uuid.UUID ]map [uuid.UUID ]struct {})
4305
- for _ , s := range q .workspaceAgentStats {
4306
- if len (arg .TemplateIDs ) > 0 && ! slices .Contains (arg .TemplateIDs , s .TemplateID ) {
4307
- continue
4308
- }
4309
- if ! arg .StartTime .Equal (s .CreatedAt ) && (s .CreatedAt .Before (arg .StartTime ) || s .CreatedAt .After (arg .EndTime )) {
4310
- continue
4311
- }
4312
- if s .ConnectionCount == 0 {
4328
+ seenTemplatesByUserID := make (map [uuid.UUID ][]uuid.UUID )
4329
+ for _ , stat := range q .templateUsageStats {
4330
+ if stat .StartTime .Before (arg .StartTime ) || stat .EndTime .After (arg .EndTime ) {
4313
4331
continue
4314
4332
}
4315
- if s . ConnectionMedianLatencyMS <= 0 {
4333
+ if len ( arg . TemplateIDs ) > 0 && ! slices . Contains ( arg . TemplateIDs , stat . TemplateID ) {
4316
4334
continue
4317
4335
}
4318
4336
4319
- latenciesByUserID [s .UserID ] = append (latenciesByUserID [s .UserID ], s .ConnectionMedianLatencyMS )
4320
- if seenTemplatesByUserID [s .UserID ] == nil {
4321
- seenTemplatesByUserID [s .UserID ] = make (map [uuid.UUID ]struct {})
4337
+ if stat .MedianLatencyMs .Valid {
4338
+ latenciesByUserID [stat .UserID ] = append (latenciesByUserID [stat .UserID ], stat .MedianLatencyMs .Float64 )
4322
4339
}
4323
- seenTemplatesByUserID [s .UserID ][s. TemplateID ] = struct {}{}
4340
+ seenTemplatesByUserID [stat .UserID ] = uniqueSortedUUIDs ( append ( seenTemplatesByUserID [ stat . UserID ], stat . TemplateID ))
4324
4341
}
4325
4342
4326
4343
tryPercentile := func (fs []float64 , p float64 ) float64 {
@@ -4333,15 +4350,6 @@ func (q *FakeQuerier) GetUserLatencyInsights(_ context.Context, arg database.Get
4333
4350
4334
4351
var rows []database.GetUserLatencyInsightsRow
4335
4352
for userID , latencies := range latenciesByUserID {
4336
- sort .Float64s (latencies )
4337
- templateIDSet := seenTemplatesByUserID [userID ]
4338
- templateIDs := make ([]uuid.UUID , 0 , len (templateIDSet ))
4339
- for templateID := range templateIDSet {
4340
- templateIDs = append (templateIDs , templateID )
4341
- }
4342
- slices .SortFunc (templateIDs , func (a , b uuid.UUID ) int {
4343
- return slice .Ascending (a .String (), b .String ())
4344
- })
4345
4353
user , err := q .getUserByIDNoLock (userID )
4346
4354
if err != nil {
4347
4355
return nil , err
@@ -4350,7 +4358,7 @@ func (q *FakeQuerier) GetUserLatencyInsights(_ context.Context, arg database.Get
4350
4358
UserID : userID ,
4351
4359
Username : user .Username ,
4352
4360
AvatarURL : user .AvatarURL ,
4353
- TemplateIDs : templateIDs ,
4361
+ TemplateIDs : seenTemplatesByUserID [ userID ] ,
4354
4362
WorkspaceConnectionLatency50 : tryPercentile (latencies , 50 ),
4355
4363
WorkspaceConnectionLatency95 : tryPercentile (latencies , 95 ),
4356
4364
}
0 commit comments