Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Refactor

Edward Cui335c341d4e60
diff --git a/server/src/lib/subUtils.ts b/server/src/lib/subUtils.ts
deleted file mode 100644
index ee7e02cc8..000000000
--- a/server/src/lib/subUtils.ts
+++ /dev/null
@@ -1,76 +0,0 @@
-import { eq } from "drizzle-orm";
-import { IS_CLOUD } from "./const.js";
-import { db } from "../db/postgres/postgres.js";
-import { organization } from "../db/postgres/schema.js";
-import { getSubscriptionInner } from "../api/stripe/getSubscription.js";
-
-export interface TierInfo {
- tier: "Free" | "Standard" | "Pro";
- monthsAllowed: number;
-}
-
-export interface SubscriptionInfo {
- planName: string;
- eventLimit: number;
- tierInfo: TierInfo;
-}
-
-export function getTierInfo(planName: string): TierInfo {
- if (planName === "free") {
- return {
- tier: "Free",
- monthsAllowed: 6,
- };
- }
-
- if (planName.startsWith("standard")) {
- return {
- tier: "Standard",
- monthsAllowed: 24,
- };
- }
-
- if (planName.startsWith("pro")) {
- return {
- tier: "Pro",
- monthsAllowed: 60,
- };
- }
-
- // Default to free tier for unknown plans
- console.warn(`Unknown plan name: "${planName}", defaulting to Free tier`);
- return {
- tier: "Free",
- monthsAllowed: 6,
- };
-}
-
-export async function getOrganizationSubscriptionInfo(organizationId: string): Promise<SubscriptionInfo | null> {
- if (!IS_CLOUD) {
- return null;
- }
-
- const [org] = await db
- .select({ id: organization.id })
- .from(organization)
- .where(eq(organization.id, organizationId))
- .limit(1);
-
- if (!org) {
- return null;
- }
-
- const subscription = await getSubscriptionInner(organizationId);
-
- if (!subscription) {
- return null;
- }
-
- const tierInfo = getTierInfo(subscription.planName);
-
- return {
- planName: subscription.planName,
- eventLimit: subscription.eventLimit,
- tierInfo,
- };
-}
diff --git a/server/src/services/import/importLimiter.ts b/server/src/services/import/importLimiter.ts
index 095267742..d3df28e0f 100644
--- a/server/src/services/import/importLimiter.ts
+++ b/server/src/services/import/importLimiter.ts
@@ -2,7 +2,6 @@ import { IS_CLOUD } from "../../lib/const.js";
import { db } from "../../db/postgres/postgres.js";
import { and, count, eq, inArray } from "drizzle-orm";
import { importStatus, sites } from "../../db/postgres/schema.js";
-import { ImportQuotaTracker } from "./importQuotaChecker.js";
import type { InsertImportStatus } from "./importStatusManager.js";
export class ImportLimiter {
@@ -97,8 +96,4 @@ export class ImportLimiter {
return { success: true };
});
}
-
- static async createQuotaTracker(organizationId: string): Promise<ImportQuotaTracker> {
- return ImportQuotaTracker.create(organizationId);
- }
}
diff --git a/server/src/services/import/importQuotaChecker.ts b/server/src/services/import/importQuotaChecker.ts
index 8d48767f4..8fec3ecc2 100644
--- a/server/src/services/import/importQuotaChecker.ts
+++ b/server/src/services/import/importQuotaChecker.ts
@@ -1,12 +1,38 @@
import { DateTime } from "luxon";
import { clickhouse } from "../../db/clickhouse/clickhouse.js";
import { db } from "../../db/postgres/postgres.js";
-import { sites } from "../../db/postgres/schema.js";
+import { sites, organization } from "../../db/postgres/schema.js";
import { eq } from "drizzle-orm";
import { processResults } from "../../api/analytics/utils.js";
-import { getOrganizationSubscriptionInfo } from "../../lib/subUtils.js";
+import { getBestSubscription, type SubscriptionInfo } from "../../lib/subscriptionUtils.js";
import { IS_CLOUD } from "../../lib/const.js";
+/**
+ * Get the number of months of historical data allowed for import based on subscription tier
+ */
+function getHistoricalWindowMonths(subscription: SubscriptionInfo): number {
+ // Free tier: 6 months
+ if (subscription.source === "free") {
+ return 6;
+ }
+
+ // AppSumo: treat as Standard tier (24 months)
+ if (subscription.source === "appsumo") {
+ return 24;
+ }
+
+ // Stripe: check if pro or standard
+ if (subscription.source === "stripe") {
+ if (subscription.planName.startsWith("pro")) {
+ return 60; // Pro tier: 60 months
+ }
+ return 24; // Standard tier: 24 months
+ }
+
+ // Default to free tier
+ return 6;
+}
+
export class ImportQuotaTracker {
private monthlyUsage: Map<string, number>;
private readonly monthlyLimit: number;
@@ -30,13 +56,20 @@ export class ImportQuotaTracker {
return new ImportQuotaTracker(new Map(), Infinity, Infinity, "190001");
}
- const subscriptionInfo = await getOrganizationSubscriptionInfo(organizationId);
- if (!subscriptionInfo) {
- throw new Error(`No subscription found for organization ${organizationId}`);
+ const [org] = await db
+ .select({ stripeCustomerId: organization.stripeCustomerId })
+ .from(organization)
+ .where(eq(organization.id, organizationId))
+ .limit(1);
+
+ if (!org) {
+ throw new Error(`Organization ${organizationId} not found`);
}
- const monthlyLimit = subscriptionInfo.eventLimit;
- const historicalWindowMonths = subscriptionInfo.tierInfo.monthsAllowed;
+ const subscription = await getBestSubscription(organizationId, org.stripeCustomerId);
+
+ const monthlyLimit = subscription.eventLimit;
+ const historicalWindowMonths = getHistoricalWindowMonths(subscription);
const oldestAllowedDate = DateTime.utc().minus({ months: historicalWindowMonths }).startOf("month");
const oldestAllowedMonth = oldestAllowedDate.toFormat("yyyyMM");
diff --git a/server/src/services/import/workers/csvParseWorker.ts b/server/src/services/import/workers/csvParseWorker.ts
index 3d4006085..4f7ce5068 100644
--- a/server/src/services/import/workers/csvParseWorker.ts
+++ b/server/src/services/import/workers/csvParseWorker.ts
@@ -7,8 +7,8 @@ import { r2Storage } from "../../storage/r2StorageService.js";
import { CSV_PARSE_QUEUE, CsvParseJob, DATA_INSERT_QUEUE, DataInsertJob } from "./jobs.js";
import { UmamiEvent, umamiHeaders } from "../mappings/umami.js";
import { updateImportStatus } from "../importStatusManager.js";
-import { ImportLimiter } from "../importLimiter.js";
import { deleteImportFile } from "../utils.js";
+import { ImportQuotaTracker } from "../importQuotaChecker.js";
const getImportDataHeaders = (platform: string) => {
switch (platform) {
@@ -92,7 +92,7 @@ export async function registerCsvParseWorker() {
let processingTimeout: NodeJS.Timeout | null = null;
try {
- const quotaTracker = await ImportLimiter.createQuotaTracker(organization);
+ const quotaTracker = await ImportQuotaTracker.create(organization);
const chunkSize = 5000;
const MAX_ROWS = 10_000_000; // 10 million rows max