Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/openstatusHQ/openstatus. ๐ซ Status page with uptime monitoring & API monitoring as code ๐ซ openstatus.dev
bun drizzle-orm monitoring monitoring-as-code nextjs observability on-call open-source shadcn-ui status-page statuspage synthetic-monitoring tinybird turso uptime uptime-checker uptime-monitor
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129import readline from "node:readline";
import { db, eq } from "@openstatus/db";import { type WorkspacePlan, workspace } from "@openstatus/db/src/schema";
import { env } from "../env";
// Function to prompt user for confirmationconst askConfirmation = async (question: string): Promise<boolean> => { const rl = readline.createInterface({ input: process.stdin, output: process.stdout, });
return new Promise((resolve) => { rl.question(`${question} (y/n): `, (answer) => { rl.close(); resolve(answer.trim().toLowerCase() === "y"); }); });};
/** * Calculates the unix timestamp in milliseconds for a given number of days in the past. * @param days The number of days to subtract from the current date. * @returns The calculated unix timestamp in milliseconds. */function calculatePastTimestamp(days: number) { const date = new Date(); date.setDate(date.getDate() - days); const timestamp = date.getTime(); console.log(`${days}d back: ${timestamp}`); return timestamp;}
/** * Get the array of workspace IDs for a given plan. * @param plan The plan to filter by. * @returns The array of workspace IDs. */async function getWorkspaceIdsByPlan(plan: WorkspacePlan) { const workspaces = await db .select() .from(workspace) .where(eq(workspace.plan, plan)) .all(); const workspaceIds = workspaces.map((w) => w.id); console.log(`${plan}: ${workspaceIds}`); return workspaceIds;}
/** * * @param timestamp timestamp to delete logs before (in milliseconds) * @param workspaceIds array of workspace IDs to delete logs for * @param reverse allows to NOT delete the logs for the given workspace IDs * @returns */async function deleteLogs( timestamp: number, workspaceIds: number[], reverse = false,) { const response = await fetch( "https://api.tinybird.co/v0/datasources/ping_response__v8/delete", { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded", Authorization: `Bearer ${env().TINY_BIRD_API_KEY}`, }, body: new URLSearchParams({ delete_condition: `timestamp <= ${timestamp} AND ${reverse ? "NOT" : ""} arrayExists(x -> x IN (${workspaceIds.join(", ")}), [workspaceId])`, }), }, ); const json = await response.json(); console.log(json);
return json;}
async function main() { // check if the script is running in production console.log(`DATABASE_URL: ${env().DATABASE_URL}`);
const isConfirmed = await askConfirmation( "Are you sure you want to run this script?", );
if (!isConfirmed) { console.log("Script execution cancelled."); return; }
const lastTwoWeeks = calculatePastTimestamp(14); const lastThreeMonths = calculatePastTimestamp(90); const lastYear = calculatePastTimestamp(365); const lastTwoYears = calculatePastTimestamp(730);
const starters = await getWorkspaceIdsByPlan("starter"); const teams = await getWorkspaceIdsByPlan("team"); const scales = await getWorkspaceIdsByPlan("scale");
// all other workspaces, we need to 'reverse' the deletion here to NOT include those workspaces const rest = [...starters, ...teams, ...scales];
if (rest.length > 0) { await deleteLogs(lastTwoWeeks, rest, true); } if (starters.length > 0) { await deleteLogs(lastThreeMonths, starters); } if (teams.length > 0) { await deleteLogs(lastYear, teams); } if (scales.length > 0) { await deleteLogs(lastTwoYears, scales); }}
/** * REMINDER: do it manually (to avoid accidental deletion on dev mode) * Within the app/workflows folder, run the following command: * $ bun src/scripts/tinybird.ts */
// main().catch(console.error);