-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathstats.ts
More file actions
83 lines (75 loc) · 1.96 KB
/
Copy pathstats.ts
File metadata and controls
83 lines (75 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* Example of collecting statistics on data not tied to a Convex table.
*/
import { mutation, query, internalMutation } from "./_generated/server";
import { v } from "convex/values";
import { resetStatusValidator } from "./utils/resetStatus";
import { DirectAggregate } from "@convex-dev/aggregate";
import { components } from "./_generated/api";
const stats = new DirectAggregate<{
Key: number;
Id: string;
}>(components.stats);
export const reportLatency = mutation({
args: {
latency: v.number(),
},
returns: v.null(),
handler: async (ctx, { latency }) => {
await stats.insert(ctx, {
key: latency,
id: new Date().toISOString(),
sumValue: latency,
});
},
});
export const getStats = query({
args: {},
handler: async (ctx) => {
const count = await stats.count(ctx);
if (count === 0) return null;
const mean = (await stats.sum(ctx)) / count;
const median = (await stats.at(ctx, Math.floor(count / 2))).key;
const p75 = (await stats.at(ctx, Math.floor(count * 0.75))).key;
const p95 = (await stats.at(ctx, Math.floor(count * 0.95))).key;
const min = (await stats.min(ctx))!.key;
const max = (await stats.max(ctx))!.key;
return {
count,
mean,
median,
p75,
p95,
max,
min,
};
},
});
// ----- internal -----
export const resetAll = internalMutation({
args: {},
returns: resetStatusValidator,
handler: async (ctx): Promise<"all_reset" | "partial_reset"> => {
console.log("Resetting stats...");
await stats.clear(ctx);
console.log("Stats reset complete");
return "all_reset";
},
});
export const addLatencies = mutation({
args: {
latencies: v.array(v.number()),
},
returns: v.null(),
handler: async (ctx, { latencies }) => {
await Promise.all(
latencies.map((latency) =>
stats.insert(ctx, {
key: latency,
id: new Date().toISOString(),
sumValue: latency,
}),
),
);
},
});