Skip to content

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

Add weekly report service and email functionality (#622)

Bill Yang13976308f09c
diff --git a/client/src/api/admin/accountSettings.ts b/client/src/api/admin/accountSettings.ts
new file mode 100644
index 000000000..e49e065d7
--- /dev/null
+++ b/client/src/api/admin/accountSettings.ts
@@ -0,0 +1,38 @@
+import { useMutation, useQueryClient } from "@tanstack/react-query";
+import { authedFetch } from "../utils";
+
+export interface UpdateAccountSettingsRequest {
+ sendAutoEmailReports?: boolean;
+ // Add more settings here in the future
+}
+
+export interface UpdateAccountSettingsResponse {
+ success: boolean;
+ settings: {
+ sendAutoEmailReports: boolean;
+ // Add more settings here as they're added
+ };
+}
+
+export function useUpdateAccountSettings() {
+ const queryClient = useQueryClient();
+
+ return useMutation<UpdateAccountSettingsResponse, Error, UpdateAccountSettingsRequest>({
+ mutationFn: async (settings) => {
+ try {
+ return await authedFetch<UpdateAccountSettingsResponse>("/user/account-settings", undefined, {
+ method: "POST",
+ data: settings,
+ });
+ } catch (error) {
+ throw new Error(error instanceof Error ? error.message : "Failed to update account settings");
+ }
+ },
+ onSuccess: () => {
+ // Invalidate session query to refetch user data
+ queryClient.invalidateQueries({
+ queryKey: ["session"],
+ });
+ },
+ });
+}
diff --git a/client/src/app/settings/account/components/AccountInner.tsx b/client/src/app/settings/account/components/AccountInner.tsx
index bf6962c3d..12115ab36 100644
--- a/client/src/app/settings/account/components/AccountInner.tsx
+++ b/client/src/app/settings/account/components/AccountInner.tsx
@@ -11,10 +11,14 @@ import { DeleteAccount } from "./DeleteAccount";
import { validateEmail } from "../../../../lib/auth-utils";
import { useRouter } from "next/navigation";
import { useQueryClient } from "@tanstack/react-query";
+import { Switch } from "../../../../components/ui/switch";
+import { useUpdateAccountSettings } from "../../../../api/admin/accountSettings";
-export function AccountInner({ session }: { session: ReturnType<typeof authClient.useSession> }) {
+export function AccountInner() {
+ const session = authClient.useSession();
const router = useRouter();
const queryClient = useQueryClient();
+ const updateAccountSettings = useUpdateAccountSettings();
const [email, setEmail] = useState(session.data?.user.email ?? "");
const [name, setName] = useState(session.data?.user.name ?? "");
@@ -43,7 +47,7 @@ export function AccountInner({ session }: { session: ReturnType<typeof authClien
}
toast.success("Name updated successfully");
- globalThis.location.reload();
+ session.refetch();
} catch (error) {
console.error("Error updating name:", error);
toast.error(error instanceof Error ? error.message : "Failed to update name");
@@ -74,9 +78,7 @@ export function AccountInner({ session }: { session: ReturnType<typeof authClien
}
toast.success("Email updated successfully");
-
- // Reload the page to refresh the session
- globalThis.location.reload();
+ session.refetch();
} catch (error) {
console.error("Error updating email:", error);
toast.error(error instanceof Error ? error.message : "Failed to update email");
@@ -85,6 +87,19 @@ export function AccountInner({ session }: { session: ReturnType<typeof authClien
}
};
+ const handleEmailReportsToggle = async (checked: boolean) => {
+ try {
+ await updateAccountSettings.mutateAsync({
+ sendAutoEmailReports: checked,
+ });
+ toast.success(`Email reports ${checked ? "enabled" : "disabled"}`);
+ session.refetch();
+ } catch (error) {
+ console.error("Error updating email reports setting:", error);
+ toast.error(error instanceof Error ? error.message : "Failed to update email reports setting");
+ }
+ };
+
return (
<div className="flex flex-col gap-4">
<Card className="p-2">
@@ -126,6 +141,21 @@ export function AccountInner({ session }: { session: ReturnType<typeof authClien
</Button>
</div>
</div>
+ {(session.data?.user as any)?.sendAutoEmailReports !== undefined && (
+ <div className="flex items-center justify-between">
+ <div className="space-y-2">
+ <h4 className="text-sm font-medium">Send Weekly Email Reports</h4>
+ <p className="text-xs text-neutral-500">Enable or disable automatic email reports for your account.</p>
+ </div>
+ <div className="flex space-x-2">
+ <Switch
+ checked={(session.data?.user as any).sendAutoEmailReports}
+ onCheckedChange={handleEmailReportsToggle}
+ disabled={updateAccountSettings.isPending}
+ />
+ </div>
+ </div>
+ )}
<Button
variant="outline"
onClick={async () => {
diff --git a/client/src/app/settings/account/page.tsx b/client/src/app/settings/account/page.tsx
index b55f42143..bb060c997 100644
--- a/client/src/app/settings/account/page.tsx
+++ b/client/src/app/settings/account/page.tsx
@@ -1,12 +1,10 @@
"use client";
import { useSetPageTitle } from "../../../hooks/useSetPageTitle";
-import { authClient } from "../../../lib/auth";
import { AccountInner } from "./components/AccountInner";
export default function AccountPage() {
useSetPageTitle("Rybbit · Account");
- const session = authClient.useSession();
return (
<>
@@ -14,7 +12,7 @@ export default function AccountPage() {
<h1 className="text-3xl font-bold tracking-tight">Account Settings</h1>
<p className="text-gray-500 dark:text-gray-400">Manage your personal account settings</p>
</div>
- <AccountInner session={session} />
+ <AccountInner />
</>
);
}
diff --git a/server/src/api/user/updateAccountSettings.ts b/server/src/api/user/updateAccountSettings.ts
new file mode 100644
index 000000000..143153e6c
--- /dev/null
+++ b/server/src/api/user/updateAccountSettings.ts
@@ -0,0 +1,74 @@
+import { FastifyRequest, FastifyReply } from "fastify";
+import { eq } from "drizzle-orm";
+import { z } from "zod";
+import { DateTime } from "luxon";
+import { db } from "../../db/postgres/postgres.js";
+import { user } from "../../db/postgres/schema.js";
+import { getSessionFromReq } from "../../lib/auth-utils.js";
+
+// Schema for account settings that can be updated
+const updateAccountSettingsSchema = z.object({
+ sendAutoEmailReports: z.boolean().optional(),
+ // Add more settings here in the future
+});
+
+export type UpdateAccountSettingsRequest = z.infer<typeof updateAccountSettingsSchema>;
+
+export const updateAccountSettings = async (
+ request: FastifyRequest<{ Body: UpdateAccountSettingsRequest }>,
+ reply: FastifyReply
+) => {
+ try {
+ const session = await getSessionFromReq(request);
+
+ if (!session?.user.id) {
+ return reply.status(401).send({ error: "Unauthorized" });
+ }
+
+ // Validate request body
+ const validation = updateAccountSettingsSchema.safeParse(request.body);
+
+ if (!validation.success) {
+ return reply.status(400).send({ error: "Invalid request body", details: validation.error });
+ }
+
+ const settings = validation.data;
+
+ // Build update object with only provided fields
+ const updateData: Partial<typeof user.$inferInsert> = {};
+
+ if (settings.sendAutoEmailReports !== undefined) {
+ updateData.sendAutoEmailReports = settings.sendAutoEmailReports;
+ }
+
+ // If no fields to update, return early
+ if (Object.keys(updateData).length === 0) {
+ return reply.status(400).send({ error: "No settings provided to update" });
+ }
+
+ // Update user settings
+ const [updatedUser] = await db
+ .update(user)
+ .set({
+ ...updateData,
+ updatedAt: DateTime.now().toISO(),
+ })
+ .where(eq(user.id, session.user.id))
+ .returning();
+
+ if (!updatedUser) {
+ return reply.status(404).send({ error: "User not found" });
+ }
+
+ return reply.send({
+ success: true,
+ settings: {
+ sendAutoEmailReports: updatedUser.sendAutoEmailReports,
+ // Add more settings here as they're added
+ },
+ });
+ } catch (error) {
+ console.error("Error updating account settings:", error);
+ return reply.status(500).send({ error: "Failed to update account settings" });
+ }
+};
diff --git a/server/src/db/postgres/schema.ts b/server/src/db/postgres/schema.ts
index 32bc6fcac..c44befe6b 100644
--- a/server/src/db/postgres/schema.ts
+++ b/server/src/db/postgres/schema.ts
@@ -31,9 +31,13 @@ export const user = pgTable(
banned: boolean(),
banReason: text(),
banExpires: timestamp({ mode: "string" }),
+ // deprecated
stripeCustomerId: text(),
+ // deprecated
overMonthlyLimit: boolean().default(false),
+ // deprecated
monthlyEventCount: integer().default(0),
+ sendAutoEmailReports: boolean().default(true),
},
table => [unique("user_username_unique").on(table.username), unique("user_email_unique").on(table.email)]
);
diff --git a/server/src/index.ts b/server/src/index.ts
index 718e2cd0e..0ba6fd275 100644
--- a/server/src/index.ts
+++ b/server/src/index.ts
@@ -63,6 +63,7 @@ import { handleWebhook } from "./api/stripe/webhook.js";
import { addUserToOrganization } from "./api/user/addUserToOrganization.js";
import { getUserOrganizations } from "./api/user/getUserOrganizations.js";
import { listOrganizationMembers } from "./api/user/listOrganizationMembers.js";
+import { updateAccountSettings } from "./api/user/updateAccountSettings.js";
import { initializeClickhouse } from "./db/clickhouse/clickhouse.js";
import { initPostgres } from "./db/postgres/initPostgres.js";
import { getSessionFromReq, mapHeaders } from "./lib/auth-utils.js";
@@ -72,6 +73,7 @@ import { siteConfig } from "./lib/siteConfig.js";
import { trackEvent } from "./services/tracker/trackEvent.js";
// need to import telemetry service here to start it
import { telemetryService } from "./services/telemetryService.js";
+import { weeklyReportService } from "./services/weekyReports/weeklyReportService.js";
import { extractSiteId } from "./utils.js";
import { getTrackingConfig } from "./api/sites/getTrackingConfig.js";
@@ -341,43 +343,44 @@ server.get("/api/site/:siteId/excluded-ips", getSiteExcludedIPs);
server.get("/api/list-organization-members/:organizationId", listOrganizationMembers);
server.get("/api/user/organizations", getUserOrganizations);
server.post("/api/add-user-to-organization", addUserToOrganization);
+server.post("/api/user/account-settings", updateAccountSettings);
// UPTIME MONITORING
// Only register uptime routes when IS_CLOUD is true (Redis is available)
-if (IS_CLOUD) {
- // Dynamically import uptime modules only when needed
- const { getMonitors } = await import("./api/uptime/getMonitors.js");
- const { getMonitor } = await import("./api/uptime/getMonitor.js");
- const { createMonitor } = await import("./api/uptime/createMonitor.js");
- const { updateMonitor } = await import("./api/uptime/updateMonitor.js");
- const { deleteMonitor } = await import("./api/uptime/deleteMonitor.js");
- const { getMonitorEvents } = await import("./api/uptime/getMonitorEvents.js");
- const { getMonitorStats } = await import("./api/uptime/getMonitorStats.js");
- const { getMonitorUptimeBuckets } = await import("./api/uptime/getMonitorUptimeBuckets.js");
- const { getMonitorStatus } = await import("./api/uptime/getMonitorStatus.js");
- const { getMonitorUptime } = await import("./api/uptime/getMonitorUptime.js");
- const { getRegions } = await import("./api/uptime/getRegions.js");
- const { incidentsRoutes } = await import("./api/uptime/incidents.js");
- const { notificationRoutes } = await import("./api/uptime/notifications.js");
-
- server.get("/api/uptime/monitors", getMonitors);
- server.get("/api/uptime/monitors/:monitorId", getMonitor);
- server.post("/api/uptime/monitors", createMonitor);
- server.put("/api/uptime/monitors/:monitorId", updateMonitor);
- server.delete("/api/uptime/monitors/:monitorId", deleteMonitor);
- server.get("/api/uptime/monitors/:monitorId/events", getMonitorEvents);
- server.get("/api/uptime/monitors/:monitorId/stats", getMonitorStats);
- server.get("/api/uptime/monitors/:monitorId/status", getMonitorStatus);
- server.get("/api/uptime/monitors/:monitorId/uptime", getMonitorUptime);
- server.get("/api/uptime/monitors/:monitorId/buckets", getMonitorUptimeBuckets);
- server.get("/api/uptime/regions", getRegions);
-
- // Register incidents routes
- server.register(incidentsRoutes);
-
- // Register notification routes
- server.register(notificationRoutes);
-}
+// if (IS_CLOUD) {
+// // Dynamically import uptime modules only when needed
+// const { getMonitors } = await import("./api/uptime/getMonitors.js");
+// const { getMonitor } = await import("./api/uptime/getMonitor.js");
+// const { createMonitor } = await import("./api/uptime/createMonitor.js");
+// const { updateMonitor } = await import("./api/uptime/updateMonitor.js");
+// const { deleteMonitor } = await import("./api/uptime/deleteMonitor.js");
+// const { getMonitorEvents } = await import("./api/uptime/getMonitorEvents.js");
+// const { getMonitorStats } = await import("./api/uptime/getMonitorStats.js");
+// const { getMonitorUptimeBuckets } = await import("./api/uptime/getMonitorUptimeBuckets.js");
+// const { getMonitorStatus } = await import("./api/uptime/getMonitorStatus.js");
+// const { getMonitorUptime } = await import("./api/uptime/getMonitorUptime.js");
+// const { getRegions } = await import("./api/uptime/getRegions.js");
+// const { incidentsRoutes } = await import("./api/uptime/incidents.js");
+// const { notificationRoutes } = await import("./api/uptime/notifications.js");
+
+// server.get("/api/uptime/monitors", getMonitors);
+// server.get("/api/uptime/monitors/:monitorId", getMonitor);
+// server.post("/api/uptime/monitors", createMonitor);
+// server.put("/api/uptime/monitors/:monitorId", updateMonitor);
+// server.delete("/api/uptime/monitors/:monitorId", deleteMonitor);
+// server.get("/api/uptime/monitors/:monitorId/events", getMonitorEvents);
+// server.get("/api/uptime/monitors/:monitorId/stats", getMonitorStats);
+// server.get("/api/uptime/monitors/:monitorId/status", getMonitorStatus);
+// server.get("/api/uptime/monitors/:monitorId/uptime", getMonitorUptime);
+// server.get("/api/uptime/monitors/:monitorId/buckets", getMonitorUptimeBuckets);
+// server.get("/api/uptime/regions", getRegions);
+
+// // Register incidents routes
+// server.register(incidentsRoutes);
+
+// // Register notification routes
+// server.register(notificationRoutes);
+// }
// STRIPE & ADMIN
@@ -407,6 +410,9 @@ const start = async () => {
await Promise.all([initializeClickhouse(), initPostgres()]);
telemetryService.startTelemetryCron();
+ if (IS_CLOUD) {
+ weeklyReportService.startWeeklyReportCron();
+ }
// Start the server first
await server.listen({ port: 3001, host: "0.0.0.0" });
diff --git a/server/src/lib/auth.ts b/server/src/lib/auth.ts
index f16585eab..3dbe22c40 100644
--- a/server/src/lib/auth.ts
+++ b/server/src/lib/auth.ts
@@ -110,6 +110,14 @@ export let auth: AuthType | null = betterAuth({
},
},
user: {
+ additionalFields: {
+ sendAutoEmailReports: {
+ type: "boolean",
+ required: true,
+ defaultValue: true,
+ input: true,
+ },
+ },
deleteUser: {
enabled: true,
},
@@ -184,10 +192,11 @@ export function initAuth(allowedOrigins: string[]) {
},
user: {
additionalFields: {
- monthlyEventCount: {
- type: "number",
- defaultValue: 0,
- required: false,
+ sendAutoEmailReports: {
+ type: "boolean",
+ required: true,
+ defaultValue: true,
+ input: true,
},
},
changeEmail: {
diff --git a/server/src/lib/email/email.ts b/server/src/lib/email/email.ts
index 3b809cb72..057fb4ffd 100644
--- a/server/src/lib/email/email.ts
+++ b/server/src/lib/email/email.ts
@@ -3,6 +3,8 @@ import { render } from "@react-email/components";
import { IS_CLOUD } from "../const.js";
import { InvitationEmail } from "./templates/InvitationEmail.js";
import { LimitExceededEmail } from "./templates/LimitExceededEmail.js";
+import { WeeklyReportEmail } from "./templates/WeeklyReportEmail.js";
+import type { OrganizationReport } from "../../services/weekyReports/weeklyReportTypes.js";
let resend: Resend | undefined;
@@ -67,3 +69,20 @@ export const sendLimitExceededEmail = async (
await sendEmail(email, `Action Required: ${organizationName} has exceeded its monthly event limit`, html);
};
+
+export const sendWeeklyReportEmail = async (
+ email: string,
+ userName: string,
+ organizationReport: OrganizationReport
+) => {
+ const html = await render(
+ WeeklyReportEmail({
+ userName,
+ organizationReport,
+ })
+ );
+
+ const subject = `Weekly Analytics Report - ${organizationReport.sites[0].siteName}`;
+
+ await sendEmail(email, subject, html);
+};
diff --git a/server/src/lib/email/templates/WeeklyReportEmail.tsx b/server/src/lib/email/templates/WeeklyReportEmail.tsx
new file mode 100644
index 000000000..0238f9622
--- /dev/null
+++ b/server/src/lib/email/templates/WeeklyReportEmail.tsx
@@ -0,0 +1,460 @@
+import {
+ Body,
+ Container,
+ Head,
+ Heading,
+ Html,
+ Link,
+ Preview,
+ Section,
+ Tailwind,
+ Text,
+ pixelBasedPreset,
+} from "@react-email/components";
+import * as React from "react";
+import type { OrganizationReport, SingleColData } from "../../../services/weekyReports/weeklyReportTypes.js";
+
+interface WeeklyReportEmailProps {
+ userName: string;
+ organizationReport: OrganizationReport;
+}
+
+interface MetricCardProps {
+ label: string;
+ currentValue: string;
+ growth: string;
+ isPositive: boolean;
+}
+
+interface TopListItemProps {
+ value: string;
+ percentage: number | null;
+ count: number;
+ barWidth: number;
+ isLast: boolean;
+ favicon?: string;
+ labelClassName?: string;
+}
+
+interface TopListSectionProps {
+ title: string;
+ items: SingleColData[];
+ renderLabel: (item: SingleColData) => React.ReactNode;
+ showFavicon?: boolean;
+ labelClassName?: string;
+ className?: string;
+}
+
+const calculateGrowth = (current: number | null | undefined, previous: number | null | undefined): string => {
+ const curr = current ?? 0;
+ const prev = previous ?? 0;
+
+ if (prev === 0) {
+ return curr > 0 ? "+100%" : "0%";
+ }
+ const growth = ((curr - prev) / prev) * 100;
+ const sign = growth > 0 ? "+" : "";
+ return `${sign}${growth.toFixed(1)}%`;
+};
+
+const formatDuration = (seconds: number): string => {
+ if (seconds < 60) {
+ return `${Math.round(seconds)}s`;
+ }
+ const minutes = Math.floor(seconds / 60);
+ const remainingSeconds = Math.round(seconds % 60);
+ return `${minutes}m ${remainingSeconds}s`;
+};
+
+const formatNumber = (num: number | null | undefined): string => {
+ if (num == null || isNaN(num)) {
+ return "0";
+ }
+ if (num >= 1000000) {
+ return `${(num / 1000000).toFixed(1)}M`;
+ }
+ if (num >= 1000) {
+ return `${(num / 1000).toFixed(1)}K`;
+ }
+ return num.toFixed(0);
+};
+
+const safeToFixed = (num: number | null | undefined, decimals: number = 1): string => {
+ if (num == null || isNaN(num)) {
+ return "0";
+ }
+ return num.toFixed(decimals);
+};
+
+const regionNamesInEnglish = new Intl.DisplayNames(["en"], { type: "region" });
+
+const getCountryFlag = (countryCode: string): string => {
+ if (!countryCode || countryCode.length !== 2) return "";
+ const codePoints = countryCode
+ .toUpperCase()
+ .split("")
+ .map(char => 127397 + char.charCodeAt(0));
+ return String.fromCodePoint(...codePoints);
+};
+
+const getCountryDisplay = (countryCode: string): string => {
+ try {
+ const flag = getCountryFlag(countryCode);
+ const name = regionNamesInEnglish.of(countryCode.toUpperCase()) || countryCode;
+ return `${flag} ${name}`;
+ } catch (error) {
+ return countryCode;
+ }
+};
+
+const MetricCard = ({ label, currentValue, growth, isPositive }: MetricCardProps) => (
+ <div
+ style={{
+ backgroundColor: "#f9fafb",
+ border: "1px solid #e5e7eb",
+ borderRadius: "8px",
+ padding: "16px",
+ }}
+ >
+ <Text style={{ color: "#6b7280", fontSize: "12px", marginBottom: "4px", marginTop: 0 }}>{label}</Text>
+ <div>
+ <span style={{ color: "#111827", fontSize: "24px", fontWeight: "bold", lineHeight: "1", marginRight: "8px" }}>
+ {currentValue}
+ </span>
+ <span
+ style={{
+ color: isPositive ? "#10b981" : "#ef4444",
+ fontSize: "12px",
+ fontWeight: 500,
+ lineHeight: "1",
+ verticalAlign: "baseline",
+ }}
+ >
+ {growth}
+ </span>
+ </div>
+ </div>
+);
+
+const TopListItem = ({ value, percentage, count, barWidth, isLast, favicon, labelClassName }: TopListItemProps) => {
+ const barStyle: React.CSSProperties = {
+ background: `linear-gradient(to right, rgba(16, 185, 129, 0.25) 0%, rgba(16, 185, 129, 0.25) ${barWidth}%, transparent ${barWidth}%, transparent 100%)`,
+ borderRadius: "6px",
+ };
+
+ return (
+ <table
+ style={{
+ width: "100%",
+ marginBottom: isLast ? "0" : "8px",
+ borderCollapse: "separate",
+ borderSpacing: "0",
+ ...barStyle,
+ }}
+ >
+ <tbody>
+ <tr>
+ <td style={{ padding: "4px 8px", verticalAlign: "middle" }}>
+ {favicon ? (
+ <table style={{ width: "100%", borderCollapse: "collapse" }}>
+ <tbody>
+ <tr>
+ <td style={{ width: "16px", paddingRight: "4px", verticalAlign: "middle" }}>
+ <img src={favicon} alt="" width="16" height="16" style={{ display: "block" }} />
+ </td>
+ <td style={{ verticalAlign: "middle" }}>
+ <Text
+ style={{
+ color: "#111827",
+ fontSize: "14px",
+ margin: 0,
+ lineHeight: "1.2",
+ overflow: "hidden",
+ textOverflow: "ellipsis",
+ whiteSpace: "nowrap",
+ }}
+ >
+ {value}
+ </Text>
+ </td>
+ </tr>
+ </tbody>
+ </table>
+ ) : (
+ <Text style={{ color: "#111827", fontSize: "14px", margin: 0, lineHeight: "1.2" }}>{value}</Text>
+ )}
+ </td>
+ <td style={{ padding: "8px", textAlign: "right", verticalAlign: "middle", whiteSpace: "nowrap" }}>
+ <Text style={{ color: "#6b7280", fontSize: "12px", margin: 0, marginRight: "12px", lineHeight: "1.2" }}>
+ {safeToFixed(percentage, 1)}%
+ </Text>
+ </td>
+ <td
+ style={{ padding: "8px", textAlign: "right", verticalAlign: "middle", whiteSpace: "nowrap", width: "40px" }}
+ >
+ <Text style={{ color: "#111827", fontSize: "14px", fontWeight: 500, margin: 0, lineHeight: "1.2" }}>
+ {formatNumber(count)}
+ </Text>
+ </td>
+ </tr>
+ </tbody>
+ </table>
+ );
+};
+
+const TopListSection = ({ title, items, renderLabel, showFavicon, labelClassName, className }: TopListSectionProps) => {
+ if (items.length === 0) return null;
+
+ const ratio = items[0]?.percentage ? 100 / items[0].percentage : 1;
+
+ return (
+ <div
+ style={{
+ backgroundColor: "#f9fafb",
+ border: "1px solid #e5e7eb",
+ borderRadius: "8px",
+ padding: "16px",
+ marginBottom: "16px",
+ }}
+ >
+ <Text style={{ color: "#111827", fontSize: "14px", fontWeight: 600, marginBottom: "12px", marginTop: 0 }}>
+ {title}
+ </Text>
+ {items.map((item, index) => {
+ const barWidth = (item.percentage ?? 0) * ratio;
+ const favicon = showFavicon ? `https://www.google.com/s2/favicons?domain=${item.value}&sz=16` : undefined;
+
+ return (
+ <TopListItem
+ key={index}
+ value={typeof renderLabel(item) === "string" ? (renderLabel(item) as string) : item.value}
+ percentage={item.percentage}
+ count={item.count}
+ barWidth={barWidth}
+ isLast={index === items.length - 1}
+ favicon={favicon}
+ labelClassName={labelClassName}
+ />
+ );
+ })}
+ </div>
+ );
+};
+
+export const WeeklyReportEmail = ({ userName, organizationReport }: WeeklyReportEmailProps) => {
+ const currentYear = new Date().getFullYear();
+
+ return (
+ <Html>
+ <Head />
+ <Preview>Weekly Analytics Report for {organizationReport.organizationName}</Preview>
+ <Tailwind
+ config={{
+ presets: [pixelBasedPreset],
+ theme: {
+ extend: {
+ colors: {
+ brand: "#10b981",
+ lightBg: "#ffffff",
+ cardBg: "#f9fafb",
+ darkText: "#111827",
+ mutedText: "#6b7280",
+ borderColor: "#e5e7eb",
+ positive: "#10b981",
+ negative: "#ef4444",
+ },
+ },
+ },
+ }}
+ >
+ <Body className="bg-lightBg font-sans">
+ <Container className="mx-auto py-10 px-6 max-w-[600px]">
+ {/* Header */}
+ <Section className="text-center mb-8">
+ <div className="inline-block bg-brand/10 text-brand px-3 py-1.5 rounded-full text-sm font-medium mb-4">
+ Weekly Report
+ </div>
+ <table
+ style={{
+ width: "100%",
+ marginBottom: "8px",
+ }}
+ >
+ <tbody>
+ <tr>
+ <td style={{ textAlign: "center" }}>
+ <table
+ style={{
+ display: "inline-block",
+ verticalAlign: "middle",
+ }}
+ >
+ <tbody>
+ <tr>
+ <td
+ style={{
+ verticalAlign: "middle",
+ paddingRight: "12px",
+ }}
+ >
+ <img
+ src={`https://www.google.com/s2/favicons?domain=${organizationReport.sites[0].siteDomain}&sz=32`}
+ alt=""
+ width="24"
+ height="24"
+ style={{ borderRadius: "4px", display: "block" }}
+ />
+ </td>
+ <td style={{ verticalAlign: "middle" }}>
+ <Heading
+ style={{
+ color: "#111827",
+ fontSize: "30px",
+ fontWeight: 600,
+ margin: 0,
+ lineHeight: "1.2",
+ }}
+ >
+ {organizationReport.sites[0].siteName}
+ </Heading>
+ </td>
+ </tr>
+ </tbody>
+ </table>
+ </td>
+ </tr>
+ </tbody>
+ </table>
+ <Text className="text-mutedText text-base">Hi {userName}, here's your weekly analytics summary</Text>
+ </Section>
+
+ {/* Sites Reports */}
+ {organizationReport.sites.map(site => (
+ <Section key={site.siteId} className="mb-10">
+ {/* Metrics Cards */}
+ <table style={{ width: "100%", marginBottom: "24px" }}>
+ <tbody>
+ <tr>
+ <td style={{ width: "50%", paddingRight: "6px", paddingBottom: "12px" }}>
+ <MetricCard
+ label="Sessions"
+ currentValue={formatNumber(site.currentWeek.sessions)}
+ growth={calculateGrowth(site.currentWeek.sessions, site.previousWeek.sessions)}
+ isPositive={site.currentWeek.sessions >= site.previousWeek.sessions}
+ />
+ </td>
+ <td style={{ width: "50%", paddingLeft: "6px", paddingBottom: "12px" }}>
+ <MetricCard
+ label="Pageviews"
+ currentValue={formatNumber(site.currentWeek.pageviews)}
+ growth={calculateGrowth(site.currentWeek.pageviews, site.previousWeek.pageviews)}
+ isPositive={site.currentWeek.pageviews >= site.previousWeek.pageviews}
+ />
+ </td>
+ </tr>
+ <tr>
+ <td style={{ width: "50%", paddingRight: "6px", paddingBottom: "12px" }}>
+ <MetricCard
+ label="Unique Users"
+ currentValue={formatNumber(site.currentWeek.users)}
+ growth={calculateGrowth(site.currentWeek.users, site.previousWeek.users)}
+ isPositive={site.currentWeek.users >= site.previousWeek.users}
+ />
+ </td>
+ <td style={{ width: "50%", paddingLeft: "6px", paddingBottom: "12px" }}>
+ <MetricCard
+ label="Avg Duration"
+ currentValue={formatDuration(site.currentWeek.session_duration)}
+ growth={calculateGrowth(
+ site.currentWeek.session_duration,
+ site.previousWeek.session_duration
+ )}
+ isPositive={site.currentWeek.session_duration >= site.previousWeek.session_duration}
+ />
+ </td>
+ </tr>
+ <tr>
+ <td style={{ width: "50%", paddingRight: "6px" }}>
+ <MetricCard
+ label="Pages/Session"
+ currentValue={safeToFixed(site.currentWeek.pages_per_session, 1)}
+ growth={calculateGrowth(
+ site.currentWeek.pages_per_session,
+ site.previousWeek.pages_per_session
+ )}
+ isPositive={
+ (site.currentWeek.pages_per_session ?? 0) >= (site.previousWeek.pages_per_session ?? 0)
+ }
+ />
+ </td>
+ <td style={{ width: "50%", paddingLeft: "6px" }}>
+ <MetricCard
+ label="Bounce Rate"
+ currentValue={`${safeToFixed(site.currentWeek.bounce_rate, 1)}%`}
+ growth={calculateGrowth(site.currentWeek.bounce_rate, site.previousWeek.bounce_rate)}
+ isPositive={(site.currentWeek.bounce_rate ?? 0) <= (site.previousWeek.bounce_rate ?? 0)}
+ />
+ </td>
+ </tr>
+ </tbody>
+ </table>
+
+ {/* Top Lists Section */}
+ <div className="mb-6">
+ <TopListSection
+ title="Top Countries"
+ items={site.topCountries}
+ renderLabel={item => getCountryDisplay(item.value)}
+ />
+ <TopListSection
+ title="Top Pages"
+ items={site.topPages}
+ renderLabel={item => item.value}
+ labelClassName="text-darkText text-sm m-0 truncate max-w-[280px]"
+ />
+ <TopListSection
+ title="Top Referrers"
+ items={site.topReferrers}
+ renderLabel={item => item.value}
+ showFavicon={true}
+ labelClassName="text-darkText text-sm m-0 truncate"
+ />
+ <TopListSection
+ title="Device Breakdown"
+ items={site.deviceBreakdown}
+ renderLabel={item => item.value}
+ labelClassName="text-darkText text-sm m-0 capitalize"
+ className="bg-cardBg border border-borderColor rounded-lg p-4"
+ />
+ </div>
+
+ {/* Dashboard Link */}
+ <div className="text-center mb-6">
+ <Link
+ href={`https://app.rybbit.io/${site.siteId}`}
+ className="inline-block bg-brand text-white px-6 py-2.5 rounded-md font-medium text-sm no-underline"
+ >
+ View Full Dashboard
+ </Link>
+ </div>
+ </Section>
+ ))}
+
+ {/* Footer */}
+ <Section className="text-center border-t border-borderColor pt-5">
+ <Text className="text-mutedText text-xs mb-2">
+ This weekly report covers the last 7 days of analytics data.
+ </Text>
+ <Text className="text-mutedText text-xs mb-3">
+ <Link href="https://rybbit.io/settings/account" className="text-brand no-underline">
+ Unsubscribe from weekly reports
+ </Link>
+ </Text>
+ <Text className="text-mutedText text-xs">© {currentYear} Rybbit Analytics</Text>
+ </Section>
+ </Container>
+ </Body>
+ </Tailwind>
+ </Html>
+ );
+};
diff --git a/server/src/services/sessions/sessionsService.ts b/server/src/services/sessions/sessionsService.ts
index 78394062e..da356d402 100644
--- a/server/src/services/sessions/sessionsService.ts
+++ b/server/src/services/sessions/sessionsService.ts
@@ -13,15 +13,19 @@ class SessionsService {
}
private initializeCleanupCron() {
- this.cleanupTask = cron.schedule("* * * * *", async () => {
- try {
- const deletedCount = await this.cleanupOldSessions();
- // Uncomment for debugging
- this.logger.debug(`Cleaned up ${deletedCount} expired sessions`);
- } catch (error) {
- this.logger.error(error as Error, "Error during session cleanup");
- }
- });
+ this.cleanupTask = cron.schedule(
+ "* * * * *",
+ async () => {
+ try {
+ const deletedCount = await this.cleanupOldSessions();
+ // Uncomment for debugging
+ this.logger.debug(`Cleaned up ${deletedCount} expired sessions`);
+ } catch (error) {
+ this.logger.error(error as Error, "Error during session cleanup");
+ }
+ },
+ { timezone: "UTC" }
+ );
this.logger.info("Session cleanup cron initialized (runs every minute)");
}
diff --git a/server/src/services/telemetryService.ts b/server/src/services/telemetryService.ts
index 4c7ae1e70..efd0768fe 100644
--- a/server/src/services/telemetryService.ts
+++ b/server/src/services/telemetryService.ts
@@ -22,13 +22,17 @@ class TelemetryService {
if (!IS_CLOUD && !DISABLE_TELEMETRY) {
this.logger.info("Initializing telemetry cron");
// Schedule telemetry to run every 24 hours at midnight
- this.telemetryTask = cron.schedule("0 0 * * *", async () => {
- try {
- await this.collectAndSendTelemetry();
- } catch (error) {
- this.logger.error(error as Error, "Error during telemetry collection");
- }
- });
+ this.telemetryTask = cron.schedule(
+ "0 0 * * *",
+ async () => {
+ try {
+ await this.collectAndSendTelemetry();
+ } catch (error) {
+ this.logger.error(error as Error, "Error during telemetry collection");
+ }
+ },
+ { timezone: "UTC" }
+ );
// Run immediately on startup
this.collectAndSendTelemetry();
diff --git a/server/src/services/usageService.ts b/server/src/services/usageService.ts
index d08cb95c4..c69828c31 100644
--- a/server/src/services/usageService.ts
+++ b/server/src/services/usageService.ts
@@ -26,13 +26,17 @@ class UsageService {
private initializeUsageCheckCron() {
if (IS_CLOUD && process.env.NODE_ENV !== "development") {
// Schedule the monthly usage checker to run every 30 minutes
- this.usageCheckTask = cron.schedule("*/30 * * * *", async () => {
- try {
- await this.updateOrganizationsMonthlyUsage();
- } catch (error) {
- this.logger.error(error as Error, "Error during usage check");
- }
- });
+ this.usageCheckTask = cron.schedule(
+ "*/30 * * * *",
+ async () => {
+ try {
+ await this.updateOrganizationsMonthlyUsage();
+ } catch (error) {
+ this.logger.error(error as Error, "Error during usage check");
+ }
+ },
+ { timezone: "UTC" }
+ );
// Run immediately on startup
this.updateOrganizationsMonthlyUsage();
diff --git a/server/src/services/weekyReports/weeklyReportService.ts b/server/src/services/weekyReports/weeklyReportService.ts
new file mode 100644
index 000000000..7aaac12fc
--- /dev/null
+++ b/server/src/services/weekyReports/weeklyReportService.ts
@@ -0,0 +1,460 @@
+import * as cron from "node-cron";
+import { DateTime } from "luxon";
+import { eq } from "drizzle-orm";
+import { db } from "../../db/postgres/postgres.js";
+import { organization, member, user, sites } from "../../db/postgres/schema.js";
+import { clickhouse } from "../../db/clickhouse/clickhouse.js";
+import { processResults } from "../../api/analytics/utils.js";
+import { createServiceLogger } from "../../lib/logger/logger.js";
+import { sendWeeklyReportEmail } from "../../lib/email/email.js";
+import { IS_CLOUD } from "../../lib/const.js";
+import type { OverviewData, SingleColData, SiteReport, OrganizationReport } from "./weeklyReportTypes.js";
+
+class WeeklyReportService {
+ private cronTask: cron.ScheduledTask | null = null;
+ private logger = createServiceLogger("weekly-report");
+
+ constructor() {}
+
+ private async fetchOverviewData(siteId: number, startDate: string, endDate: string): Promise<OverviewData | null> {
+ try {
+ const query = `SELECT
+ session_stats.sessions,
+ session_stats.pages_per_session,
+ session_stats.bounce_rate * 100 AS bounce_rate,
+ session_stats.session_duration,
+ page_stats.pageviews,
+ page_stats.users
+ FROM
+ (
+ -- Session-level metrics
+ SELECT
+ COUNT() AS sessions,
+ AVG(pages_in_session) AS pages_per_session,
+ sumIf(1, pages_in_session = 1) / COUNT() AS bounce_rate,
+ AVG(end_time - start_time) AS session_duration
+ FROM
+ (
+ -- One row per session
+ SELECT
+ session_id,
+ MIN(timestamp) AS start_time,
+ MAX(timestamp) AS end_time,
+ COUNT(CASE WHEN type = 'pageview' THEN 1 END) AS pages_in_session
+ FROM events
+ WHERE
+ site_id = {siteId:Int32}
+ AND timestamp >= toDateTime({startDate:String})
+ AND timestamp < toDateTime({endDate:String})
+ GROUP BY session_id
+ )
+ ) AS session_stats
+ CROSS JOIN
+ (
+ -- Page-level and user-level metrics
+ SELECT
+ COUNT(*) AS pageviews,
+ COUNT(DISTINCT user_id) AS users
+ FROM events
+ WHERE
+ site_id = {siteId:Int32}
+ AND timestamp >= toDateTime({startDate:String})
+ AND timestamp < toDateTime({endDate:String})
+ AND type = 'pageview'
+ ) AS page_stats`;
+
+ const result = await clickhouse.query({
+ query,
+ format: "JSONEachRow",
+ query_params: {
+ siteId,
+ startDate,
+ endDate,
+ },
+ });
+
+ const data = await processResults<OverviewData>(result);
+ return data[0] || null;
+ } catch (error) {
+ this.logger.error({ error, siteId }, "Error fetching overview data");
+ return null;
+ }
+ }
+
+ private async fetchTopN(
+ siteId: number,
+ parameter: string,
+ startDate: string,
+ endDate: string,
+ limit: number = 5
+ ): Promise<SingleColData[]> {
+ try {
+ let query = "";
+
+ if (parameter === "country") {
+ query = `
+ WITH PageStats AS (
+ SELECT
+ country as value,
+ COUNT(distinct(session_id)) as unique_sessions,
+ COUNT() as pageviews
+ FROM events
+ WHERE
+ site_id = {siteId:Int32}
+ AND country IS NOT NULL
+ AND country <> ''
+ AND timestamp >= toDateTime({startDate:String})
+ AND timestamp < toDateTime({endDate:String})
+ GROUP BY value
+ )
+ SELECT
+ value,
+ unique_sessions as count,
+ round((unique_sessions / sum(unique_sessions) OVER ()) * 100, 2) as percentage
+ FROM PageStats
+ ORDER BY count desc
+ LIMIT {limit:Int32}`;
+ } else if (parameter === "pathname") {
+ query = `
+ WITH EventTimes AS (
+ SELECT
+ session_id,
+ pathname,
+ timestamp,
+ leadInFrame(timestamp) OVER (PARTITION BY session_id ORDER BY timestamp ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) as next_timestamp
+ FROM events
+ WHERE
+ site_id = {siteId:Int32}
+ AND type = 'pageview'
+ AND timestamp >= toDateTime({startDate:String})
+ AND timestamp < toDateTime({endDate:String})
+ ),
+ PageDurations AS (
+ SELECT
+ session_id,
+ pathname,
+ timestamp,
+ next_timestamp,
+ if(isNull(next_timestamp), 0, dateDiff('second', timestamp, next_timestamp)) as time_diff_seconds
+ FROM EventTimes
+ ),
+ PathStats AS (
+ SELECT
+ pathname,
+ count() as visits,
+ count(DISTINCT session_id) as unique_sessions
+ FROM PageDurations
+ GROUP BY pathname
+ )
+ SELECT
+ pathname as value,
+ unique_sessions as count,
+ round((unique_sessions / sum(unique_sessions) OVER ()) * 100, 2) as percentage
+ FROM PathStats
+ ORDER BY unique_sessions DESC
+ LIMIT {limit:Int32}`;
+ } else if (parameter === "referrer") {
+ query = `
+ WITH PageStats AS (
+ SELECT
+ domainWithoutWWW(referrer) as value,
+ COUNT(distinct(session_id)) as unique_sessions,
+ COUNT() as pageviews
+ FROM events
+ WHERE
+ site_id = {siteId:Int32}
+ AND domainWithoutWWW(referrer) IS NOT NULL
+ AND domainWithoutWWW(referrer) <> ''
+ AND timestamp >= toDateTime({startDate:String})
+ AND timestamp < toDateTime({endDate:String})
+ GROUP BY value
+ )
+ SELECT
+ value,
+ unique_sessions as count,
+ round((unique_sessions / sum(unique_sessions) OVER ()) * 100, 2) as percentage
+ FROM PageStats
+ ORDER BY count desc
+ LIMIT {limit:Int32}`;
+ } else if (parameter === "device_type") {
+ query = `
+ WITH PageStats AS (
+ SELECT
+ device_type as value,
+ COUNT(distinct(session_id)) as unique_sessions,
+ COUNT() as pageviews
+ FROM events
+ WHERE
+ site_id = {siteId:Int32}
+ AND device_type IS NOT NULL
+ AND device_type <> ''
+ AND timestamp >= toDateTime({startDate:String})
+ AND timestamp < toDateTime({endDate:String})
+ GROUP BY value
+ )
+ SELECT
+ value,
+ unique_sessions as count,
+ round((unique_sessions / sum(unique_sessions) OVER ()) * 100, 2) as percentage
+ FROM PageStats
+ ORDER BY count desc
+ LIMIT {limit:Int32}`;
+ }
+
+ const result = await clickhouse.query({
+ query,
+ format: "JSONEachRow",
+ query_params: {
+ siteId,
+ startDate,
+ endDate,
+ limit,
+ },
+ });
+
+ return await processResults<SingleColData>(result);
+ } catch (error) {
+ this.logger.error({ error, siteId, parameter }, "Error fetching top N data");
+ return [];
+ }
+ }
+
+ private async generateSiteReport(siteId: number, siteName: string, siteDomain: string): Promise<SiteReport | null> {
+ try {
+ // Use UTC timezone for consistency
+ const now = DateTime.utc();
+
+ // Calculate current week (last 7 days)
+ const currentWeekEnd = now;
+ const currentWeekStart = now.minus({ days: 7 });
+
+ // Calculate previous week (8-14 days ago)
+ const previousWeekEnd = currentWeekStart;
+ const previousWeekStart = currentWeekStart.minus({ days: 7 });
+
+ // Format dates for ClickHouse (YYYY-MM-DD HH:mm:ss)
+ const formatDate = (date: DateTime) => date.toFormat("yyyy-MM-dd HH:mm:ss");
+
+ const [currentWeek, previousWeek, topCountries, topPages, topReferrers, deviceBreakdown] = await Promise.all([
+ this.fetchOverviewData(siteId, formatDate(currentWeekStart), formatDate(currentWeekEnd)),
+ this.fetchOverviewData(siteId, formatDate(previousWeekStart), formatDate(previousWeekEnd)),
+ this.fetchTopN(siteId, "country", formatDate(currentWeekStart), formatDate(currentWeekEnd), 5),
+ this.fetchTopN(siteId, "pathname", formatDate(currentWeekStart), formatDate(currentWeekEnd), 5),
+ this.fetchTopN(siteId, "referrer", formatDate(currentWeekStart), formatDate(currentWeekEnd), 5),
+ this.fetchTopN(siteId, "device_type", formatDate(currentWeekStart), formatDate(currentWeekEnd), 5),
+ ]);
+
+ if (!currentWeek) {
+ return null;
+ }
+
+ // Skip sites with no pageviews
+ if (!currentWeek.pageviews || currentWeek.pageviews === 0) {
+ return null;
+ }
+
+ return {
+ siteId,
+ siteName,
+ siteDomain,
+ currentWeek,
+ previousWeek: previousWeek || {
+ sessions: 0,
+ pageviews: 0,
+ users: 0,
+ pages_per_session: 0,
+ bounce_rate: 0,
+ session_duration: 0,
+ },
+ topCountries,
+ topPages,
+ topReferrers,
+ deviceBreakdown,
+ };
+ } catch (error) {
+ this.logger.error({ error, siteId }, "Error generating site report");
+ return null;
+ }
+ }
+
+ private async generateOrganizationReport(organizationId: string): Promise<OrganizationReport | null> {
+ try {
+ // Fetch organization details
+ const [org] = await db.select().from(organization).where(eq(organization.id, organizationId));
+
+ if (!org) {
+ return null;
+ }
+
+ // Fetch all sites for this organization
+ const orgSites = await db.select().from(sites).where(eq(sites.organizationId, org.id));
+
+ if (orgSites.length === 0) {
+ return null;
+ }
+
+ const siteReports: SiteReport[] = [];
+
+ for (const site of orgSites) {
+ const report = await this.generateSiteReport(site.siteId, site.name, site.domain);
+ if (report) {
+ siteReports.push(report);
+ }
+ }
+
+ if (siteReports.length === 0) {
+ return null;
+ }
+
+ return {
+ organizationId: org.id,
+ organizationName: org.name,
+ sites: siteReports,
+ };
+ } catch (error) {
+ this.logger.error({ error, organizationId }, "Error generating organization report");
+ return null;
+ }
+ }
+
+ private async sendReportsToOrganization(report: OrganizationReport): Promise<void> {
+ try {
+ // Fetch all members of the organization
+ const members = await db
+ .select({
+ userId: member.userId,
+ email: user.email,
+ name: user.name,
+ sendAutoEmailReports: user.sendAutoEmailReports,
+ })
+ .from(member)
+ .innerJoin(user, eq(member.userId, user.id))
+ .where(eq(member.organizationId, report.organizationId));
+
+ // Send a separate email for each site to each member
+ for (const memberData of members) {
+ // Skip users who have disabled email reports
+ if (memberData.sendAutoEmailReports === false) {
+ continue;
+ }
+
+ for (const site of report.sites) {
+ try {
+ // Create a report with just this single site
+ const singleSiteReport: OrganizationReport = {
+ organizationId: report.organizationId,
+ organizationName: report.organizationName,
+ sites: [site],
+ };
+
+ await sendWeeklyReportEmail(memberData.email, memberData.name, singleSiteReport);
+ this.logger.info(
+ {
+ email: memberData.email,
+ organizationId: report.organizationId,
+ siteId: site.siteId,
+ siteName: site.siteName,
+ },
+ "Sent weekly report email for site"
+ );
+ } catch (error) {
+ this.logger.error(
+ { error, email: memberData.email, organizationId: report.organizationId, siteId: site.siteId },
+ "Failed to send email to member for site"
+ );
+ }
+ }
+ }
+ } catch (error) {
+ this.logger.error({ error, organizationId: report.organizationId }, "Error sending reports to organization");
+ }
+ }
+
+ public async generateAndSendReports(): Promise<void> {
+ if (!IS_CLOUD) {
+ this.logger.info("Skipping weekly reports for non-cloud instance");
+ return;
+ }
+
+ this.logger.info("Starting weekly report generation and sending");
+
+ try {
+ // Fetch all organizations
+ const organizations = await db.select().from(organization);
+ const totalOrgs = organizations.length;
+
+ this.logger.info({ totalOrganizations: totalOrgs }, "Processing organizations");
+
+ let processedCount = 0;
+ let sentCount = 0;
+
+ for (let i = 0; i < organizations.length; i++) {
+ const org = organizations[i];
+
+ // Generate report for this organization
+ const report = await this.generateOrganizationReport(org.id);
+
+ if (report) {
+ // Send reports immediately after generation
+ await this.sendReportsToOrganization(report);
+ sentCount++;
+ }
+
+ processedCount++;
+
+ // Log progress every 10 organizations
+ if (processedCount % 10 === 0 || processedCount === totalOrgs) {
+ this.logger.info(
+ { processed: processedCount, total: totalOrgs, sent: sentCount },
+ `Progress: ${processedCount}/${totalOrgs} organizations processed, ${sentCount} reports sent`
+ );
+ }
+ }
+
+ this.logger.info(
+ { totalProcessed: processedCount, totalSent: sentCount },
+ "Completed weekly report generation and sending"
+ );
+ } catch (error) {
+ this.logger.error({ error }, "Error in weekly report generation");
+ }
+ }
+
+ private initializeWeeklyReportCron(): void {
+ if (!IS_CLOUD) {
+ this.logger.info("Skipping weekly report cron initialization for non-cloud instance");
+ return;
+ }
+
+ this.logger.info("Initializing weekly report cron");
+
+ // Schedule weekly reports to run every Monday at midnight UTC
+ this.cronTask = cron.schedule(
+ "0 0 * * 1",
+ async () => {
+ try {
+ await this.generateAndSendReports();
+ } catch (error) {
+ this.logger.error(error as Error, "Error during weekly report generation");
+ }
+ },
+ { timezone: "UTC" }
+ );
+
+ this.logger.info("Weekly report cron initialized (runs every Monday at midnight UTC)");
+ }
+
+ public stopWeeklyReportCron(): void {
+ if (this.cronTask) {
+ this.cronTask.stop();
+ this.logger.info("Weekly report cron stopped");
+ }
+ }
+
+ public startWeeklyReportCron(): void {
+ this.initializeWeeklyReportCron();
+ }
+}
+
+// Create a singleton instance
+export const weeklyReportService = new WeeklyReportService();
diff --git a/server/src/services/weekyReports/weeklyReportTypes.ts b/server/src/services/weekyReports/weeklyReportTypes.ts
new file mode 100644
index 000000000..a83c7e288
--- /dev/null
+++ b/server/src/services/weekyReports/weeklyReportTypes.ts
@@ -0,0 +1,32 @@
+export interface OverviewData {
+ sessions: number;
+ pageviews: number;
+ users: number;
+ pages_per_session: number | null;
+ bounce_rate: number | null;
+ session_duration: number;
+}
+
+export interface SingleColData {
+ value: string;
+ count: number;
+ percentage: number | null;
+}
+
+export interface SiteReport {
+ siteId: number;
+ siteName: string;
+ siteDomain: string;
+ currentWeek: OverviewData;
+ previousWeek: OverviewData;
+ topCountries: SingleColData[];
+ topPages: SingleColData[];
+ topReferrers: SingleColData[];
+ deviceBreakdown: SingleColData[];
+}
+
+export interface OrganizationReport {
+ organizationId: string;
+ organizationName: string;
+ sites: SiteReport[];
+}