Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4bb2c73

Browse files
committed
lower app intervals to 1m and increase test coverage
1 parent 14aa1af commit 4bb2c73

8 files changed

+30
-26
lines changed

coderd/database/dbfake/dbfake.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2004,15 +2004,6 @@ func (q *FakeQuerier) GetTemplateAppInsights(ctx context.Context, arg database.G
20042004

20052005
appUsageIntervalsByUserAgentApp := make(map[uniqueKey]map[time.Time]int64)
20062006
for _, s := range q.workspaceAppStats {
2007-
// (was.session_started_at >= ts.from_ AND was.session_started_at < ts.to_)
2008-
// OR (was.session_ended_at > ts.from_ AND was.session_ended_at < ts.to_)
2009-
// OR (was.session_started_at < ts.from_ AND was.session_ended_at >= ts.to_)
2010-
if !(((s.SessionStartedAt.After(arg.StartTime) || s.SessionStartedAt.Equal(arg.StartTime)) && s.SessionStartedAt.Before(arg.EndTime)) ||
2011-
(s.SessionEndedAt.After(arg.StartTime) && s.SessionEndedAt.Before(arg.EndTime)) ||
2012-
(s.SessionStartedAt.Before(arg.StartTime) && (s.SessionEndedAt.After(arg.EndTime) || s.SessionEndedAt.Equal(arg.EndTime)))) {
2013-
continue
2014-
}
2015-
20162007
w, err := q.getWorkspaceByIDNoLock(ctx, s.WorkspaceID)
20172008
if err != nil {
20182009
return nil, err
@@ -2048,8 +2039,8 @@ func (q *FakeQuerier) GetTemplateAppInsights(ctx context.Context, arg database.G
20482039
t = arg.StartTime
20492040
}
20502041
for t.Before(s.SessionEndedAt) && t.Before(arg.EndTime) {
2051-
appUsageIntervalsByUserAgentApp[key][t] = 300 // 5 minutes.
2052-
t = t.Add(5 * time.Minute)
2042+
appUsageIntervalsByUserAgentApp[key][t] = 60 // 5 minutes.
2043+
t = t.Add(1 * time.Minute)
20532044
}
20542045
}
20552046

coderd/database/queries.sql.go

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/insights.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ FROM agent_stats_by_interval_and_user;
6464
WITH ts AS (
6565
SELECT
6666
d::timestamptz AS from_,
67-
(d::timestamptz + '5 minute'::interval) AS to_,
68-
EXTRACT(epoch FROM '5 minute'::interval) AS seconds
67+
(d::timestamptz + '1 minute'::interval) AS to_,
68+
EXTRACT(epoch FROM '1 minute'::interval) AS seconds
6969
FROM
7070
-- Subtract 1 second from end_time to avoid including the next interval in the results.
71-
generate_series(@start_time::timestamptz, (@end_time::timestamptz) - '1 second'::interval, '5 minute'::interval) d
71+
generate_series(@start_time::timestamptz, (@end_time::timestamptz) - '1 second'::interval, '1 minute'::interval) d
7272
), app_stats_by_user_and_agent AS (
7373
SELECT
7474
ts.from_,

coderd/insights_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,19 @@ func TestTemplateInsights_Golden(t *testing.T) {
778778
endedAt: frozenWeekAgo.Add(time.Hour),
779779
requests: 1,
780780
},
781-
{ // used an app on the last day, counts as active user, 12m -> 15m rounded.
781+
{ // 30s of app usage -> 1m rounded.
782+
app: users[0].workspaces[0].apps[0],
783+
startedAt: frozenWeekAgo.Add(2*time.Hour + 10*time.Second),
784+
endedAt: frozenWeekAgo.Add(2*time.Hour + 40*time.Second),
785+
requests: 1,
786+
},
787+
{ // 1m30s of app usage -> 2m rounded (included in São Paulo).
788+
app: users[0].workspaces[0].apps[0],
789+
startedAt: frozenWeekAgo.Add(3*time.Hour + 30*time.Second),
790+
endedAt: frozenWeekAgo.Add(3*time.Hour + 90*time.Second),
791+
requests: 1,
792+
},
793+
{ // used an app on the last day, counts as active user, 12m.
782794
app: users[0].workspaces[0].apps[2],
783795
startedAt: frozenWeekAgo.AddDate(0, 0, 6),
784796
endedAt: frozenWeekAgo.AddDate(0, 0, 6).Add(12 * time.Minute),

coderd/testdata/insights/multiple_users_and_workspaces_week_all_templates.json.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
"display_name": "app1",
6767
"slug": "app1",
6868
"icon": "/icon1.png",
69-
"seconds": 25200
69+
"seconds": 25380
7070
},
7171
{
7272
"template_ids": [
@@ -76,7 +76,7 @@
7676
"display_name": "app3",
7777
"slug": "app3",
7878
"icon": "/icon2.png",
79-
"seconds": 900
79+
"seconds": 720
8080
},
8181
{
8282
"template_ids": [

coderd/testdata/insights/multiple_users_and_workspaces_week_deployment_wide.json.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
"display_name": "app1",
6767
"slug": "app1",
6868
"icon": "/icon1.png",
69-
"seconds": 25200
69+
"seconds": 25380
7070
},
7171
{
7272
"template_ids": [
@@ -76,7 +76,7 @@
7676
"display_name": "app3",
7777
"slug": "app3",
7878
"icon": "/icon2.png",
79-
"seconds": 900
79+
"seconds": 720
8080
},
8181
{
8282
"template_ids": [

coderd/testdata/insights/multiple_users_and_workspaces_week_first_template.json.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"display_name": "app1",
5656
"slug": "app1",
5757
"icon": "/icon1.png",
58-
"seconds": 3600
58+
"seconds": 3780
5959
},
6060
{
6161
"template_ids": [
@@ -65,7 +65,7 @@
6565
"display_name": "app3",
6666
"slug": "app3",
6767
"icon": "/icon2.png",
68-
"seconds": 900
68+
"seconds": 720
6969
}
7070
],
7171
"parameters_usage": []

coderd/testdata/insights/multiple_users_and_workspaces_week_other_timezone_(São_Paulo).json.golden

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,14 @@
5151
},
5252
{
5353
"template_ids": [
54+
"00000000-0000-0000-0000-000000000001",
5455
"00000000-0000-0000-0000-000000000002"
5556
],
5657
"type": "app",
5758
"display_name": "app1",
5859
"slug": "app1",
5960
"icon": "/icon1.png",
60-
"seconds": 21600
61+
"seconds": 21720
6162
},
6263
{
6364
"template_ids": [
@@ -67,7 +68,7 @@
6768
"display_name": "app3",
6869
"slug": "app3",
6970
"icon": "/icon2.png",
70-
"seconds": 4500
71+
"seconds": 4320
7172
},
7273
{
7374
"template_ids": [

0 commit comments

Comments
 (0)