Skip to content

Commit 362911e

Browse files
fix: throttle prebuild notifications
1 parent d61353f commit 362911e

15 files changed

+399
-12
lines changed

coderd/database/dbauthz/dbauthz.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2636,6 +2636,14 @@ func (q *querier) GetTemplateParameterInsights(ctx context.Context, arg database
26362636
return q.db.GetTemplateParameterInsights(ctx, arg)
26372637
}
26382638

2639+
func (q *querier) GetTemplatePrebuildNotificationCooldown(ctx context.Context, arg database.GetTemplatePrebuildNotificationCooldownParams) (database.TemplatePrebuildNotificationCooldown, error) {
2640+
_, err := q.GetTemplateByID(ctx, arg.TemplateID)
2641+
if err != nil {
2642+
return database.TemplatePrebuildNotificationCooldown{}, xerrors.Errorf("failed to get template by id: %w", err)
2643+
}
2644+
return q.db.GetTemplatePrebuildNotificationCooldown(ctx, arg)
2645+
}
2646+
26392647
func (q *querier) GetTemplatePresetsWithPrebuilds(ctx context.Context, templateID uuid.NullUUID) ([]database.GetTemplatePresetsWithPrebuildsRow, error) {
26402648
// GetTemplatePresetsWithPrebuilds retrieves template versions with configured presets and prebuilds.
26412649
// Presets and prebuilds are part of the template, so if you can access templates - you can access them as well.
@@ -5123,6 +5131,17 @@ func (q *querier) UpsertTelemetryItem(ctx context.Context, arg database.UpsertTe
51235131
return q.db.UpsertTelemetryItem(ctx, arg)
51245132
}
51255133

5134+
func (q *querier) UpsertTemplatePrebuildNotificationCooldown(ctx context.Context, arg database.UpsertTemplatePrebuildNotificationCooldownParams) error {
5135+
tmpl, err := q.db.GetTemplateByID(ctx, arg.TemplateID)
5136+
if err != nil {
5137+
return xerrors.Errorf("verify template by id: %w", err)
5138+
}
5139+
if err := q.authorizeContext(ctx, policy.ActionCreate, tmpl); err != nil {
5140+
return err
5141+
}
5142+
return q.db.UpsertTemplatePrebuildNotificationCooldown(ctx, arg)
5143+
}
5144+
51265145
func (q *querier) UpsertTemplateUsageStats(ctx context.Context) error {
51275146
if err := q.authorizeContext(ctx, policy.ActionUpdate, rbac.ResourceSystem); err != nil {
51285147
return err

coderd/database/dbauthz/dbauthz_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/coder/coder/v2/coderd/database/dbtime"
3333
"github.com/coder/coder/v2/coderd/rbac"
3434
"github.com/coder/coder/v2/coderd/util/slice"
35+
"github.com/coder/coder/v2/enterprise/coderd/prebuilds"
3536
"github.com/coder/coder/v2/provisionersdk"
3637
"github.com/coder/coder/v2/testutil"
3738
)
@@ -5087,6 +5088,39 @@ func (s *MethodTestSuite) TestPrebuilds() {
50875088
check.Args(req).
50885089
Asserts(rbac.ResourceTemplate.WithID(template.ID).InOrg(org.ID), policy.ActionUpdate)
50895090
}))
5091+
s.Run("GetTemplatePrebuildNotificationCooldown", s.Subtest(func(db database.Store, check *expects) {
5092+
org := dbgen.Organization(s.T(), db, database.Organization{})
5093+
user := dbgen.User(s.T(), db, database.User{})
5094+
template := dbgen.Template(s.T(), db, database.Template{
5095+
OrganizationID: org.ID,
5096+
CreatedBy: user.ID,
5097+
})
5098+
arg := database.GetTemplatePrebuildNotificationCooldownParams{
5099+
TemplateID: template.ID,
5100+
NotificationType: prebuilds.NotificationTypeAdmin,
5101+
}
5102+
check.Args(arg).
5103+
Asserts(template, policy.ActionRead).
5104+
Returns(database.TemplatePrebuildNotificationCooldown{}).
5105+
ErrorsWithPG(sql.ErrNoRows).
5106+
ErrorsWithInMemDB(dbmem.ErrUnimplemented)
5107+
}))
5108+
s.Run("UpsertTemplatePrebuildNotificationCooldown", s.Subtest(func(db database.Store, check *expects) {
5109+
org := dbgen.Organization(s.T(), db, database.Organization{})
5110+
user := dbgen.User(s.T(), db, database.User{})
5111+
template := dbgen.Template(s.T(), db, database.Template{
5112+
OrganizationID: org.ID,
5113+
CreatedBy: user.ID,
5114+
})
5115+
arg := database.UpsertTemplatePrebuildNotificationCooldownParams{
5116+
TemplateID: template.ID,
5117+
NotificationType: prebuilds.NotificationTypeAdmin,
5118+
}
5119+
check.Args(arg).
5120+
Asserts(template, policy.ActionCreate).
5121+
Returns().
5122+
ErrorsWithInMemDB(dbmem.ErrUnimplemented)
5123+
}))
50905124
}
50915125

50925126
func (s *MethodTestSuite) TestOAuth2ProviderApps() {

coderd/database/dbmem/dbmem.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6181,6 +6181,15 @@ func (q *FakeQuerier) GetTemplateParameterInsights(ctx context.Context, arg data
61816181
return rows, nil
61826182
}
61836183

6184+
func (q *FakeQuerier) GetTemplatePrebuildNotificationCooldown(ctx context.Context, arg database.GetTemplatePrebuildNotificationCooldownParams) (database.TemplatePrebuildNotificationCooldown, error) {
6185+
err := validateDatabaseType(arg)
6186+
if err != nil {
6187+
return database.TemplatePrebuildNotificationCooldown{}, err
6188+
}
6189+
6190+
return database.TemplatePrebuildNotificationCooldown{}, ErrUnimplemented
6191+
}
6192+
61846193
func (*FakeQuerier) GetTemplatePresetsWithPrebuilds(_ context.Context, _ uuid.NullUUID) ([]database.GetTemplatePresetsWithPrebuildsRow, error) {
61856194
return nil, ErrUnimplemented
61866195
}
@@ -12555,6 +12564,15 @@ func (q *FakeQuerier) UpsertTelemetryItem(_ context.Context, arg database.Upsert
1255512564
return nil
1255612565
}
1255712566

12567+
func (q *FakeQuerier) UpsertTemplatePrebuildNotificationCooldown(ctx context.Context, arg database.UpsertTemplatePrebuildNotificationCooldownParams) error {
12568+
err := validateDatabaseType(arg)
12569+
if err != nil {
12570+
return err
12571+
}
12572+
12573+
return ErrUnimplemented
12574+
}
12575+
1255812576
func (q *FakeQuerier) UpsertTemplateUsageStats(ctx context.Context) error {
1255912577
q.mutex.Lock()
1256012578
defer q.mutex.Unlock()

coderd/database/dbmetrics/querymetrics.go

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

coderd/database/dbmock/dbmock.go

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

coderd/database/dump.sql

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE IF EXISTS template_prebuild_notification_cooldowns;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CREATE TABLE template_prebuild_notification_cooldowns (
2+
template_id UUID NOT NULL,
3+
notification_type TEXT NOT NULL,
4+
last_notification_sent TIMESTAMPTZ NOT NULL,
5+
PRIMARY KEY (template_id, notification_type)
6+
);
7+
8+
COMMENT ON TABLE template_prebuild_notification_cooldowns IS 'Tracks when prebuild failure notifications were last sent to prevent notification noise';
9+
COMMENT ON COLUMN template_prebuild_notification_cooldowns.notification_type IS 'Type of notification: admin or author';

coderd/database/models.go

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

coderd/database/querier.go

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

0 commit comments

Comments
 (0)