@@ -1917,22 +1917,127 @@ func (q *FakeQuerier) GetTemplateDAUs(_ context.Context, arg database.GetTemplat
1917
1917
return rs , nil
1918
1918
}
1919
1919
1920
- func (q * FakeQuerier ) GetTemplateDailyInsights (ctx context.Context , arg database.GetTemplateDailyInsightsParams ) ([]database.GetTemplateDailyInsightsRow , error ) {
1920
+ func (q * FakeQuerier ) GetTemplateDailyInsights (_ context.Context , arg database.GetTemplateDailyInsightsParams ) ([]database.GetTemplateDailyInsightsRow , error ) {
1921
1921
err := validateDatabaseType (arg )
1922
1922
if err != nil {
1923
1923
return nil , err
1924
1924
}
1925
1925
1926
- panic ("not implemented" )
1926
+ type dailyStat struct {
1927
+ startTime , endTime time.Time
1928
+ userSet map [uuid.UUID ]struct {}
1929
+ templateIDSet map [uuid.UUID ]struct {}
1930
+ }
1931
+ dailyStats := []dailyStat {{arg .StartTime , arg .StartTime .AddDate (0 , 0 , 1 ), make (map [uuid.UUID ]struct {}), make (map [uuid.UUID ]struct {})}}
1932
+ for dailyStats [len (dailyStats )- 1 ].endTime .Before (arg .EndTime ) {
1933
+ 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 {})})
1934
+ }
1935
+ if dailyStats [len (dailyStats )- 1 ].endTime .After (arg .EndTime ) {
1936
+ dailyStats [len (dailyStats )- 1 ].endTime = arg .EndTime
1937
+ }
1938
+
1939
+ for _ , s := range q .workspaceAgentStats {
1940
+ if s .CreatedAt .Before (arg .StartTime ) || s .CreatedAt .Equal (arg .EndTime ) || s .CreatedAt .After (arg .EndTime ) {
1941
+ continue
1942
+ }
1943
+ if len (arg .TemplateIDs ) > 0 && ! slices .Contains (arg .TemplateIDs , s .TemplateID ) {
1944
+ continue
1945
+ }
1946
+ if s .ConnectionCount == 0 {
1947
+ continue
1948
+ }
1949
+
1950
+ for _ , ds := range dailyStats {
1951
+ if s .CreatedAt .Before (ds .startTime ) || s .CreatedAt .Equal (ds .endTime ) || s .CreatedAt .After (ds .endTime ) {
1952
+ continue
1953
+ }
1954
+ ds .userSet [s .UserID ] = struct {}{}
1955
+ ds .templateIDSet [s .TemplateID ] = struct {}{}
1956
+ break
1957
+ }
1958
+ }
1959
+
1960
+ var result []database.GetTemplateDailyInsightsRow
1961
+ for _ , ds := range dailyStats {
1962
+ templateIDs := make ([]uuid.UUID , 0 , len (ds .templateIDSet ))
1963
+ for templateID := range ds .templateIDSet {
1964
+ templateIDs = append (templateIDs , templateID )
1965
+ }
1966
+ slices .SortFunc (templateIDs , func (a , b uuid.UUID ) bool {
1967
+ return a .String () < b .String ()
1968
+ })
1969
+ result = append (result , database.GetTemplateDailyInsightsRow {
1970
+ StartTime : ds .startTime ,
1971
+ EndTime : ds .endTime ,
1972
+ TemplateIDs : templateIDs ,
1973
+ ActiveUsers : int64 (len (ds .userSet )),
1974
+ })
1975
+ }
1976
+ return result , nil
1927
1977
}
1928
1978
1929
- func (q * FakeQuerier ) GetTemplateInsights (ctx context.Context , arg database.GetTemplateInsightsParams ) (database.GetTemplateInsightsRow , error ) {
1979
+ func (q * FakeQuerier ) GetTemplateInsights (_ context.Context , arg database.GetTemplateInsightsParams ) (database.GetTemplateInsightsRow , error ) {
1930
1980
err := validateDatabaseType (arg )
1931
1981
if err != nil {
1932
1982
return database.GetTemplateInsightsRow {}, err
1933
1983
}
1934
1984
1935
- panic ("not implemented" )
1985
+ templateIDSet := make (map [uuid.UUID ]struct {})
1986
+ appUsageIntervalsByUser := make (map [uuid.UUID ]map [time.Time ]* database.GetTemplateInsightsRow )
1987
+ for _ , s := range q .workspaceAgentStats {
1988
+ if s .CreatedAt .Before (arg .StartTime ) || s .CreatedAt .Equal (arg .EndTime ) || s .CreatedAt .After (arg .EndTime ) {
1989
+ continue
1990
+ }
1991
+ if len (arg .TemplateIDs ) > 0 && ! slices .Contains (arg .TemplateIDs , s .TemplateID ) {
1992
+ continue
1993
+ }
1994
+ if s .ConnectionCount == 0 {
1995
+ continue
1996
+ }
1997
+
1998
+ templateIDSet [s .TemplateID ] = struct {}{}
1999
+ if appUsageIntervalsByUser [s .UserID ] == nil {
2000
+ appUsageIntervalsByUser [s .UserID ] = make (map [time.Time ]* database.GetTemplateInsightsRow )
2001
+ }
2002
+ t := s .CreatedAt .Truncate (5 * time .Minute )
2003
+ if _ , ok := appUsageIntervalsByUser [s.UserID ][t ]; ! ok {
2004
+ appUsageIntervalsByUser [s.UserID ][t ] = & database.GetTemplateInsightsRow {}
2005
+ }
2006
+
2007
+ if s .SessionCountJetBrains > 0 {
2008
+ appUsageIntervalsByUser [s.UserID ][t ].UsageJetbrainsSeconds = 300
2009
+ }
2010
+ if s .SessionCountVSCode > 0 {
2011
+ appUsageIntervalsByUser [s.UserID ][t ].UsageVscodeSeconds = 300
2012
+ }
2013
+ if s .SessionCountReconnectingPTY > 0 {
2014
+ appUsageIntervalsByUser [s.UserID ][t ].UsageReconnectingPtySeconds = 300
2015
+ }
2016
+ if s .SessionCountSSH > 0 {
2017
+ appUsageIntervalsByUser [s.UserID ][t ].UsageSshSeconds = 300
2018
+ }
2019
+ }
2020
+
2021
+ templateIDs := make ([]uuid.UUID , 0 , len (templateIDSet ))
2022
+ for templateID := range templateIDSet {
2023
+ templateIDs = append (templateIDs , templateID )
2024
+ }
2025
+ slices .SortFunc (templateIDs , func (a , b uuid.UUID ) bool {
2026
+ return a .String () < b .String ()
2027
+ })
2028
+ result := database.GetTemplateInsightsRow {
2029
+ TemplateIDs : templateIDs ,
2030
+ ActiveUsers : int64 (len (appUsageIntervalsByUser )),
2031
+ }
2032
+ for _ , intervals := range appUsageIntervalsByUser {
2033
+ for _ , interval := range intervals {
2034
+ result .UsageJetbrainsSeconds += interval .UsageJetbrainsSeconds
2035
+ result .UsageVscodeSeconds += interval .UsageVscodeSeconds
2036
+ result .UsageReconnectingPtySeconds += interval .UsageReconnectingPtySeconds
2037
+ result .UsageSshSeconds += interval .UsageSshSeconds
2038
+ }
2039
+ }
2040
+ return result , nil
1936
2041
}
1937
2042
1938
2043
func (q * FakeQuerier ) GetTemplateVersionByID (ctx context.Context , templateVersionID uuid.UUID ) (database.TemplateVersion , error ) {
@@ -2182,7 +2287,7 @@ func (q *FakeQuerier) GetUserCount(_ context.Context) (int64, error) {
2182
2287
return existing , nil
2183
2288
}
2184
2289
2185
- func (q * FakeQuerier ) GetUserLatencyInsights (ctx context.Context , arg database.GetUserLatencyInsightsParams ) ([]database.GetUserLatencyInsightsRow , error ) {
2290
+ func (q * FakeQuerier ) GetUserLatencyInsights (_ context.Context , arg database.GetUserLatencyInsightsParams ) ([]database.GetUserLatencyInsightsRow , error ) {
2186
2291
err := validateDatabaseType (arg )
2187
2292
if err != nil {
2188
2293
return nil , err
@@ -2222,9 +2327,9 @@ func (q *FakeQuerier) GetUserLatencyInsights(ctx context.Context, arg database.G
2222
2327
var rows []database.GetUserLatencyInsightsRow
2223
2328
for userID , latencies := range latenciesByUserID {
2224
2329
sort .Float64s (latencies )
2225
- templateSet := seenTemplatesByUserID [userID ]
2226
- templateIDs := make ([]uuid.UUID , 0 , len (templateSet ))
2227
- for templateID := range templateSet {
2330
+ templateIDSet := seenTemplatesByUserID [userID ]
2331
+ templateIDs := make ([]uuid.UUID , 0 , len (templateIDSet ))
2332
+ for templateID := range templateIDSet {
2228
2333
templateIDs = append (templateIDs , templateID )
2229
2334
}
2230
2335
slices .SortFunc (templateIDs , func (a , b uuid.UUID ) bool {
0 commit comments