Refactor database integration and clean up unused code

- Removed PostgreSQL initialization and related session types to streamline database management.
- Updated Clickhouse integration by commenting out unused table and view creation queries for future reference.
- Enhanced button styles in the UI components for improved consistency and user experience.
This commit is contained in:
Bill Yang 2025-04-16 13:03:11 -07:00
parent 6638ea32e4
commit 9c605ef00b
8 changed files with 80 additions and 146 deletions

View file

@ -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: {

View file

@ -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,
// 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,
// 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,
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;
`,
});
};
// 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;

View file

@ -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;
};

View file

@ -20,13 +20,3 @@ export const db = drizzle(client, { schema });
// For compatibility with raw SQL if needed
export 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;
}
}

View file

@ -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_sessions
WHERE last_activity < NOW() - INTERVAL '30 minute'
RETURNING *

View file

@ -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;
};

View file

@ -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 cache

View file

@ -1,18 +1,15 @@
import { betterAuth } from "better-auth";
import { username, admin, organization } from "better-auth/plugins";
import dotenv from "dotenv";
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 { admin, organization, username } from "better-auth/plugins";
import dotenv from "dotenv";
import { eq } from "drizzle-orm";
import Stripe from "stripe";
import pg from "pg";
import { db } from "../db/postgres/postgres.js";
import * as schema from "../db/postgres/schema.js";
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