Skip to content

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

wip

Bill Yangc928e9ccbb8f
diff --git a/server/src/cron/monthly-usage-checker.ts b/server/src/cron/monthly-usage-checker.ts
index fd8063250..c0ac0d0f8 100644
--- a/server/src/cron/monthly-usage-checker.ts
+++ b/server/src/cron/monthly-usage-checker.ts
@@ -8,6 +8,9 @@ import { processResults } from "../api/utils.js";
// Default event limit for users without an active subscription
const DEFAULT_EVENT_LIMIT = 20_000;
+// Global set to track site IDs that have exceeded their monthly limits
+export const sitesOverLimit = new Set<number>();
+
/**
* Gets the first day of the current month in YYYY-MM-DD format
*/
@@ -133,6 +136,9 @@ export async function updateUsersMonthlyUsage() {
);
try {
+ // Clear the previous list of sites over their limit
+ sitesOverLimit.clear();
+
// Get all users
const users = await db.select().from(user);
@@ -154,15 +160,28 @@ export async function updateUsersMonthlyUsage() {
// Get monthly pageview count from ClickHouse using the subscription period
const pageviewCount = await getMonthlyPageviews(siteIds, periodStart);
+ // Check if over limit and update global set
+ const isOverLimit = pageviewCount > eventLimit;
+
// Update user's monthlyEventCount and overMonthlyLimit fields
await db
.update(user)
.set({
monthlyEventCount: pageviewCount,
- overMonthlyLimit: pageviewCount > eventLimit,
+ overMonthlyLimit: isOverLimit,
})
.where(eq(user.id, userData.id));
+ // If over the limit, add all this user's sites to the global set
+ if (isOverLimit) {
+ for (const siteId of siteIds) {
+ sitesOverLimit.add(siteId);
+ }
+ console.log(
+ `[Monthly Usage Checker] User ${userData.email} is over limit. Added ${siteIds.length} sites to blocked list.`
+ );
+ }
+
console.log(
`[Monthly Usage Checker] Updated user ${
userData.email
@@ -178,7 +197,9 @@ export async function updateUsersMonthlyUsage() {
}
}
- console.log("[Monthly Usage Checker] Completed monthly event usage check");
+ console.log(
+ `[Monthly Usage Checker] Completed monthly event usage check. ${sitesOverLimit.size} sites are over their limit.`
+ );
} catch (error) {
console.error(
"[Monthly Usage Checker] Error updating monthly usage:",
diff --git a/server/src/tracker/pageviewQueue.ts b/server/src/tracker/pageviewQueue.ts
index 7efd0ec25..173419290 100644
--- a/server/src/tracker/pageviewQueue.ts
+++ b/server/src/tracker/pageviewQueue.ts
@@ -56,11 +56,14 @@ class PageviewQueue {
// Process each pageview with its geo data
const processedPageviews = batch.map((pv) => {
const countryCode = geoData?.[pv.ipAddress]?.data?.countryIso || "";
- const regionCode = geoData?.[pv.ipAddress]?.data?.subdivisions?.[0]?.isoCode || "";
+ const regionCode =
+ geoData?.[pv.ipAddress]?.data?.subdivisions?.[0]?.isoCode || "";
return {
site_id: pv.site_id,
- timestamp: DateTime.fromISO(pv.timestamp).toFormat("yyyy-MM-dd HH:mm:ss"),
+ timestamp: DateTime.fromISO(pv.timestamp).toFormat(
+ "yyyy-MM-dd HH:mm:ss"
+ ),
session_id: pv.sessionId,
user_id: pv.userId,
hostname: pv.hostname || "",
@@ -77,8 +80,9 @@ class PageviewQueue {
screen_height: pv.screenHeight || 0,
device_type: getDeviceType(pv.screenWidth, pv.screenHeight, pv.ua),
country: countryCode,
- iso_3166_2: countryCode && regionCode ? countryCode + "-" + regionCode : "",
- }
+ iso_3166_2:
+ countryCode && regionCode ? countryCode + "-" + regionCode : "",
+ };
});
console.info("bulk insert: ", processedPageviews.length);
diff --git a/server/src/tracker/trackPageView.ts b/server/src/tracker/trackPageView.ts
index b744ab3c6..b95739d7b 100644
--- a/server/src/tracker/trackPageView.ts
+++ b/server/src/tracker/trackPageView.ts
@@ -1,13 +1,13 @@
-import { FastifyReply, FastifyRequest } from "fastify";
-import { TrackingPayload } from "../types.js";
-import { getUserId, getDeviceType, getIpAddress } from "../utils.js";
import crypto from "crypto";
+import { eq } from "drizzle-orm";
+import { FastifyReply, FastifyRequest } from "fastify";
+import UAParser, { UAParser as userAgentParser } from "ua-parser-js";
import { db, sql } from "../db/postgres/postgres.js";
import { activeSessions } from "../db/postgres/schema.js";
-import UAParser, { UAParser as userAgentParser } from "ua-parser-js";
-import { eq } from "drizzle-orm";
+import { TrackingPayload } from "../types.js";
+import { getDeviceType, getIpAddress, getUserId } from "../utils.js";
-import { Pageview } from "../db/clickhouse/types.js";
+import { sitesOverLimit } from "../cron/monthly-usage-checker.js";
import { pageviewQueue } from "./pageviewQueue.js";
type TotalPayload = TrackingPayload & {
@@ -99,6 +99,16 @@ export async function trackPageView(
reply: FastifyReply
) {
try {
+ // Check if the site has exceeded its monthly limit
+ if (sitesOverLimit.has(Number(request.body.site_id))) {
+ console.log(
+ `[Tracking] Skipping pageview for site ${request.body.site_id} - over monthly limit`
+ );
+ return reply
+ .status(200)
+ .send("Site over monthly limit, pageview not tracked");
+ }
+
const userAgent = request.headers["user-agent"] || "";
const ipAddress = getIpAddress(request);
const userId = getUserId(ipAddress, userAgent);
@@ -114,7 +124,6 @@ export async function trackPageView(
};
pageviewQueue.add(payload);
- // insertPageview(payload);
await updateSession(payload, existingSession);
return reply.status(200).send();