🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
Refactor database integration and clean up unused code
Bill Yang9c605ef00b3b
diff --git a/client/src/components/ui/button.tsx b/client/src/components/ui/button.tsxindex 8cde26852..8949c84ca 100644--- a/client/src/components/ui/button.tsx+++ b/client/src/components/ui/button.tsx@@ -10,21 +10,21 @@ const buttonVariants = cva(variants: {variant: {default:- "bg-neutral-50 text-neutral-950 shadow hover:bg-neutral-900/90 dark:bg-neutral-850 dark:text-neutral-50 dark:hover:bg-neutral-750/90",+ "bg-neutral-50 text-neutral-950 border border-neutral-200 shadow hover:bg-neutral-900/90 dark:bg-neutral-850 dark:border-neutral-750 dark:text-neutral-50 dark:hover:bg-neutral-800/90 dark:hover:border-neutral-650",secondary:"bg-neutral-100 text-neutral-900 shadow-sm hover:bg-neutral-100/80 dark:bg-neutral-50 dark:text-neutral-800 dark:hover:bg-neutral-50/90",accent:"bg-neutral-500 text-neutral-900 shadow-sm hover:bg-neutral-500/80 dark:bg-accent-600 dark:text-neutral-50 dark:hover:bg-accent-600/90",success:- "bg-green-500 text-neutral-50 shadow-sm hover:bg-green-500/90 dark:bg-green-800 dark:text-neutral-50 dark:hover:bg-green-700/90",+ "bg-green-500 text-neutral-50 border border-green-500 shadow-sm hover:bg-green-500/90 dark:bg-green-800 dark:border-green-600 dark:text-neutral-50 dark:hover:bg-green-800/90 dark:hover:border-green-500",destructive:- "bg-red-500 text-neutral-50 shadow-sm hover:bg-red-500/90 dark:bg-red-900 dark:text-neutral-50 dark:hover:bg-red-900/90",+ "bg-red-500 text-neutral-50 border border-red-500 shadow-sm hover:bg-red-500/90 dark:bg-red-900 dark:border-red-700 dark:text-neutral-50 dark:hover:bg-red-900/90 dark:hover:border-red-500",warning:- "bg-yellow-500 text-neutral-900 shadow-sm hover:bg-yellow-500/90 dark:bg-yellow-600 dark:text-neutral-50 dark:hover:bg-yellow-600/90",+ "bg-yellow-500 text-neutral-900 border border-yellow-500 shadow-sm hover:bg-yellow-500/90 dark:bg-yellow-600 dark:border-yellow-500 dark:text-neutral-50 dark:hover:bg-yellow-600/90 dark:hover:border-yellow-400",outline:- "border border-neutral-200 shadow-sm hover:bg-neutral-100 hover:text-neutral-900 dark:border-neutral-700 dark:hover:bg-neutral-800 dark:hover:text-neutral-50",+ "border border-neutral-200 shadow-sm hover:bg-neutral-100 hover:text-neutral-900 dark:border-neutral-700 dark:hover:bg-neutral-900 dark:hover:border-neutral-600 dark:hover:text-neutral-50",ghost:- "hover:bg-neutral-100 hover:text-neutral-900 dark:hover:bg-neutral-800 dark:hover:text-neutral-50",+ "hover:bg-neutral-100 hover:text-neutral-900 dark:hover:bg-neutral-800 dark:hover:text-neutral-50 border border-transparent",link: "text-neutral-900 underline-offset-4 hover:underline dark:text-neutral-50",},size: {diff --git a/server/src/db/clickhouse/clickhouse.ts b/server/src/db/clickhouse/clickhouse.tsindex f142ea6e5..530366f30 100644--- a/server/src/db/clickhouse/clickhouse.ts+++ b/server/src/db/clickhouse/clickhouse.ts@@ -1,5 +1,4 @@import { createClient } from "@clickhouse/client";-import { Session } from "../postgres/types.js";export const clickhouse = createClient({host: process.env.CLICKHOUSE_HOST,@@ -85,89 +84,74 @@ export const initializeClickhouse = async () => {// `,// });- await clickhouse.exec({- query: `- CREATE TABLE IF NOT EXISTS sessions- (- site_id UInt16,- session_id String,- session_start DateTime,- session_end DateTime,- user_id String,- pageviews UInt32,- entry_page String,- exit_page String,-- hostname String,- referrer String,- browser LowCardinality(String),- browser_version LowCardinality(String),- operating_system LowCardinality(String),- operating_system_version LowCardinality(String),- language LowCardinality(String),- country LowCardinality(FixedString(2)),- iso_3166_2 LowCardinality(String),- screen_width UInt16,- screen_height UInt16,- device_type LowCardinality(String),- -- Version column for ReplacingMergeTree- version UInt64- )- ENGINE = ReplacingMergeTree(version)- PARTITION BY toYYYYMM(session_start)- ORDER BY (site_id, session_id);- `,- });- await clickhouse.exec({- query: `- CREATE MATERIALIZED VIEW IF NOT EXISTS sessions_mv- TO sessions- AS- SELECT- site_id,- session_id,- min(timestamp) AS session_start,- max(timestamp) AS session_end,- any(user_id) AS user_id,- countIf(type = 'pageview') AS pageviews,- argMinIf(pathname, timestamp, type = 'pageview') AS entry_page,- argMaxIf(pathname, timestamp, type = 'pageview') AS exit_page,+ // await clickhouse.exec({+ // query: `+ // CREATE TABLE IF NOT EXISTS sessions+ // (+ // site_id UInt16,+ // session_id String,+ // session_start DateTime,+ // session_end DateTime,+ // user_id String,+ // pageviews UInt32,+ // entry_page String,+ // exit_page String,- any(hostname) AS hostname,- any(referrer) AS referrer,- any(browser) AS browser,- any(browser_version) AS browser_version,- any(operating_system) AS operating_system,- any(operating_system_version) AS operating_system_version,- any(language) AS language,- any(country) AS country,- any(iso_3166_2) AS iso_3166_2,- any(screen_width) AS screen_width,- any(screen_height) AS screen_height,- any(device_type) AS device_type,- -- Use the largest timestamp as the 'version'- max(toUInt64(timestamp)) AS version- FROM pageviews- GROUP BY- site_id,- session_id;- `,- });-};+ // hostname String,+ // referrer String,+ // browser LowCardinality(String),+ // browser_version LowCardinality(String),+ // operating_system LowCardinality(String),+ // operating_system_version LowCardinality(String),+ // language LowCardinality(String),+ // country LowCardinality(FixedString(2)),+ // iso_3166_2 LowCardinality(String),+ // screen_width UInt16,+ // screen_height UInt16,+ // device_type LowCardinality(String),+ // -- Version column for ReplacingMergeTree+ // version UInt64+ // )+ // ENGINE = ReplacingMergeTree(version)+ // PARTITION BY toYYYYMM(session_start)+ // ORDER BY (site_id, session_id);+ // `,+ // });+ // await clickhouse.exec({+ // query: `+ // CREATE MATERIALIZED VIEW IF NOT EXISTS sessions_mv+ // TO sessions+ // AS+ // SELECT+ // site_id,+ // session_id,+ // min(timestamp) AS session_start,+ // max(timestamp) AS session_end,+ // any(user_id) AS user_id,+ // countIf(type = 'pageview') AS pageviews,+ // argMinIf(pathname, timestamp, type = 'pageview') AS entry_page,+ // argMaxIf(pathname, timestamp, type = 'pageview') AS exit_page,-// Function to insert session data-export const insertSessions = async (sessions: Session[]) => {- try {- await clickhouse.insert({- table: "sessions",- values: sessions,- format: "JSONEachRow",- });- return true;- } catch (error) {- console.error("Error inserting session:", error);- return false;- }+ // any(hostname) AS hostname,+ // any(referrer) AS referrer,+ // any(browser) AS browser,+ // any(browser_version) AS browser_version,+ // any(operating_system) AS operating_system,+ // any(operating_system_version) AS operating_system_version,+ // any(language) AS language,+ // any(country) AS country,+ // any(iso_3166_2) AS iso_3166_2,+ // any(screen_width) AS screen_width,+ // any(screen_height) AS screen_height,+ // any(device_type) AS device_type,+ // -- Use the largest timestamp as the 'version'+ // max(toUInt64(timestamp)) AS version+ // FROM pageviews+ // GROUP BY+ // site_id,+ // session_id;+ // `,+ // });};export default clickhouse;diff --git a/server/src/db/clickhouse/types.ts b/server/src/db/clickhouse/types.tsdeleted file mode 100644index 43b677c9e..000000000--- a/server/src/db/clickhouse/types.ts+++ /dev/null@@ -1,18 +0,0 @@-import { DateTime } from "luxon";--export type Pageview = {- timestamp: DateTime;- session_id: string;- user_id: string;- hostname: string;- pathname: string;- querystring: string;- page_title: string;- referrer: string;- browser: string;- operating_system: string;- language: string;- screen_width: number;- screen_height: number;- device_type: string;-};diff --git a/server/src/db/postgres/postgres.ts b/server/src/db/postgres/postgres.tsindex 7f6916133..1fd283c39 100644--- a/server/src/db/postgres/postgres.ts+++ b/server/src/db/postgres/postgres.ts@@ -20,13 +20,3 @@ export const db = drizzle(client, { schema });// For compatibility with raw SQL if neededexport const sql = client;--export async function initializePostgres() {- try {- console.log("Initializing PostgreSQL database...");- console.log("PostgreSQL initialization completed successfully.");- } catch (error) {- console.error("Error initializing PostgreSQL:", error);- throw error;- }-}diff --git a/server/src/db/postgres/session-cleanup.ts b/server/src/db/postgres/session-cleanup.tsindex 3b92cfeee..8ffa9abaf 100644--- a/server/src/db/postgres/session-cleanup.ts+++ b/server/src/db/postgres/session-cleanup.ts@@ -1,5 +1,4 @@import { sql } from "./postgres.js";-import { Session } from "./types.js";// function convertPostgresToClickhouse(postgresTimestamp: string): string {// console.log("Parsing timestamp:", postgresTimestamp);@@ -35,7 +34,7 @@ function convertPostgresToClickhouse(postgresTimestamp: string): string {}export async function cleanupOldSessions() {- const deletedSessions = await sql<Session[]>`+ const deletedSessions = await sql`DELETE FROM active_sessionsWHERE last_activity < NOW() - INTERVAL '30 minute'RETURNING *diff --git a/server/src/db/postgres/types.ts b/server/src/db/postgres/types.tsdeleted file mode 100644index 0d6dcaffb..000000000--- a/server/src/db/postgres/types.ts+++ /dev/null@@ -1,17 +0,0 @@-export type Session = {- session_id: string;- user_id: string;- hostname: string;- start_time: string;- end_time: string;- pageviews: number;- entry_page: string;- exit_page: string;- device_type: string;- screen_width: number;- screen_height: number;- browser: string;- operating_system: string;- language: string;- referrer: string;-};diff --git a/server/src/index.ts b/server/src/index.tsindex 98a98d115..ca14b95a3 100644--- a/server/src/index.ts+++ b/server/src/index.ts@@ -35,7 +35,6 @@ import { getUserOrganizations } from "./api/user/getUserOrganizations.js";import { listOrganizationMembers } from "./api/user/listOrganizationMembers.js";import { initializeCronJobs } from "./cron/index.js";import { initializeClickhouse } from "./db/clickhouse/clickhouse.js";-import { initializePostgres } from "./db/postgres/postgres.js";import { allowList, loadAllowedDomains } from "./lib/allowedDomains.js";import { mapHeaders } from "./lib/auth-utils.js";import { auth } from "./lib/auth.js";@@ -234,7 +233,7 @@ const start = async () => {try {console.info("Starting server...");// Initialize the database- await Promise.all([initializeClickhouse(), initializePostgres()]);+ await Promise.all([initializeClickhouse()]);await loadAllowedDomains();// Load public sites cachediff --git a/server/src/lib/auth.ts b/server/src/lib/auth.tsindex c13ceb553..67fb67eb5 100644--- a/server/src/lib/auth.ts+++ b/server/src/lib/auth.ts@@ -1,18 +1,15 @@import { betterAuth } from "better-auth";-import { username, admin, organization } from "better-auth/plugins";+import { drizzleAdapter } from "better-auth/adapters/drizzle";+import { admin, organization, username } from "better-auth/plugins";import dotenv from "dotenv";+import { eq } from "drizzle-orm";import pg from "pg";-import { drizzleAdapter } from "better-auth/adapters/drizzle";import { db } from "../db/postgres/postgres.js";-import { IS_CLOUD, STRIPE_PRICES } from "./const.js";import * as schema from "../db/postgres/schema.js";-import { eq } from "drizzle-orm";-import Stripe from "stripe";+import { IS_CLOUD } from "./const.js";dotenv.config();-const stripeClient = new Stripe(process.env.STRIPE_SECRET_KEY!);-type AuthType = ReturnType<typeof betterAuth> | null;const pluginList = IS_CLOUD