Skip to content

Commit

Permalink
add status api
Browse files Browse the repository at this point in the history
  • Loading branch information
SkidGod4444 committed Nov 20, 2024
1 parent 57a640a commit a3cb0e0
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 207 deletions.
1 change: 1 addition & 0 deletions apps/api/app/api/[[...route]]/hello.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Hono } from "hono";
const app = new Hono();
import { prisma } from "@repo/db";

app
.get("/", async (c) => {
const user = await prisma.user.findMany();
Expand Down
2 changes: 2 additions & 0 deletions apps/api/app/api/[[...route]]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import mail from "./mail";
import hello from "./hello";
import session from "./session";
import auth from "./auth";
import status from "./status";

const allowedOrigins = [
"http://localhost:3003",
Expand Down Expand Up @@ -58,6 +59,7 @@ app.route("/session", session);
app.route("/hello", hello);
app.route("/mail", mail);
app.route("/auth", auth);
app.route("/status", status);

const GET = handle(app);
const POST = handle(app);
Expand Down
2 changes: 2 additions & 0 deletions apps/api/app/api/[[...route]]/session.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Hono } from "hono";
import { auth } from "@repo/auth";

const app = new Hono();

app.get("/", async (c) => {
const session = await auth.api.getSession({ headers: c.req.raw.headers });

Expand Down
29 changes: 29 additions & 0 deletions apps/api/app/api/[[...route]]/status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Hono } from "hono";
import { cache } from "@repo/cache";

const app = new Hono();

app.get("/", async (c) => {
const dbStatus = await cache.lrange("db-latency:history", 0, -1);
const siteStatus = await cache.lrange("site-latency:history", 0, -1);
return c.json({
dbStatus,
siteStatus,
});
});

app.get("/db", async (c) => {
const dbStatus = await cache.lrange("db-latency:history", 0, -1);
return c.json({
dbStatus,
});
});

app.get("/site", async (c) => {
const siteStatus = await cache.lrange("site-latency:history", 0, -1);
return c.json({
siteStatus,
});
});

export default app;
1 change: 1 addition & 0 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@repo/db": "workspace:*",
"@repo/mail": "workspace:*",
"@repo/types": "workspace:*",
"@repo/cache": "workspace:*",
"hono": "^4.6.9",
"next": "15.0.2",
"prisma": "^5.22.0",
Expand Down
4 changes: 2 additions & 2 deletions apps/triggers/src/db.status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const db = createClient(supabaseUrl, supabaseKey);

export const dbStatusTask = schedules.task({
id: "db-status",
cron: "*/10 * * * *",
cron: "*/12 * * * *",
maxDuration: 600,
run: async (payload, { ctx }) => {
const latencies: Record<string, number | null> = {};
Expand Down Expand Up @@ -93,6 +93,6 @@ export const dbStatusTask = schedules.task({
};

await cache.rpush("db-latency:history", JSON.stringify(latencyRecord));
await cache.ltrim("db-latency:history", -100, -1);
await cache.ltrim("db-latency:history", -120, -1);
},
});
24 changes: 9 additions & 15 deletions apps/triggers/src/site.status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const LATENCY_THRESHOLD = 1000;

export const siteStatusTask = schedules.task({
id: "site-status",
cron: "*/15 * * * *", // Every 15 minutes
cron: "*/12 * * * *",
maxDuration: 600,
run: async () => {
const urls = {
Expand Down Expand Up @@ -56,19 +56,7 @@ export const siteStatusTask = schedules.task({

// Push the status record to a list in Redis
await cache.rpush("site-latency:history", JSON.stringify(statusRecord));
await cache.ltrim("site-latency:history", -100, -1); // Keep last 100 entries

const cachedStatusHistory = await cache.lrange("site-latency:history", 0, -1);
if (cachedStatusHistory) {
cachedStatusHistory.forEach((record) => {
try {
const parsedRecord = JSON.parse(record);
logger.log("Cached Status Record", parsedRecord);
} catch (error) {
logger.error("Failed to parse cached status record", { error });
}
});
}
await cache.ltrim("site-latency:history", -120, -1);
},
});

Expand All @@ -79,7 +67,12 @@ async function sendDiscordNotification(
latency: number | null = null,
error: string | null = null
) {
const ROLE_ID = "1308042212319428668";
const downMsg = `🚨 Service **${serviceName}** is experiencing issues!`;
const upMsg = `📢 Service **${serviceName}** is having high latency!`;
const NotifyMsg = status === "UP" ? upMsg : downMsg;
const message = {
content: `<@&${ROLE_ID}> ${NotifyMsg}`,
embeds: [
{
title: `Status Alert for ${serviceName}`,
Expand All @@ -95,7 +88,7 @@ async function sendDiscordNotification(
name: "Error",
value: error || "None",
inline: true,
}
},
],
timestamp: new Date().toISOString(),
},
Expand All @@ -115,3 +108,4 @@ async function sendDiscordNotification(
logger.error("Failed to send notification to Discord", { notificationError });
}
}

2 changes: 1 addition & 1 deletion packages/database/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
".": "./src/index.ts"
},
"scripts": {
"db:generate": "prisma generate",
"db:generate": "prisma generate --no-engine",
"db:push": "prisma db push --skip-generate"
},
"dependencies": {
Expand Down
Loading

0 comments on commit a3cb0e0

Please sign in to comment.