-
Notifications
You must be signed in to change notification settings - Fork 875
chore: test metricscache on postgres #16711
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
metricscache_test has been running tests against dbmem only, instead of against postgres. Unfortunately the implementations of GetTemplateAverageBuildTime have diverged between dbmem and postgres. This change gets the tests working on Postgres and test for the behaviour postgres provides.
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ import ( | |
"github.com/coder/coder/v2/coderd/database/dbauthz" | ||
"github.com/coder/coder/v2/coderd/database/dbtime" | ||
"github.com/coder/coder/v2/codersdk" | ||
"github.com/coder/quartz" | ||
"github.com/coder/retry" | ||
) | ||
|
||
|
@@ -26,6 +27,7 @@ import ( | |
type Cache struct { | ||
database database.Store | ||
log slog.Logger | ||
clock quartz.Clock | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
intervals Intervals | ||
|
||
templateWorkspaceOwners atomic.Pointer[map[uuid.UUID]int] | ||
|
@@ -45,7 +47,7 @@ type Intervals struct { | |
DeploymentStats time.Duration | ||
} | ||
|
||
func New(db database.Store, log slog.Logger, intervals Intervals, usage bool) *Cache { | ||
func New(db database.Store, log slog.Logger, clock quartz.Clock, intervals Intervals, usage bool) *Cache { | ||
if intervals.TemplateBuildTimes <= 0 { | ||
intervals.TemplateBuildTimes = time.Hour | ||
} | ||
|
@@ -55,6 +57,7 @@ func New(db database.Store, log slog.Logger, intervals Intervals, usage bool) *C | |
ctx, cancel := context.WithCancel(context.Background()) | ||
|
||
c := &Cache{ | ||
clock: clock, | ||
database: db, | ||
intervals: intervals, | ||
log: log, | ||
|
@@ -84,7 +87,7 @@ func (c *Cache) refreshTemplateBuildTimes(ctx context.Context) error { | |
//nolint:gocritic // This is a system service. | ||
ctx = dbauthz.AsSystemRestricted(ctx) | ||
|
||
templates, err := c.database.GetTemplates(ctx) | ||
templates, err := c.database.GetTemplatesWithFilter(ctx, database.GetTemplatesWithFilterParams{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -104,7 +107,7 @@ func (c *Cache) refreshTemplateBuildTimes(ctx context.Context) error { | |
Valid: true, | ||
}, | ||
StartTime: sql.NullTime{ | ||
Time: dbtime.Time(time.Now().AddDate(0, 0, -30)), | ||
Time: dbtime.Time(c.clock.Now().AddDate(0, 0, -30)), | ||
Valid: true, | ||
}, | ||
}) | ||
|
@@ -131,7 +134,7 @@ func (c *Cache) refreshTemplateBuildTimes(ctx context.Context) error { | |
|
||
func (c *Cache) refreshDeploymentStats(ctx context.Context) error { | ||
var ( | ||
from = dbtime.Now().Add(-15 * time.Minute) | ||
from = c.clock.Now().Add(-15 * time.Minute) | ||
agentStats database.GetDeploymentWorkspaceAgentStatsRow | ||
err error | ||
) | ||
|
@@ -155,8 +158,8 @@ func (c *Cache) refreshDeploymentStats(ctx context.Context) error { | |
} | ||
c.deploymentStatsResponse.Store(&codersdk.DeploymentStats{ | ||
AggregatedFrom: from, | ||
CollectedAt: dbtime.Now(), | ||
NextUpdateAt: dbtime.Now().Add(c.intervals.DeploymentStats), | ||
CollectedAt: c.clock.Now(), | ||
NextUpdateAt: c.clock.Now().Add(c.intervals.DeploymentStats), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should these be wrapped in a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, forgot to add that |
||
Workspaces: codersdk.WorkspaceDeploymentStats{ | ||
Pending: workspaceStats.PendingWorkspaces, | ||
Building: workspaceStats.BuildingWorkspaces, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rewrote this query as the test-suite expected this query to be able to return
unique_owners_sum=0
, however the previous implementation would return nothing if there were no workspaces.