Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 27 additions & 11 deletions admin/src/db/clickhouse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ export interface ClickHouseStats {
tables: TableStats[];
}

const EVENT_TABLE = "events";
const PROJECT_ACTIVITY_EVENT_TYPES = [
"pageview",
"custom_event",
"error",
"captcha",
"performance",
];
const PROJECT_ACTIVITY_TYPE_LIST = PROJECT_ACTIVITY_EVENT_TYPES.map(
(type) => `'${type}'`
).join(", ");
Comment thread
Blaumaus marked this conversation as resolved.

function formatBytes(bytes: number): string {
if (bytes === 0) return "0 B";
const k = 1024;
Expand All @@ -47,20 +59,16 @@ export async function getClickHouseStats(): Promise<ClickHouseStats> {
const database = process.env.CLICKHOUSE_DATABASE || "analytics";

const tables = [
"analytics",
"customEV",
"performance",
"errors",
"captcha",
EVENT_TABLE,
"sessions",
"revenue",
"feature_flag_evaluations",
"experiment_exposures",
"error_statuses",
"bot_blocks",
];

const tableStats: TableStats[] = [];
let totalEvents = 0;

for (const table of tables) {
try {
Expand Down Expand Up @@ -93,8 +101,6 @@ export async function getClickHouseStats(): Promise<ClickHouseStats> {
bytes,
bytesFormatted: formatBytes(bytes),
});

totalEvents += rows;
} catch {
// Table might not exist, skip it
tableStats.push({
Expand All @@ -106,6 +112,9 @@ export async function getClickHouseStats(): Promise<ClickHouseStats> {
}
}

const totalEvents =
tableStats.find((table) => table.table === EVENT_TABLE)?.rows || 0;

return {
totalEvents,
tables: tableStats,
Expand All @@ -118,7 +127,12 @@ export async function getProjectEventCount(pid: string): Promise<number> {

try {
const result = await client.query({
query: `SELECT count() as count FROM ${database}.analytics WHERE pid = {pid:String}`,
query: `
SELECT count() as count
FROM ${database}.${EVENT_TABLE}
WHERE pid = {pid:FixedString(12)}
AND type IN (${PROJECT_ACTIVITY_TYPE_LIST})
`,
query_params: { pid },
format: "JSONEachRow",
});
Expand Down Expand Up @@ -158,8 +172,9 @@ export async function getTopProjectsByEvents(
SELECT
pid,
count() as eventCount
FROM ${database}.analytics
FROM ${database}.${EVENT_TABLE}
WHERE created >= now() - INTERVAL ${days} DAY
AND type IN (${PROJECT_ACTIVITY_TYPE_LIST})
GROUP BY pid
ORDER BY eventCount DESC
LIMIT ${limit}
Expand All @@ -183,8 +198,9 @@ export async function getProjectsWithRecentEvents(
const result = await client.query({
query: `
SELECT DISTINCT pid
FROM ${database}.analytics
FROM ${database}.${EVENT_TABLE}
WHERE created >= now() - INTERVAL ${days} DAY
AND type IN (${PROJECT_ACTIVITY_TYPE_LIST})
`,
format: "JSONEachRow",
});
Expand Down
Loading
Loading