@vllnt/convex-analytics 0.1.0-canary.e8e8f2b → 0.1.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/LICENSE +21 -0
- package/README.md +198 -0
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js.map +1 -1
- package/dist/component/_generated/api.d.ts +30 -6
- package/dist/component/_generated/api.d.ts.map +1 -1
- package/dist/component/_generated/api.js +21 -2
- package/dist/component/_generated/api.js.map +1 -1
- package/dist/component/_generated/component.d.ts +181 -0
- package/dist/component/_generated/component.d.ts.map +1 -0
- package/dist/component/_generated/component.js +11 -0
- package/dist/component/_generated/component.js.map +1 -0
- package/dist/component/_generated/dataModel.d.ts +33 -0
- package/dist/component/_generated/dataModel.d.ts.map +1 -1
- package/dist/component/_generated/dataModel.js +3 -0
- package/dist/component/_generated/dataModel.js.map +1 -1
- package/dist/component/_generated/server.d.ts +113 -0
- package/dist/component/_generated/server.d.ts.map +1 -1
- package/dist/component/_generated/server.js +65 -0
- package/dist/component/_generated/server.js.map +1 -1
- package/dist/component/convex.config.d.ts +1 -1
- package/dist/component/convex.config.d.ts.map +1 -1
- package/dist/component/crons.d.ts +3 -0
- package/dist/component/crons.d.ts.map +1 -0
- package/dist/component/crons.js +7 -0
- package/dist/component/crons.js.map +1 -0
- package/dist/component/http.d.ts.map +1 -1
- package/dist/component/internal_mutations.d.ts +5 -2
- package/dist/component/internal_mutations.d.ts.map +1 -1
- package/dist/component/internal_mutations.js +72 -42
- package/dist/component/internal_mutations.js.map +1 -1
- package/dist/component/mutations.d.ts +11 -11
- package/dist/component/mutations.d.ts.map +1 -1
- package/dist/component/mutations.js +24 -9
- package/dist/component/mutations.js.map +1 -1
- package/dist/component/queries.d.ts +21 -21
- package/dist/component/queries.d.ts.map +1 -1
- package/dist/component/queries.js +20 -20
- package/dist/component/queries.js.map +1 -1
- package/dist/component/schema.d.ts +35 -26
- package/dist/component/schema.d.ts.map +1 -1
- package/dist/component/schema.js +6 -1
- package/dist/component/schema.js.map +1 -1
- package/dist/component/validators.d.ts +40 -40
- package/dist/shared.d.ts +2 -0
- package/dist/shared.d.ts.map +1 -1
- package/dist/shared.js +4 -0
- package/dist/shared.js.map +1 -1
- package/dist/test.d.ts +35 -26
- package/dist/test.d.ts.map +1 -1
- package/dist/test.js.map +1 -1
- package/dist/web/index.d.ts.map +1 -1
- package/package.json +25 -20
- package/src/component/_generated/api.ts +36 -9
- package/src/component/_generated/component.ts +196 -0
- package/src/component/_generated/dataModel.ts +36 -0
- package/src/component/_generated/server.ts +130 -3
- package/src/component/crons.ts +19 -0
- package/src/component/internal_mutations.ts +102 -55
- package/src/component/mutations.ts +26 -9
- package/src/component/queries.ts +22 -20
- package/src/component/schema.ts +7 -1
- package/src/shared.ts +5 -0
|
@@ -6,7 +6,7 @@ import { DirectAggregate } from "@convex-dev/aggregate";
|
|
|
6
6
|
import { ShardedCounter } from "@convex-dev/sharded-counter";
|
|
7
7
|
import { RateLimiter, MINUTE } from "@convex-dev/rate-limiter";
|
|
8
8
|
import { propsValidator, granularitiesValidator } from "./validators.js";
|
|
9
|
-
import { bucketStart, valKey } from "../shared.js";
|
|
9
|
+
import { bucketStart, tupleKey, valKey } from "../shared.js";
|
|
10
10
|
import type { Granularity, Scalar } from "../shared.js";
|
|
11
11
|
|
|
12
12
|
type AggregateType = { Key: number; Id: string; Namespace: string };
|
|
@@ -25,6 +25,16 @@ const rateLimiter = new RateLimiter(components.rateLimiter as never, {
|
|
|
25
25
|
|
|
26
26
|
const TOTAL = "";
|
|
27
27
|
|
|
28
|
+
async function ensureScope(ctx: MutationCtx, scope: string): Promise<void> {
|
|
29
|
+
const existing = await ctx.db
|
|
30
|
+
.query("scopes")
|
|
31
|
+
.withIndex("by_scope", (q) => q.eq("scope", scope))
|
|
32
|
+
.unique();
|
|
33
|
+
if (existing === null) {
|
|
34
|
+
await ctx.db.insert("scopes", { scope });
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
28
38
|
/** Increment one rollup row `(scope,name,gran,bucket,dim,val)` by 1, inserting if absent. */
|
|
29
39
|
async function bumpRollup(
|
|
30
40
|
ctx: MutationCtx,
|
|
@@ -48,7 +58,7 @@ async function bumpRollup(
|
|
|
48
58
|
)
|
|
49
59
|
.unique();
|
|
50
60
|
if (existing) {
|
|
51
|
-
await ctx.db.patch(existing._id, { count: existing.count + 1 });
|
|
61
|
+
await ctx.db.patch("rollups", existing._id, { count: existing.count + 1 });
|
|
52
62
|
} else {
|
|
53
63
|
await ctx.db.insert("rollups", {
|
|
54
64
|
scope,
|
|
@@ -84,7 +94,9 @@ export const track = mutation({
|
|
|
84
94
|
const grans = args.granularities.length > 0 ? args.granularities : (["day"] as const);
|
|
85
95
|
|
|
86
96
|
if (args.sessionRef !== undefined) {
|
|
87
|
-
const rl = await rateLimiter.limit(ctx, "trackEvent", {
|
|
97
|
+
const rl = await rateLimiter.limit(ctx, "trackEvent", {
|
|
98
|
+
key: tupleKey(scope, args.sessionRef),
|
|
99
|
+
});
|
|
88
100
|
if (!rl.ok) {
|
|
89
101
|
return "dropped";
|
|
90
102
|
}
|
|
@@ -118,6 +130,8 @@ export const track = mutation({
|
|
|
118
130
|
}
|
|
119
131
|
const seq = sessionDoc ? sessionDoc.eventCount : 0;
|
|
120
132
|
|
|
133
|
+
await ensureScope(ctx, scope);
|
|
134
|
+
|
|
121
135
|
const eventId = await ctx.db.insert("events", {
|
|
122
136
|
scope,
|
|
123
137
|
name: args.name,
|
|
@@ -131,8 +145,9 @@ export const track = mutation({
|
|
|
131
145
|
|
|
132
146
|
if (args.sessionRef !== undefined) {
|
|
133
147
|
if (sessionDoc) {
|
|
134
|
-
await ctx.db.patch(sessionDoc._id, {
|
|
135
|
-
lastTs: ts,
|
|
148
|
+
await ctx.db.patch("sessions", sessionDoc._id, {
|
|
149
|
+
lastTs: Math.max(ts, sessionDoc.lastTs),
|
|
150
|
+
endTs: undefined,
|
|
136
151
|
eventCount: sessionDoc.eventCount + 1,
|
|
137
152
|
subjectRef: args.subjectRef ?? sessionDoc.subjectRef,
|
|
138
153
|
});
|
|
@@ -156,7 +171,7 @@ export const track = mutation({
|
|
|
156
171
|
)
|
|
157
172
|
.unique();
|
|
158
173
|
if (subject) {
|
|
159
|
-
await ctx.db.patch(subject._id, {
|
|
174
|
+
await ctx.db.patch("subjects", subject._id, {
|
|
160
175
|
lastSeen: ts > subject.lastSeen ? ts : subject.lastSeen,
|
|
161
176
|
firstSeen: ts < subject.firstSeen ? ts : subject.firstSeen,
|
|
162
177
|
eventCount: subject.eventCount + 1,
|
|
@@ -193,9 +208,9 @@ export const track = mutation({
|
|
|
193
208
|
await aggregate.insert(ctx, {
|
|
194
209
|
key: ts,
|
|
195
210
|
id: eventId as string,
|
|
196
|
-
namespace:
|
|
211
|
+
namespace: tupleKey(scope, args.name),
|
|
197
212
|
});
|
|
198
|
-
await counter.add(ctx,
|
|
213
|
+
await counter.add(ctx, tupleKey(scope, args.name));
|
|
199
214
|
|
|
200
215
|
return "tracked";
|
|
201
216
|
},
|
|
@@ -213,7 +228,7 @@ async function upsertConfig(
|
|
|
213
228
|
.withIndex("by_scope_key", (q) => q.eq("scope", scope).eq("key", key))
|
|
214
229
|
.unique();
|
|
215
230
|
if (existing) {
|
|
216
|
-
await ctx.db.patch(existing._id, { value });
|
|
231
|
+
await ctx.db.patch("config", existing._id, { value });
|
|
217
232
|
} else {
|
|
218
233
|
await ctx.db.insert("config", { scope, key, value });
|
|
219
234
|
}
|
|
@@ -223,6 +238,7 @@ export const configSet = mutation({
|
|
|
223
238
|
args: { scope: v.string(), key: v.string(), value: v.string() },
|
|
224
239
|
returns: v.null(),
|
|
225
240
|
handler: async (ctx, args) => {
|
|
241
|
+
await ensureScope(ctx, args.scope);
|
|
226
242
|
await upsertConfig(ctx, args.scope, args.key, args.value);
|
|
227
243
|
return null;
|
|
228
244
|
},
|
|
@@ -237,6 +253,7 @@ export const configure = mutation({
|
|
|
237
253
|
},
|
|
238
254
|
returns: v.null(),
|
|
239
255
|
handler: async (ctx, args) => {
|
|
256
|
+
await ensureScope(ctx, args.scope);
|
|
240
257
|
const entries: Array<[string, string]> = [];
|
|
241
258
|
if (args.retentionDays !== undefined) {
|
|
242
259
|
entries.push(["retentionDays", String(args.retentionDays)]);
|
package/src/component/queries.ts
CHANGED
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
eventPage,
|
|
15
15
|
distributionView,
|
|
16
16
|
} from "./validators.js";
|
|
17
|
-
import { bucketStart, bucketSize, valKey } from "../shared.js";
|
|
17
|
+
import { bucketStart, bucketSize, tupleKey, valKey } from "../shared.js";
|
|
18
18
|
import type { Granularity } from "../shared.js";
|
|
19
19
|
|
|
20
20
|
const counter = new ShardedCounter(components.shardedCounter as never, {
|
|
@@ -37,7 +37,7 @@ export const metric = query({
|
|
|
37
37
|
const val = args.where ? valKey(args.where.val) : TOTAL;
|
|
38
38
|
|
|
39
39
|
if (!args.range && !args.where) {
|
|
40
|
-
return await counter.count(ctx,
|
|
40
|
+
return await counter.count(ctx, tupleKey(args.scope, args.name));
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
const rollups = await ctx.db
|
|
@@ -220,32 +220,34 @@ export const funnel = query({
|
|
|
220
220
|
q.eq("scope", args.scope).eq("name", step).gte("ts", from).lte("ts", to),
|
|
221
221
|
)
|
|
222
222
|
.take(10000);
|
|
223
|
-
const
|
|
224
|
-
for (const
|
|
225
|
-
if (
|
|
226
|
-
const
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
}
|
|
223
|
+
const timestamps = new Map<string, number[]>();
|
|
224
|
+
for (const event of events) {
|
|
225
|
+
if (event.subjectRef === undefined) continue;
|
|
226
|
+
const subjectTimestamps = timestamps.get(event.subjectRef) ?? [];
|
|
227
|
+
subjectTimestamps.push(event.ts);
|
|
228
|
+
timestamps.set(event.subjectRef, subjectTimestamps);
|
|
230
229
|
}
|
|
231
|
-
return
|
|
230
|
+
return timestamps;
|
|
232
231
|
}),
|
|
233
232
|
);
|
|
234
233
|
|
|
235
234
|
const results: Array<{ name: string; count: number; rate: number }> = [];
|
|
236
|
-
let
|
|
237
|
-
|
|
235
|
+
let qualifiedAt = new Map(
|
|
236
|
+
[...stepSubjects[0]!.entries()].map(([subject, timestamps]) => [
|
|
237
|
+
subject,
|
|
238
|
+
timestamps[0]!,
|
|
239
|
+
]),
|
|
240
|
+
);
|
|
241
|
+
const firstCount = qualifiedAt.size;
|
|
238
242
|
results.push({ name: args.steps[0]!, count: firstCount, rate: 1 });
|
|
239
243
|
|
|
240
244
|
for (let i = 1; i < args.steps.length; i++) {
|
|
241
|
-
const prevTimes = stepSubjects[i - 1]!;
|
|
242
245
|
const thisTimes = stepSubjects[i]!;
|
|
243
|
-
const next = new
|
|
244
|
-
for (const subject of
|
|
245
|
-
const
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
next.add(subject);
|
|
246
|
+
const next = new Map<string, number>();
|
|
247
|
+
for (const [subject, previousTs] of qualifiedAt) {
|
|
248
|
+
const thisTs = thisTimes.get(subject)?.find((timestamp) => timestamp >= previousTs);
|
|
249
|
+
if (thisTs !== undefined) {
|
|
250
|
+
next.set(subject, thisTs);
|
|
249
251
|
}
|
|
250
252
|
}
|
|
251
253
|
results.push({
|
|
@@ -253,7 +255,7 @@ export const funnel = query({
|
|
|
253
255
|
count: next.size,
|
|
254
256
|
rate: firstCount > 0 ? next.size / firstCount : 0,
|
|
255
257
|
});
|
|
256
|
-
|
|
258
|
+
qualifiedAt = next;
|
|
257
259
|
}
|
|
258
260
|
|
|
259
261
|
return results;
|
package/src/component/schema.ts
CHANGED
|
@@ -23,6 +23,7 @@ export default defineSchema({
|
|
|
23
23
|
dedupeKey: v.optional(v.string()),
|
|
24
24
|
})
|
|
25
25
|
.index("by_scope_name_ts", ["scope", "name", "ts"])
|
|
26
|
+
.index("by_scope_ts", ["scope", "ts"])
|
|
26
27
|
.index("by_scope_subject_ts", ["scope", "subjectRef", "ts"])
|
|
27
28
|
.index("by_scope_session_ts", ["scope", "sessionRef", "ts"])
|
|
28
29
|
.index("by_dedupe", ["scope", "dedupeKey"]),
|
|
@@ -66,7 +67,12 @@ export default defineSchema({
|
|
|
66
67
|
eventCount: v.number(),
|
|
67
68
|
})
|
|
68
69
|
.index("by_scope_session", ["scope", "sessionRef"])
|
|
69
|
-
.index("by_scope_lastTs", ["scope", "lastTs"])
|
|
70
|
+
.index("by_scope_lastTs", ["scope", "lastTs"])
|
|
71
|
+
.index("by_scope_endTs_lastTs", ["scope", "endTs", "lastTs"]),
|
|
72
|
+
|
|
73
|
+
scopes: defineTable({
|
|
74
|
+
scope: v.string(),
|
|
75
|
+
}).index("by_scope", ["scope"]),
|
|
70
76
|
|
|
71
77
|
config: defineTable({
|
|
72
78
|
scope: v.string(),
|
package/src/shared.ts
CHANGED
|
@@ -24,6 +24,11 @@ export function bucketStart(ts: number, granularity: Granularity): number {
|
|
|
24
24
|
return Math.floor(ts / size) * size;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
+
/** Serialize a string tuple without delimiter collisions. */
|
|
28
|
+
export function tupleKey(...parts: string[]): string {
|
|
29
|
+
return JSON.stringify(parts);
|
|
30
|
+
}
|
|
31
|
+
|
|
27
32
|
/** Coerce a scalar prop value to the string form used as a rollup `val` key. */
|
|
28
33
|
export function valKey(value: Scalar): string {
|
|
29
34
|
return value === null ? "null" : String(value);
|