@@ -2340,7 +2340,76 @@ func (q *FakeQuerier) GetTemplateDAUs(_ context.Context, arg database.GetTemplat
2340
2340
return rs , nil
2341
2341
}
2342
2342
2343
- func (q * FakeQuerier ) GetTemplateDailyInsights (ctx context.Context , arg database.GetTemplateDailyInsightsParams ) ([]database.GetTemplateDailyInsightsRow , error ) {
2343
+ func (q * FakeQuerier ) GetTemplateInsights (_ context.Context , arg database.GetTemplateInsightsParams ) (database.GetTemplateInsightsRow , error ) {
2344
+ err := validateDatabaseType (arg )
2345
+ if err != nil {
2346
+ return database.GetTemplateInsightsRow {}, err
2347
+ }
2348
+
2349
+ templateIDSet := make (map [uuid.UUID ]struct {})
2350
+ appUsageIntervalsByUser := make (map [uuid.UUID ]map [time.Time ]* database.GetTemplateInsightsRow )
2351
+ for _ , s := range q .workspaceAgentStats {
2352
+ if s .CreatedAt .Before (arg .StartTime ) || s .CreatedAt .Equal (arg .EndTime ) || s .CreatedAt .After (arg .EndTime ) {
2353
+ continue
2354
+ }
2355
+ if len (arg .TemplateIDs ) > 0 && ! slices .Contains (arg .TemplateIDs , s .TemplateID ) {
2356
+ continue
2357
+ }
2358
+ if s .ConnectionCount == 0 {
2359
+ continue
2360
+ }
2361
+
2362
+ templateIDSet [s .TemplateID ] = struct {}{}
2363
+ if appUsageIntervalsByUser [s .UserID ] == nil {
2364
+ appUsageIntervalsByUser [s .UserID ] = make (map [time.Time ]* database.GetTemplateInsightsRow )
2365
+ }
2366
+ t := s .CreatedAt .Truncate (time .Minute )
2367
+ if _ , ok := appUsageIntervalsByUser [s.UserID ][t ]; ! ok {
2368
+ appUsageIntervalsByUser [s.UserID ][t ] = & database.GetTemplateInsightsRow {}
2369
+ }
2370
+
2371
+ if s .SessionCountJetBrains > 0 {
2372
+ appUsageIntervalsByUser [s.UserID ][t ].UsageJetbrainsSeconds = 60
2373
+ }
2374
+ if s .SessionCountVSCode > 0 {
2375
+ appUsageIntervalsByUser [s.UserID ][t ].UsageVscodeSeconds = 60
2376
+ }
2377
+ if s .SessionCountReconnectingPTY > 0 {
2378
+ appUsageIntervalsByUser [s.UserID ][t ].UsageReconnectingPtySeconds = 60
2379
+ }
2380
+ if s .SessionCountSSH > 0 {
2381
+ appUsageIntervalsByUser [s.UserID ][t ].UsageSshSeconds = 60
2382
+ }
2383
+ }
2384
+
2385
+ templateIDs := make ([]uuid.UUID , 0 , len (templateIDSet ))
2386
+ for templateID := range templateIDSet {
2387
+ templateIDs = append (templateIDs , templateID )
2388
+ }
2389
+ slices .SortFunc (templateIDs , func (a , b uuid.UUID ) int {
2390
+ return slice .Ascending (a .String (), b .String ())
2391
+ })
2392
+ activeUserIDs := make ([]uuid.UUID , 0 , len (appUsageIntervalsByUser ))
2393
+ for userID := range appUsageIntervalsByUser {
2394
+ activeUserIDs = append (activeUserIDs , userID )
2395
+ }
2396
+
2397
+ result := database.GetTemplateInsightsRow {
2398
+ TemplateIDs : templateIDs ,
2399
+ ActiveUserIDs : activeUserIDs ,
2400
+ }
2401
+ for _ , intervals := range appUsageIntervalsByUser {
2402
+ for _ , interval := range intervals {
2403
+ result .UsageJetbrainsSeconds += interval .UsageJetbrainsSeconds
2404
+ result .UsageVscodeSeconds += interval .UsageVscodeSeconds
2405
+ result .UsageReconnectingPtySeconds += interval .UsageReconnectingPtySeconds
2406
+ result .UsageSshSeconds += interval .UsageSshSeconds
2407
+ }
2408
+ }
2409
+ return result , nil
2410
+ }
2411
+
2412
+ func (q * FakeQuerier ) GetTemplateInsightsByInterval (ctx context.Context , arg database.GetTemplateInsightsByIntervalParams ) ([]database.GetTemplateInsightsByIntervalRow , error ) {
2344
2413
err := validateDatabaseType (arg )
2345
2414
if err != nil {
2346
2415
return nil , err
@@ -2349,17 +2418,18 @@ func (q *FakeQuerier) GetTemplateDailyInsights(ctx context.Context, arg database
2349
2418
q .mutex .RLock ()
2350
2419
defer q .mutex .RUnlock ()
2351
2420
2352
- type dailyStat struct {
2421
+ type statByInterval struct {
2353
2422
startTime , endTime time.Time
2354
2423
userSet map [uuid.UUID ]struct {}
2355
2424
templateIDSet map [uuid.UUID ]struct {}
2356
2425
}
2357
- dailyStats := []dailyStat {{arg .StartTime , arg .StartTime .AddDate (0 , 0 , 1 ), make (map [uuid.UUID ]struct {}), make (map [uuid.UUID ]struct {})}}
2358
- for dailyStats [len (dailyStats )- 1 ].endTime .Before (arg .EndTime ) {
2359
- dailyStats = append (dailyStats , dailyStat {dailyStats [len (dailyStats )- 1 ].endTime , dailyStats [len (dailyStats )- 1 ].endTime .AddDate (0 , 0 , 1 ), make (map [uuid.UUID ]struct {}), make (map [uuid.UUID ]struct {})})
2426
+
2427
+ statsByInterval := []statByInterval {{arg .StartTime , arg .StartTime .AddDate (0 , 0 , int (arg .IntervalDays )), make (map [uuid.UUID ]struct {}), make (map [uuid.UUID ]struct {})}}
2428
+ for statsByInterval [len (statsByInterval )- 1 ].endTime .Before (arg .EndTime ) {
2429
+ statsByInterval = append (statsByInterval , statByInterval {statsByInterval [len (statsByInterval )- 1 ].endTime , statsByInterval [len (statsByInterval )- 1 ].endTime .AddDate (0 , 0 , int (arg .IntervalDays )), make (map [uuid.UUID ]struct {}), make (map [uuid.UUID ]struct {})})
2360
2430
}
2361
- if dailyStats [len (dailyStats )- 1 ].endTime .After (arg .EndTime ) {
2362
- dailyStats [len (dailyStats )- 1 ].endTime = arg .EndTime
2431
+ if statsByInterval [len (statsByInterval )- 1 ].endTime .After (arg .EndTime ) {
2432
+ statsByInterval [len (statsByInterval )- 1 ].endTime = arg .EndTime
2363
2433
}
2364
2434
2365
2435
for _ , s := range q .workspaceAgentStats {
@@ -2373,7 +2443,7 @@ func (q *FakeQuerier) GetTemplateDailyInsights(ctx context.Context, arg database
2373
2443
continue
2374
2444
}
2375
2445
2376
- for _ , ds := range dailyStats {
2446
+ for _ , ds := range statsByInterval {
2377
2447
if s .CreatedAt .Before (ds .startTime ) || s .CreatedAt .Equal (ds .endTime ) || s .CreatedAt .After (ds .endTime ) {
2378
2448
continue
2379
2449
}
@@ -2392,7 +2462,7 @@ func (q *FakeQuerier) GetTemplateDailyInsights(ctx context.Context, arg database
2392
2462
continue
2393
2463
}
2394
2464
2395
- for _ , ds := range dailyStats {
2465
+ for _ , ds := range statsByInterval {
2396
2466
// (was.session_started_at >= ts.from_ AND was.session_started_at < ts.to_)
2397
2467
// OR (was.session_ended_at > ts.from_ AND was.session_ended_at < ts.to_)
2398
2468
// OR (was.session_started_at < ts.from_ AND was.session_ended_at >= ts.to_)
@@ -2407,16 +2477,16 @@ func (q *FakeQuerier) GetTemplateDailyInsights(ctx context.Context, arg database
2407
2477
}
2408
2478
}
2409
2479
2410
- var result []database.GetTemplateDailyInsightsRow
2411
- for _ , ds := range dailyStats {
2480
+ var result []database.GetTemplateInsightsByIntervalRow
2481
+ for _ , ds := range statsByInterval {
2412
2482
templateIDs := make ([]uuid.UUID , 0 , len (ds .templateIDSet ))
2413
2483
for templateID := range ds .templateIDSet {
2414
2484
templateIDs = append (templateIDs , templateID )
2415
2485
}
2416
2486
slices .SortFunc (templateIDs , func (a , b uuid.UUID ) int {
2417
2487
return slice .Ascending (a .String (), b .String ())
2418
2488
})
2419
- result = append (result , database.GetTemplateDailyInsightsRow {
2489
+ result = append (result , database.GetTemplateInsightsByIntervalRow {
2420
2490
StartTime : ds .startTime ,
2421
2491
EndTime : ds .endTime ,
2422
2492
TemplateIDs : templateIDs ,
@@ -2426,75 +2496,6 @@ func (q *FakeQuerier) GetTemplateDailyInsights(ctx context.Context, arg database
2426
2496
return result , nil
2427
2497
}
2428
2498
2429
- func (q * FakeQuerier ) GetTemplateInsights (_ context.Context , arg database.GetTemplateInsightsParams ) (database.GetTemplateInsightsRow , error ) {
2430
- err := validateDatabaseType (arg )
2431
- if err != nil {
2432
- return database.GetTemplateInsightsRow {}, err
2433
- }
2434
-
2435
- templateIDSet := make (map [uuid.UUID ]struct {})
2436
- appUsageIntervalsByUser := make (map [uuid.UUID ]map [time.Time ]* database.GetTemplateInsightsRow )
2437
- for _ , s := range q .workspaceAgentStats {
2438
- if s .CreatedAt .Before (arg .StartTime ) || s .CreatedAt .Equal (arg .EndTime ) || s .CreatedAt .After (arg .EndTime ) {
2439
- continue
2440
- }
2441
- if len (arg .TemplateIDs ) > 0 && ! slices .Contains (arg .TemplateIDs , s .TemplateID ) {
2442
- continue
2443
- }
2444
- if s .ConnectionCount == 0 {
2445
- continue
2446
- }
2447
-
2448
- templateIDSet [s .TemplateID ] = struct {}{}
2449
- if appUsageIntervalsByUser [s .UserID ] == nil {
2450
- appUsageIntervalsByUser [s .UserID ] = make (map [time.Time ]* database.GetTemplateInsightsRow )
2451
- }
2452
- t := s .CreatedAt .Truncate (time .Minute )
2453
- if _ , ok := appUsageIntervalsByUser [s.UserID ][t ]; ! ok {
2454
- appUsageIntervalsByUser [s.UserID ][t ] = & database.GetTemplateInsightsRow {}
2455
- }
2456
-
2457
- if s .SessionCountJetBrains > 0 {
2458
- appUsageIntervalsByUser [s.UserID ][t ].UsageJetbrainsSeconds = 60
2459
- }
2460
- if s .SessionCountVSCode > 0 {
2461
- appUsageIntervalsByUser [s.UserID ][t ].UsageVscodeSeconds = 60
2462
- }
2463
- if s .SessionCountReconnectingPTY > 0 {
2464
- appUsageIntervalsByUser [s.UserID ][t ].UsageReconnectingPtySeconds = 60
2465
- }
2466
- if s .SessionCountSSH > 0 {
2467
- appUsageIntervalsByUser [s.UserID ][t ].UsageSshSeconds = 60
2468
- }
2469
- }
2470
-
2471
- templateIDs := make ([]uuid.UUID , 0 , len (templateIDSet ))
2472
- for templateID := range templateIDSet {
2473
- templateIDs = append (templateIDs , templateID )
2474
- }
2475
- slices .SortFunc (templateIDs , func (a , b uuid.UUID ) int {
2476
- return slice .Ascending (a .String (), b .String ())
2477
- })
2478
- activeUserIDs := make ([]uuid.UUID , 0 , len (appUsageIntervalsByUser ))
2479
- for userID := range appUsageIntervalsByUser {
2480
- activeUserIDs = append (activeUserIDs , userID )
2481
- }
2482
-
2483
- result := database.GetTemplateInsightsRow {
2484
- TemplateIDs : templateIDs ,
2485
- ActiveUserIDs : activeUserIDs ,
2486
- }
2487
- for _ , intervals := range appUsageIntervalsByUser {
2488
- for _ , interval := range intervals {
2489
- result .UsageJetbrainsSeconds += interval .UsageJetbrainsSeconds
2490
- result .UsageVscodeSeconds += interval .UsageVscodeSeconds
2491
- result .UsageReconnectingPtySeconds += interval .UsageReconnectingPtySeconds
2492
- result .UsageSshSeconds += interval .UsageSshSeconds
2493
- }
2494
- }
2495
- return result , nil
2496
- }
2497
-
2498
2499
func (q * FakeQuerier ) GetTemplateParameterInsights (ctx context.Context , arg database.GetTemplateParameterInsightsParams ) ([]database.GetTemplateParameterInsightsRow , error ) {
2499
2500
err := validateDatabaseType (arg )
2500
2501
if err != nil {
0 commit comments