Skip to content

Open-source status page, incident response and uptime monitoring

perf(server): reuse validated page monitors (#2583)

유민호07d885a06c5f
diff --git a/apps/server/src/routes/v1/pages/post.ts b/apps/server/src/routes/v1/pages/post.ts
index c128e8d37..260b56e28 100644
--- a/apps/server/src/routes/v1/pages/post.ts
+++ b/apps/server/src/routes/v1/pages/post.ts
@@ -134,24 +134,28 @@ export function registerPostPage(api: typeof pagesApi) {
const { monitors, ...rest } = input;
- if (monitors?.length) {
- const monitorIds = isNumberArray(monitors)
+ const monitorIds = monitors
+ ? isNumberArray(monitors)
? monitors
- : monitors.map((m) => m.monitorId);
-
- const _monitors = await db
- .select()
- .from(monitor)
- .where(
- and(
- inArray(monitor.id, monitorIds),
- eq(monitor.workspaceId, workspaceId),
- isNull(monitor.deletedAt),
- ),
- )
- .all();
-
- if (_monitors.length !== monitors.length) {
+ : monitors.map((m) => m.monitorId)
+ : [];
+
+ const monitorsData = monitors?.length
+ ? await db
+ .select()
+ .from(monitor)
+ .where(
+ and(
+ inArray(monitor.id, monitorIds),
+ eq(monitor.workspaceId, workspaceId),
+ isNull(monitor.deletedAt),
+ ),
+ )
+ .all()
+ : [];
+
+ if (monitors?.length) {
+ if (monitorsData.length !== monitors.length) {
throw new OpenStatusApiError({
code: "BAD_REQUEST",
message: `Some of the monitors ${monitorIds.join(", ")} not found`,
@@ -159,6 +163,8 @@ export function registerPostPage(api: typeof pagesApi) {
}
}
+ const monitorsById = new Map(monitorsData.map((m) => [m.id, m]));
+
const _page = await db
.insert(page)
.values({
@@ -177,13 +183,7 @@ export function registerPostPage(api: typeof pagesApi) {
for (const [index, m] of monitors.entries()) {
const values = typeof m === "number" ? { monitorId: m } : m;
- const _monitor = await db.query.monitor.findFirst({
- where: and(
- eq(monitor.id, values.monitorId),
- eq(monitor.workspaceId, workspaceId),
- isNull(monitor.deletedAt),
- ),
- });
+ const _monitor = monitorsById.get(values.monitorId);
if (!_monitor) {
throw new OpenStatusApiError({
diff --git a/apps/server/src/routes/v1/pages/put.ts b/apps/server/src/routes/v1/pages/put.ts
index 5514ba6e9..7911e1482 100644
--- a/apps/server/src/routes/v1/pages/put.ts
+++ b/apps/server/src/routes/v1/pages/put.ts
@@ -143,19 +143,21 @@ export function registerPutPage(api: typeof pagesApi) {
: monitors.map((m) => m.monitorId)
: [];
- if (monitors?.length) {
- const monitorsData = await db
- .select()
- .from(monitor)
- .where(
- and(
- inArray(monitor.id, monitorIds),
- eq(monitor.workspaceId, workspaceId),
- isNull(monitor.deletedAt),
- ),
- )
- .all();
+ const monitorsData = monitors?.length
+ ? await db
+ .select()
+ .from(monitor)
+ .where(
+ and(
+ inArray(monitor.id, monitorIds),
+ eq(monitor.workspaceId, workspaceId),
+ isNull(monitor.deletedAt),
+ ),
+ )
+ .all()
+ : [];
+ if (monitors?.length) {
if (monitorsData.length !== monitors.length) {
throw new OpenStatusApiError({
code: "BAD_REQUEST",
@@ -164,6 +166,8 @@ export function registerPutPage(api: typeof pagesApi) {
}
}
+ const monitorsById = new Map(monitorsData.map((m) => [m.id, m]));
+
const newPage = await db
.update(page)
.set({
@@ -209,13 +213,7 @@ export function registerPutPage(api: typeof pagesApi) {
for (const [index, m] of monitors.entries()) {
const values = typeof m === "number" ? { monitorId: m } : m;
- const _monitor = await db.query.monitor.findFirst({
- where: and(
- eq(monitor.id, values.monitorId),
- eq(monitor.workspaceId, workspaceId),
- isNull(monitor.deletedAt),
- ),
- });
+ const _monitor = monitorsById.get(values.monitorId);
if (!_monitor) {
throw new OpenStatusApiError({