Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

wip

Bill Yangc6293ac062b0
diff --git a/server/src/db/postgres/session-cleanup.ts b/server/src/db/postgres/session-cleanup.ts
index c2e346c59..f439cb25b 100644
--- a/server/src/db/postgres/session-cleanup.ts
+++ b/server/src/db/postgres/session-cleanup.ts
@@ -3,22 +3,55 @@ import { sql } from "./postgres";
import { Session } from "./types";
import { DateTime } from "luxon";
+// function convertPostgresToClickhouse(postgresTimestamp: string): string {
+// console.log("Parsing timestamp:", postgresTimestamp);
+// const dateTime = DateTime.fromISO(postgresTimestamp, {
+// zone: "utc",
+// });
+// if (!dateTime.isValid) {
+// console.error("Invalid timestamp:", postgresTimestamp);
+// console.error("Reason:", dateTime.invalidReason);
+// throw new Error("Invalid timestamp format");
+// }
+// return dateTime.toFormat("yyyy-MM-dd HH:mm:ss");
+// }
+
+// luxon is not working, so we need to use this
function convertPostgresToClickhouse(postgresTimestamp: string): string {
- return DateTime.fromISO(postgresTimestamp).toFormat("yyyy-MM-dd HH:mm:ss");
+ try {
+ const date = new Date(postgresTimestamp);
+ if (isNaN(date.getTime())) {
+ throw new Error("Invalid Date");
+ }
+ const year = date.getUTCFullYear();
+ const month = String(date.getUTCMonth() + 1).padStart(2, "0");
+ const day = String(date.getUTCDate()).padStart(2, "0");
+ const hours = String(date.getUTCHours()).padStart(2, "0");
+ const minutes = String(date.getUTCMinutes()).padStart(2, "0");
+ const seconds = String(date.getUTCSeconds()).padStart(2, "0");
+ return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
+ } catch (error) {
+ console.error("Error parsing timestamp:", postgresTimestamp, error);
+ throw new Error("Invalid timestamp format");
+ }
}
export async function cleanupOldSessions() {
const deletedSessions = await sql<Session[]>`
DELETE FROM active_sessions
- WHERE last_activity < NOW() - INTERVAL '1 minute'
+ WHERE last_activity < NOW() - INTERVAL '10 minute'
RETURNING *
`;
+
console.log(`Cleaned up ${deletedSessions.length} sessions`);
- await insertSessions(
- deletedSessions.map((e) => ({
- ...e,
- start_time: convertPostgresToClickhouse(e.start_time),
- last_activity: convertPostgresToClickhouse(e.last_activity),
- }))
- );
+
+ if (deletedSessions.length > 0) {
+ await insertSessions(
+ deletedSessions.map((e) => ({
+ ...e,
+ start_time: convertPostgresToClickhouse(e.start_time),
+ last_activity: convertPostgresToClickhouse(e.last_activity),
+ }))
+ );
+ }
}
diff --git a/server/src/index.ts b/server/src/index.ts
index 6d0f687b3..3cf4d04b5 100644
--- a/server/src/index.ts
+++ b/server/src/index.ts
@@ -62,6 +62,10 @@ const start = async () => {
await Promise.allSettled([initializeClickhouse(), initializePostgres()]);
// Start the server
await server.listen({ port: 3001, host: "0.0.0.0" });
+ cron.schedule("* * * * * *", () => {
+ console.log("Cleaning up old sessions");
+ cleanupOldSessions();
+ });
} catch (err) {
server.log.error(err);
process.exit(1);
@@ -69,8 +73,3 @@ const start = async () => {
};
start();
-
-cron.schedule("* * * * *", () => {
- console.log("Cleaning up old sessions");
- cleanupOldSessions();
-});