Skip to content

Commit e0c1aac

Browse files
authored
fix(coderd): fix template insight intervals (#8662)
1 parent 65583ec commit e0c1aac

File tree

4 files changed

+33
-14
lines changed

4 files changed

+33
-14
lines changed

coderd/database/dbfake/dbfake.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2326,12 +2326,15 @@ func (q *FakeQuerier) GetUserLatencyInsights(_ context.Context, arg database.Get
23262326
if len(arg.TemplateIDs) > 0 && !slices.Contains(arg.TemplateIDs, s.TemplateID) {
23272327
continue
23282328
}
2329-
if !arg.StartTime.Equal(s.CreatedAt) && !(s.CreatedAt.After(arg.StartTime) && s.CreatedAt.Before(arg.EndTime)) {
2329+
if !arg.StartTime.Equal(s.CreatedAt) && (s.CreatedAt.Before(arg.StartTime) || s.CreatedAt.After(arg.EndTime)) {
23302330
continue
23312331
}
23322332
if s.ConnectionCount == 0 {
23332333
continue
23342334
}
2335+
if s.ConnectionMedianLatencyMS <= 0 {
2336+
continue
2337+
}
23352338

23362339
latenciesByUserID[s.UserID] = append(latenciesByUserID[s.UserID], s.ConnectionMedianLatencyMS)
23372340
if seenTemplatesByUserID[s.UserID] == nil {

coderd/database/queries.sql.go

Lines changed: 10 additions & 5 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: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ ORDER BY user_id ASC;
2424
-- GetTemplateInsights has a granularity of 5 minutes where if a session/app was
2525
-- in use, we will add 5 minutes to the total usage for that session (per user).
2626
WITH d AS (
27-
SELECT generate_series(@start_time::timestamptz, @end_time::timestamptz, '5 minute'::interval) AS d
27+
-- Subtract 1 second from end_time to avoid including the next interval in the results.
28+
SELECT generate_series(@start_time::timestamptz, (@end_time::timestamptz) - '1 second'::interval, '5 minute'::interval) AS d
2829
), ts AS (
2930
SELECT
3031
d::timestamptz AS from_,
@@ -71,7 +72,8 @@ FROM usage_by_user;
7172
-- interval/template, it will be included in the results with 0 active users.
7273
WITH d AS (
7374
-- sqlc workaround, use SELECT generate_series instead of SELECT * FROM generate_series.
74-
SELECT generate_series(@start_time::timestamptz, @end_time::timestamptz, '1 day'::interval) AS d
75+
-- Subtract 1 second from end_time to avoid including the next interval in the results.
76+
SELECT generate_series(@start_time::timestamptz, (@end_time::timestamptz) - '1 second'::interval, '1 day'::interval) AS d
7577
), ts AS (
7678
SELECT
7779
d::timestamptz AS from_,
@@ -91,15 +93,18 @@ WITH d AS (
9193
)
9294
GROUP BY ts.from_, ts.to_, was.user_id
9395
), template_ids AS (
94-
SELECT array_agg(DISTINCT template_id) AS ids
96+
SELECT
97+
from_,
98+
array_agg(DISTINCT template_id) AS ids
9599
FROM usage_by_day, unnest(template_ids) template_id
96100
WHERE template_id IS NOT NULL
101+
GROUP BY from_, template_ids
97102
)
98103

99104
SELECT
100105
from_ AS start_time,
101106
to_ AS end_time,
102-
COALESCE((SELECT ids FROM template_ids), '{}')::uuid[] AS template_ids,
107+
COALESCE((SELECT template_ids.ids FROM template_ids WHERE template_ids.from_ = usage_by_day.from_), '{}')::uuid[] AS template_ids,
103108
COUNT(DISTINCT user_id) AS active_users
104-
FROM usage_by_day, unnest(template_ids) as template_id
109+
FROM usage_by_day
105110
GROUP BY from_, to_;

coderd/insights_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,17 @@ func TestUserLatencyInsights(t *testing.T) {
168168
defer r.Close()
169169
defer w.Close()
170170
sess.Stdin = r
171+
sess.Stdout = io.Discard
171172
err = sess.Start("cat")
172173
require.NoError(t, err)
173174

174175
var userLatencies codersdk.UserLatencyInsightsResponse
175176
require.Eventuallyf(t, func() bool {
177+
// Keep connection active.
178+
_, err := w.Write([]byte("hello world\n"))
179+
if !assert.NoError(t, err) {
180+
return false
181+
}
176182
userLatencies, err = client.UserLatencyInsights(ctx, codersdk.UserLatencyInsightsRequest{
177183
StartTime: today,
178184
EndTime: time.Now().UTC().Truncate(time.Hour).Add(time.Hour), // Round up to include the current hour.
@@ -182,7 +188,7 @@ func TestUserLatencyInsights(t *testing.T) {
182188
return false
183189
}
184190
return len(userLatencies.Report.Users) > 0 && userLatencies.Report.Users[0].LatencyMS.P50 > 0
185-
}, testutil.WaitShort, testutil.IntervalFast, "user latency is missing")
191+
}, testutil.WaitMedium, testutil.IntervalFast, "user latency is missing")
186192

187193
// We got our latency data, close the connection.
188194
_ = sess.Close()
@@ -318,8 +324,8 @@ func TestTemplateInsights(t *testing.T) {
318324
return false
319325
}
320326
}
321-
require.Eventually(t, waitForAppSeconds("reconnecting-pty"), testutil.WaitShort, testutil.IntervalFast, "reconnecting-pty seconds missing")
322-
require.Eventually(t, waitForAppSeconds("ssh"), testutil.WaitShort, testutil.IntervalFast, "ssh seconds missing")
327+
require.Eventually(t, waitForAppSeconds("reconnecting-pty"), testutil.WaitMedium, testutil.IntervalFast, "reconnecting-pty seconds missing")
328+
require.Eventually(t, waitForAppSeconds("ssh"), testutil.WaitMedium, testutil.IntervalFast, "ssh seconds missing")
323329

324330
// We got our data, close down sessions and connections.
325331
_ = rpty.Close()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy