@jant/core 1.0.1 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/lib/site-snapshot.js +10 -0
- package/dist/{app-Gt7_skt6.js → app-CbOZ2Irh.js} +8704 -5180
- package/dist/client/.vite/manifest.json +125 -85
- package/dist/client/_assets/chunks/{confirm-D7GrwsCN.js → confirm-CMe01lne.js} +1 -1
- package/dist/client/_assets/chunks/{create-editor-5Y49x7h5.js → create-editor-hWapNE40.js} +26 -26
- package/dist/client/_assets/chunks/footnote-rail-9zwl6jtV.js +2 -0
- package/dist/client/_assets/chunks/{jant-locale-picker-DL6mIOT5.js → jant-locale-picker-2L4rkDaz.js} +1 -1
- package/dist/client/_assets/chunks/lit-Bw_voPoW.js +2 -0
- package/dist/client/_assets/chunks/preload-helper-Czpn1I53.js +1 -0
- package/dist/client/_assets/chunks/runtime-paths-y0_gHowM.js +1 -0
- package/dist/client/_assets/chunks/{settings-paths-h6RjIe3C.js → settings-paths-BBVT8zKj.js} +1 -1
- package/dist/client/_assets/chunks/slug-format-BuCPjSrB.js +1 -0
- package/dist/client/_assets/chunks/sortable-list-DJXwkaOv.js +2 -0
- package/dist/client/_assets/chunks/types-DrYFqJH2.js +1 -0
- package/dist/client/_assets/chunks/{unsafe-html-B5ZEXaxg.js → unsafe-html-CWos3v6N.js} +1 -1
- package/dist/client/_assets/chunks/unsafe-svg-CUrgo4Xg.js +1 -0
- package/dist/client/_assets/client-BO8GOFfT.js +354 -0
- package/dist/client/_assets/client-D8Rrsd73.css +2 -0
- package/dist/client/_assets/client-auth-CsUGM718.js +859 -0
- package/dist/client/_assets/client-author-QPm1whbO.css +2 -0
- package/dist/client/_assets/client-compose-BB4U7Wz0.js +2004 -0
- package/dist/client/_assets/{client-manage-C1eDB1Rc.js → client-manage-CI9-eh-8.js} +176 -176
- package/dist/client/_assets/{client-settings-DIM44Zqh.js → client-settings-BnGPuqwc.js} +166 -166
- package/dist/client/_assets/client-share-0DICJjAc.css +1 -0
- package/dist/client/_assets/client-share-C59mOLJ1.js +149 -0
- package/dist/index.js +1 -1
- package/dist/node.js +3 -2
- package/package.json +2 -1
- package/src/app.tsx +9 -0
- package/src/client/components/compose-types.ts +1 -0
- package/src/client/components/jant-compose-dialog.ts +1 -0
- package/src/client/components/jant-compose-editor.ts +30 -0
- package/src/client/components/jant-compose-fullscreen.ts +1 -0
- package/src/client/components/jant-post-menu.ts +80 -13
- package/src/client/components/jant-share-dialog.ts +422 -0
- package/src/client/components/jant-share-menu.ts +188 -0
- package/src/client/compose-bridge.ts +61 -1
- package/src/client/feed-video-player.ts +6 -0
- package/src/client/lazy-entries.ts +21 -1
- package/src/client/post-menu-shared.ts +100 -0
- package/src/client/share-capture.ts +443 -0
- package/src/client/share-launch.ts +133 -0
- package/src/client/tiptap/bubble-menu.ts +25 -2
- package/src/client/tiptap/create-editor.ts +2 -0
- package/src/client/tiptap/extensions.ts +4 -0
- package/src/client/video-processor.ts +106 -52
- package/src/client/view-confirm.ts +108 -0
- package/src/client-share.ts +8 -0
- package/src/client.ts +2 -0
- package/src/db/migrations/0035_brainy_next_avengers.sql +9 -0
- package/src/db/migrations/0036_gorgeous_smasher.sql +7 -0
- package/src/db/migrations/meta/0035_snapshot.json +2877 -0
- package/src/db/migrations/meta/0036_snapshot.json +2924 -0
- package/src/db/migrations/meta/_journal.json +14 -0
- package/src/db/migrations/pg/0033_cute_magdalene.sql +10 -0
- package/src/db/migrations/pg/0034_material_agent_brand.sql +8 -0
- package/src/db/migrations/pg/meta/0033_snapshot.json +3719 -0
- package/src/db/migrations/pg/meta/0034_snapshot.json +3766 -0
- package/src/db/migrations/pg/meta/_journal.json +14 -0
- package/src/db/pg/schema.ts +40 -0
- package/src/db/schema.ts +61 -0
- package/src/i18n/locales/public/en.po +5 -0
- package/src/i18n/locales/public/en.ts +1 -1
- package/src/i18n/locales/public/zh-Hans.po +5 -0
- package/src/i18n/locales/public/zh-Hans.ts +1 -1
- package/src/i18n/locales/public/zh-Hant.po +5 -0
- package/src/i18n/locales/public/zh-Hant.ts +1 -1
- package/src/i18n/locales/settings/en.po +149 -0
- package/src/i18n/locales/settings/en.ts +1 -1
- package/src/i18n/locales/settings/zh-Hans.po +149 -0
- package/src/i18n/locales/settings/zh-Hans.ts +1 -1
- package/src/i18n/locales/settings/zh-Hant.po +149 -0
- package/src/i18n/locales/settings/zh-Hant.ts +1 -1
- package/src/lib/discover.ts +31 -0
- package/src/lib/env.ts +20 -0
- package/src/lib/github-app-state.ts +1 -1
- package/src/lib/github-app.ts +183 -1
- package/src/lib/markdown-manager.ts +129 -1
- package/src/lib/post-meta.ts +13 -3
- package/src/lib/referrer-aliases.ts +311 -0
- package/src/lib/render.tsx +10 -0
- package/src/lib/resolve-config.ts +14 -0
- package/src/lib/share-card.ts +653 -0
- package/src/lib/summary.ts +152 -54
- package/src/lib/tiptap-render.ts +1 -0
- package/src/lib/upload.ts +21 -1
- package/src/lib/version.ts +2 -0
- package/src/lib/video-playback.ts +55 -0
- package/src/lib/view-buffer.ts +201 -0
- package/src/lib/view-counting.ts +539 -0
- package/src/lib/view-token.ts +177 -0
- package/src/lib/view.ts +13 -1
- package/src/middleware/view-counter.ts +140 -0
- package/src/node/runtime.ts +4 -0
- package/src/preset.css +31 -14
- package/src/routes/api/media.ts +21 -3
- package/src/routes/auth/setup.tsx +37 -1
- package/src/routes/dash/settings.tsx +433 -168
- package/src/routes/pages/archive.tsx +1 -0
- package/src/routes/pages/collection.tsx +6 -0
- package/src/routes/pages/collections.tsx +1 -0
- package/src/routes/pages/featured.tsx +1 -0
- package/src/routes/pages/home.tsx +1 -0
- package/src/routes/pages/page.tsx +3 -0
- package/src/routes/pages/partials.tsx +23 -0
- package/src/routes/pages/smart-collection.tsx +2 -0
- package/src/routes/pages/subscribe.tsx +1 -0
- package/src/routes/view-confirm.ts +63 -0
- package/src/services/export-theme/assets/client-site.js +1 -1
- package/src/services/export-theme/styles/main.css +39 -21
- package/src/services/export.ts +2 -0
- package/src/services/index.ts +17 -0
- package/src/services/mcp.ts +31 -1
- package/src/services/media.ts +63 -0
- package/src/services/stats.ts +1130 -0
- package/src/style-share.css +16 -0
- package/src/styles/components-author.css +177 -0
- package/src/styles/components.css +25 -3
- package/src/styles/share.css +1103 -0
- package/src/styles/tokens.css +65 -30
- package/src/styles/ui.css +62 -13
- package/src/types/app-context.ts +9 -0
- package/src/types/bindings.ts +5 -0
- package/src/types/config.ts +54 -0
- package/src/types/views.ts +6 -0
- package/src/ui/compose/ComposeDialog.tsx +6 -0
- package/src/ui/dash/settings/SettingsRootContent.tsx +24 -6
- package/src/ui/dash/settings/StatsContent.tsx +855 -0
- package/src/ui/font-themes.ts +39 -6
- package/src/ui/layouts/BaseLayout.tsx +11 -2
- package/src/ui/layouts/SiteLayout.tsx +9 -3
- package/src/ui/share/ShareCard.tsx +298 -0
- package/src/ui/shared/Icon.tsx +23 -3
- package/src/ui/shared/MediaGallery.tsx +31 -4
- package/src/ui/shared/PostFooter.tsx +20 -4
- package/src/ui/shared/icon-collector.ts +28 -0
- package/src/ui/shared/post-article-attributes.ts +3 -0
- package/dist/client/_assets/chunks/footnote-rail-BJwswtVQ.js +0 -1
- package/dist/client/_assets/chunks/lit-DW8VmJAT.js +0 -2
- package/dist/client/_assets/chunks/slug-format-Dsi0e9Xn.js +0 -1
- package/dist/client/_assets/chunks/sortable-list-DWNKv782.js +0 -2
- package/dist/client/_assets/chunks/types-D5iF3H1a.js +0 -1
- package/dist/client/_assets/chunks/unsafe-svg-DTZZEvLU.js +0 -2
- package/dist/client/_assets/client-BaqmpPQ-.css +0 -2
- package/dist/client/_assets/client-C6melOtG.js +0 -296
- package/dist/client/_assets/client-auth-Ccq8kSqq.js +0 -828
- package/dist/client/_assets/client-author-CGVvrNZ8.css +0 -2
- package/dist/client/_assets/client-compose-oxzPb3SS.js +0 -2004
|
@@ -0,0 +1,1130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* View stats: what is kept, how it is written, and what the Stats page reads.
|
|
3
|
+
*
|
|
4
|
+
* Jant counts page views so an author can see, on one quiet page, which days
|
|
5
|
+
* and which pages were read and where readers came from — and nothing else.
|
|
6
|
+
* No reader is identified: there are no uniques, no countries, no devices,
|
|
7
|
+
* and no record of any visit. What exists is one counter per site, day, page
|
|
8
|
+
* and referring host (`view_count`), and this file is the whole policy for
|
|
9
|
+
* that table.
|
|
10
|
+
*
|
|
11
|
+
* - **A row is a counter.** Ten thousand readers opening one post from one
|
|
12
|
+
* link are one row. The table grows with the variety of a day's traffic,
|
|
13
|
+
* never its volume.
|
|
14
|
+
* - **A key names a page.** A post, a collection or a smart collection is
|
|
15
|
+
* counted by id, so its count survives a change of address; the home page,
|
|
16
|
+
* the archive and an aggregate selection are counted by path. The key's
|
|
17
|
+
* shape says which (`viewPageKind`), and the list is named from it when
|
|
18
|
+
* read, never stored.
|
|
19
|
+
* - **Thirty days.** Rows older than `VIEW_COUNT_RETENTION_DAYS` are deleted,
|
|
20
|
+
* once a day per site, by whichever flush comes first. Nothing is rolled up:
|
|
21
|
+
* a lifetime total is exactly the scoreboard this feature is not.
|
|
22
|
+
* - **Hours for today alone.** `view_hour` keeps one total per site and hour,
|
|
23
|
+
* no page and no referrer, so today's chart can show when readers came;
|
|
24
|
+
* today's lists still come from the day table. Two days of hours are kept.
|
|
25
|
+
* - **Where a reader came from, in three kinds.** A host or a tag, the site
|
|
26
|
+
* itself (`VIEW_INTERNAL_REFERRER`: a reader moving from one page to the
|
|
27
|
+
* next), or nothing at all: a typed address, a bookmark, an app, a feed
|
|
28
|
+
* reader. Direct means the last of these only.
|
|
29
|
+
* - **Bounded breakdown.** A site-day holds at most
|
|
30
|
+
* `VIEW_COUNT_ROWS_PER_SITE_DAY` rows; past that, new page-referrer pairs
|
|
31
|
+
* land on one overflow row, so a script spraying made-up referrers cannot
|
|
32
|
+
* grow the table. Totals stay exact, the breakdown stops.
|
|
33
|
+
* - **Bounded writes.** On Cloudflare, free-tier D1 allows 100,000 rows
|
|
34
|
+
* written per UTC day and refuses every query once that is spent, so a
|
|
35
|
+
* well-read day must never be able to take the site down. Writes arrive in
|
|
36
|
+
* batches from `lib/view-buffer.ts`, and once a day's recorded views reach
|
|
37
|
+
* the instance's `STATS_DAILY_LIMIT` the buffer stops until the next day.
|
|
38
|
+
* - **A record with a start.** The day counting began is kept in the
|
|
39
|
+
* `STATS_SINCE` setting, written by whichever comes first: the switch
|
|
40
|
+
* being turned on, the first look at the page, or the first flush of the
|
|
41
|
+
* site's counts. The page says "since Nov 9" and draws nothing for the
|
|
42
|
+
* days before, instead of showing a launch week as a month of zeros.
|
|
43
|
+
* Turning stats off deletes it with the counts.
|
|
44
|
+
*
|
|
45
|
+
* `record()` is the one write path. It takes entries for any site, because a
|
|
46
|
+
* batch holds whatever the process saw in its last few seconds; the reads are
|
|
47
|
+
* scoped to the service's site like every other service.
|
|
48
|
+
*/
|
|
49
|
+
|
|
50
|
+
import {
|
|
51
|
+
and,
|
|
52
|
+
asc,
|
|
53
|
+
desc,
|
|
54
|
+
eq,
|
|
55
|
+
gte,
|
|
56
|
+
inArray,
|
|
57
|
+
lt,
|
|
58
|
+
lte,
|
|
59
|
+
ne,
|
|
60
|
+
sql,
|
|
61
|
+
} from "drizzle-orm";
|
|
62
|
+
import { supportsDrizzleTransaction, type Database } from "../db/index.js";
|
|
63
|
+
import type { DatabaseDialect } from "../db/dialect.js";
|
|
64
|
+
import {
|
|
65
|
+
sqliteSchemaBundle,
|
|
66
|
+
type DatabaseSchema,
|
|
67
|
+
} from "../db/schema-bundle.js";
|
|
68
|
+
import { getPostDisplayTitle } from "../lib/post-meta.js";
|
|
69
|
+
import { now } from "../lib/time.js";
|
|
70
|
+
import type { ViewCountEntry, ViewRecordResult } from "../lib/view-buffer.js";
|
|
71
|
+
import {
|
|
72
|
+
dayKeysEndingAt,
|
|
73
|
+
hourKeysOfDay,
|
|
74
|
+
isDayKey,
|
|
75
|
+
shiftDayKey,
|
|
76
|
+
VIEW_INTERNAL_REFERRER,
|
|
77
|
+
VIEW_OVERFLOW_KEY,
|
|
78
|
+
viewPageKind,
|
|
79
|
+
} from "../lib/view-counting.js";
|
|
80
|
+
import type { Format } from "../types/constants.js";
|
|
81
|
+
import type { SettingsService } from "./settings.js";
|
|
82
|
+
|
|
83
|
+
/** Days a counter row is kept before the sweep deletes it. */
|
|
84
|
+
export const VIEW_COUNT_RETENTION_DAYS = 30;
|
|
85
|
+
|
|
86
|
+
/** Days an hour row is kept: today, and yesterday until today's sweep. */
|
|
87
|
+
export const VIEW_HOUR_RETENTION_DAYS = 2;
|
|
88
|
+
|
|
89
|
+
/** Rows one site may hold for one day before new pairs fold into overflow. */
|
|
90
|
+
export const VIEW_COUNT_ROWS_PER_SITE_DAY = 5_000;
|
|
91
|
+
|
|
92
|
+
/** How many pages and how many referrers the page lists. */
|
|
93
|
+
export const STATS_TOP_LIMIT = 10;
|
|
94
|
+
|
|
95
|
+
/** The windows the Stats page offers, in days; `1` is today, by the hour. */
|
|
96
|
+
export const STATS_WINDOWS = [1, 7, 30] as const;
|
|
97
|
+
export type StatsWindow = (typeof STATS_WINDOWS)[number];
|
|
98
|
+
export const DEFAULT_STATS_WINDOW: StatsWindow = 1;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Rows per upsert statement. Five bound parameters a row keeps a chunk under
|
|
102
|
+
* D1's limit of 100 parameters per statement.
|
|
103
|
+
*/
|
|
104
|
+
const UPSERT_CHUNK_ROWS = 16;
|
|
105
|
+
/** Hour rows per statement: three parameters each. */
|
|
106
|
+
const HOUR_UPSERT_CHUNK_ROWS = 32;
|
|
107
|
+
|
|
108
|
+
/** Keys remembered as already in the table, before the set is emptied. */
|
|
109
|
+
const SEEN_KEYS_LIMIT = 50_000;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* What one process remembers between flushes: which counter keys it has
|
|
113
|
+
* already written, so the row cap is only checked when a batch brings new
|
|
114
|
+
* ones, and which day it last swept each site on.
|
|
115
|
+
*
|
|
116
|
+
* Module state rather than per-request state on purpose: on Workers a new
|
|
117
|
+
* services object is built per request, and this has to survive them.
|
|
118
|
+
*/
|
|
119
|
+
export interface ViewCountWriterState {
|
|
120
|
+
seen: Set<string>;
|
|
121
|
+
sweptOn: Map<string, string>;
|
|
122
|
+
/** Sites whose start day this process has already made sure of. */
|
|
123
|
+
stamped: Set<string>;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* A fresh writer state. Tests construct one per test; production uses the
|
|
128
|
+
* module's own.
|
|
129
|
+
*
|
|
130
|
+
* @returns Empty state
|
|
131
|
+
* @example
|
|
132
|
+
* ```ts
|
|
133
|
+
* const stats = createStatsService(db, siteId, schema, "sqlite", {
|
|
134
|
+
* settings,
|
|
135
|
+
* writerState: createViewCountWriterState(),
|
|
136
|
+
* });
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
export function createViewCountWriterState(): ViewCountWriterState {
|
|
140
|
+
return { seen: new Set(), sweptOn: new Map(), stamped: new Set() };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const sharedWriterState = createViewCountWriterState();
|
|
144
|
+
|
|
145
|
+
/** Views at one point of a series: a day key, or an hour key for today. */
|
|
146
|
+
export interface StatsSeriesPoint {
|
|
147
|
+
at: string;
|
|
148
|
+
views: number;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* The chart's series. Today is drawn by the hour, up to the current one;
|
|
153
|
+
* a longer window by the day, one point per day of the window.
|
|
154
|
+
*/
|
|
155
|
+
export interface StatsSeries {
|
|
156
|
+
unit: "hour" | "day";
|
|
157
|
+
points: StatsSeriesPoint[];
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** A counter row of the day table, as written. */
|
|
161
|
+
interface ViewDayRow {
|
|
162
|
+
siteId: string;
|
|
163
|
+
day: string;
|
|
164
|
+
page: string;
|
|
165
|
+
referrer: string;
|
|
166
|
+
count: number;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** A counter row of the hour table, as written. */
|
|
170
|
+
interface ViewHourRow {
|
|
171
|
+
siteId: string;
|
|
172
|
+
hour: string;
|
|
173
|
+
count: number;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** Views from one referring host. */
|
|
177
|
+
export interface StatsReferrerRow {
|
|
178
|
+
host: string;
|
|
179
|
+
views: number;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* A post among the pages. `post` is `null` once the post is deleted: its
|
|
184
|
+
* rows stay until the sweep, and the list still accounts for them.
|
|
185
|
+
*/
|
|
186
|
+
export interface StatsPostPage {
|
|
187
|
+
kind: "post";
|
|
188
|
+
key: string;
|
|
189
|
+
post: {
|
|
190
|
+
/** The post's name as the rest of Jant writes it; empty when nothing gives one. */
|
|
191
|
+
title: string;
|
|
192
|
+
slug: string;
|
|
193
|
+
format: Format;
|
|
194
|
+
} | null;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** A collection or smart collection among the pages; `null` once deleted. */
|
|
198
|
+
export interface StatsCollectionPage {
|
|
199
|
+
kind: "collection";
|
|
200
|
+
key: string;
|
|
201
|
+
collection: { title: string; slug: string } | null;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** Any other page, by the path it was opened at. */
|
|
205
|
+
export interface StatsPathPage {
|
|
206
|
+
kind: "path";
|
|
207
|
+
key: string;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** One counted page, named. */
|
|
211
|
+
export type StatsPage = StatsPostPage | StatsCollectionPage | StatsPathPage;
|
|
212
|
+
|
|
213
|
+
/** A page in the top list. */
|
|
214
|
+
export type StatsPageRow = StatsPage & { views: number };
|
|
215
|
+
|
|
216
|
+
/** Where views came from, in the shape the page shows. */
|
|
217
|
+
export interface StatsReferrers {
|
|
218
|
+
/** No referrer at all: a typed address, a bookmark, an app, a feed reader. */
|
|
219
|
+
direct: number;
|
|
220
|
+
/** A link from another page of the site. */
|
|
221
|
+
internal: number;
|
|
222
|
+
/** The busiest hosts, most views first. */
|
|
223
|
+
top: StatsReferrerRow[];
|
|
224
|
+
/** Every host past the top list, and the overflow row. */
|
|
225
|
+
other: number;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** The Stats page: the site over a window of days. */
|
|
229
|
+
export interface StatsOverview {
|
|
230
|
+
days: number;
|
|
231
|
+
/** First day of the window. */
|
|
232
|
+
since: string;
|
|
233
|
+
/** Last day of the window: today. */
|
|
234
|
+
until: string;
|
|
235
|
+
/**
|
|
236
|
+
* The day counting began, when it falls inside the window; `null` when
|
|
237
|
+
* every day of the window was counted. The days before it were never
|
|
238
|
+
* counted, which is not the same as zero views.
|
|
239
|
+
*/
|
|
240
|
+
start: string | null;
|
|
241
|
+
total: number;
|
|
242
|
+
/** Oldest first, zero where nothing was counted. */
|
|
243
|
+
series: StatsSeries;
|
|
244
|
+
/** The most-viewed pages, posts among them, most views first. */
|
|
245
|
+
pages: StatsPageRow[];
|
|
246
|
+
referrers: StatsReferrers;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** One page over a window of days. */
|
|
250
|
+
export interface StatsPageDetail {
|
|
251
|
+
page: StatsPage;
|
|
252
|
+
days: number;
|
|
253
|
+
since: string;
|
|
254
|
+
until: string;
|
|
255
|
+
/** As on the overview: the first counted day when inside the window. */
|
|
256
|
+
start: string | null;
|
|
257
|
+
total: number;
|
|
258
|
+
/**
|
|
259
|
+
* By the day; `null` for today, since hours are kept for the site as a
|
|
260
|
+
* whole and not per page.
|
|
261
|
+
*/
|
|
262
|
+
series: StatsSeries | null;
|
|
263
|
+
referrers: StatsReferrers;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** One referrer over a window of days: the pages it sent readers to. */
|
|
267
|
+
export interface StatsReferrerDetail {
|
|
268
|
+
/** The referrer's counter key: a host or a tag, `""` for Direct, `/` for Internal. */
|
|
269
|
+
referrer: string;
|
|
270
|
+
days: number;
|
|
271
|
+
since: string;
|
|
272
|
+
until: string;
|
|
273
|
+
/** As on the overview: the first counted day when inside the window. */
|
|
274
|
+
start: string | null;
|
|
275
|
+
total: number;
|
|
276
|
+
/**
|
|
277
|
+
* By the day; `null` for today, since hours are kept for the site as a
|
|
278
|
+
* whole and not per referrer.
|
|
279
|
+
*/
|
|
280
|
+
series: StatsSeries | null;
|
|
281
|
+
/** The pages its readers opened most, most views first. */
|
|
282
|
+
pages: StatsPageRow[];
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
export interface StatsService {
|
|
286
|
+
/**
|
|
287
|
+
* Write a batch of counted views, for any site: the day table by page and
|
|
288
|
+
* referrer, the hour table as totals, applying the row cap and the sweep
|
|
289
|
+
* on the way, and giving a site that has no start day yet the batch's
|
|
290
|
+
* earliest day as its start. Entries of a site whose switch is off are
|
|
291
|
+
* dropped: a hit added before the author turned stats off and flushed
|
|
292
|
+
* after must not undo the deletion that came with it.
|
|
293
|
+
*
|
|
294
|
+
* @param entries - Views, already summed per site, hour, page and referrer
|
|
295
|
+
* @param opts.dailyLimit - Views this instance may record per day; `0` for no limit
|
|
296
|
+
* @param opts.today - Today's UTC day key, for the sweep and the limit
|
|
297
|
+
* @returns Whether the daily limit has been reached
|
|
298
|
+
*/
|
|
299
|
+
record(
|
|
300
|
+
entries: ViewCountEntry[],
|
|
301
|
+
opts: { dailyLimit: number; today: string },
|
|
302
|
+
): Promise<ViewRecordResult>;
|
|
303
|
+
/**
|
|
304
|
+
* The site's views over the last `days` days. A site with no start day
|
|
305
|
+
* yet gets today as its start: the record begins with the first look at
|
|
306
|
+
* it.
|
|
307
|
+
*
|
|
308
|
+
* @param opts.days - Length of the window; `1` is today, by the hour
|
|
309
|
+
* @param opts.today - The site's current day key, the window's last day
|
|
310
|
+
* @param opts.hour - The site's current hour key, the last hour drawn today
|
|
311
|
+
* @returns The overview
|
|
312
|
+
*/
|
|
313
|
+
getOverview(opts: {
|
|
314
|
+
days: number;
|
|
315
|
+
today: string;
|
|
316
|
+
hour: string;
|
|
317
|
+
}): Promise<StatsOverview>;
|
|
318
|
+
/**
|
|
319
|
+
* One page's views over the last `days` days, by its counter key: a
|
|
320
|
+
* post's or collection's id, or a path.
|
|
321
|
+
*
|
|
322
|
+
* @param key - The page's counter key
|
|
323
|
+
* @param opts.days - Length of the window; `1` is today
|
|
324
|
+
* @param opts.today - The site's current day key, the window's last day
|
|
325
|
+
* @param opts.hour - The site's current hour key
|
|
326
|
+
* @returns The detail, or `null` for a key that names nothing the site
|
|
327
|
+
* has or ever counted
|
|
328
|
+
*/
|
|
329
|
+
getPage(
|
|
330
|
+
key: string,
|
|
331
|
+
opts: { days: number; today: string; hour: string },
|
|
332
|
+
): Promise<StatsPageDetail | null>;
|
|
333
|
+
/**
|
|
334
|
+
* One referrer's views over the last `days` days, by its counter key: a
|
|
335
|
+
* host or a tag as the referrer list shows it, `""` for direct visits,
|
|
336
|
+
* `/` for internal navigation. Direct and Internal are referrers every
|
|
337
|
+
* site has; a host or a tag is one of this site's once something was
|
|
338
|
+
* counted under it, on any day still kept.
|
|
339
|
+
*
|
|
340
|
+
* @param key - The referrer's counter key
|
|
341
|
+
* @param opts.days - Length of the window; `1` is today
|
|
342
|
+
* @param opts.today - The site's current day key, the window's last day
|
|
343
|
+
* @returns The detail, or `null` for a referrer the site never counted
|
|
344
|
+
*/
|
|
345
|
+
getReferrer(
|
|
346
|
+
key: string,
|
|
347
|
+
opts: { days: number; today: string },
|
|
348
|
+
): Promise<StatsReferrerDetail | null>;
|
|
349
|
+
/**
|
|
350
|
+
* Delete every counter row the site has, and the day its record began:
|
|
351
|
+
* whatever is counted next starts a new record.
|
|
352
|
+
*
|
|
353
|
+
* @returns Nothing
|
|
354
|
+
*/
|
|
355
|
+
clear(): Promise<void>;
|
|
356
|
+
/**
|
|
357
|
+
* Turn counting on or off. Off also deletes the site's rows: a switch that
|
|
358
|
+
* says off while ninety days of counts sit behind it would be a lie. On
|
|
359
|
+
* makes today the start of the record unless one is already set.
|
|
360
|
+
*
|
|
361
|
+
* @param enabled - The new state
|
|
362
|
+
* @param opts.today - The site's current day key
|
|
363
|
+
* @returns Nothing
|
|
364
|
+
*/
|
|
365
|
+
setEnabled(enabled: boolean, opts: { today: string }): Promise<void>;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
function entryKey(entry: ViewDayRow): string {
|
|
369
|
+
return `${entry.siteId}\n${entry.day}\n${entry.page}\n${entry.referrer}`;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/** The day table's rows for a batch: the hour folded away. */
|
|
373
|
+
function foldByDay(entries: ViewCountEntry[]): ViewDayRow[] {
|
|
374
|
+
const rows = new Map<string, ViewDayRow>();
|
|
375
|
+
for (const { siteId, day, page, referrer, count } of entries) {
|
|
376
|
+
const row: ViewDayRow = { siteId, day, page, referrer, count };
|
|
377
|
+
const known = rows.get(entryKey(row));
|
|
378
|
+
if (known) known.count += count;
|
|
379
|
+
else rows.set(entryKey(row), row);
|
|
380
|
+
}
|
|
381
|
+
return [...rows.values()];
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/** The hour table's rows for a batch: the total per site and hour. */
|
|
385
|
+
function foldByHour(entries: ViewCountEntry[]): ViewHourRow[] {
|
|
386
|
+
const rows = new Map<string, ViewHourRow>();
|
|
387
|
+
for (const { siteId, hour, count } of entries) {
|
|
388
|
+
const key = `${siteId}\n${hour}`;
|
|
389
|
+
const known = rows.get(key);
|
|
390
|
+
if (known) known.count += count;
|
|
391
|
+
else rows.set(key, { siteId, hour, count });
|
|
392
|
+
}
|
|
393
|
+
return [...rows.values()];
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
function chunk<T>(items: T[], size: number): T[][] {
|
|
397
|
+
const chunks: T[][] = [];
|
|
398
|
+
for (let index = 0; index < items.length; index += size) {
|
|
399
|
+
chunks.push(items.slice(index, index + size));
|
|
400
|
+
}
|
|
401
|
+
return chunks;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* Summarize referrer rows the way the page shows them: direct on its own,
|
|
406
|
+
* the busiest hosts, and everything else as one number.
|
|
407
|
+
*/
|
|
408
|
+
function summarizeReferrers(rows: StatsReferrerRow[]): StatsReferrers {
|
|
409
|
+
let direct = 0;
|
|
410
|
+
let internal = 0;
|
|
411
|
+
let other = 0;
|
|
412
|
+
const named: StatsReferrerRow[] = [];
|
|
413
|
+
for (const row of rows) {
|
|
414
|
+
if (row.host === "") direct += row.views;
|
|
415
|
+
else if (row.host === VIEW_INTERNAL_REFERRER) internal += row.views;
|
|
416
|
+
else if (row.host === VIEW_OVERFLOW_KEY) other += row.views;
|
|
417
|
+
else named.push(row);
|
|
418
|
+
}
|
|
419
|
+
named.sort((a, b) => b.views - a.views || a.host.localeCompare(b.host));
|
|
420
|
+
const top = named.slice(0, STATS_TOP_LIMIT);
|
|
421
|
+
for (const row of named.slice(STATS_TOP_LIMIT)) other += row.views;
|
|
422
|
+
return { direct, internal, top, other };
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* Fill a window with one point per day, zero where nothing was counted.
|
|
427
|
+
*/
|
|
428
|
+
function fillDays(
|
|
429
|
+
rows: StatsSeriesPoint[],
|
|
430
|
+
today: string,
|
|
431
|
+
days: number,
|
|
432
|
+
): StatsSeriesPoint[] {
|
|
433
|
+
const byDay = new Map(rows.map((row) => [row.at, row.views]));
|
|
434
|
+
return dayKeysEndingAt(today, days).map((day) => ({
|
|
435
|
+
at: day,
|
|
436
|
+
views: byDay.get(day) ?? 0,
|
|
437
|
+
}));
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* Fill today with one point per hour up to the current one, zero where
|
|
442
|
+
* nothing was counted; the hours still to come are not points.
|
|
443
|
+
*/
|
|
444
|
+
function fillHours(
|
|
445
|
+
rows: StatsSeriesPoint[],
|
|
446
|
+
today: string,
|
|
447
|
+
hour: string,
|
|
448
|
+
): StatsSeriesPoint[] {
|
|
449
|
+
const byHour = new Map(rows.map((row) => [row.at, row.views]));
|
|
450
|
+
return hourKeysOfDay(today)
|
|
451
|
+
.filter((key) => key <= hour)
|
|
452
|
+
.map((key) => ({ at: key, views: byHour.get(key) ?? 0 }));
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* Create the stats service.
|
|
457
|
+
*
|
|
458
|
+
* @param db - Database
|
|
459
|
+
* @param siteId - The site the reads are scoped to
|
|
460
|
+
* @param schema - Dialect schema bundle
|
|
461
|
+
* @param dialect - Database dialect
|
|
462
|
+
* @param deps.settings - The settings service, for the on/off switch
|
|
463
|
+
* @param deps.writerState - Process memory for the write path; tests pass a fresh one
|
|
464
|
+
* @param deps.rowsPerSiteDay - The row cap, `VIEW_COUNT_ROWS_PER_SITE_DAY` unless a test lowers it
|
|
465
|
+
* @returns The service
|
|
466
|
+
* @example
|
|
467
|
+
* ```ts
|
|
468
|
+
* const stats = createStatsService(db, siteId, schema, dialect, { settings });
|
|
469
|
+
* const overview = await stats.getOverview({ days: 30, today: "2026-11-08" });
|
|
470
|
+
* ```
|
|
471
|
+
*/
|
|
472
|
+
export function createStatsService(
|
|
473
|
+
db: Database,
|
|
474
|
+
siteId: string,
|
|
475
|
+
schema: DatabaseSchema = sqliteSchemaBundle,
|
|
476
|
+
dialect: DatabaseDialect = "sqlite",
|
|
477
|
+
deps: {
|
|
478
|
+
settings: SettingsService;
|
|
479
|
+
writerState?: ViewCountWriterState;
|
|
480
|
+
/** The row cap; tests lower it. */
|
|
481
|
+
rowsPerSiteDay?: number;
|
|
482
|
+
},
|
|
483
|
+
): StatsService {
|
|
484
|
+
const {
|
|
485
|
+
viewCounts,
|
|
486
|
+
viewHours,
|
|
487
|
+
posts,
|
|
488
|
+
collections,
|
|
489
|
+
smartCollections,
|
|
490
|
+
pathRegistry,
|
|
491
|
+
settings,
|
|
492
|
+
} = schema;
|
|
493
|
+
const state = deps.writerState ?? sharedWriterState;
|
|
494
|
+
const rowsPerSiteDay = deps.rowsPerSiteDay ?? VIEW_COUNT_ROWS_PER_SITE_DAY;
|
|
495
|
+
|
|
496
|
+
/** A post's address lives in `path_registry`, one `slug` row per post. */
|
|
497
|
+
const postSlugJoin = and(
|
|
498
|
+
eq(pathRegistry.siteId, posts.siteId),
|
|
499
|
+
eq(pathRegistry.postId, posts.id),
|
|
500
|
+
eq(pathRegistry.kind, "slug"),
|
|
501
|
+
);
|
|
502
|
+
|
|
503
|
+
/**
|
|
504
|
+
* Apply the row cap: a group of entries for one site-day whose new keys
|
|
505
|
+
* would push the day past the cap has those keys folded into the overflow
|
|
506
|
+
* row. Only groups that carry keys this process has not written before cost
|
|
507
|
+
* a count; a busy site's keys are all known after the first flush of the
|
|
508
|
+
* day.
|
|
509
|
+
*/
|
|
510
|
+
async function capRows(entries: ViewDayRow[]): Promise<ViewDayRow[]> {
|
|
511
|
+
const groups = new Map<string, ViewDayRow[]>();
|
|
512
|
+
for (const entry of entries) {
|
|
513
|
+
const key = `${entry.siteId}\n${entry.day}`;
|
|
514
|
+
const group = groups.get(key);
|
|
515
|
+
if (group) group.push(entry);
|
|
516
|
+
else groups.set(key, [entry]);
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
const rows: ViewDayRow[] = [];
|
|
520
|
+
for (const group of groups.values()) {
|
|
521
|
+
const unseen = new Set(
|
|
522
|
+
group.filter((entry) => !state.seen.has(entryKey(entry))),
|
|
523
|
+
);
|
|
524
|
+
if (unseen.size === 0) {
|
|
525
|
+
rows.push(...group);
|
|
526
|
+
continue;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
const first = group[0];
|
|
530
|
+
if (!first) continue;
|
|
531
|
+
const { siteId: groupSite, day } = first;
|
|
532
|
+
const [counted] = await db
|
|
533
|
+
.select({ total: sql<number>`count(*)`.mapWith(Number) })
|
|
534
|
+
.from(viewCounts)
|
|
535
|
+
.where(and(eq(viewCounts.siteId, groupSite), eq(viewCounts.day, day)));
|
|
536
|
+
const room = Math.max(0, rowsPerSiteDay - (counted?.total ?? 0));
|
|
537
|
+
if (unseen.size <= room) {
|
|
538
|
+
rows.push(...group);
|
|
539
|
+
continue;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
// The busiest new pairs keep their own rows; the rest share one.
|
|
543
|
+
const kept = new Set(
|
|
544
|
+
[...unseen].sort((a, b) => b.count - a.count).slice(0, room),
|
|
545
|
+
);
|
|
546
|
+
let overflow = 0;
|
|
547
|
+
for (const entry of group) {
|
|
548
|
+
if (!unseen.has(entry) || kept.has(entry)) rows.push(entry);
|
|
549
|
+
else overflow += entry.count;
|
|
550
|
+
}
|
|
551
|
+
if (overflow > 0) {
|
|
552
|
+
rows.push({
|
|
553
|
+
siteId: groupSite,
|
|
554
|
+
day,
|
|
555
|
+
page: VIEW_OVERFLOW_KEY,
|
|
556
|
+
referrer: VIEW_OVERFLOW_KEY,
|
|
557
|
+
count: overflow,
|
|
558
|
+
});
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
return rows;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
function upsertDays(executor: Database, rows: ViewDayRow[]) {
|
|
565
|
+
return executor
|
|
566
|
+
.insert(viewCounts)
|
|
567
|
+
.values(rows)
|
|
568
|
+
.onConflictDoUpdate({
|
|
569
|
+
target: [
|
|
570
|
+
viewCounts.siteId,
|
|
571
|
+
viewCounts.day,
|
|
572
|
+
viewCounts.page,
|
|
573
|
+
viewCounts.referrer,
|
|
574
|
+
],
|
|
575
|
+
// Qualified on purpose: Postgres has both the row and `excluded` in
|
|
576
|
+
// scope in a DO UPDATE and rejects a bare `count` as ambiguous; SQLite
|
|
577
|
+
// accepts either form.
|
|
578
|
+
set: { count: sql`${viewCounts.count} + excluded.count` },
|
|
579
|
+
});
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
function upsertHours(executor: Database, rows: ViewHourRow[]) {
|
|
583
|
+
return executor
|
|
584
|
+
.insert(viewHours)
|
|
585
|
+
.values(rows)
|
|
586
|
+
.onConflictDoUpdate({
|
|
587
|
+
target: [viewHours.siteId, viewHours.hour],
|
|
588
|
+
set: { count: sql`${viewHours.count} + excluded.count` },
|
|
589
|
+
});
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
/** Both tables' upserts, in one transaction or one batch. */
|
|
593
|
+
async function writeRows(
|
|
594
|
+
dayRows: ViewDayRow[],
|
|
595
|
+
hourRows: ViewHourRow[],
|
|
596
|
+
): Promise<void> {
|
|
597
|
+
const dayChunks = chunk(dayRows, UPSERT_CHUNK_ROWS);
|
|
598
|
+
const hourChunks = chunk(hourRows, HOUR_UPSERT_CHUNK_ROWS);
|
|
599
|
+
if (supportsDrizzleTransaction(db, dialect)) {
|
|
600
|
+
await db.transaction(async (tx) => {
|
|
601
|
+
const executor = tx as unknown as Database;
|
|
602
|
+
for (const part of dayChunks) await upsertDays(executor, part);
|
|
603
|
+
for (const part of hourChunks) await upsertHours(executor, part);
|
|
604
|
+
});
|
|
605
|
+
return;
|
|
606
|
+
}
|
|
607
|
+
const queries = [
|
|
608
|
+
...dayChunks.map((part) => upsertDays(db, part)),
|
|
609
|
+
...hourChunks.map((part) => upsertHours(db, part)),
|
|
610
|
+
];
|
|
611
|
+
await db.batch(
|
|
612
|
+
queries as [(typeof queries)[number], ...(typeof queries)[number][]],
|
|
613
|
+
);
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
/**
|
|
617
|
+
* The batch without the sites whose switch is off. Read from the table
|
|
618
|
+
* rather than the settings service because a batch spans sites, and read
|
|
619
|
+
* on every flush, so the promise `clear()` made holds across processes.
|
|
620
|
+
*/
|
|
621
|
+
async function withoutSwitchedOff(
|
|
622
|
+
entries: ViewCountEntry[],
|
|
623
|
+
): Promise<ViewCountEntry[]> {
|
|
624
|
+
if (entries.length === 0) return entries;
|
|
625
|
+
const siteIds = [...new Set(entries.map((entry) => entry.siteId))];
|
|
626
|
+
const off = await db
|
|
627
|
+
.select({ siteId: settings.siteId })
|
|
628
|
+
.from(settings)
|
|
629
|
+
.where(
|
|
630
|
+
and(
|
|
631
|
+
inArray(settings.siteId, siteIds),
|
|
632
|
+
eq(settings.key, "STATS_ENABLED"),
|
|
633
|
+
eq(settings.value, "false"),
|
|
634
|
+
),
|
|
635
|
+
);
|
|
636
|
+
if (off.length === 0) return entries;
|
|
637
|
+
const offSites = new Set(off.map((row) => row.siteId));
|
|
638
|
+
return entries.filter((entry) => !offSites.has(entry.siteId));
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
function rememberRows(rows: ViewDayRow[]): void {
|
|
642
|
+
if (state.seen.size + rows.length > SEEN_KEYS_LIMIT) state.seen.clear();
|
|
643
|
+
for (const row of rows) state.seen.add(entryKey(row));
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
/**
|
|
647
|
+
* Give each touched site a start day if it has none: the batch's earliest
|
|
648
|
+
* day for that site. Once per site per process, like the sweep; `clear()`
|
|
649
|
+
* forgets the site so the next flush stamps it again. Written through the
|
|
650
|
+
* table rather than the settings service because a batch spans sites.
|
|
651
|
+
*/
|
|
652
|
+
async function stampStart(rows: ViewDayRow[]): Promise<void> {
|
|
653
|
+
const firstDays = new Map<string, string>();
|
|
654
|
+
for (const row of rows) {
|
|
655
|
+
const known = firstDays.get(row.siteId);
|
|
656
|
+
if (known === undefined || row.day < known) {
|
|
657
|
+
firstDays.set(row.siteId, row.day);
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
for (const [touchedSite, day] of firstDays) {
|
|
661
|
+
if (state.stamped.has(touchedSite)) continue;
|
|
662
|
+
state.stamped.add(touchedSite);
|
|
663
|
+
await db
|
|
664
|
+
.insert(settings)
|
|
665
|
+
.values({
|
|
666
|
+
siteId: touchedSite,
|
|
667
|
+
key: "STATS_SINCE",
|
|
668
|
+
value: day,
|
|
669
|
+
updatedAt: now(),
|
|
670
|
+
})
|
|
671
|
+
.onConflictDoNothing();
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
/**
|
|
676
|
+
* Where the record starts, for a window: the stored start day when it
|
|
677
|
+
* falls inside the window, pulled back to the first counted day should a
|
|
678
|
+
* row predate it, and `null` when every day of the window was counted. A
|
|
679
|
+
* site with no start yet gets today as its start, stored for next time.
|
|
680
|
+
*/
|
|
681
|
+
async function startOfRecord(
|
|
682
|
+
stored: string | null,
|
|
683
|
+
{ since, today }: { since: string; today: string },
|
|
684
|
+
counted: StatsSeriesPoint[],
|
|
685
|
+
): Promise<string | null> {
|
|
686
|
+
let start = stored !== null && isDayKey(stored) ? stored : null;
|
|
687
|
+
if (start === null) {
|
|
688
|
+
start = today;
|
|
689
|
+
state.stamped.add(siteId);
|
|
690
|
+
await deps.settings.set("STATS_SINCE", today);
|
|
691
|
+
}
|
|
692
|
+
if (start > today) start = today;
|
|
693
|
+
for (const row of counted) {
|
|
694
|
+
if (row.at < start) start = row.at;
|
|
695
|
+
}
|
|
696
|
+
return start > since ? start : null;
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
/**
|
|
700
|
+
* Delete each touched site's rows past the retention windows, once a day:
|
|
701
|
+
* the last `VIEW_COUNT_RETENTION_DAYS` days of counters, today included,
|
|
702
|
+
* and the last `VIEW_HOUR_RETENTION_DAYS` days of hours.
|
|
703
|
+
*/
|
|
704
|
+
async function sweep(rows: ViewDayRow[], today: string): Promise<void> {
|
|
705
|
+
const cutoff = shiftDayKey(today, -(VIEW_COUNT_RETENTION_DAYS - 1));
|
|
706
|
+
const hourCutoff = `${shiftDayKey(today, -(VIEW_HOUR_RETENTION_DAYS - 1))}T00`;
|
|
707
|
+
for (const touchedSite of new Set(rows.map((row) => row.siteId))) {
|
|
708
|
+
if (state.sweptOn.get(touchedSite) === today) continue;
|
|
709
|
+
state.sweptOn.set(touchedSite, today);
|
|
710
|
+
await db
|
|
711
|
+
.delete(viewCounts)
|
|
712
|
+
.where(
|
|
713
|
+
and(eq(viewCounts.siteId, touchedSite), lt(viewCounts.day, cutoff)),
|
|
714
|
+
);
|
|
715
|
+
await db
|
|
716
|
+
.delete(viewHours)
|
|
717
|
+
.where(
|
|
718
|
+
and(
|
|
719
|
+
eq(viewHours.siteId, touchedSite),
|
|
720
|
+
lt(viewHours.hour, hourCutoff),
|
|
721
|
+
),
|
|
722
|
+
);
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
/**
|
|
727
|
+
* Whether the views recorded for the batch's days have reached the
|
|
728
|
+
* instance's daily limit. The limit guards a quota that resets at UTC
|
|
729
|
+
* midnight while rows are filed by the site's own day; the two differ by
|
|
730
|
+
* at most a time zone, and the limit leaves room for that.
|
|
731
|
+
*/
|
|
732
|
+
async function limitReached(
|
|
733
|
+
rows: ViewDayRow[],
|
|
734
|
+
dailyLimit: number,
|
|
735
|
+
): Promise<boolean> {
|
|
736
|
+
if (dailyLimit <= 0) return false;
|
|
737
|
+
const [summed] = await db
|
|
738
|
+
.select({
|
|
739
|
+
total: sql<number>`coalesce(sum(${viewCounts.count}), 0)`.mapWith(
|
|
740
|
+
Number,
|
|
741
|
+
),
|
|
742
|
+
})
|
|
743
|
+
.from(viewCounts)
|
|
744
|
+
.where(
|
|
745
|
+
and(
|
|
746
|
+
inArray(viewCounts.siteId, [...new Set(rows.map((r) => r.siteId))]),
|
|
747
|
+
inArray(viewCounts.day, [...new Set(rows.map((r) => r.day))]),
|
|
748
|
+
),
|
|
749
|
+
);
|
|
750
|
+
return (summed?.total ?? 0) >= dailyLimit;
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
/** What a window query is narrowed to: one page, or one referrer. */
|
|
754
|
+
interface WindowFilter {
|
|
755
|
+
page?: string;
|
|
756
|
+
referrer?: string;
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
function windowCondition(
|
|
760
|
+
since: string,
|
|
761
|
+
today: string,
|
|
762
|
+
{ page, referrer }: WindowFilter = {},
|
|
763
|
+
) {
|
|
764
|
+
return and(
|
|
765
|
+
eq(viewCounts.siteId, siteId),
|
|
766
|
+
gte(viewCounts.day, since),
|
|
767
|
+
lte(viewCounts.day, today),
|
|
768
|
+
page === undefined ? undefined : eq(viewCounts.page, page),
|
|
769
|
+
referrer === undefined ? undefined : eq(viewCounts.referrer, referrer),
|
|
770
|
+
);
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
function seriesQuery(since: string, today: string, filter?: WindowFilter) {
|
|
774
|
+
return db
|
|
775
|
+
.select({
|
|
776
|
+
at: viewCounts.day,
|
|
777
|
+
views: sql<number>`sum(${viewCounts.count})`.mapWith(Number),
|
|
778
|
+
})
|
|
779
|
+
.from(viewCounts)
|
|
780
|
+
.where(windowCondition(since, today, filter))
|
|
781
|
+
.groupBy(viewCounts.day);
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
/** Today's hour totals, for the chart. */
|
|
785
|
+
function hoursQuery(today: string) {
|
|
786
|
+
return db
|
|
787
|
+
.select({ at: viewHours.hour, views: viewHours.count })
|
|
788
|
+
.from(viewHours)
|
|
789
|
+
.where(
|
|
790
|
+
and(
|
|
791
|
+
eq(viewHours.siteId, siteId),
|
|
792
|
+
gte(viewHours.hour, `${today}T00`),
|
|
793
|
+
lte(viewHours.hour, `${today}T23`),
|
|
794
|
+
),
|
|
795
|
+
);
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
function referrersQuery(since: string, today: string, filter?: WindowFilter) {
|
|
799
|
+
return db
|
|
800
|
+
.select({
|
|
801
|
+
host: viewCounts.referrer,
|
|
802
|
+
views: sql<number>`sum(${viewCounts.count})`.mapWith(Number),
|
|
803
|
+
})
|
|
804
|
+
.from(viewCounts)
|
|
805
|
+
.where(windowCondition(since, today, filter))
|
|
806
|
+
.groupBy(viewCounts.referrer);
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
/**
|
|
810
|
+
* The busiest keys of the window, whatever they name; the overflow row is
|
|
811
|
+
* a remainder, not a page. Names come after, by kind.
|
|
812
|
+
*/
|
|
813
|
+
function topPagesQuery(since: string, today: string, filter?: WindowFilter) {
|
|
814
|
+
const views = sql<number>`sum(${viewCounts.count})`;
|
|
815
|
+
return db
|
|
816
|
+
.select({ key: viewCounts.page, views: views.mapWith(Number) })
|
|
817
|
+
.from(viewCounts)
|
|
818
|
+
.where(
|
|
819
|
+
and(
|
|
820
|
+
windowCondition(since, today, filter),
|
|
821
|
+
ne(viewCounts.page, VIEW_OVERFLOW_KEY),
|
|
822
|
+
),
|
|
823
|
+
)
|
|
824
|
+
.groupBy(viewCounts.page)
|
|
825
|
+
.orderBy(desc(views), asc(viewCounts.page))
|
|
826
|
+
.limit(STATS_TOP_LIMIT);
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
/** The posts among some ids, named and addressed; a deleted one is absent. */
|
|
830
|
+
async function describePosts(
|
|
831
|
+
ids: string[],
|
|
832
|
+
): Promise<Map<string, NonNullable<StatsPostPage["post"]>>> {
|
|
833
|
+
if (ids.length === 0) return new Map();
|
|
834
|
+
const rows = await db
|
|
835
|
+
.select({
|
|
836
|
+
id: posts.id,
|
|
837
|
+
title: posts.title,
|
|
838
|
+
format: posts.format,
|
|
839
|
+
url: posts.url,
|
|
840
|
+
body: posts.body,
|
|
841
|
+
bodyText: posts.bodyText,
|
|
842
|
+
quoteText: posts.quoteText,
|
|
843
|
+
summary: posts.summary,
|
|
844
|
+
slug: pathRegistry.path,
|
|
845
|
+
})
|
|
846
|
+
.from(posts)
|
|
847
|
+
.innerJoin(pathRegistry, postSlugJoin)
|
|
848
|
+
.where(and(eq(posts.siteId, siteId), inArray(posts.id, ids)));
|
|
849
|
+
return new Map(
|
|
850
|
+
rows.map((row) => [
|
|
851
|
+
row.id,
|
|
852
|
+
{
|
|
853
|
+
title: getPostDisplayTitle(row),
|
|
854
|
+
slug: row.slug,
|
|
855
|
+
format: row.format,
|
|
856
|
+
},
|
|
857
|
+
]),
|
|
858
|
+
);
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
/** The collections among some ids, with their address; a deleted one is absent. */
|
|
862
|
+
async function describeCollections(
|
|
863
|
+
ids: string[],
|
|
864
|
+
): Promise<Map<string, NonNullable<StatsCollectionPage["collection"]>>> {
|
|
865
|
+
if (ids.length === 0) return new Map();
|
|
866
|
+
const rows = await db
|
|
867
|
+
.select({
|
|
868
|
+
id: collections.id,
|
|
869
|
+
title: collections.title,
|
|
870
|
+
slug: pathRegistry.path,
|
|
871
|
+
})
|
|
872
|
+
.from(collections)
|
|
873
|
+
.innerJoin(
|
|
874
|
+
pathRegistry,
|
|
875
|
+
and(
|
|
876
|
+
eq(pathRegistry.siteId, collections.siteId),
|
|
877
|
+
eq(pathRegistry.collectionId, collections.id),
|
|
878
|
+
eq(pathRegistry.kind, "slug"),
|
|
879
|
+
),
|
|
880
|
+
)
|
|
881
|
+
.where(and(eq(collections.siteId, siteId), inArray(collections.id, ids)));
|
|
882
|
+
return new Map(
|
|
883
|
+
rows.map((row) => [row.id, { title: row.title, slug: row.slug }]),
|
|
884
|
+
);
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
/** The smart collections among some ids, the same way. */
|
|
888
|
+
async function describeSmartCollections(
|
|
889
|
+
ids: string[],
|
|
890
|
+
): Promise<Map<string, NonNullable<StatsCollectionPage["collection"]>>> {
|
|
891
|
+
if (ids.length === 0) return new Map();
|
|
892
|
+
const rows = await db
|
|
893
|
+
.select({
|
|
894
|
+
id: smartCollections.id,
|
|
895
|
+
title: smartCollections.title,
|
|
896
|
+
slug: pathRegistry.path,
|
|
897
|
+
})
|
|
898
|
+
.from(smartCollections)
|
|
899
|
+
.innerJoin(
|
|
900
|
+
pathRegistry,
|
|
901
|
+
and(
|
|
902
|
+
eq(pathRegistry.siteId, smartCollections.siteId),
|
|
903
|
+
eq(pathRegistry.smartCollectionId, smartCollections.id),
|
|
904
|
+
eq(pathRegistry.kind, "slug"),
|
|
905
|
+
),
|
|
906
|
+
)
|
|
907
|
+
.where(
|
|
908
|
+
and(
|
|
909
|
+
eq(smartCollections.siteId, siteId),
|
|
910
|
+
inArray(smartCollections.id, ids),
|
|
911
|
+
),
|
|
912
|
+
);
|
|
913
|
+
return new Map(
|
|
914
|
+
rows.map((row) => [row.id, { title: row.title, slug: row.slug }]),
|
|
915
|
+
);
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
/**
|
|
919
|
+
* Name each key: a post or a collection from its table, a path as itself.
|
|
920
|
+
* Three lookups at most, side by side; a key of no known shape is left out.
|
|
921
|
+
*/
|
|
922
|
+
async function describePages(
|
|
923
|
+
keys: string[],
|
|
924
|
+
): Promise<Map<string, StatsPage>> {
|
|
925
|
+
const ids = {
|
|
926
|
+
post: [] as string[],
|
|
927
|
+
collection: [] as string[],
|
|
928
|
+
smart: [] as string[],
|
|
929
|
+
};
|
|
930
|
+
for (const key of keys) {
|
|
931
|
+
const kind = viewPageKind(key);
|
|
932
|
+
if (kind === "post") ids.post.push(key);
|
|
933
|
+
else if (kind === "collection") ids.collection.push(key);
|
|
934
|
+
else if (kind === "smartCollection") ids.smart.push(key);
|
|
935
|
+
}
|
|
936
|
+
const [postRows, collectionRows, smartRows] = await Promise.all([
|
|
937
|
+
describePosts(ids.post),
|
|
938
|
+
describeCollections(ids.collection),
|
|
939
|
+
describeSmartCollections(ids.smart),
|
|
940
|
+
]);
|
|
941
|
+
const pages = new Map<string, StatsPage>();
|
|
942
|
+
for (const key of keys) {
|
|
943
|
+
switch (viewPageKind(key)) {
|
|
944
|
+
case "path":
|
|
945
|
+
pages.set(key, { kind: "path", key });
|
|
946
|
+
break;
|
|
947
|
+
case "post":
|
|
948
|
+
pages.set(key, {
|
|
949
|
+
kind: "post",
|
|
950
|
+
key,
|
|
951
|
+
post: postRows.get(key) ?? null,
|
|
952
|
+
});
|
|
953
|
+
break;
|
|
954
|
+
case "collection":
|
|
955
|
+
pages.set(key, {
|
|
956
|
+
kind: "collection",
|
|
957
|
+
key,
|
|
958
|
+
collection: collectionRows.get(key) ?? null,
|
|
959
|
+
});
|
|
960
|
+
break;
|
|
961
|
+
case "smartCollection":
|
|
962
|
+
pages.set(key, {
|
|
963
|
+
kind: "collection",
|
|
964
|
+
key,
|
|
965
|
+
collection: smartRows.get(key) ?? null,
|
|
966
|
+
});
|
|
967
|
+
break;
|
|
968
|
+
default:
|
|
969
|
+
break;
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
return pages;
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
/** Whether the site ever counted a key, on any day still kept. */
|
|
976
|
+
async function hasRows(key: string): Promise<boolean> {
|
|
977
|
+
const [row] = await db
|
|
978
|
+
.select({ page: viewCounts.page })
|
|
979
|
+
.from(viewCounts)
|
|
980
|
+
.where(and(eq(viewCounts.siteId, siteId), eq(viewCounts.page, key)))
|
|
981
|
+
.limit(1);
|
|
982
|
+
return row !== undefined;
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
/** Whether the site ever counted a view from a referrer, on any day still kept. */
|
|
986
|
+
async function hasReferrerRows(key: string): Promise<boolean> {
|
|
987
|
+
const [row] = await db
|
|
988
|
+
.select({ referrer: viewCounts.referrer })
|
|
989
|
+
.from(viewCounts)
|
|
990
|
+
.where(and(eq(viewCounts.siteId, siteId), eq(viewCounts.referrer, key)))
|
|
991
|
+
.limit(1);
|
|
992
|
+
return row !== undefined;
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
return {
|
|
996
|
+
async record(entries, opts) {
|
|
997
|
+
const counted = await withoutSwitchedOff(entries);
|
|
998
|
+
if (counted.length === 0) return { limitReached: false };
|
|
999
|
+
const rows = await capRows(foldByDay(counted));
|
|
1000
|
+
await writeRows(rows, foldByHour(counted));
|
|
1001
|
+
rememberRows(rows);
|
|
1002
|
+
await stampStart(rows);
|
|
1003
|
+
await sweep(rows, opts.today);
|
|
1004
|
+
return { limitReached: await limitReached(rows, opts.dailyLimit) };
|
|
1005
|
+
},
|
|
1006
|
+
|
|
1007
|
+
async getOverview({ days, today, hour }) {
|
|
1008
|
+
const since = shiftDayKey(today, -(days - 1));
|
|
1009
|
+
const [seriesRows, hourRows, topRows, referrerRows, stored] =
|
|
1010
|
+
await Promise.all([
|
|
1011
|
+
seriesQuery(since, today),
|
|
1012
|
+
days === 1 ? hoursQuery(today) : Promise.resolve([]),
|
|
1013
|
+
topPagesQuery(since, today),
|
|
1014
|
+
referrersQuery(since, today),
|
|
1015
|
+
deps.settings.get("STATS_SINCE"),
|
|
1016
|
+
]);
|
|
1017
|
+
const named = await describePages(topRows.map((row) => row.key));
|
|
1018
|
+
const dayPoints = fillDays(seriesRows, today, days);
|
|
1019
|
+
const series: StatsSeries =
|
|
1020
|
+
days === 1
|
|
1021
|
+
? { unit: "hour", points: fillHours(hourRows, today, hour) }
|
|
1022
|
+
: { unit: "day", points: dayPoints };
|
|
1023
|
+
return {
|
|
1024
|
+
days,
|
|
1025
|
+
since,
|
|
1026
|
+
until: today,
|
|
1027
|
+
start: await startOfRecord(stored, { since, today }, seriesRows),
|
|
1028
|
+
// The day table's sum: the same rows the lists come from.
|
|
1029
|
+
total: dayPoints.reduce((sum, point) => sum + point.views, 0),
|
|
1030
|
+
series,
|
|
1031
|
+
pages: topRows.flatMap((row) => {
|
|
1032
|
+
const page = named.get(row.key);
|
|
1033
|
+
return page ? [{ ...page, views: row.views }] : [];
|
|
1034
|
+
}),
|
|
1035
|
+
referrers: summarizeReferrers(referrerRows),
|
|
1036
|
+
};
|
|
1037
|
+
},
|
|
1038
|
+
|
|
1039
|
+
async getPage(key, { days, today }) {
|
|
1040
|
+
const kind = viewPageKind(key);
|
|
1041
|
+
if (kind === null || kind === "overflow") return null;
|
|
1042
|
+
const since = shiftDayKey(today, -(days - 1));
|
|
1043
|
+
const [named, seriesRows, referrerRows, stored] = await Promise.all([
|
|
1044
|
+
describePages([key]),
|
|
1045
|
+
seriesQuery(since, today, { page: key }),
|
|
1046
|
+
referrersQuery(since, today, { page: key }),
|
|
1047
|
+
deps.settings.get("STATS_SINCE"),
|
|
1048
|
+
]);
|
|
1049
|
+
const page = named.get(key);
|
|
1050
|
+
if (!page) return null;
|
|
1051
|
+
// A path, or an id whose entity is gone, is a page of this site only
|
|
1052
|
+
// if something was ever counted under it; anything else typed into
|
|
1053
|
+
// the address bar is not.
|
|
1054
|
+
const exists =
|
|
1055
|
+
page.kind === "post"
|
|
1056
|
+
? page.post !== null
|
|
1057
|
+
: page.kind === "collection"
|
|
1058
|
+
? page.collection !== null
|
|
1059
|
+
: false;
|
|
1060
|
+
if (!exists && !(await hasRows(key))) return null;
|
|
1061
|
+
const points = fillDays(seriesRows, today, days);
|
|
1062
|
+
return {
|
|
1063
|
+
page,
|
|
1064
|
+
days,
|
|
1065
|
+
since,
|
|
1066
|
+
until: today,
|
|
1067
|
+
// The site's start, not the page's: the page's own first view is
|
|
1068
|
+
// a bar on the chart, not where the record begins.
|
|
1069
|
+
start: await startOfRecord(stored, { since, today }, []),
|
|
1070
|
+
total: points.reduce((sum, point) => sum + point.views, 0),
|
|
1071
|
+
// A page has no hours of its own: today shows its total and sources.
|
|
1072
|
+
series: days === 1 ? null : { unit: "day", points },
|
|
1073
|
+
referrers: summarizeReferrers(referrerRows),
|
|
1074
|
+
};
|
|
1075
|
+
},
|
|
1076
|
+
|
|
1077
|
+
async getReferrer(key, { days, today }) {
|
|
1078
|
+
// The overflow row is a remainder, not somewhere readers came from.
|
|
1079
|
+
if (key === VIEW_OVERFLOW_KEY) return null;
|
|
1080
|
+
const since = shiftDayKey(today, -(days - 1));
|
|
1081
|
+
const [seriesRows, topRows, stored] = await Promise.all([
|
|
1082
|
+
seriesQuery(since, today, { referrer: key }),
|
|
1083
|
+
topPagesQuery(since, today, { referrer: key }),
|
|
1084
|
+
deps.settings.get("STATS_SINCE"),
|
|
1085
|
+
]);
|
|
1086
|
+
// Direct and Internal exist for every site. A host or a tag typed into
|
|
1087
|
+
// the address bar is a referrer of this site only if it ever sent a
|
|
1088
|
+
// view, inside the window or on an older day still kept.
|
|
1089
|
+
const known =
|
|
1090
|
+
key === "" || key === VIEW_INTERNAL_REFERRER || topRows.length > 0;
|
|
1091
|
+
if (!known && !(await hasReferrerRows(key))) return null;
|
|
1092
|
+
const named = await describePages(topRows.map((row) => row.key));
|
|
1093
|
+
const points = fillDays(seriesRows, today, days);
|
|
1094
|
+
return {
|
|
1095
|
+
referrer: key,
|
|
1096
|
+
days,
|
|
1097
|
+
since,
|
|
1098
|
+
until: today,
|
|
1099
|
+
// The site's start, as on a page's own numbers.
|
|
1100
|
+
start: await startOfRecord(stored, { since, today }, []),
|
|
1101
|
+
total: points.reduce((sum, point) => sum + point.views, 0),
|
|
1102
|
+
// A referrer has no hours of its own: today shows its total and pages.
|
|
1103
|
+
series: days === 1 ? null : { unit: "day", points },
|
|
1104
|
+
pages: topRows.flatMap((row) => {
|
|
1105
|
+
const page = named.get(row.key);
|
|
1106
|
+
return page ? [{ ...page, views: row.views }] : [];
|
|
1107
|
+
}),
|
|
1108
|
+
};
|
|
1109
|
+
},
|
|
1110
|
+
|
|
1111
|
+
async clear() {
|
|
1112
|
+
await db.delete(viewCounts).where(eq(viewCounts.siteId, siteId));
|
|
1113
|
+
await db.delete(viewHours).where(eq(viewHours.siteId, siteId));
|
|
1114
|
+
await deps.settings.remove("STATS_SINCE");
|
|
1115
|
+
state.stamped.delete(siteId);
|
|
1116
|
+
},
|
|
1117
|
+
|
|
1118
|
+
async setEnabled(enabled, { today }) {
|
|
1119
|
+
await deps.settings.set("STATS_ENABLED", enabled ? "true" : "false");
|
|
1120
|
+
if (!enabled) {
|
|
1121
|
+
await this.clear();
|
|
1122
|
+
return;
|
|
1123
|
+
}
|
|
1124
|
+
if ((await deps.settings.get("STATS_SINCE")) === null) {
|
|
1125
|
+
state.stamped.add(siteId);
|
|
1126
|
+
await deps.settings.set("STATS_SINCE", today);
|
|
1127
|
+
}
|
|
1128
|
+
},
|
|
1129
|
+
};
|
|
1130
|
+
}
|