Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6dd51f9

Browse files
chore: test metricscache on postgres (#16711)
metricscache_test has been running tests against dbmem only, instead ofagainst postgres. Unfortunately the implementations ofGetTemplateAverageBuildTime have diverged between dbmem and postgres.This change gets the tests working on Postgres and test for thebehaviour postgres provides.
1 parent95363c9 commit6dd51f9

File tree

6 files changed

+126
-96
lines changed

6 files changed

+126
-96
lines changed

‎coderd/coderd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ func New(options *Options) *API {
422422
metricsCache:=metricscache.New(
423423
options.Database,
424424
options.Logger.Named("metrics_cache"),
425+
options.Clock,
425426
metricscache.Intervals{
426427
TemplateBuildTimes:options.MetricsCacheRefreshInterval,
427428
DeploymentStats:options.AgentStatsRefreshInterval,

‎coderd/database/dbmem/dbmem.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ type data struct {
269269
presetParameters []database.TemplateVersionPresetParameter
270270
}
271271

272-
functryPercentile(fs []float64,pfloat64)float64 {
272+
functryPercentileCont(fs []float64,pfloat64)float64 {
273273
iflen(fs)==0 {
274274
return-1
275275
}
@@ -282,6 +282,14 @@ func tryPercentile(fs []float64, p float64) float64 {
282282
returnfs[lower]+ (fs[upper]-fs[lower])*(pos-float64(lower))
283283
}
284284

285+
functryPercentileDisc(fs []float64,pfloat64)float64 {
286+
iflen(fs)==0 {
287+
return-1
288+
}
289+
sort.Float64s(fs)
290+
returnfs[max(int(math.Ceil(float64(len(fs))*p/100-1)),0)]
291+
}
292+
285293
funcvalidateDatabaseTypeWithValid(v reflect.Value) (handledbool,errerror) {
286294
ifv.Kind()==reflect.Struct {
287295
returnfalse,nil
@@ -2790,8 +2798,8 @@ func (q *FakeQuerier) GetDeploymentWorkspaceAgentStats(_ context.Context, create
27902798
latencies=append(latencies,agentStat.ConnectionMedianLatencyMS)
27912799
}
27922800

2793-
stat.WorkspaceConnectionLatency50=tryPercentile(latencies,50)
2794-
stat.WorkspaceConnectionLatency95=tryPercentile(latencies,95)
2801+
stat.WorkspaceConnectionLatency50=tryPercentileCont(latencies,50)
2802+
stat.WorkspaceConnectionLatency95=tryPercentileCont(latencies,95)
27952803

27962804
returnstat,nil
27972805
}
@@ -2839,8 +2847,8 @@ func (q *FakeQuerier) GetDeploymentWorkspaceAgentUsageStats(_ context.Context, c
28392847
stat.WorkspaceTxBytes+=agentStat.TxBytes
28402848
latencies=append(latencies,agentStat.ConnectionMedianLatencyMS)
28412849
}
2842-
stat.WorkspaceConnectionLatency50=tryPercentile(latencies,50)
2843-
stat.WorkspaceConnectionLatency95=tryPercentile(latencies,95)
2850+
stat.WorkspaceConnectionLatency50=tryPercentileCont(latencies,50)
2851+
stat.WorkspaceConnectionLatency95=tryPercentileCont(latencies,95)
28442852

28452853
for_,agentStat:=rangesessions {
28462854
stat.SessionCountVSCode+=agentStat.SessionCountVSCode
@@ -4987,9 +4995,9 @@ func (q *FakeQuerier) GetTemplateAverageBuildTime(ctx context.Context, arg datab
49874995
}
49884996

49894997
varrow database.GetTemplateAverageBuildTimeRow
4990-
row.Delete50,row.Delete95=tryPercentile(deleteTimes,50),tryPercentile(deleteTimes,95)
4991-
row.Stop50,row.Stop95=tryPercentile(stopTimes,50),tryPercentile(stopTimes,95)
4992-
row.Start50,row.Start95=tryPercentile(startTimes,50),tryPercentile(startTimes,95)
4998+
row.Delete50,row.Delete95=tryPercentileDisc(deleteTimes,50),tryPercentileDisc(deleteTimes,95)
4999+
row.Stop50,row.Stop95=tryPercentileDisc(stopTimes,50),tryPercentileDisc(stopTimes,95)
5000+
row.Start50,row.Start95=tryPercentileDisc(startTimes,50),tryPercentileDisc(startTimes,95)
49935001
returnrow,nil
49945002
}
49955003

@@ -6024,8 +6032,8 @@ func (q *FakeQuerier) GetUserLatencyInsights(_ context.Context, arg database.Get
60246032
Username:user.Username,
60256033
AvatarURL:user.AvatarURL,
60266034
TemplateIDs:seenTemplatesByUserID[userID],
6027-
WorkspaceConnectionLatency50:tryPercentile(latencies,50),
6028-
WorkspaceConnectionLatency95:tryPercentile(latencies,95),
6035+
WorkspaceConnectionLatency50:tryPercentileCont(latencies,50),
6036+
WorkspaceConnectionLatency95:tryPercentileCont(latencies,95),
60296037
}
60306038
rows=append(rows,row)
60316039
}
@@ -6669,8 +6677,8 @@ func (q *FakeQuerier) GetWorkspaceAgentStats(_ context.Context, createdAfter tim
66696677
if!ok {
66706678
continue
66716679
}
6672-
stat.WorkspaceConnectionLatency50=tryPercentile(latencies,50)
6673-
stat.WorkspaceConnectionLatency95=tryPercentile(latencies,95)
6680+
stat.WorkspaceConnectionLatency50=tryPercentileCont(latencies,50)
6681+
stat.WorkspaceConnectionLatency95=tryPercentileCont(latencies,95)
66746682
statByAgent[stat.AgentID]=stat
66756683
}
66766684

@@ -6807,8 +6815,8 @@ func (q *FakeQuerier) GetWorkspaceAgentUsageStats(_ context.Context, createdAt t
68076815
forkey,latencies:=rangelatestAgentLatencies {
68086816
val,ok:=latestAgentStats[key]
68096817
ifok {
6810-
val.WorkspaceConnectionLatency50=tryPercentile(latencies,50)
6811-
val.WorkspaceConnectionLatency95=tryPercentile(latencies,95)
6818+
val.WorkspaceConnectionLatency50=tryPercentileCont(latencies,50)
6819+
val.WorkspaceConnectionLatency95=tryPercentileCont(latencies,95)
68126820
}
68136821
latestAgentStats[key]=val
68146822
}

‎coderd/database/queries.sql.go

Lines changed: 5 additions & 7 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎coderd/database/queries/workspaces.sql

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -415,13 +415,11 @@ WHERE
415415
ORDER BY created_atDESC;
416416

417417
-- name: GetWorkspaceUniqueOwnerCountByTemplateIDs :many
418-
SELECT
419-
template_id,COUNT(DISTINCT owner_id)AS unique_owners_sum
420-
FROM
421-
workspaces
422-
WHERE
423-
template_id= ANY(@template_ids :: uuid[])AND deleted= false
424-
GROUP BY template_id;
418+
SELECTtemplates.idAS template_id,COUNT(DISTINCTworkspaces.owner_id)AS unique_owners_sum
419+
FROM templates
420+
LEFT JOIN workspacesONworkspaces.template_id=templates.idANDworkspaces.deleted= false
421+
WHEREtemplates.id= ANY(@template_ids :: uuid[])
422+
GROUP BYtemplates.id;
425423

426424
-- name: InsertWorkspace :one
427425
INSERT INTO

‎coderd/metricscache/metricscache.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/coder/coder/v2/coderd/database/dbauthz"
1616
"github.com/coder/coder/v2/coderd/database/dbtime"
1717
"github.com/coder/coder/v2/codersdk"
18+
"github.com/coder/quartz"
1819
"github.com/coder/retry"
1920
)
2021

@@ -26,6 +27,7 @@ import (
2627
typeCachestruct {
2728
database database.Store
2829
log slog.Logger
30+
clock quartz.Clock
2931
intervalsIntervals
3032

3133
templateWorkspaceOwners atomic.Pointer[map[uuid.UUID]int]
@@ -45,7 +47,7 @@ type Intervals struct {
4547
DeploymentStats time.Duration
4648
}
4749

48-
funcNew(db database.Store,log slog.Logger,intervalsIntervals,usagebool)*Cache {
50+
funcNew(db database.Store,log slog.Logger,clock quartz.Clock,intervalsIntervals,usagebool)*Cache {
4951
ifintervals.TemplateBuildTimes<=0 {
5052
intervals.TemplateBuildTimes=time.Hour
5153
}
@@ -55,6 +57,7 @@ func New(db database.Store, log slog.Logger, intervals Intervals, usage bool) *C
5557
ctx,cancel:=context.WithCancel(context.Background())
5658

5759
c:=&Cache{
60+
clock:clock,
5861
database:db,
5962
intervals:intervals,
6063
log:log,
@@ -104,7 +107,7 @@ func (c *Cache) refreshTemplateBuildTimes(ctx context.Context) error {
104107
Valid:true,
105108
},
106109
StartTime: sql.NullTime{
107-
Time:dbtime.Time(time.Now().AddDate(0,0,-30)),
110+
Time:dbtime.Time(c.clock.Now().AddDate(0,0,-30)),
108111
Valid:true,
109112
},
110113
})
@@ -131,7 +134,7 @@ func (c *Cache) refreshTemplateBuildTimes(ctx context.Context) error {
131134

132135
func (c*Cache)refreshDeploymentStats(ctx context.Context)error {
133136
var (
134-
from=dbtime.Now().Add(-15*time.Minute)
137+
from=c.clock.Now().Add(-15*time.Minute)
135138
agentStats database.GetDeploymentWorkspaceAgentStatsRow
136139
errerror
137140
)
@@ -155,8 +158,8 @@ func (c *Cache) refreshDeploymentStats(ctx context.Context) error {
155158
}
156159
c.deploymentStatsResponse.Store(&codersdk.DeploymentStats{
157160
AggregatedFrom:from,
158-
CollectedAt:dbtime.Now(),
159-
NextUpdateAt:dbtime.Now().Add(c.intervals.DeploymentStats),
161+
CollectedAt:dbtime.Time(c.clock.Now()),
162+
NextUpdateAt:dbtime.Time(c.clock.Now().Add(c.intervals.DeploymentStats)),
160163
Workspaces: codersdk.WorkspaceDeploymentStats{
161164
Pending:workspaceStats.PendingWorkspaces,
162165
Building:workspaceStats.BuildingWorkspaces,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp