Skip to content

Commit aef1287

Browse files
committed
TODO
1 parent 9a414f0 commit aef1287

File tree

12 files changed

+126
-11
lines changed

12 files changed

+126
-11
lines changed

coderd/database/dbauthz/dbauthz.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,10 @@ func (q *querier) DeleteOldProvisionerDaemons(ctx context.Context) error {
11441144
return q.db.DeleteOldProvisionerDaemons(ctx)
11451145
}
11461146

1147+
func (q *querier) DeleteOldReportGeneratorLogs(ctx context.Context, frequencyDays int32) error {
1148+
panic("not implemented")
1149+
}
1150+
11471151
func (q *querier) DeleteOldWorkspaceAgentLogs(ctx context.Context, threshold time.Time) error {
11481152
if err := q.authorizeContext(ctx, policy.ActionDelete, rbac.ResourceSystem); err != nil {
11491153
return err
@@ -3906,6 +3910,10 @@ func (q *querier) UpsertProvisionerDaemon(ctx context.Context, arg database.Upse
39063910
return q.db.UpsertProvisionerDaemon(ctx, arg)
39073911
}
39083912

3913+
func (q *querier) UpsertReportGeneratorLog(ctx context.Context, arg database.UpsertReportGeneratorLogParams) error {
3914+
panic("not implemented")
3915+
}
3916+
39093917
func (q *querier) UpsertTailnetAgent(ctx context.Context, arg database.UpsertTailnetAgentParams) (database.TailnetAgent, error) {
39103918
if err := q.authorizeContext(ctx, policy.ActionUpdate, rbac.ResourceTailnetCoordinator); err != nil {
39113919
return database.TailnetAgent{}, err

coderd/database/dbmem/dbmem.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1706,6 +1706,10 @@ func (q *FakeQuerier) DeleteOldProvisionerDaemons(_ context.Context) error {
17061706
return nil
17071707
}
17081708

1709+
func (q *FakeQuerier) DeleteOldReportGeneratorLogs(ctx context.Context, frequencyDays int32) error {
1710+
panic("not implemented")
1711+
}
1712+
17091713
func (q *FakeQuerier) DeleteOldWorkspaceAgentLogs(_ context.Context, threshold time.Time) error {
17101714
q.mutex.Lock()
17111715
defer q.mutex.Unlock()
@@ -9186,6 +9190,15 @@ func (q *FakeQuerier) UpsertProvisionerDaemon(_ context.Context, arg database.Up
91869190
return d, nil
91879191
}
91889192

9193+
func (q *FakeQuerier) UpsertReportGeneratorLog(ctx context.Context, arg database.UpsertReportGeneratorLogParams) error {
9194+
err := validateDatabaseType(arg)
9195+
if err != nil {
9196+
return err
9197+
}
9198+
9199+
panic("not implemented")
9200+
}
9201+
91899202
func (*FakeQuerier) UpsertTailnetAgent(context.Context, database.UpsertTailnetAgentParams) (database.TailnetAgent, error) {
91909203
return database.TailnetAgent{}, ErrUnimplemented
91919204
}

coderd/database/dbmetrics/dbmetrics.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/dump.sql

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
DELETE FROM notification_templates WHERE id = '34a20db2-e9cc-4a93-b0e4-8569699d7a00';
2+
3+
DROP TABLE report_generator_logs;

coderd/database/migrations/000249_email_reports.up.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,15 @@ We recommend reviewing these issues to ensure future builds are successful.',
1818
"url": "{{ base_url }}/workspaces?filter=template%3A{{.Labels.template_name}}"
1919
}
2020
]'::jsonb);
21+
22+
CREATE TABLE report_generator_logs
23+
(
24+
user_id uuid NOT NULL,
25+
notification_template_id uuid NOT NULL,
26+
last_generated_at timestamp with time zone,
27+
28+
PRIMARY KEY (user_id, notification_template_id),
29+
UNIQUE (user_id, notification_template_id)
30+
);
31+
32+
COMMENT ON TABLE report_generator_logs IS 'Logs with generated reports for users.';

coderd/database/models.go

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

coderd/database/querier.go

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

coderd/database/queries.sql.go

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

coderd/database/queries/notifications.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,12 @@ SELECT *
173173
FROM notification_templates
174174
WHERE kind = @kind::notification_template_kind
175175
ORDER BY name ASC;
176+
177+
-- name: UpsertReportGeneratorLog :exec
178+
-- Insert or update report generator logs with recent activity.
179+
INSERT INTO report_generator_logs (user_id, notification_template_id, last_generated_at) VALUES ($1, $2, $3)
180+
ON CONFLICT (user_id, notification_template_id) DO UPDATE set last_generated_at = $3 WHERE (user_id = $1 AND notification_template_id = $2);
181+
182+
-- name: DeleteOldReportGeneratorLogs :exec
183+
-- Delete report generator logs that have been created at least a <frequency_days> +5m ago.
184+
DELETE FROM report_generator_logs WHERE last_generated_at < (NOW() - CONCAT(@frequency_days::int, ' days')::interval - INTERVAL '5 min');

coderd/database/unique_constraint.go

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

coderd/notifications/reports/generator.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,22 @@ func NewReportGenerator(ctx context.Context, logger slog.Logger, db database.Sto
4141
return nil
4242
}
4343

44-
// TODO:
44+
// TODO Report - workspace_builds_failed:
4545
//
46-
// 1. select(workspace_builds_failed): templates + (template admins + users with "write" permissions) + matching entry for `report_generator_log`:
47-
// 1. check last run `report_generator_log`
48-
// 2. generate report
49-
// 3. send notification
50-
// 4. upsert into `report_generator_log`
51-
//
52-
// 2. clean stale `report_generator_log` entries
46+
// 1. Fetch template admins.
47+
// 2. Fetch templates.
48+
// 3. For every template:
49+
// 1. Fetch failed builds.
50+
// 2. If failed builds == 0, continue.
51+
// 3. Render the report.
52+
// 4. Fetch template RW users.
53+
// 5. For user := range template admins + RW users:
54+
// 1. Check if report is enabled for the person.
55+
// 2. Check `report_generator_log`.
56+
// 3. If sent recently, continue
57+
// 4. Send notification
58+
// 5. Upsert into `report_generator_log`.
59+
// 4. clean stale `report_generator_log` entries
5360

5461
logger.Info(ctx, "report generator finished", slog.F("duration", clk.Since(start)))
5562

0 commit comments

Comments
 (0)