Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

fix: cohort refresh

Carl-Gerhard Lindesvärd31c2f6478d94
diff --git a/apps/start/src/components/ui/input-enter.tsx b/apps/start/src/components/ui/input-enter.tsx
index 16054e378..b01f6a595 100644
--- a/apps/start/src/components/ui/input-enter.tsx
+++ b/apps/start/src/components/ui/input-enter.tsx
@@ -37,6 +37,7 @@ export function InputEnter({
}}
onKeyDown={(e) => {
if (e.key === 'Enter') {
+ e.preventDefault();
onChangeValue(internalValue);
}
}}
@@ -46,6 +47,7 @@ export function InputEnter({
{!immediate && internalValue !== value && (
<motion.button
key="refresh"
+ type="button"
initial={{ opacity: 0, scale: 0.8 }}
animate={{ opacity: 1, scale: 1 }}
exit={{ opacity: 0, scale: 0.8 }}
diff --git a/packages/db/src/services/cohort.service.ts b/packages/db/src/services/cohort.service.ts
index 3a73d4416..91025db08 100644
--- a/packages/db/src/services/cohort.service.ts
+++ b/packages/db/src/services/cohort.service.ts
@@ -10,7 +10,12 @@ import type {
} from '@openpanel/validation';
import { cohortComputeQueue } from '@openpanel/queue';
-import { TABLE_NAMES, ch, chQuery } from '../clickhouse/client';
+import {
+ TABLE_NAMES,
+ ch,
+ chQuery,
+ getReplicatedTableName,
+} from '../clickhouse/client';
import { db } from '../prisma-client';
import { getProfiles, type IServiceProfile } from './profile.service';
@@ -538,6 +543,16 @@ export async function updateCohortMembership(
const version = Date.now();
+ // ReplacingMergeTree only dedupes within the same ORDER BY key
+ // (project_id, cohort_id, profile_id), so profiles that fell out of the
+ // cohort definition would otherwise linger forever. Clear them first.
+ await ch.command({
+ query: `DELETE FROM ${getReplicatedTableName(TABLE_NAMES.cohort_members)} WHERE cohort_id = ${sqlstring.escape(cohort.id)} AND project_id = ${sqlstring.escape(cohort.projectId)}`,
+ clickhouse_settings: {
+ lightweight_deletes_sync: '1',
+ },
+ });
+
await storeCohortMembership(
cohort.projectId,
cohort.id,
@@ -558,12 +573,15 @@ export async function deleteCohortMembership(
cohortId: string,
projectId: string,
): Promise<void> {
- await ch.command({
- query: `ALTER TABLE ${TABLE_NAMES.cohort_members} DELETE WHERE cohort_id = ${sqlstring.escape(cohortId)} AND project_id = ${sqlstring.escape(projectId)}`,
- });
- await ch.command({
- query: `ALTER TABLE ${TABLE_NAMES.cohort_metadata} DELETE WHERE cohort_id = ${sqlstring.escape(cohortId)} AND project_id = ${sqlstring.escape(projectId)}`,
- });
+ const where = `cohort_id = ${sqlstring.escape(cohortId)} AND project_id = ${sqlstring.escape(projectId)}`;
+ for (const table of [TABLE_NAMES.cohort_members, TABLE_NAMES.cohort_metadata]) {
+ await ch.command({
+ query: `DELETE FROM ${getReplicatedTableName(table)} WHERE ${where}`,
+ clickhouse_settings: {
+ lightweight_deletes_sync: '0',
+ },
+ });
+ }
}
export async function getProfilesInCohort(
diff --git a/packages/trpc/src/routers/cohort.ts b/packages/trpc/src/routers/cohort.ts
index 909173aa0..98210d1a0 100644
--- a/packages/trpc/src/routers/cohort.ts
+++ b/packages/trpc/src/routers/cohort.ts
@@ -1,5 +1,3 @@
-import { z } from 'zod';
-
import {
computeCohort,
countCohort,
@@ -20,7 +18,7 @@ import {
zCohortInput,
zCohortUpdate,
} from '@openpanel/validation';
-
+import { z } from 'zod';
import { getProjectAccess } from '../access';
import { TRPCAccessError, TRPCNotFoundError } from '../errors';
import { createTRPCRouter, protectedProcedure } from '../trpc';
@@ -31,7 +29,7 @@ export const cohortRouter = createTRPCRouter({
z.object({
projectId: z.string(),
includeCount: z.boolean().optional().default(false),
- }),
+ })
)
.query(async ({ input }) => {
const cohorts = await db.cohort.findMany({
@@ -72,23 +70,21 @@ export const cohortRouter = createTRPCRouter({
return cohort;
}),
- create: protectedProcedure
- .input(zCohortInput)
- .mutation(async ({ input }) => {
- const cohort = await db.cohort.create({
- data: {
- name: input.name,
- description: input.description,
- projectId: input.projectId,
- definition: input.definition,
- isStatic: input.isStatic,
- },
- });
+ create: protectedProcedure.input(zCohortInput).mutation(async ({ input }) => {
+ const cohort = await db.cohort.create({
+ data: {
+ name: input.name,
+ description: input.description,
+ projectId: input.projectId,
+ definition: input.definition,
+ isStatic: input.isStatic,
+ },
+ });
- await enqueueCohortCompute(cohort.id);
+ await enqueueCohortCompute(cohort.id);
- return cohort;
- }),
+ return cohort;
+ }),
update: protectedProcedure
.input(zCohortUpdate)
@@ -122,7 +118,11 @@ export const cohortRouter = createTRPCRouter({
},
});
+ console.log('cohort', cohort);
+
if (data.definition) {
+ console.log('enqueueing cohort compute');
+ await removeCohortComputeJob(cohort.id);
await enqueueCohortCompute(cohort.id);
}
@@ -164,7 +164,7 @@ export const cohortRouter = createTRPCRouter({
cursor: z.number().optional(),
take: z.number().default(50),
search: z.string().optional(),
- }),
+ })
)
.query(async ({ input }) => {
const { data, count } = await listCohortMemberProfiles(input);
@@ -175,27 +175,21 @@ export const cohortRouter = createTRPCRouter({
}),
mostEvents: protectedProcedure
- .input(
- z.object({ projectId: z.string(), cohortId: z.string() }),
- )
+ .input(z.object({ projectId: z.string(), cohortId: z.string() }))
.query(({ input }) =>
- getCohortMemberEvents(input.projectId, input.cohortId),
+ getCohortMemberEvents(input.projectId, input.cohortId)
),
eventsPerDay: protectedProcedure
- .input(
- z.object({ projectId: z.string(), cohortId: z.string() }),
- )
+ .input(z.object({ projectId: z.string(), cohortId: z.string() }))
.query(({ input }) =>
- getCohortEventsPerDay(input.projectId, input.cohortId),
+ getCohortEventsPerDay(input.projectId, input.cohortId)
),
popularRoutes: protectedProcedure
- .input(
- z.object({ projectId: z.string(), cohortId: z.string() }),
- )
+ .input(z.object({ projectId: z.string(), cohortId: z.string() }))
.query(({ input }) =>
- getCohortMemberRoutes(input.projectId, input.cohortId),
+ getCohortMemberRoutes(input.projectId, input.cohortId)
),
getCount: protectedProcedure
@@ -227,7 +221,7 @@ export const cohortRouter = createTRPCRouter({
z.object({
projectId: z.string(),
definition: zCohortDefinition,
- }),
+ })
)
.query(async ({ input }) => {
const definition = input.definition as CohortDefinition;
@@ -235,7 +229,7 @@ export const cohortRouter = createTRPCRouter({
const sampleProfiles = await computeCohort(
input.projectId,
definition,
- 10,
+ 10
);
return { count, sampleProfiles };
}),
@@ -244,9 +238,9 @@ export const cohortRouter = createTRPCRouter({
.input(
z.object({
cohortId: z.string(),
- limit: z.number().int().min(1).max(10000).default(10000),
+ limit: z.number().int().min(1).max(10_000).default(10_000),
offset: z.number().int().min(0).default(0),
- }),
+ })
)
.query(async ({ input, ctx }) => {
const cohort = await db.cohort.findUnique({
@@ -300,7 +294,7 @@ export const cohortRouter = createTRPCRouter({
if (cohort.isStatic) {
throw new Error(
- 'Cannot refresh static cohorts — they are one-time snapshots',
+ 'Cannot refresh static cohorts — they are one-time snapshots'
);
}