Skip to content

Commit 6c6240b

Browse files
chore: add workspace reached resource threshold notification
1 parent 84081e9 commit 6c6240b

File tree

6 files changed

+148
-8
lines changed

6 files changed

+148
-8
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DELETE FROM notification_templates WHERE id = 'a9d027b4-ac49-4fb1-9f6d-45af15f64e7a';
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
INSERT INTO notification_templates
2+
(id, name, title_template, body_template, "group", actions)
3+
VALUES (
4+
'a9d027b4-ac49-4fb1-9f6d-45af15f64e7a',
5+
'Workspace Reached Resource Threshold',
6+
E'Workspace "{{.Labels.workspace}}" reached resource threshold',
7+
E'Hi {{.UserName}},\n\n'||
8+
E'Your workspace **{{.Labels.workspace}}** has reached the {{.Labels.threshold_type}} threshold set at **{{.Labels.threshold}}**.',
9+
'Workspace Events',
10+
'[
11+
{
12+
"label": "View workspace",
13+
"url": "{{base_url}}/@{{.UserUsername}}/{{.Labels.workspace}}"
14+
}
15+
]'::jsonb
16+
);

coderd/notifications/events.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import "github.com/google/uuid"
77

88
// Workspace-related events.
99
var (
10-
TemplateWorkspaceCreated = uuid.MustParse("281fdf73-c6d6-4cbb-8ff5-888baf8a2fff")
11-
TemplateWorkspaceManuallyUpdated = uuid.MustParse("d089fe7b-d5c5-4c0c-aaf5-689859f7d392")
12-
TemplateWorkspaceDeleted = uuid.MustParse("f517da0b-cdc9-410f-ab89-a86107c420ed")
13-
TemplateWorkspaceAutobuildFailed = uuid.MustParse("381df2a9-c0c0-4749-420f-80a9280c66f9")
14-
TemplateWorkspaceDormant = uuid.MustParse("0ea69165-ec14-4314-91f1-69566ac3c5a0")
15-
TemplateWorkspaceAutoUpdated = uuid.MustParse("c34a0c09-0704-4cac-bd1c-0c0146811c2b")
16-
TemplateWorkspaceMarkedForDeletion = uuid.MustParse("51ce2fdf-c9ca-4be1-8d70-628674f9bc42")
17-
TemplateWorkspaceManualBuildFailed = uuid.MustParse("2faeee0f-26cb-4e96-821c-85ccb9f71513")
10+
TemplateWorkspaceCreated = uuid.MustParse("281fdf73-c6d6-4cbb-8ff5-888baf8a2fff")
11+
TemplateWorkspaceManuallyUpdated = uuid.MustParse("d089fe7b-d5c5-4c0c-aaf5-689859f7d392")
12+
TemplateWorkspaceDeleted = uuid.MustParse("f517da0b-cdc9-410f-ab89-a86107c420ed")
13+
TemplateWorkspaceAutobuildFailed = uuid.MustParse("381df2a9-c0c0-4749-420f-80a9280c66f9")
14+
TemplateWorkspaceDormant = uuid.MustParse("0ea69165-ec14-4314-91f1-69566ac3c5a0")
15+
TemplateWorkspaceAutoUpdated = uuid.MustParse("c34a0c09-0704-4cac-bd1c-0c0146811c2b")
16+
TemplateWorkspaceMarkedForDeletion = uuid.MustParse("51ce2fdf-c9ca-4be1-8d70-628674f9bc42")
17+
TemplateWorkspaceManualBuildFailed = uuid.MustParse("2faeee0f-26cb-4e96-821c-85ccb9f71513")
18+
TemplateWorkspaceReachedResourceThreshold = uuid.MustParse("a9d027b4-ac49-4fb1-9f6d-45af15f64e7a")
1819
)
1920

2021
// Account-related events.

coderd/notifications/notifications_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,20 @@ func TestNotificationTemplates_Golden(t *testing.T) {
10641064
},
10651065
},
10661066
},
1067+
{
1068+
name: "TemplateWorkspaceReachedResourceThreshold",
1069+
id: notifications.TemplateWorkspaceReachedResourceThreshold,
1070+
payload: types.MessagePayload{
1071+
UserName: "Bobby",
1072+
UserEmail: "bobby@coder.com",
1073+
UserUsername: "bobby",
1074+
Labels: map[string]string{
1075+
"workspace": "bobby-workspace",
1076+
"threshold_type": "memory usage",
1077+
"threshold": "90%",
1078+
},
1079+
},
1080+
},
10671081
}
10681082

10691083
// We must have a test case for every notification_template. This is enforced below:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
From: system@coder.com
2+
To: bobby@coder.com
3+
Subject: Workspace "bobby-workspace" reached resource threshold
4+
Message-Id: 02ee4935-73be-4fa1-a290-ff9999026b13@blush-whale-48
5+
Date: Fri, 11 Oct 2024 09:03:06 +0000
6+
Content-Type: multipart/alternative; boundary=bbe61b741255b6098bb6b3c1f41b885773df633cb18d2a3002b68e4bc9c4
7+
MIME-Version: 1.0
8+
9+
--bbe61b741255b6098bb6b3c1f41b885773df633cb18d2a3002b68e4bc9c4
10+
Content-Transfer-Encoding: quoted-printable
11+
Content-Type: text/plain; charset=UTF-8
12+
13+
Hi Bobby,
14+
15+
Your workspace bobby-workspace has reached the memory usage threshold set a=
16+
t 90%.
17+
18+
19+
View workspace: http://test.com/@bobby/bobby-workspace
20+
21+
--bbe61b741255b6098bb6b3c1f41b885773df633cb18d2a3002b68e4bc9c4
22+
Content-Transfer-Encoding: quoted-printable
23+
Content-Type: text/html; charset=UTF-8
24+
25+
<!doctype html>
26+
<html lang=3D"en">
27+
<head>
28+
<meta charset=3D"UTF-8" />
29+
<meta name=3D"viewport" content=3D"width=3Ddevice-width, initial-scale=
30+
=3D1.0" />
31+
<title>Workspace "bobby-workspace" reached resource threshold</title>
32+
</head>
33+
<body style=3D"margin: 0; padding: 0; font-family: -apple-system, system-=
34+
ui, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarel=
35+
l', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif; color: #020617=
36+
; background: #f8fafc;">
37+
<div style=3D"max-width: 600px; margin: 20px auto; padding: 60px; borde=
38+
r: 1px solid #e2e8f0; border-radius: 8px; background-color: #fff; text-alig=
39+
n: left; font-size: 14px; line-height: 1.5;">
40+
<div style=3D"text-align: center;">
41+
<img src=3D"https://coder.com/coder-logo-horizontal.png" alt=3D"Cod=
42+
er Logo" style=3D"height: 40px;" />
43+
</div>
44+
<h1 style=3D"text-align: center; font-size: 24px; font-weight: 400; m=
45+
argin: 8px 0 32px; line-height: 1.5;">
46+
Workspace "bobby-workspace" reached resource threshold
47+
</h1>
48+
<div style=3D"line-height: 1.5;">
49+
<p>Hi Bobby,</p>
50+
51+
<p>Your workspace <strong>bobby-workspace</strong> has reached the memory u=
52+
sage threshold set at <strong>90%</strong>.</p>
53+
</div>
54+
<div style=3D"text-align: center; margin-top: 32px;">
55+
=20
56+
<a href=3D"http://test.com/@bobby/bobby-workspace" style=3D"display=
57+
: inline-block; padding: 13px 24px; background-color: #020617; color: #f8fa=
58+
fc; text-decoration: none; border-radius: 8px; margin: 0 4px;">
59+
View workspace
60+
</a>
61+
=20
62+
</div>
63+
<div style=3D"border-top: 1px solid #e2e8f0; color: #475569; font-siz=
64+
e: 12px; margin-top: 64px; padding-top: 24px; line-height: 1.6;">
65+
<p>&copy;&nbsp;2024&nbsp;Coder. All rights reserved&nbsp;-&nbsp;<a =
66+
href=3D"http://test.com" style=3D"color: #2563eb; text-decoration: none;">h=
67+
ttp://test.com</a></p>
68+
<p><a href=3D"http://test.com/settings/notifications" style=3D"colo=
69+
r: #2563eb; text-decoration: none;">Click here to manage your notification =
70+
settings</a></p>
71+
<p><a href=3D"http://test.com/settings/notifications?disabled=3Da9d=
72+
027b4-ac49-4fb1-9f6d-45af15f64e7a" style=3D"color: #2563eb; text-decoration=
73+
: none;">Stop receiving emails like this</a></p>
74+
</div>
75+
</div>
76+
</body>
77+
</html>
78+
79+
--bbe61b741255b6098bb6b3c1f41b885773df633cb18d2a3002b68e4bc9c4--
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"_version": "1.1",
3+
"msg_id": "00000000-0000-0000-0000-000000000000",
4+
"payload": {
5+
"_version": "1.1",
6+
"notification_name": "Workspace Reached Resource Threshold",
7+
"notification_template_id": "00000000-0000-0000-0000-000000000000",
8+
"user_id": "00000000-0000-0000-0000-000000000000",
9+
"user_email": "bobby@coder.com",
10+
"user_name": "Bobby",
11+
"user_username": "bobby",
12+
"actions": [
13+
{
14+
"label": "View workspace",
15+
"url": "http://test.com/@bobby/bobby-workspace"
16+
}
17+
],
18+
"labels": {
19+
"threshold": "90%",
20+
"threshold_type": "memory usage",
21+
"workspace": "bobby-workspace"
22+
},
23+
"data": null
24+
},
25+
"title": "Workspace \"bobby-workspace\" reached resource threshold",
26+
"title_markdown": "Workspace \"bobby-workspace\" reached resource threshold",
27+
"body": "Hi Bobby,\n\nYour workspace bobby-workspace has reached the memory usage threshold set at 90%.",
28+
"body_markdown": "Hi Bobby,\n\nYour workspace **bobby-workspace** has reached the memory usage threshold set at **90%**."
29+
}

0 commit comments

Comments
 (0)