Skip to content

Open-source status page, incident response and uptime monitoring

🚀 monitor (#42)

Thibault Le Ouay9beb5c7042ea
diff --git a/apps/web/src/app/app/(dashboard)/[workspaceId]/incident/page.tsx b/apps/web/src/app/app/(dashboard)/[workspaceId]/incident/page.tsx
index 0f3c84b52..73a61b313 100644
--- a/apps/web/src/app/app/(dashboard)/[workspaceId]/incident/page.tsx
+++ b/apps/web/src/app/app/(dashboard)/[workspaceId]/incident/page.tsx
@@ -3,9 +3,16 @@ import * as React from "react";
import { Container } from "@/components/dashboard/container";
import { Header } from "@/components/dashboard/header";
import { wait } from "@/lib/utils";
+import { api } from "@/trpc/server";
-export default async function IncidentPage() {
- await wait(1000);
+export default async function IncidentPage({
+ params,
+}: {
+ params: { workspaceId: string };
+}) {
+ const incidents = await api.incident.getIncidentByWorkspace.query({
+ workspaceId: Number(params.workspaceId),
+ });
return (
<div className="grid gap-6 md:grid-cols-2 md:gap-8">
<Header title="Monitor" description="Overview of all the responses." />
diff --git a/apps/web/src/app/app/(dashboard)/[workspaceId]/monitor/page.tsx b/apps/web/src/app/app/(dashboard)/[workspaceId]/monitor/page.tsx
index 183b6f334..c49040a54 100644
--- a/apps/web/src/app/app/(dashboard)/[workspaceId]/monitor/page.tsx
+++ b/apps/web/src/app/app/(dashboard)/[workspaceId]/monitor/page.tsx
@@ -2,10 +2,18 @@ import * as React from "react";
import { Container } from "@/components/dashboard/container";
import { Header } from "@/components/dashboard/header";
-import { wait } from "@/lib/utils";
+import { api } from "@/trpc/server";
-export default async function MonitorPage() {
- await wait(1000);
+export default async function MonitorPage({
+ params,
+}: {
+ params: { workspaceId: string };
+}) {
+ const monitors = await api.monitor.getMonitorsByWorkspace.query({
+ workspaceId: Number(params.workspaceId),
+ });
+ console.log(monitors);
+ // iterate over monitors
return (
<div className="grid gap-6 md:grid-cols-2 md:gap-8">
<Header title="Monitor" description="Overview of all the responses." />
diff --git a/apps/web/src/app/app/(dashboard)/[workspaceId]/page.tsx b/apps/web/src/app/app/(dashboard)/[workspaceId]/page.tsx
index b8d44a328..c35d4ca58 100644
--- a/apps/web/src/app/app/(dashboard)/[workspaceId]/page.tsx
+++ b/apps/web/src/app/app/(dashboard)/[workspaceId]/page.tsx
@@ -5,9 +5,15 @@ import { Header } from "@/components/dashboard/header";
import { DialogForm } from "@/components/forms/dialog-form";
import { Button } from "@/components/ui/button";
import { wait } from "@/lib/utils";
+import { api } from "@/trpc/server";
+import Loading from "./loading";
export default async function DashboardPage() {
- await wait(1000);
+ const workspace = await api.workspace.getUserWithWorkspace.query();
+ if (!workspace) {
+ return <Loading />;
+ }
+
return (
<div className="grid gap-6 md:grid-cols-2 md:gap-8">
<div className="col-span-full flex w-full justify-between">
diff --git a/apps/web/src/app/app/(dashboard)/[workspaceId]/page/loading.tsx b/apps/web/src/app/app/(dashboard)/[workspaceId]/page/loading.tsx
new file mode 100644
index 000000000..3c4e9f583
--- /dev/null
+++ b/apps/web/src/app/app/(dashboard)/[workspaceId]/page/loading.tsx
@@ -0,0 +1,13 @@
+import { Container } from "@/components/dashboard/container";
+import { Header } from "@/components/dashboard/header";
+
+export default function Loading() {
+ return (
+ <div className="grid gap-6 md:grid-cols-2 md:gap-8">
+ <Header.Skeleton />
+ <Container.Skeleton />
+ <Container.Skeleton />
+ <Container.Skeleton />
+ </div>
+ );
+}
diff --git a/apps/web/src/app/app/(dashboard)/[workspaceId]/page/page.tsx b/apps/web/src/app/app/(dashboard)/[workspaceId]/page/page.tsx
new file mode 100644
index 000000000..53ffee221
--- /dev/null
+++ b/apps/web/src/app/app/(dashboard)/[workspaceId]/page/page.tsx
@@ -0,0 +1,27 @@
+import * as React from "react";
+
+import { Container } from "@/components/dashboard/container";
+import { Header } from "@/components/dashboard/header";
+import { wait } from "@/lib/utils";
+import { api } from "@/trpc/server";
+
+export default async function Page({
+ params,
+}: {
+ params: { workspaceId: string };
+}) {
+ const pages = await api.page.getPageByWorkspace.query({
+ workspaceId: Number(params.workspaceId),
+ });
+ // iterate over pages
+ return (
+ <div className="grid gap-6 md:grid-cols-2 md:gap-8">
+ <Header
+ title="Status Page"
+ description="Overview of all your status page."
+ />
+ <Container title="Hello"></Container>
+ <Container title="World"></Container>
+ </div>
+ );
+}
diff --git a/apps/web/src/components/icons.tsx b/apps/web/src/components/icons.tsx
index c74426089..af6ca2a8e 100644
--- a/apps/web/src/components/icons.tsx
+++ b/apps/web/src/components/icons.tsx
@@ -1,4 +1,4 @@
-import { Activity, LayoutDashboard, Link, Siren } from "lucide-react";
+import { Activity, LayoutDashboard, Link, PanelTop, Siren } from "lucide-react";
import type { Icon as LucideIcon, LucideProps } from "lucide-react";
export type Icon = LucideIcon;
@@ -9,4 +9,5 @@ export const Icons = {
"layout-dashboard": LayoutDashboard,
link: Link,
siren: Siren,
+ "panel-top": PanelTop,
} as const;
diff --git a/apps/web/src/config/pages.ts b/apps/web/src/config/pages.ts
index 0d7f38884..0889a09f2 100644
--- a/apps/web/src/config/pages.ts
+++ b/apps/web/src/config/pages.ts
@@ -16,19 +16,25 @@ export const pagesConfig = [
icon: "layout-dashboard",
},
{
- title: "Endpoint",
+ title: "Endpoints",
description: "Keep track of all your endpoints.",
href: "/endpoint",
icon: "link",
},
{
- title: "Monitor",
+ title: "Monitors",
description: "Check all the responses in one place.",
href: "/monitor",
icon: "activity",
},
{
- title: "Incident",
+ title: "Pages",
+ description: "Wher you can see all the pages.",
+ href: "/page",
+ icon: "panel-top",
+ },
+ {
+ title: "Incidents",
description: "War room where you handle the incidents.",
href: "/incident",
icon: "siren",
diff --git a/apps/web/src/middleware.ts b/apps/web/src/middleware.ts
index defffb594..ac7181e02 100644
--- a/apps/web/src/middleware.ts
+++ b/apps/web/src/middleware.ts
@@ -28,7 +28,12 @@ export default authMiddleware({
}
// redirect them to organization selection page
- if (auth.userId && req.nextUrl.pathname === "/app") {
+ if (
+ auth.userId &&
+ (req.nextUrl.pathname === "/app" || req.nextUrl.pathname === "/app/")
+ ) {
+ console.log(auth.userId);
+ // improve on sign-up if the webhook has not been triggered yet
const userQuery = db
.select()
.from(user)
@@ -37,14 +42,19 @@ export default authMiddleware({
const result = await db
.select()
.from(usersToWorkspaces)
- .leftJoin(userQuery, eq(userQuery.id, usersToWorkspaces.userId))
+ .innerJoin(userQuery, eq(userQuery.id, usersToWorkspaces.userId))
.execute();
+
if (result.length) {
const orgSelection = new URL(
`/app/${result[0].users_to_workspaces.workspaceId}`,
req.url,
);
return NextResponse.redirect(orgSelection);
+ } else {
+ // return NextResponse.redirect(new URL("/app/onboarding", req.url));
+ // probably redirect to onboarding
+ // or find a way to wait for the webhook
}
}
},
diff --git a/packages/api/src/router/incident.ts b/packages/api/src/router/incident.ts
index a9b99b06a..1d7b3e816 100644
--- a/packages/api/src/router/incident.ts
+++ b/packages/api/src/router/incident.ts
@@ -6,6 +6,7 @@ import {
incidentUpdate,
insertIncidentSchema,
insertIncidentUpdateSchema,
+ page,
} from "@openstatus/db/src/schema";
import { createTRPCRouter, protectedProcedure, publicProcedure } from "../trpc";
@@ -37,4 +38,18 @@ export const incidentRouter = createTRPCRouter({
.where(eq(incident.id, opts.input.incidentId))
.execute();
}),
+ getIncidentByWorkspace: protectedProcedure
+ .input(z.object({ workspaceId: z.number() }))
+ .query(async (opts) => {
+ const pageQuery = opts.ctx.db
+ .select()
+ .from(page)
+ .where(eq(page.workspaceId, opts.input.workspaceId))
+ .as("pageQuery");
+ return opts.ctx.db
+ .select()
+ .from(incident)
+ .innerJoin(pageQuery, eq(incident.pageId, pageQuery.id))
+ .execute();
+ }),
});
diff --git a/packages/api/src/router/monitor.ts b/packages/api/src/router/monitor.ts
index 44eb36879..cdeb7a777 100644
--- a/packages/api/src/router/monitor.ts
+++ b/packages/api/src/router/monitor.ts
@@ -57,4 +57,13 @@ export const monitorRouter = createTRPCRouter({
.delete(monitor)
.where(eq(monitor.id, opts.input.monitorId));
}),
+ getMonitorsByWorkspace: protectedProcedure
+ .input(z.object({ workspaceId: z.number() }))
+ .query(async (opts) => {
+ return await opts.ctx.db
+ .select()
+ .from(monitor)
+ .where(eq(monitor.workspaceId, opts.input.workspaceId))
+ .execute();
+ }),
});
diff --git a/packages/api/src/router/page.ts b/packages/api/src/router/page.ts
index 7c8f2e65e..71e71a8d0 100644
--- a/packages/api/src/router/page.ts
+++ b/packages/api/src/router/page.ts
@@ -22,6 +22,14 @@ export const pageRouter = createTRPCRouter({
})
.execute();
}),
+ getPageByWorkspace: protectedProcedure
+ .input(z.object({ workspaceId: z.number() }))
+ .query(async (opts) => {
+ return await opts.ctx.db
+ .select()
+ .from(page)
+ .where(eq(page.workspaceId, opts.input.workspaceId));
+ }),
// public if we use trpc hooks to get the page from the url
getPageBySlug: publicProcedure
.input(z.object({ slug: z.string() }))
diff --git a/packages/api/src/router/workspace.ts b/packages/api/src/router/workspace.ts
index 787500cff..911be6d76 100644
--- a/packages/api/src/router/workspace.ts
+++ b/packages/api/src/router/workspace.ts
@@ -1,19 +1,26 @@
import { z } from "zod";
import { eq } from "@openstatus/db";
-import { page, user, workspace } from "@openstatus/db/src/schema";
+import {
+ page,
+ user,
+ usersToWorkspaces,
+ workspace,
+} from "@openstatus/db/src/schema";
import { createTRPCRouter, protectedProcedure } from "../trpc";
export const workspaceRouter = createTRPCRouter({
- getUserWorkspace: protectedProcedure.query(async (opts) => {
- return await opts.ctx.db.query.workspace.findMany({
+ getUserWithWorkspace: protectedProcedure.query(async (opts) => {
+ return await opts.ctx.db.query.user.findMany({
with: {
usersToWorkspaces: {
- where: eq(user.tenantId, opts.ctx.auth.userId),
- with: { user: true },
+ with: {
+ workspace: true,
+ },
},
},
+ where: eq(user.tenantId, opts.ctx.auth.userId),
});
}),
getWorkspace: protectedProcedure
diff --git a/packages/db/src/schema/monitor.ts b/packages/db/src/schema/monitor.ts
index 0299d4715..66ab4a522 100644
--- a/packages/db/src/schema/monitor.ts
+++ b/packages/db/src/schema/monitor.ts
@@ -10,6 +10,7 @@ import {
import { createInsertSchema, createSelectSchema } from "drizzle-zod";
import { page } from "./page";
+import { workspace } from "./workspace";
export const monitor = mysqlTable("monitor", {
id: int("id").autoincrement().primaryKey(),
@@ -35,7 +36,8 @@ export const monitor = mysqlTable("monitor", {
name: varchar("name", { length: 256 }),
description: text("description"),
- pageId: int("page_id").notNull(),
+ pageId: int("page_id"),
+ workspaceId: int("workspace_id"),
createdAt: timestamp("created_at").notNull().defaultNow(),
updateddAt: timestamp("updated_at").notNull().defaultNow().onUpdateNow(),
@@ -46,6 +48,10 @@ export const monitorRelation = relations(monitor, ({ one }) => ({
fields: [monitor.pageId],
references: [page.id],
}),
+ workspace: one(workspace, {
+ fields: [monitor.workspaceId],
+ references: [workspace.id],
+ }),
}));
// Schema for inserting a Monitor - can be used to validate API requests
diff --git a/packages/db/src/schema/user.ts b/packages/db/src/schema/user.ts
index f68801678..d1ac895d5 100644
--- a/packages/db/src/schema/user.ts
+++ b/packages/db/src/schema/user.ts
@@ -31,3 +31,17 @@ export const usersToWorkspaces = mysqlTable(
pk: primaryKey(t.userId, t.workspaceId),
}),
);
+
+export const usersToWorkspaceRelations = relations(
+ usersToWorkspaces,
+ ({ one }) => ({
+ workspace: one(workspace, {
+ fields: [usersToWorkspaces.workspaceId],
+ references: [workspace.id],
+ }),
+ user: one(user, {
+ fields: [usersToWorkspaces.userId],
+ references: [user.id],
+ }),
+ }),
+);