prep events partition

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-07-19 22:08:22 +02:00
parent ddc2ce338f
commit 3993b493e3
27 changed files with 136 additions and 71 deletions

View File

@@ -1,8 +1,8 @@
import { ch, chQuery } from '@openpanel/db';
import { ch, chQuery, TABLE_NAMES } from '@openpanel/db';
async function main() {
const projects = await chQuery(
`SELECT distinct project_id FROM events ORDER BY project_id`
`SELECT distinct project_id FROM ${TABLE_NAMES.events} ORDER BY project_id`
);
const withOrigin = [];
@@ -10,10 +10,10 @@ async function main() {
try {
const [eventWithOrigin, eventWithoutOrigin] = await Promise.all([
await chQuery(
`SELECT * FROM events WHERE origin != '' AND project_id = '${project.project_id}' ORDER BY created_at DESC LIMIT 1`
`SELECT * FROM ${TABLE_NAMES.events} WHERE origin != '' AND project_id = '${project.project_id}' ORDER BY created_at DESC LIMIT 1`
),
await chQuery(
`SELECT * FROM events WHERE origin = '' AND project_id = '${project.project_id}' AND path != '' ORDER BY created_at DESC LIMIT 1`
`SELECT * FROM ${TABLE_NAMES.events} WHERE origin = '' AND project_id = '${project.project_id}' AND path != '' ORDER BY created_at DESC LIMIT 1`
),
]);
@@ -22,7 +22,7 @@ async function main() {
console.log(`- Origin: ${eventWithOrigin[0].origin}`);
withOrigin.push(project.project_id);
const events = await chQuery(
`SELECT count(*) as count FROM events WHERE project_id = '${project.project_id}' AND path != '' AND origin = ''`
`SELECT count(*) as count FROM ${TABLE_NAMES.events} WHERE project_id = '${project.project_id}' AND path != '' AND origin = ''`
);
console.log(`🤠🤠🤠🤠 Will update ${events[0]?.count} events`);
await ch.command({

View File

@@ -10,6 +10,7 @@ import {
getEvents,
getLiveVisitors,
getProfileById,
TABLE_NAMES,
transformMinimalEvent,
} from '@openpanel/db';
import { redis, redisPub, redisSub } from '@openpanel/redis';
@@ -28,8 +29,7 @@ export async function testVisitors(
reply: FastifyReply
) {
const events = await getEvents(
`SELECT * FROM events LIMIT 500`
// `SELECT * FROM events WHERE name = 'screen_view' LIMIT 500`
`SELECT * FROM ${TABLE_NAMES.events} LIMIT 500`
);
const event = events[Math.floor(Math.random() * events.length)];
if (!event) {
@@ -55,8 +55,7 @@ export async function testEvents(
reply: FastifyReply
) {
const events = await getEvents(
`SELECT * FROM events LIMIT 500`
// `SELECT * FROM events WHERE name = 'screen_view' LIMIT 500`
`SELECT * FROM ${TABLE_NAMES.events} LIMIT 500`
);
const event = events[Math.floor(Math.random() * events.length)];
if (!event) {

View File

@@ -7,7 +7,7 @@ import Fastify from 'fastify';
import metricsPlugin from 'fastify-metrics';
import { round } from '@openpanel/common';
import { chQuery, db } from '@openpanel/db';
import { chQuery, db, TABLE_NAMES } from '@openpanel/db';
import type { IServiceClient } from '@openpanel/db';
import { eventsQueue } from '@openpanel/queue';
import { redis, redisPub } from '@openpanel/redis';
@@ -101,7 +101,9 @@ const startServer = async () => {
const redisRes = await withTimings(redis.keys('*'));
const dbRes = await withTimings(db.project.findFirst());
const queueRes = await withTimings(eventsQueue.getCompleted());
const chRes = await withTimings(chQuery('SELECT * FROM events LIMIT 1'));
const chRes = await withTimings(
chQuery(`SELECT * FROM ${TABLE_NAMES.events} LIMIT 1`)
);
const status = redisRes && dbRes && queueRes && chRes ? 200 : 500;
reply.status(status).send({