All files / dal analytics.dal.ts

100% Statements 10/10
100% Branches 1/1
100% Functions 7/7
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114            15x                   1x         1x             1x                                 1x           1x             2x                                                   1x                                 1x                 1x      
// Data Access Layer for Analytics
import { eq, and, gte, lte, desc, sql } from "drizzle-orm";
import type { DrizzleD1Database } from "drizzle-orm/d1";
import * as schema from "../db/schema";
 
export class AnalyticsDal {
	constructor(private db: DrizzleD1Database<typeof schema>) {}
 
	// ============================================================================
	// Pro Analytics
	// ============================================================================
 
	/**
	 * Get pro summary analytics (totals and rolling windows)
	 */
	async getProSummary(proId: string) {
		const result = await this.db
			.select()
			.from(schema.analyticsProSummary)
			.where(eq(schema.analyticsProSummary.proId, proId))
			.limit(1);
		return result[0];
	}
 
	/**
	 * Get daily analytics for a pro within a date range
	 */
	async getProDaily(proId: string, startDate: string, endDate: string) {
		return this.db
			.select()
			.from(schema.analyticsProDaily)
			.where(
				and(
					eq(schema.analyticsProDaily.proId, proId),
					gte(schema.analyticsProDaily.date, startDate),
					lte(schema.analyticsProDaily.date, endDate),
				),
			)
			.orderBy(schema.analyticsProDaily.date);
	}
 
	/**
	 * Get latest daily record for a pro (for sources, devices, cities breakdown)
	 */
	async getProLatestDaily(proId: string) {
		const result = await this.db
			.select()
			.from(schema.analyticsProDaily)
			.where(eq(schema.analyticsProDaily.proId, proId))
			.orderBy(desc(schema.analyticsProDaily.date))
			.limit(1);
		return result[0];
	}
 
	/**
	 * Get top projects by views for a pro
	 */
	async getTopProjects(proId: string, limit = 5) {
		return this.db
			.select({
				id: schema.projects.id,
				title: schema.projects.title,
				slug: schema.projects.slug,
				viewCount: schema.projects.viewCount,
			})
			.from(schema.projects)
			.where(
				and(
					eq(schema.projects.proId, proId),
					eq(schema.projects.status, "published"),
				),
			)
			.orderBy(desc(schema.projects.viewCount))
			.limit(limit);
	}
 
	// ============================================================================
	// Project Analytics
	// ============================================================================
 
	/**
	 * Get daily analytics for a project within a date range
	 */
	async getProjectDaily(projectId: string, startDate: string, endDate: string) {
		return this.db
			.select()
			.from(schema.analyticsProjectDaily)
			.where(
				and(
					eq(schema.analyticsProjectDaily.projectId, projectId),
					gte(schema.analyticsProjectDaily.date, startDate),
					lte(schema.analyticsProjectDaily.date, endDate),
				),
			)
			.orderBy(schema.analyticsProjectDaily.date);
	}
 
	/**
	 * Get aggregated totals for a project
	 */
	async getProjectTotals(projectId: string) {
		const result = await this.db
			.select({
				totalPageViews: sql<number>`COALESCE(SUM(${schema.analyticsProjectDaily.pageViews}), 0)`,
				totalImageClicks: sql<number>`COALESCE(SUM(${schema.analyticsProjectDaily.imageClicks}), 0)`,
				totalUniqueSessions: sql<number>`COALESCE(SUM(${schema.analyticsProjectDaily.uniqueSessions}), 0)`,
			})
			.from(schema.analyticsProjectDaily)
			.where(eq(schema.analyticsProjectDaily.projectId, projectId));
 
		return result[0];
	}
}