clarify visit counts and asset routing #61
No files matched your search
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'@goto/web': patch
|
||||
---
|
||||
|
||||
Clarify how the web server routes published assets separately from short links, without changing behavior.
|
||||
@@ -50,28 +50,27 @@ export function handleIngress<AssetResponse extends Response | Promise<Response>
|
||||
|
||||
const assetPath = publishedAssetPath(decodedRawPathname);
|
||||
if (assetPath && assetMethod) {
|
||||
if (assetPath !== rawPathname) {
|
||||
const query = target.slice(rawPathname.length);
|
||||
const response = emptyResponse(308);
|
||||
response.headers.set('location', assetPath + query);
|
||||
return response;
|
||||
}
|
||||
const isRedirectOnlyHost = redirectDomain !== undefined && !isSharedHost;
|
||||
const assetReader = isRedirectOnlyHost ? serveRedirectHostAsset : undefined;
|
||||
const response = publishedAssetResponse(
|
||||
assetPath,
|
||||
assetMethod,
|
||||
target,
|
||||
rawPathname,
|
||||
assetReader
|
||||
);
|
||||
const response = isRedirectOnlyHost
|
||||
? serveRedirectHostAsset?.(assetPath, assetMethod)
|
||||
: undefined;
|
||||
if (response !== undefined) return response;
|
||||
bindApplication(request, application, ingress.url.origin, snapshot);
|
||||
return;
|
||||
}
|
||||
|
||||
if (redirectDomain && !isReservedPath) {
|
||||
} else if (redirectDomain && !isReservedPath) {
|
||||
if (decodedRawPathname?.startsWith(reservedStaticPrefix)) {
|
||||
return renderPublicError(emptyResponse(404), ingress.url.origin, method);
|
||||
}
|
||||
return handleRedirectHostRequest(application, request, snapshot, ingress, redirectDomain);
|
||||
return handleShortLinkRequest(application, request, snapshot, ingress, redirectDomain);
|
||||
}
|
||||
|
||||
bindApplication(request, application, ingress.url.origin, snapshot);
|
||||
// Hand off to the production adapter or Vite middleware.
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function adminCacheControl(request: IncomingMessage): string | undefined {
|
||||
@@ -89,22 +88,7 @@ export function adminCacheControl(request: IncomingMessage): string | undefined
|
||||
}
|
||||
}
|
||||
|
||||
function publishedAssetResponse<AssetResponse extends Response | Promise<Response>>(
|
||||
path: PublishedAssetPath,
|
||||
method: AssetMethod,
|
||||
target: string,
|
||||
rawPathname: string,
|
||||
assetReader: RedirectHostAssetReader<AssetResponse> | undefined
|
||||
): Response | AssetResponse | undefined {
|
||||
if (path === rawPathname) return assetReader?.(path, method);
|
||||
|
||||
const query = target.slice(rawPathname.length);
|
||||
const response = emptyResponse(308);
|
||||
response.headers.set('location', path + query);
|
||||
return response;
|
||||
}
|
||||
|
||||
function handleRedirectHostRequest(
|
||||
function handleShortLinkRequest(
|
||||
application: Application,
|
||||
request: IncomingMessage,
|
||||
snapshot: ConfigurationSnapshot,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { openDb, readLinkVisitCounts } from '@goto/db';
|
||||
import { openDb, readLinkVisitCountsIncludingPending } from '@goto/db';
|
||||
|
||||
export async function readStoredLinkVisitCounts(
|
||||
databasePath: string,
|
||||
@@ -7,7 +7,7 @@ export async function readStoredLinkVisitCounts(
|
||||
try {
|
||||
const db = await openDb(databasePath, { readOnly: true });
|
||||
try {
|
||||
return await readLinkVisitCounts(db, links);
|
||||
return await readLinkVisitCountsIncludingPending(db, links);
|
||||
} finally {
|
||||
await db.destroy();
|
||||
}
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
import type { ReportScope, VisitTotals } from '@goto/analytics';
|
||||
import type { GotoConfiguration } from '@goto/config';
|
||||
import { openDb, readDomainVisitTotals, readOverviewVisitTotals, readVisitTotals } from '@goto/db';
|
||||
import {
|
||||
openDb,
|
||||
readDomainLifetimeVisitTotals as queryDomainLifetimeVisitTotals,
|
||||
readOverviewLifetimeVisitTotals as queryOverviewLifetimeVisitTotals,
|
||||
readLifetimeVisitTotals as queryLifetimeVisitTotals
|
||||
} from '@goto/db';
|
||||
import type { Immutable } from '../snapshot.js';
|
||||
|
||||
type VisitTotalsConfiguration = Immutable<Pick<GotoConfiguration, 'database'>>;
|
||||
@@ -9,14 +14,14 @@ export function readLifetimeVisitTotals(
|
||||
configuration: VisitTotalsConfiguration,
|
||||
scope: ReportScope
|
||||
): Promise<VisitTotals | null> {
|
||||
return readPreparedTotals(configuration, (db) => readVisitTotals(db, scope));
|
||||
return readPreparedTotals(configuration, (db) => queryLifetimeVisitTotals(db, scope));
|
||||
}
|
||||
|
||||
export function readOverviewLifetimeVisitTotals(
|
||||
configuration: VisitTotalsConfiguration,
|
||||
domains: readonly string[]
|
||||
) {
|
||||
return readPreparedTotals(configuration, (db) => readOverviewVisitTotals(db, domains));
|
||||
return readPreparedTotals(configuration, (db) => queryOverviewLifetimeVisitTotals(db, domains));
|
||||
}
|
||||
|
||||
export function readDomainLifetimeVisitTotals(
|
||||
@@ -24,7 +29,9 @@ export function readDomainLifetimeVisitTotals(
|
||||
domain: string,
|
||||
paths: readonly string[]
|
||||
) {
|
||||
return readPreparedTotals(configuration, (db) => readDomainVisitTotals(db, domain, paths));
|
||||
return readPreparedTotals(configuration, (db) =>
|
||||
queryDomainLifetimeVisitTotals(db, domain, paths)
|
||||
);
|
||||
}
|
||||
|
||||
async function readPreparedTotals<T>(
|
||||
|
||||
@@ -7,7 +7,7 @@ import {
|
||||
processVisitBatch,
|
||||
deleteExpiredVisitBatch,
|
||||
deleteProcessedVisitBatch,
|
||||
readVisitTotals,
|
||||
readLifetimeVisitTotals,
|
||||
type ClassifiedVisit
|
||||
} from '@goto/db';
|
||||
import { readVisitDetails } from '@goto/db/visit-reports';
|
||||
@@ -94,7 +94,10 @@ test('expires invalid clicks from their path report without adding to lifetime l
|
||||
await maintainVisits(ctx, reportNow);
|
||||
|
||||
expect(await readTotals(ctx.db, docs)).toEqual({ validVisits: 0, invalidVisits: 1 });
|
||||
expect(await readVisitTotals(ctx.db, docs)).toEqual({ validVisits: 0, invalidVisits: 0 });
|
||||
expect(await readLifetimeVisitTotals(ctx.db, docs)).toEqual({
|
||||
validVisits: 0,
|
||||
invalidVisits: 0
|
||||
});
|
||||
} finally {
|
||||
await ctx.db.destroy();
|
||||
}
|
||||
@@ -131,14 +134,23 @@ test('accounts visits once across a batch boundary and keeps the cutoff-day surv
|
||||
accept_language: 'en;q=0.8'
|
||||
});
|
||||
await maintainVisits(ctx, now);
|
||||
expect(await readVisitTotals(ctx.db, overview)).toEqual({
|
||||
expect(await readLifetimeVisitTotals(ctx.db, overview)).toEqual({
|
||||
validVisits: 1003,
|
||||
invalidVisits: 1
|
||||
});
|
||||
expect(await readVisitTotals(ctx.db, domain)).toEqual({ validVisits: 1003, invalidVisits: 1 });
|
||||
expect(await readVisitTotals(ctx.db, docs)).toEqual({ validVisits: 1002, invalidVisits: 0 });
|
||||
expect(await readVisitTotals(ctx.db, root)).toEqual({ validVisits: 1, invalidVisits: 0 });
|
||||
expect(await readVisitTotals(ctx.db, { ...docs, path: '/missing' })).toEqual({
|
||||
expect(await readLifetimeVisitTotals(ctx.db, domain)).toEqual({
|
||||
validVisits: 1003,
|
||||
invalidVisits: 1
|
||||
});
|
||||
expect(await readLifetimeVisitTotals(ctx.db, docs)).toEqual({
|
||||
validVisits: 1002,
|
||||
invalidVisits: 0
|
||||
});
|
||||
expect(await readLifetimeVisitTotals(ctx.db, root)).toEqual({
|
||||
validVisits: 1,
|
||||
invalidVisits: 0
|
||||
});
|
||||
expect(await readLifetimeVisitTotals(ctx.db, { ...docs, path: '/missing' })).toEqual({
|
||||
validVisits: 0,
|
||||
invalidVisits: 0
|
||||
});
|
||||
@@ -192,7 +204,10 @@ test('zero counts minimal pending visits before cleanup, retries marked leftover
|
||||
const failure = new Error('Processed cleanup interrupted');
|
||||
vi.mocked(deleteProcessedVisitBatch).mockRejectedValueOnce(failure);
|
||||
await expect(maintainVisits(ctx, reportNow)).rejects.toBe(failure);
|
||||
expect(await readVisitTotals(ctx.db, docs)).toEqual({ validVisits: 2, invalidVisits: 0 });
|
||||
expect(await readLifetimeVisitTotals(ctx.db, docs)).toEqual({
|
||||
validVisits: 2,
|
||||
invalidVisits: 0
|
||||
});
|
||||
expect(await ctx.db.selectFrom('visits').select('processed').orderBy('id').execute()).toEqual([
|
||||
{ processed: 1 },
|
||||
{ processed: 1 },
|
||||
@@ -203,10 +218,16 @@ test('zero counts minimal pending visits before cleanup, retries marked leftover
|
||||
expect(await ctx.db.selectFrom('visits').select('processed').execute()).toEqual([
|
||||
{ processed: 0 }
|
||||
]);
|
||||
expect(await readVisitTotals(ctx.db, docs)).toEqual({ validVisits: 2, invalidVisits: 0 });
|
||||
expect(await readLifetimeVisitTotals(ctx.db, docs)).toEqual({
|
||||
validVisits: 2,
|
||||
invalidVisits: 0
|
||||
});
|
||||
await maintainVisits(ctx, reportNow + 2);
|
||||
expect(await ctx.db.selectFrom('visits').selectAll().execute()).toEqual([]);
|
||||
expect(await readVisitTotals(ctx.db, docs)).toEqual({ validVisits: 3, invalidVisits: 0 });
|
||||
expect(await readLifetimeVisitTotals(ctx.db, docs)).toEqual({
|
||||
validVisits: 3,
|
||||
invalidVisits: 0
|
||||
});
|
||||
expect(addDailyStatistics).not.toHaveBeenCalled();
|
||||
} finally {
|
||||
await ctx.db.destroy();
|
||||
@@ -356,9 +377,15 @@ test('a failed pending batch rolls back counters and daily writes with its proce
|
||||
expect(await ctx.db.selectFrom('visits').select('processed').execute()).toEqual([
|
||||
{ processed: 0 }
|
||||
]);
|
||||
expect(await readVisitTotals(ctx.db, docs)).toEqual({ validVisits: 0, invalidVisits: 0 });
|
||||
expect(await readLifetimeVisitTotals(ctx.db, docs)).toEqual({
|
||||
validVisits: 0,
|
||||
invalidVisits: 0
|
||||
});
|
||||
await maintainVisits(ctx, reportNow);
|
||||
expect(await readVisitTotals(ctx.db, docs)).toEqual({ validVisits: 1, invalidVisits: 0 });
|
||||
expect(await readLifetimeVisitTotals(ctx.db, docs)).toEqual({
|
||||
validVisits: 1,
|
||||
invalidVisits: 0
|
||||
});
|
||||
expect((await readTotals(ctx.db, docs)).validVisits).toBe(1);
|
||||
expect(await ctx.db.selectFrom('visits').select('processed').execute()).toEqual([
|
||||
{ processed: 1 }
|
||||
|
||||
@@ -6,7 +6,7 @@ import { DatabaseSync } from 'node:sqlite';
|
||||
import { emptyDailyStatistics } from '@goto/analytics';
|
||||
import { ConfigError } from '@goto/config';
|
||||
import * as visitDatabase from '@goto/db';
|
||||
import { insertVisit, openDb, readVisitTotals, runMigrations } from '@goto/db';
|
||||
import { insertVisit, openDb, readLifetimeVisitTotals, runMigrations } from '@goto/db';
|
||||
import { addDailyVisitStatistics } from '@goto/db/visit-reports';
|
||||
import { Worker } from 'openworkflow';
|
||||
import { afterEach, beforeEach, expect, test, vi } from 'vitest';
|
||||
@@ -85,7 +85,7 @@ test('zero-policy startup accounts pending visits before readiness and clears ra
|
||||
try {
|
||||
expect(await reader.selectFrom('visits').selectAll().execute()).toEqual([]);
|
||||
expect(await reader.selectFrom('visit_daily_statistics').select('id').execute()).toEqual([]);
|
||||
expect(await readVisitTotals(reader, { kind: 'overview' })).toEqual({
|
||||
expect(await readLifetimeVisitTotals(reader, { kind: 'overview' })).toEqual({
|
||||
validVisits: 1,
|
||||
invalidVisits: 0
|
||||
});
|
||||
|
||||
@@ -9,10 +9,10 @@ export { runMigrations } from './migrate.js';
|
||||
export { insertVisit, purgeVisits } from './visits.js';
|
||||
export {
|
||||
addVisitTotals,
|
||||
readVisitTotals,
|
||||
readOverviewVisitTotals,
|
||||
readDomainVisitTotals,
|
||||
readLinkVisitCounts
|
||||
readLifetimeVisitTotals,
|
||||
readOverviewLifetimeVisitTotals,
|
||||
readDomainLifetimeVisitTotals,
|
||||
readLinkVisitCountsIncludingPending
|
||||
} from './visit-totals.js';
|
||||
export {
|
||||
captureVisitBoundary,
|
||||
|
||||
@@ -9,7 +9,7 @@ import {
|
||||
addVisitTotals,
|
||||
insertVisit,
|
||||
openDb,
|
||||
readLinkVisitCounts,
|
||||
readLinkVisitCountsIncludingPending,
|
||||
runMigrations,
|
||||
type ClassifiedVisit
|
||||
} from './index.js';
|
||||
@@ -147,7 +147,7 @@ test('link recovery keeps the same visit counts across an uncommitted worker rol
|
||||
{ ...links[0], visits: 1 },
|
||||
{ ...links[1], visits: 0 }
|
||||
];
|
||||
const recovery = () => readLinkVisitCounts(reader!, links);
|
||||
const recovery = () => readLinkVisitCountsIncludingPending(reader!, links);
|
||||
expect(await recovery()).toEqual(expected);
|
||||
await writer.transaction().execute(async (transaction) => {
|
||||
await addVisitTotals(transaction, [
|
||||
|
||||
@@ -3,10 +3,10 @@ import { afterEach, beforeEach, expect, test } from 'vitest';
|
||||
import {
|
||||
addVisitTotals,
|
||||
openDb,
|
||||
readDomainVisitTotals,
|
||||
readOverviewVisitTotals,
|
||||
readVisitTotals,
|
||||
readLinkVisitCounts,
|
||||
readDomainLifetimeVisitTotals,
|
||||
readOverviewLifetimeVisitTotals,
|
||||
readLifetimeVisitTotals,
|
||||
readLinkVisitCountsIncludingPending,
|
||||
runMigrations,
|
||||
type Database
|
||||
} from './index.js';
|
||||
@@ -24,7 +24,7 @@ beforeEach(async () => {
|
||||
afterEach(async () => db.destroy());
|
||||
|
||||
test('adds numeric totals per scope inside a caller transaction and reads missing scopes as zero', async () => {
|
||||
expect(await readVisitTotals(db, overview)).toEqual({ validVisits: 0, invalidVisits: 0 });
|
||||
expect(await readLifetimeVisitTotals(db, overview)).toEqual({ validVisits: 0, invalidVisits: 0 });
|
||||
const additions = [
|
||||
{ scope: overview, totals: { validVisits: 2, invalidVisits: 1 } },
|
||||
{ scope: domain, totals: { validVisits: 2, invalidVisits: 1 } },
|
||||
@@ -35,10 +35,10 @@ test('adds numeric totals per scope inside a caller transaction and reads missin
|
||||
await addVisitTotals(transaction, additions);
|
||||
await addVisitTotals(transaction, additions);
|
||||
});
|
||||
expect(await readVisitTotals(db, overview)).toEqual({ validVisits: 4, invalidVisits: 2 });
|
||||
expect(await readVisitTotals(db, domain)).toEqual({ validVisits: 4, invalidVisits: 2 });
|
||||
expect(await readVisitTotals(db, link)).toEqual({ validVisits: 4, invalidVisits: 0 });
|
||||
expect(await readVisitTotals(db, { ...link, path: '/' })).toEqual({
|
||||
expect(await readLifetimeVisitTotals(db, overview)).toEqual({ validVisits: 4, invalidVisits: 2 });
|
||||
expect(await readLifetimeVisitTotals(db, domain)).toEqual({ validVisits: 4, invalidVisits: 2 });
|
||||
expect(await readLifetimeVisitTotals(db, link)).toEqual({ validVisits: 4, invalidVisits: 0 });
|
||||
expect(await readLifetimeVisitTotals(db, { ...link, path: '/' })).toEqual({
|
||||
validVisits: 0,
|
||||
invalidVisits: 0
|
||||
});
|
||||
@@ -60,7 +60,7 @@ test('separates lifetime Root visits, including former domains in the overview',
|
||||
])
|
||||
);
|
||||
|
||||
expect(await readOverviewVisitTotals(db, [domain.domain, 'empty.example.test'])).toEqual({
|
||||
expect(await readOverviewLifetimeVisitTotals(db, [domain.domain, 'empty.example.test'])).toEqual({
|
||||
overview: { validVisits: 3, invalidVisits: 2, rootVisits: 3 },
|
||||
byDomain: new Map([
|
||||
[domain.domain, { validVisits: 3, invalidVisits: 1 }],
|
||||
@@ -83,7 +83,7 @@ test('reads stored domain, Root, and current path totals without including other
|
||||
])
|
||||
);
|
||||
|
||||
expect(await readDomainVisitTotals(db, domain.domain, ['/docs', '/new'])).toEqual({
|
||||
expect(await readDomainLifetimeVisitTotals(db, domain.domain, ['/docs', '/new'])).toEqual({
|
||||
domain: { validVisits: 7, invalidVisits: 2, rootVisits: 2 },
|
||||
byPath: new Map([
|
||||
['/', { validVisits: 2, invalidVisits: 0 }],
|
||||
@@ -109,8 +109,8 @@ test('unsafe accumulated totals roll back the whole addition transaction', async
|
||||
])
|
||||
)
|
||||
).rejects.toThrow();
|
||||
expect(await readVisitTotals(db, overview)).toEqual({ validVisits: 0, invalidVisits: 0 });
|
||||
expect(await readVisitTotals(db, link)).toEqual({
|
||||
expect(await readLifetimeVisitTotals(db, overview)).toEqual({ validVisits: 0, invalidVisits: 0 });
|
||||
expect(await readLifetimeVisitTotals(db, link)).toEqual({
|
||||
validVisits: Number.MAX_SAFE_INTEGER,
|
||||
invalidVisits: 0
|
||||
});
|
||||
@@ -118,6 +118,6 @@ test('unsafe accumulated totals roll back the whole addition transaction', async
|
||||
|
||||
test('missing storage is a read failure, not a fabricated zero', async () => {
|
||||
await db.schema.dropTable('visit_totals').execute();
|
||||
await expect(readVisitTotals(db, link)).rejects.toThrow();
|
||||
await expect(readLinkVisitCounts(db, [link])).rejects.toThrow();
|
||||
await expect(readLifetimeVisitTotals(db, link)).rejects.toThrow();
|
||||
await expect(readLinkVisitCountsIncludingPending(db, [link])).rejects.toThrow();
|
||||
});
|
||||
@@ -31,7 +31,7 @@ export async function addVisitTotals(
|
||||
}
|
||||
}
|
||||
|
||||
export async function readVisitTotals(
|
||||
export async function readLifetimeVisitTotals(
|
||||
db: Kysely<Database>,
|
||||
scope: ReportScope
|
||||
): Promise<VisitTotals> {
|
||||
@@ -49,7 +49,7 @@ export async function readVisitTotals(
|
||||
|
||||
type AdminVisitTotals = VisitTotals & { rootVisits: number };
|
||||
|
||||
export async function readOverviewVisitTotals(
|
||||
export async function readOverviewLifetimeVisitTotals(
|
||||
db: Kysely<Database>,
|
||||
domains: readonly string[]
|
||||
): Promise<{ overview: AdminVisitTotals; byDomain: Map<string, VisitTotals> }> {
|
||||
@@ -95,7 +95,7 @@ export async function readOverviewVisitTotals(
|
||||
return { overview: separateRootVisits(overview, rootVisits), byDomain };
|
||||
}
|
||||
|
||||
export async function readDomainVisitTotals(
|
||||
export async function readDomainLifetimeVisitTotals(
|
||||
db: Kysely<Database>,
|
||||
domain: string,
|
||||
currentPaths: readonly string[]
|
||||
@@ -132,7 +132,7 @@ export async function readDomainVisitTotals(
|
||||
};
|
||||
}
|
||||
|
||||
export async function readLinkVisitCounts(
|
||||
export async function readLinkVisitCountsIncludingPending(
|
||||
db: Kysely<Database>,
|
||||
links: readonly { domain: string; path: string }[]
|
||||
): Promise<Array<{ domain: string; path: string; visits: number }>> {
|
||||
|
||||
Reference in new issue
Block a user