Skip to content

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

fix(self-hosting): generate insights and weekly digests (#598)

I'm Groot 🌳6521604dff22
diff --git a/packages/core/src/modules/insight/insight.service.test.ts b/packages/core/src/modules/insight/insight.service.test.ts
index 8cafa199d..b152bed3b 100644
--- a/packages/core/src/modules/insight/insight.service.test.ts
+++ b/packages/core/src/modules/insight/insight.service.test.ts
@@ -40,7 +40,7 @@ const redis = {
// granted this project and a member granted another one.
const digestProject = { id: 'p1', name: 'Project', organizationId: 'org_1' };
const project = {
- findMany: mock(async () => [
+ findMany: mock(async (_query: unknown) => [
{ ...digestProject, organization: { subscriptionState: 'active' } },
]),
};
@@ -270,3 +270,57 @@ test('sendWeeklyDigests mails only the members who can open the project', async
{ to: '[email protected]', template: 'weekly-digest' },
]);
});
+
+test.each([
+ { selfHosted: true, state: 'self_hosted', sent: 1 },
+ { selfHosted: false, state: 'active', sent: 1 },
+ { selfHosted: false, state: 'trialing', sent: 1 },
+ { selfHosted: false, state: 'expired', sent: 0 },
+ { selfHosted: false, state: 'canceled', sent: 0 },
+ { selfHosted: false, state: 'self_hosted', sent: 0 },
+])('weekly digest eligibility: %j', async ({ selfHosted, state, sent }) => {
+ project.findMany.mockClear();
+ project.findMany.mockResolvedValueOnce([
+ { ...digestProject, organization: { subscriptionState: state } },
+ ]);
+ sentEmails.length = 0;
+ getAnalyticsOverviewCore.mockClear();
+ const noop = () => undefined;
+ const logger: import('../../logger').Logger = {
+ fatal: noop,
+ error: noop,
+ warn: noop,
+ info: noop,
+ debug: noop,
+ trace: noop,
+ child: () => logger,
+ };
+ const result = await subject.sendWeeklyDigests(
+ { ...deps, config: testCoreConfig({ selfHosted }) },
+ logger
+ );
+
+ expect(project.findMany).toHaveBeenCalledTimes(1);
+ const query = project.findMany.mock.calls[0]![0] as {
+ where: Record<string, unknown>;
+ };
+ expect(query.where).toEqual({
+ deleteAt: null,
+ eventsCount: { gt: expect.any(Number) },
+ ...(selfHosted
+ ? {}
+ : {
+ organization: { subscriptionStatus: { in: ['active', 'trialing'] } },
+ }),
+ });
+ expect(result).toEqual({ projects: 1, sent });
+ expect(getAnalyticsOverviewCore).toHaveBeenCalledTimes(sent * 2);
+ expect(sentEmails).toEqual(
+ sent
+ ? [
+ { to: '[email protected]', template: 'weekly-digest' },
+ { to: '[email protected]', template: 'weekly-digest' },
+ ]
+ : []
+ );
+});
diff --git a/packages/core/src/modules/insight/insight.service.ts b/packages/core/src/modules/insight/insight.service.ts
index 10e88cede..84ae9aa90 100644
--- a/packages/core/src/modules/insight/insight.service.ts
+++ b/packages/core/src/modules/insight/insight.service.ts
@@ -491,7 +491,13 @@ export async function sendWeeklyDigests(
where: {
deleteAt: null,
eventsCount: { gt: WEEKLY_DIGEST_MIN_EVENTS },
- organization: { subscriptionStatus: { in: ['active', 'trialing'] } },
+ ...(deps.config.selfHosted
+ ? {}
+ : {
+ organization: {
+ subscriptionStatus: { in: ['active', 'trialing'] },
+ },
+ }),
},
select: {
id: true,
@@ -505,7 +511,11 @@ export async function sendWeeklyDigests(
for (const project of projects) {
try {
const state = project.organization.subscriptionState;
- if (state !== 'active' && state !== 'trialing') {
+ if (
+ !deps.config.selfHosted &&
+ state !== 'active' &&
+ state !== 'trialing'
+ ) {
continue;
}
diff --git a/packages/core/src/modules/insight/src/store.test.ts b/packages/core/src/modules/insight/src/store.test.ts
new file mode 100644
index 000000000..69d77da4d
--- /dev/null
+++ b/packages/core/src/modules/insight/src/store.test.ts
@@ -0,0 +1,33 @@
+import { expect, mock, test } from 'bun:test';
+import { testCoreConfig } from '../../../../test/config-fixture';
+import type { ServiceDeps } from '../../../services';
+import { createInsightStore } from './store';
+
+test.each([
+ false,
+ true,
+])('daily candidates preserve activity filters with selfHosted=%s', async (selfHosted) => {
+ const findMany = mock(async (_query: unknown) => [{ id: 'project' }]);
+ const deps = {
+ db: { project: { findMany } },
+ prisma: { DbNull: null },
+ config: testCoreConfig({ selfHosted }),
+ } as unknown as ServiceDeps;
+
+ const result =
+ await createInsightStore(deps).listProjectIdsForCadence('daily');
+
+ expect(result).toEqual(['project']);
+ expect(findMany).toHaveBeenCalledTimes(1);
+ const query = findMany.mock.calls[0]![0] as {
+ where: Record<string, unknown>;
+ select: unknown;
+ };
+ expect(query.where).toEqual({
+ deleteAt: null,
+ eventsCount: { gt: 10_000 },
+ updatedAt: { gt: expect.any(Date) },
+ ...(selfHosted ? {} : { organization: { subscriptionStatus: 'active' } }),
+ });
+ expect(query.select).toEqual({ id: true });
+});
diff --git a/packages/core/src/modules/insight/src/store.ts b/packages/core/src/modules/insight/src/store.ts
index 3a4a084a4..8bac061c7 100644
--- a/packages/core/src/modules/insight/src/store.ts
+++ b/packages/core/src/modules/insight/src/store.ts
@@ -38,6 +38,7 @@ function toPersistedInsight(row: InsightRow): PersistedInsight {
};
}
+/** Keep insight persistence and candidate queries on the caller's dependency scope. */
export function createInsightStore(deps: ServiceDeps): InsightStore {
const db = deps.db;
// `Prisma.DbNull` lives in the same module as the constructed client, so it
@@ -52,9 +53,9 @@ export function createInsightStore(deps: ServiceDeps): InsightStore {
deleteAt: null,
eventsCount: { gt: 10_000 },
updatedAt: { gt: new Date(Date.now() - 1000 * 60 * 60 * 24) },
- organization: {
- subscriptionStatus: 'active',
- },
+ ...(deps.config.selfHosted
+ ? {}
+ : { organization: { subscriptionStatus: 'active' } }),
},
select: { id: true },
});