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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | 29x 2x 2x 2x 2x 2x 2x 2x 6x 6x 6x | // Data Access Layer for Blog Analytics
import { eq, desc, and, gte, lte } from "drizzle-orm";
import type { DrizzleD1Database } from "drizzle-orm/d1";
import * as schema from "../db/schema";
import type {
AnalyticsBlogDaily,
NewAnalyticsBlogDaily,
AnalyticsBlogSummary,
NewAnalyticsBlogSummary,
} from "../db/schema";
export class BlogAnalyticsDal {
constructor(private db: DrizzleD1Database<typeof schema>) {}
/**
* Get blog summary analytics (totals and rolling windows)
*/
async getBlogSummary(
blogId: string,
): Promise<AnalyticsBlogSummary | undefined> {
const result = await this.db
.select()
.from(schema.analyticsBlogSummary)
.where(eq(schema.analyticsBlogSummary.blogId, blogId))
.limit(1);
return result[0];
}
/**
* Upsert blog summary analytics
*/
async upsertBlogSummary(
data: NewAnalyticsBlogSummary,
): Promise<AnalyticsBlogSummary> {
const result = await this.db
.insert(schema.analyticsBlogSummary)
.values(data)
.onConflictDoUpdate({
target: schema.analyticsBlogSummary.blogId,
set: {
...data,
updatedAt: new Date(),
},
})
.returning();
return result[0];
}
/**
* Get daily analytics for a blog within a date range
*/
async getBlogDailyStats(
blogId: string,
startDate: string,
endDate: string,
): Promise<AnalyticsBlogDaily[]> {
return this.db
.select()
.from(schema.analyticsBlogDaily)
.where(
and(
eq(schema.analyticsBlogDaily.blogId, blogId),
gte(schema.analyticsBlogDaily.date, startDate),
lte(schema.analyticsBlogDaily.date, endDate),
),
)
.orderBy(schema.analyticsBlogDaily.date);
}
/**
* Upsert blog daily analytics
*/
async upsertBlogDailyStats(
data: NewAnalyticsBlogDaily,
): Promise<AnalyticsBlogDaily> {
const result = await this.db
.insert(schema.analyticsBlogDaily)
.values(data)
.onConflictDoUpdate({
target: [
schema.analyticsBlogDaily.blogId,
schema.analyticsBlogDaily.date,
],
set: {
pageViews: data.pageViews,
proClicks: data.proClicks,
projectClicks: data.projectClicks,
uniqueSessions: data.uniqueSessions,
sources: data.sources,
devices: data.devices,
cities: data.cities,
},
})
.returning();
return result[0];
}
/**
* Get top performing blogs by views or clicks
*/
async getTopPerformingBlogs(
limit = 10,
metric: "views" | "clicks" = "views",
): Promise<
Array<{
blogId: string;
title: string;
slug: string;
totalPageViews: number;
totalProClicks: number;
totalProjectClicks: number;
views30d: number;
clicks30d: number;
}>
> {
const orderByColumn =
metric === "views"
? schema.analyticsBlogSummary.views30d
: schema.analyticsBlogSummary.clicks30d;
const result = await this.db
.select({
blogId: schema.analyticsBlogSummary.blogId,
title: schema.blogs.title,
slug: schema.blogs.slug,
totalPageViews: schema.analyticsBlogSummary.totalPageViews,
totalProClicks: schema.analyticsBlogSummary.totalProClicks,
totalProjectClicks: schema.analyticsBlogSummary.totalProjectClicks,
views30d: schema.analyticsBlogSummary.views30d,
clicks30d: schema.analyticsBlogSummary.clicks30d,
})
.from(schema.analyticsBlogSummary)
.innerJoin(
schema.blogs,
eq(schema.analyticsBlogSummary.blogId, schema.blogs.id),
)
.where(eq(schema.blogs.status, "published"))
.orderBy(desc(orderByColumn))
.limit(limit);
return result;
}
}
|