@iann29/rastro 0.1.0-alpha.4 → 0.1.0-alpha.6
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/README.md +203 -56
- package/agent/integration.md +17 -15
- package/agent/manifest.json +6 -4
- package/agent/manifest.schema.json +8 -5
- package/dist/client/federation.d.ts +34 -8
- package/dist/client/federation.d.ts.map +1 -1
- package/dist/client/federation.js +17 -3
- package/dist/client/federation.js.map +1 -1
- package/dist/client/index.d.ts +486 -31
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +209 -41
- package/dist/client/index.js.map +1 -1
- package/dist/component/_generated/api.d.ts +8 -0
- package/dist/component/_generated/api.d.ts.map +1 -1
- package/dist/component/_generated/api.js.map +1 -1
- package/dist/component/_generated/component.d.ts +82 -8
- package/dist/component/_generated/component.d.ts.map +1 -1
- package/dist/component/affiliates.d.ts.map +1 -1
- package/dist/component/affiliates.js +6 -2
- package/dist/component/affiliates.js.map +1 -1
- package/dist/component/cardinality.d.ts.map +1 -1
- package/dist/component/cardinality.js.map +1 -1
- package/dist/component/constants.d.ts +6 -0
- package/dist/component/constants.d.ts.map +1 -1
- package/dist/component/constants.js +12 -0
- package/dist/component/constants.js.map +1 -1
- package/dist/component/diagnostics.d.ts.map +1 -1
- package/dist/component/diagnostics.js.map +1 -1
- package/dist/component/eventStore.d.ts +3 -1
- package/dist/component/eventStore.d.ts.map +1 -1
- package/dist/component/eventStore.js +13 -10
- package/dist/component/eventStore.js.map +1 -1
- package/dist/component/funnels.d.ts.map +1 -1
- package/dist/component/funnels.js +5 -3
- package/dist/component/funnels.js.map +1 -1
- package/dist/component/geo.d.ts.map +1 -1
- package/dist/component/geo.js +3 -2
- package/dist/component/geo.js.map +1 -1
- package/dist/component/goals.d.ts.map +1 -1
- package/dist/component/goals.js +8 -5
- package/dist/component/goals.js.map +1 -1
- package/dist/component/guards.d.ts.map +1 -1
- package/dist/component/guards.js.map +1 -1
- package/dist/component/http.d.ts.map +1 -1
- package/dist/component/http.js +71 -37
- package/dist/component/http.js.map +1 -1
- package/dist/component/identity.d.ts +13 -0
- package/dist/component/identity.d.ts.map +1 -0
- package/dist/component/identity.js +58 -0
- package/dist/component/identity.js.map +1 -0
- package/dist/component/ingest.d.ts +3 -1
- package/dist/component/ingest.d.ts.map +1 -1
- package/dist/component/ingest.js +168 -29
- package/dist/component/ingest.js.map +1 -1
- package/dist/component/live.d.ts.map +1 -1
- package/dist/component/live.js +1 -3
- package/dist/component/live.js.map +1 -1
- package/dist/component/reports.d.ts +70 -4
- package/dist/component/reports.d.ts.map +1 -1
- package/dist/component/reports.js +232 -59
- package/dist/component/reports.js.map +1 -1
- package/dist/component/retention.d.ts +4 -4
- package/dist/component/retention.d.ts.map +1 -1
- package/dist/component/retention.js +54 -22
- package/dist/component/retention.js.map +1 -1
- package/dist/component/sanitize.d.ts.map +1 -1
- package/dist/component/sanitize.js +42 -11
- package/dist/component/sanitize.js.map +1 -1
- package/dist/component/schema.d.ts +83 -10
- package/dist/component/schema.js +34 -1
- package/dist/component/schema.js.map +1 -1
- package/dist/component/sites.d.ts.map +1 -1
- package/dist/component/sites.js +6 -6
- package/dist/component/sites.js.map +1 -1
- package/dist/component/useragent.d.ts +9 -0
- package/dist/component/useragent.d.ts.map +1 -0
- package/dist/component/useragent.js +152 -0
- package/dist/component/useragent.js.map +1 -0
- package/dist/component/validators.d.ts +29 -18
- package/dist/component/validators.d.ts.map +1 -1
- package/dist/component/validators.js +8 -2
- package/dist/component/validators.js.map +1 -1
- package/dist/component/visitors.d.ts +19 -0
- package/dist/component/visitors.d.ts.map +1 -0
- package/dist/component/visitors.js +86 -0
- package/dist/component/visitors.js.map +1 -0
- package/dist/component/vitals.d.ts +41 -0
- package/dist/component/vitals.d.ts.map +1 -0
- package/dist/component/vitals.js +115 -0
- package/dist/component/vitals.js.map +1 -0
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react/index.js.map +1 -1
- package/dist/tracker/generated.d.ts +8 -4
- package/dist/tracker/generated.d.ts.map +1 -1
- package/dist/tracker/generated.js +8 -4
- package/dist/tracker/generated.js.map +1 -1
- package/dist/tracker/tracker.d.ts.map +1 -1
- package/dist/tracker/tracker.js +23 -11
- package/dist/tracker/tracker.js.map +1 -1
- package/dist/tracker/vitals.d.ts +10 -0
- package/dist/tracker/vitals.d.ts.map +1 -0
- package/dist/tracker/vitals.js +137 -0
- package/dist/tracker/vitals.js.map +1 -0
- package/dist/tracker.min.js +1 -1
- package/dist/vitals.min.js +1 -0
- package/docs/benchmarks/2026-08-20-realistic.md +71 -71
- package/docs/benchmarks/2026-08-21-formal-certification.md +50 -30
- package/docs/benchmarks/2026-08-30-alpha6-recertification.md +206 -0
- package/docs/federation.md +1 -1
- package/package.json +27 -9
- package/scripts/benchmark-ingest.mjs +101 -48
- package/src/component/_generated/api.ts +8 -0
- package/src/component/_generated/component.ts +87 -5
- package/src/component/affiliates.ts +20 -5
- package/src/component/cardinality.ts +8 -7
- package/src/component/constants.ts +12 -0
- package/src/component/diagnostics.ts +3 -2
- package/src/component/eventStore.ts +53 -46
- package/src/component/funnels.ts +19 -16
- package/src/component/geo.ts +16 -14
- package/src/component/goals.ts +29 -21
- package/src/component/guards.ts +3 -1
- package/src/component/http.ts +126 -78
- package/src/component/identity.ts +74 -0
- package/src/component/ingest.ts +349 -130
- package/src/component/live.ts +5 -4
- package/src/component/reports.ts +572 -221
- package/src/component/retention.ts +221 -98
- package/src/component/sanitize.ts +77 -27
- package/src/component/schema.ts +46 -5
- package/src/component/sites.ts +22 -11
- package/src/component/useragent.ts +171 -0
- package/src/component/validators.ts +19 -7
- package/src/component/visitors.ts +116 -0
- package/src/component/vitals.ts +146 -0
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { v } from "convex/values";
|
|
2
|
+
import { MAX_VISITOR_ALIASES } from "./constants.js";
|
|
3
|
+
import { fail } from "./errors.js";
|
|
4
|
+
import { sanitizeOpaqueId } from "./sanitize.js";
|
|
5
|
+
import type { Id } from "./_generated/dataModel.js";
|
|
6
|
+
import { mutation, type QueryCtx } from "./_generated/server.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Folds an anonymous visitor id into the pseudonymous identity the host
|
|
10
|
+
* supplies after signup or sign-in. Links are one level deep: an alias can
|
|
11
|
+
* belong to one identity, an identity can never become an alias, and an
|
|
12
|
+
* alias can never own aliases, so journeys stay bounded and unambiguous.
|
|
13
|
+
*/
|
|
14
|
+
export const link = mutation({
|
|
15
|
+
args: {
|
|
16
|
+
siteId: v.id("sites"),
|
|
17
|
+
visitorId: v.string(),
|
|
18
|
+
previousVisitorId: v.string(),
|
|
19
|
+
},
|
|
20
|
+
returns: v.object({ linked: v.boolean(), aliasCount: v.number() }),
|
|
21
|
+
handler: async (ctx, args) => {
|
|
22
|
+
const site = await ctx.db.get("sites", args.siteId);
|
|
23
|
+
if (!site) fail("NOT_FOUND", "site not found");
|
|
24
|
+
let visitorId: string;
|
|
25
|
+
let previousVisitorId: string;
|
|
26
|
+
try {
|
|
27
|
+
visitorId = sanitizeOpaqueId(args.visitorId, "visitorId");
|
|
28
|
+
previousVisitorId = sanitizeOpaqueId(
|
|
29
|
+
args.previousVisitorId,
|
|
30
|
+
"previousVisitorId",
|
|
31
|
+
);
|
|
32
|
+
} catch (error) {
|
|
33
|
+
fail(
|
|
34
|
+
"INVALID_ARGUMENT",
|
|
35
|
+
error instanceof Error ? error.message : "invalid visitor id",
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
if (visitorId === previousVisitorId) {
|
|
39
|
+
fail("INVALID_ARGUMENT", "previousVisitorId must differ from visitorId");
|
|
40
|
+
}
|
|
41
|
+
if (await findAlias(ctx, args.siteId, visitorId)) {
|
|
42
|
+
fail("CONFLICT", "visitorId is already linked to another visitor");
|
|
43
|
+
}
|
|
44
|
+
const previousOwnsAliases = await ctx.db
|
|
45
|
+
.query("visitorAliases")
|
|
46
|
+
.withIndex("by_siteId_and_visitorId", (range) =>
|
|
47
|
+
range.eq("siteId", args.siteId).eq("visitorId", previousVisitorId),
|
|
48
|
+
)
|
|
49
|
+
.first();
|
|
50
|
+
if (previousOwnsAliases) {
|
|
51
|
+
fail("CONFLICT", "previousVisitorId already owns linked visitors");
|
|
52
|
+
}
|
|
53
|
+
const aliases = await ctx.db
|
|
54
|
+
.query("visitorAliases")
|
|
55
|
+
.withIndex("by_siteId_and_visitorId", (range) =>
|
|
56
|
+
range.eq("siteId", args.siteId).eq("visitorId", visitorId),
|
|
57
|
+
)
|
|
58
|
+
.take(MAX_VISITOR_ALIASES);
|
|
59
|
+
const existing = await findAlias(ctx, args.siteId, previousVisitorId);
|
|
60
|
+
if (existing) {
|
|
61
|
+
if (existing.visitorId !== visitorId) {
|
|
62
|
+
fail(
|
|
63
|
+
"CONFLICT",
|
|
64
|
+
"previousVisitorId is already linked to a different visitor",
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
return { linked: false, aliasCount: aliases.length };
|
|
68
|
+
}
|
|
69
|
+
if (aliases.length >= MAX_VISITOR_ALIASES) {
|
|
70
|
+
fail(
|
|
71
|
+
"LIMIT_EXCEEDED",
|
|
72
|
+
`a visitor can hold at most ${MAX_VISITOR_ALIASES} aliases`,
|
|
73
|
+
{
|
|
74
|
+
limit: MAX_VISITOR_ALIASES,
|
|
75
|
+
},
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
await ctx.db.insert("visitorAliases", {
|
|
79
|
+
siteId: args.siteId,
|
|
80
|
+
visitorId,
|
|
81
|
+
previousVisitorId,
|
|
82
|
+
linkedAt: Date.now(),
|
|
83
|
+
});
|
|
84
|
+
return { linked: true, aliasCount: aliases.length + 1 };
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
/** The identity a visitor id resolves to, followed by every alias it owns. */
|
|
89
|
+
export async function resolveVisitorIdentities(
|
|
90
|
+
ctx: QueryCtx,
|
|
91
|
+
siteId: Id<"sites">,
|
|
92
|
+
visitorId: string,
|
|
93
|
+
): Promise<string[]> {
|
|
94
|
+
const alias = await findAlias(ctx, siteId, visitorId);
|
|
95
|
+
const identity = alias?.visitorId ?? visitorId;
|
|
96
|
+
const aliases = await ctx.db
|
|
97
|
+
.query("visitorAliases")
|
|
98
|
+
.withIndex("by_siteId_and_visitorId", (range) =>
|
|
99
|
+
range.eq("siteId", siteId).eq("visitorId", identity),
|
|
100
|
+
)
|
|
101
|
+
.take(MAX_VISITOR_ALIASES);
|
|
102
|
+
return [identity, ...aliases.map((row) => row.previousVisitorId)];
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function findAlias(
|
|
106
|
+
ctx: QueryCtx,
|
|
107
|
+
siteId: Id<"sites">,
|
|
108
|
+
previousVisitorId: string,
|
|
109
|
+
) {
|
|
110
|
+
return ctx.db
|
|
111
|
+
.query("visitorAliases")
|
|
112
|
+
.withIndex("by_siteId_and_previousVisitorId", (range) =>
|
|
113
|
+
range.eq("siteId", siteId).eq("previousVisitorId", previousVisitorId),
|
|
114
|
+
)
|
|
115
|
+
.unique();
|
|
116
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { VITAL_HISTOGRAM_EDGES, MAX_VITAL_VALUE } from "./constants.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Field-measured Web Vitals. Values are integers: milliseconds for the time
|
|
5
|
+
* metrics, and CLS scaled by 1000 so one shared histogram covers every metric.
|
|
6
|
+
*/
|
|
7
|
+
export const VITAL_METRICS = ["LCP", "CLS", "INP", "FCP", "TTFB"] as const;
|
|
8
|
+
|
|
9
|
+
export type VitalMetric = (typeof VITAL_METRICS)[number];
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The Google-published rating thresholds. Every threshold is also a histogram
|
|
13
|
+
* edge, so good/needs-improvement/poor counts are exact, never interpolated.
|
|
14
|
+
*/
|
|
15
|
+
export const VITAL_THRESHOLDS: Record<
|
|
16
|
+
VitalMetric,
|
|
17
|
+
{ good: number; poor: number }
|
|
18
|
+
> = {
|
|
19
|
+
LCP: { good: 2_500, poor: 4_000 },
|
|
20
|
+
CLS: { good: 100, poor: 250 },
|
|
21
|
+
INP: { good: 200, poor: 500 },
|
|
22
|
+
FCP: { good: 1_800, poor: 3_000 },
|
|
23
|
+
TTFB: { good: 800, poor: 1_800 },
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/** Sentinel row keys; sanitized paths always start with "/" so neither collides. */
|
|
27
|
+
export const VITAL_ALL = "(all)";
|
|
28
|
+
export const VITAL_OTHER_PAGES = "(other)";
|
|
29
|
+
|
|
30
|
+
/** Bounded device classes; anything else folds into "unknown". */
|
|
31
|
+
export const VITAL_DEVICES = ["desktop", "mobile", "tablet"] as const;
|
|
32
|
+
|
|
33
|
+
export function vitalDevice(device: string | undefined): string {
|
|
34
|
+
return device && (VITAL_DEVICES as readonly string[]).includes(device)
|
|
35
|
+
? device
|
|
36
|
+
: "unknown";
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export const VITAL_HISTOGRAM_BUCKETS = VITAL_HISTOGRAM_EDGES.length + 1;
|
|
40
|
+
|
|
41
|
+
export function isVitalMetric(value: string): value is VitalMetric {
|
|
42
|
+
return (VITAL_METRICS as readonly string[]).includes(value);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function isValidVitalValue(value: number): boolean {
|
|
46
|
+
return Number.isFinite(value) && value >= 0 && value <= MAX_VITAL_VALUE;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function emptyVitalHistogram(): number[] {
|
|
50
|
+
return Array.from({ length: VITAL_HISTOGRAM_BUCKETS }, () => 0);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Bucket i holds values in (edge[i-1], edge[i]]; the last bucket is open. */
|
|
54
|
+
export function vitalHistogramBucket(value: number): number {
|
|
55
|
+
for (let index = 0; index < VITAL_HISTOGRAM_EDGES.length; index += 1) {
|
|
56
|
+
if (value <= VITAL_HISTOGRAM_EDGES[index]) return index;
|
|
57
|
+
}
|
|
58
|
+
return VITAL_HISTOGRAM_EDGES.length;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function addToVitalHistogram(
|
|
62
|
+
histogram: number[],
|
|
63
|
+
value: number,
|
|
64
|
+
): number[] {
|
|
65
|
+
const next = normalizedVitalHistogram(histogram);
|
|
66
|
+
next[vitalHistogramBucket(value)] += 1;
|
|
67
|
+
return next;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function mergeVitalHistograms(
|
|
71
|
+
left: number[],
|
|
72
|
+
right: number[],
|
|
73
|
+
): number[] {
|
|
74
|
+
const result = normalizedVitalHistogram(left);
|
|
75
|
+
const addition = normalizedVitalHistogram(right);
|
|
76
|
+
for (let index = 0; index < result.length; index += 1) {
|
|
77
|
+
result[index] += addition[index];
|
|
78
|
+
}
|
|
79
|
+
return result;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Percentile estimated by linear interpolation inside the winning bucket. The
|
|
84
|
+
* top bucket is open-ended, so estimates saturate at the highest edge; a
|
|
85
|
+
* reported p75 equal to that edge means "at least this much".
|
|
86
|
+
*/
|
|
87
|
+
export function vitalHistogramPercentile(
|
|
88
|
+
histogram: number[],
|
|
89
|
+
fraction: number,
|
|
90
|
+
): number {
|
|
91
|
+
const buckets = normalizedVitalHistogram(histogram);
|
|
92
|
+
const total = buckets.reduce((sum, count) => sum + count, 0);
|
|
93
|
+
if (total === 0) return 0;
|
|
94
|
+
const target = Math.ceil(total * fraction);
|
|
95
|
+
let cumulative = 0;
|
|
96
|
+
for (let index = 0; index < buckets.length; index += 1) {
|
|
97
|
+
const count = buckets[index];
|
|
98
|
+
if (count === 0) continue;
|
|
99
|
+
if (cumulative + count >= target) {
|
|
100
|
+
const lower = index === 0 ? 0 : VITAL_HISTOGRAM_EDGES[index - 1];
|
|
101
|
+
const upper =
|
|
102
|
+
index < VITAL_HISTOGRAM_EDGES.length
|
|
103
|
+
? VITAL_HISTOGRAM_EDGES[index]
|
|
104
|
+
: VITAL_HISTOGRAM_EDGES[VITAL_HISTOGRAM_EDGES.length - 1];
|
|
105
|
+
const position = (target - cumulative) / count;
|
|
106
|
+
return Math.round(Math.min(upper, lower + position * (upper - lower)));
|
|
107
|
+
}
|
|
108
|
+
cumulative += count;
|
|
109
|
+
}
|
|
110
|
+
return VITAL_HISTOGRAM_EDGES[VITAL_HISTOGRAM_EDGES.length - 1];
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Exact rating counts; every threshold is a bucket edge by construction. */
|
|
114
|
+
export function vitalRatingCounts(
|
|
115
|
+
histogram: number[],
|
|
116
|
+
metric: VitalMetric,
|
|
117
|
+
): { good: number; needsImprovement: number; poor: number } {
|
|
118
|
+
const buckets = normalizedVitalHistogram(histogram);
|
|
119
|
+
const thresholds = VITAL_THRESHOLDS[metric];
|
|
120
|
+
let good = 0;
|
|
121
|
+
let needsImprovement = 0;
|
|
122
|
+
let poor = 0;
|
|
123
|
+
for (let index = 0; index < buckets.length; index += 1) {
|
|
124
|
+
const upper =
|
|
125
|
+
index < VITAL_HISTOGRAM_EDGES.length
|
|
126
|
+
? VITAL_HISTOGRAM_EDGES[index]
|
|
127
|
+
: Number.POSITIVE_INFINITY;
|
|
128
|
+
if (upper <= thresholds.good) good += buckets[index];
|
|
129
|
+
else if (upper <= thresholds.poor) needsImprovement += buckets[index];
|
|
130
|
+
else poor += buckets[index];
|
|
131
|
+
}
|
|
132
|
+
return { good, needsImprovement, poor };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function normalizedVitalHistogram(histogram: number[]): number[] {
|
|
136
|
+
const result = Array.from({ length: VITAL_HISTOGRAM_BUCKETS }, () => 0);
|
|
137
|
+
for (
|
|
138
|
+
let index = 0;
|
|
139
|
+
index < Math.min(histogram.length, result.length);
|
|
140
|
+
index += 1
|
|
141
|
+
) {
|
|
142
|
+
const count = histogram[index];
|
|
143
|
+
result[index] = Number.isSafeInteger(count) && count > 0 ? count : 0;
|
|
144
|
+
}
|
|
145
|
+
return result;
|
|
146
|
+
}
|