next-evolve 0.1.0 → 0.3.1
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 +55 -1
- package/dist/_virtual/_rolldown/runtime.js +12 -0
- package/dist/aggregate/attribution.d.ts +40 -0
- package/dist/aggregate/attribution.js +115 -0
- package/dist/aggregate/run.d.ts +21 -0
- package/dist/aggregate/run.js +139 -0
- package/dist/cli/aggregate.d.ts +9 -0
- package/dist/cli/aggregate.js +104 -0
- package/dist/cli/args.d.ts +12 -0
- package/dist/cli/args.js +23 -0
- package/dist/cli/cloud.d.ts +41 -0
- package/dist/cli/cloud.js +74 -0
- package/dist/cli/connect.d.ts +8 -0
- package/dist/cli/connect.js +46 -0
- package/dist/cli/detect.d.ts +26 -0
- package/dist/cli/detect.js +97 -0
- package/dist/cli/doctor.d.ts +34 -0
- package/dist/cli/doctor.js +230 -0
- package/dist/cli/env-file.d.ts +4 -0
- package/dist/cli/env-file.js +15 -0
- package/dist/cli/files.d.ts +25 -0
- package/dist/cli/files.js +173 -0
- package/dist/cli/index.js +73 -32
- package/dist/cli/init.d.ts +26 -0
- package/dist/cli/init.js +240 -0
- package/dist/cli/load-app.d.ts +19 -0
- package/dist/cli/load-app.js +67 -0
- package/dist/cli/migrate.d.ts +7 -0
- package/dist/cli/migrate.js +30 -0
- package/dist/cli/pages.d.ts +17 -0
- package/dist/cli/pages.js +304 -0
- package/dist/cli/plan.d.ts +27 -0
- package/dist/cli/plan.js +129 -0
- package/dist/cli/pull.d.ts +13 -0
- package/dist/cli/pull.js +62 -0
- package/dist/cli/push.d.ts +14 -0
- package/dist/cli/push.js +33 -0
- package/dist/cloud/manifest.d.ts +2 -0
- package/dist/cloud/manifest.js +6 -2
- package/dist/core/assign.d.ts +13 -1
- package/dist/core/assign.js +25 -3
- package/dist/db/writer.d.ts +2 -0
- package/dist/db/writer.js +1 -1
- package/dist/instrumentation/index.d.ts +3 -2
- package/dist/instrumentation/index.js +17 -2
- package/dist/node_modules/.pnpm/typescript@5.9.3/node_modules/typescript/lib/typescript.d.ts +11169 -0
- package/dist/proxy/index.d.ts +2 -2
- package/dist/proxy/index.js +1 -1
- package/dist/proxy/proxy.d.ts +7 -1
- package/dist/proxy/proxy.js +23 -6
- package/dist/react/render-bundle.d.ts +10 -4
- package/dist/react/render-bundle.js +34 -5
- package/dist/server/aggregate-route.d.ts +16 -0
- package/dist/server/aggregate-route.js +68 -0
- package/dist/server/collect.d.ts +16 -0
- package/dist/server/collect.js +34 -8
- package/dist/server/handler.d.ts +3 -2
- package/dist/server/handler.js +37 -4
- package/dist/server/sync-route.d.ts +28 -0
- package/dist/server/sync-route.js +100 -0
- package/dist/store/disk.d.ts +6 -0
- package/dist/store/disk.js +23 -0
- package/dist/store/fetch.d.ts +4 -0
- package/dist/store/fetch.js +36 -1
- package/dist/store/global-config.d.ts +39 -0
- package/dist/store/global-config.js +122 -0
- package/dist/store/memory.d.ts +11 -2
- package/dist/store/memory.js +6 -2
- package/dist/store/runtime.d.ts +14 -0
- package/dist/store/runtime.js +39 -7
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -2,7 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|
Self-improving Next.js sites: server-side variants with no flicker, measured in your own Postgres. Needs an Evolve Cloud account.
|
|
4
4
|
|
|
5
|
-
> Alpha.
|
|
5
|
+
> Alpha.
|
|
6
|
+
|
|
7
|
+
## Setup
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npx next-evolve init --routes "/ /pricing" # add --dry-run to see the diff first
|
|
11
|
+
npx next-evolve doctor # checks env, key, DB, bundle, proxy, pages, cloud → app
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
`init` installs the package, connects to your site (secret key and webhook secret from the dashboard), sets up the database, and writes the config, route handler, proxy, layout script, instrumentation and `prebuild` hook. Static-mode pages move under `app/[evolve]/` and get wrapped with `withEvolve()`; when that isn't safe (a top-level `[slug]`, `force-dynamic`, a `"use client"` page…) the route stays in dynamic mode and `init` says why. It never overwrites your `.env.local` lines, and a re-run changes nothing. `--yes` skips every prompt.
|
|
15
|
+
|
|
16
|
+
Other commands: `migrate`, `pull`, `push`, `connect --test [--app-url]`, `aggregate`. The sections below describe what `init` writes.
|
|
6
17
|
|
|
7
18
|
## Database
|
|
8
19
|
|
|
@@ -81,3 +92,46 @@ if (event.type === "invoice.paid") {
|
|
|
81
92
|
});
|
|
82
93
|
}
|
|
83
94
|
```
|
|
95
|
+
|
|
96
|
+
## Aggregation
|
|
97
|
+
|
|
98
|
+
Evolve Cloud never reads your database. It sends a signed request to `POST /api/evolve/aggregate` (verified with `EVOLVE_WEBHOOK_SECRET`), and your app answers with counts and sums per experiment × variant × goal × day, signed with the same secret. No visitor or user ids leave your app. Set `EVOLVE_SECRET_KEY`, `EVOLVE_WEBHOOK_SECRET` and `EVOLVE_DATABASE_URL`; the route needs `maxDuration = 60`.
|
|
99
|
+
|
|
100
|
+
In local development the cloud can't reach `localhost`. Run the same request yourself:
|
|
101
|
+
|
|
102
|
+
```sh
|
|
103
|
+
npx next-evolve aggregate # print the statistics
|
|
104
|
+
npx next-evolve aggregate --upload # and send them to Evolve Cloud
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## Sync
|
|
108
|
+
|
|
109
|
+
Your app keeps a signed copy of its configuration (the bundle) in memory and serves from it with no network call on the request path. It gets new bundles three ways:
|
|
110
|
+
|
|
111
|
+
- **Polling:** every 60 s (5 s in dev), in the background.
|
|
112
|
+
- **Webhook:** when you change an experiment, Evolve Cloud calls `POST /api/evolve/sync` (signed with `EVOLVE_WEBHOOK_SECRET`). The receiving instance pulls the new bundle within seconds. The webhook carries no configuration, so a forged call can at most cause one extra fetch.
|
|
113
|
+
- **Build time:** add `"prebuild": "next-evolve pull"` to `package.json` and pass the result to the proxy, so a fresh instance serves the last bundle before its first fetch:
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
// proxy.ts
|
|
117
|
+
import { createEvolveProxy } from "next-evolve/proxy";
|
|
118
|
+
import buildBundle from "./.evolve/bundle.generated";
|
|
119
|
+
import evolveConfig from "./evolve.config";
|
|
120
|
+
|
|
121
|
+
export const proxy = createEvolveProxy(evolveConfig, { buildBundle });
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
Add `.evolve/` to `.gitignore`. `pull` never fails the build: without a key or a reachable cloud it keeps the previous bundle or writes an empty one.
|
|
125
|
+
|
|
126
|
+
**Long-running Node (`next start`, Docker):** call `registerEvolve()` from `instrumentation.ts`. It polls in the background and keeps the last bundle on disk (`os.tmpdir()`, or `EVOLVE_CACHE_DIR`), so a restart serves it even when the cloud is unreachable.
|
|
127
|
+
|
|
128
|
+
**Vercel + Global Config (recommended on Vercel):** without it, a webhook reaches only the instance that receives it and the others catch up within 60 s. With a [Global Config](https://vercel.com/docs/edge-config) store, the sync route writes the bundle there and every instance picks it up within ~10 s. Set:
|
|
129
|
+
|
|
130
|
+
| Var | Purpose |
|
|
131
|
+
| ------------------------- | --------------------------------------- |
|
|
132
|
+
| `EVOLVE_GLOBAL_CONFIG` | The store's connection string (read) |
|
|
133
|
+
| `EVOLVE_GLOBAL_CONFIG_ID` | The store id (`ecfg_…`), for writes |
|
|
134
|
+
| `EVOLVE_VERCEL_API_TOKEN` | A Vercel token that can write the store |
|
|
135
|
+
| `EVOLVE_VERCEL_TEAM_ID` | Optional, for team-owned stores |
|
|
136
|
+
|
|
137
|
+
Bundles read from Global Config are verified like any other. With an existing proxy that calls `applyEvolve`, `await evolveReady(evolveConfig)` first (a fresh instance waits ≤ 20 ms for one read).
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// \0rolldown/runtime.js
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __exportAll = (all, no_symbols) => {
|
|
4
|
+
let target = {};
|
|
5
|
+
for (var name in all) __defProp(target, name, {
|
|
6
|
+
get: all[name],
|
|
7
|
+
enumerable: true
|
|
8
|
+
});
|
|
9
|
+
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
10
|
+
return target;
|
|
11
|
+
};
|
|
12
|
+
export { __exportAll };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { Sql } from "../db/client.js";
|
|
2
|
+
import { AggregateRow, SerializedGoal } from "@next-evolve/protocol";
|
|
3
|
+
export type AttributionInput = {
|
|
4
|
+
experimentId: string;
|
|
5
|
+
/** Only exposures to these variants count. */
|
|
6
|
+
variants: readonly string[];
|
|
7
|
+
goalKey: string;
|
|
8
|
+
goal: SerializedGoal;
|
|
9
|
+
/** Upper bound for exposures and events (exclusive). */
|
|
10
|
+
asOf: Date;
|
|
11
|
+
/** Only units whose cohort day (UTC day of first exposure) is on/after this `YYYY-MM-DD`. */
|
|
12
|
+
cohortsFrom: string;
|
|
13
|
+
};
|
|
14
|
+
type RawRow = {
|
|
15
|
+
variant_id: string;
|
|
16
|
+
seg: string;
|
|
17
|
+
day: string;
|
|
18
|
+
n: number;
|
|
19
|
+
c: number;
|
|
20
|
+
s1: number;
|
|
21
|
+
s2: number;
|
|
22
|
+
mn: number;
|
|
23
|
+
mc: number;
|
|
24
|
+
ms1: number;
|
|
25
|
+
ms2: number;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* The attribution query for one experiment × goal. An event counts for a unit
|
|
29
|
+
* when it falls in `[first exposure, first exposure + window)`, capped at
|
|
30
|
+
* `asOf`. A unit is mature when its whole window ended before `asOf`.
|
|
31
|
+
*
|
|
32
|
+
* - visitor unit: one unit per exposed anon id; events match on the anon id,
|
|
33
|
+
* or on the linked user id (cross-device and server events).
|
|
34
|
+
* - user unit: identified exposures only, one unit per user at their earliest
|
|
35
|
+
* exposure; events match on the user id or any of the user's anon ids.
|
|
36
|
+
*/
|
|
37
|
+
export declare function attributionQuery(sql: Sql, input: AttributionInput): import("postgres").PendingQuery<RawRow[]>;
|
|
38
|
+
/** Runs the attribution query and returns protocol rows. */
|
|
39
|
+
export declare function runAttribution(sql: Sql, input: AttributionInput): Promise<AggregateRow[]>;
|
|
40
|
+
export {};
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { parseWindowSeconds } from "@next-evolve/protocol";
|
|
2
|
+
// src/aggregate/attribution.ts
|
|
3
|
+
const NUMERIC_RE = "^\\s*-?(\\d{1,30}(\\.\\d{0,30})?|\\.\\d{1,30})([eE][-+]?\\d{1,2})?\\s*$";
|
|
4
|
+
function numericText(sql, text) {
|
|
5
|
+
return sql`(case when ${text} ~ ${NUMERIC_RE} then (${text})::float8 end)`;
|
|
6
|
+
}
|
|
7
|
+
/** The event's value for this goal (null → conversion only). */
|
|
8
|
+
function valueExpr(sql, key, goal) {
|
|
9
|
+
if (goal.kind === "pageview" || goal.value === null) return sql`e.value`;
|
|
10
|
+
if (goal.value.kind === "local-fn") return numericText(sql, sql`e.goal_values ->> ${key}::text`);
|
|
11
|
+
return numericText(sql, sql`e.props #>> string_to_array(${goal.value.path}::text, '.')`);
|
|
12
|
+
}
|
|
13
|
+
/** Which events count for this goal (window and unit join come from the caller). */
|
|
14
|
+
function matchExpr(sql, goal) {
|
|
15
|
+
if (goal.kind === "pageview") return sql`e.name = '$pageview' and ${goal.path.endsWith("*") ? sql`starts_with(e.path, ${goal.path.slice(0, -1)}::text)` : sql`e.path = ${goal.path}::text`}`;
|
|
16
|
+
const source = goal.source === null ? sql`` : sql`and e.source = ${goal.source}::text`;
|
|
17
|
+
const where = goal.where === null ? sql`` : sql`and e.props @> (${JSON.stringify(goal.where)}::text)::jsonb`;
|
|
18
|
+
return sql`e.name = ${goal.event}::text ${source} ${where}`;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The attribution query for one experiment × goal. An event counts for a unit
|
|
22
|
+
* when it falls in `[first exposure, first exposure + window)`, capped at
|
|
23
|
+
* `asOf`. A unit is mature when its whole window ended before `asOf`.
|
|
24
|
+
*
|
|
25
|
+
* - visitor unit: one unit per exposed anon id; events match on the anon id,
|
|
26
|
+
* or on the linked user id (cross-device and server events).
|
|
27
|
+
* - user unit: identified exposures only, one unit per user at their earliest
|
|
28
|
+
* exposure; events match on the user id or any of the user's anon ids.
|
|
29
|
+
*/
|
|
30
|
+
function attributionQuery(sql, input) {
|
|
31
|
+
const windowSeconds = parseWindowSeconds(input.goal.window);
|
|
32
|
+
if (windowSeconds === null) throw new Error(`invalid goal window ${input.goal.window}`);
|
|
33
|
+
const asOf = input.asOf.toISOString();
|
|
34
|
+
const from = `${input.cohortsFrom}T00:00:00Z`;
|
|
35
|
+
const win = sql`make_interval(secs => ${windowSeconds}::int)`;
|
|
36
|
+
const asOfTs = sql`${asOf}::timestamptz`;
|
|
37
|
+
const val = valueExpr(sql, input.goalKey, input.goal);
|
|
38
|
+
const match = matchExpr(sql, input.goal);
|
|
39
|
+
const inWindow = sql`e.ts >= u.first_at and e.ts < least(u.first_at + ${win}, ${asOfTs})`;
|
|
40
|
+
const variants = sql`string_to_array(${input.variants.join(",")}::text, ',')`;
|
|
41
|
+
return sql`
|
|
42
|
+
with ${input.goal.unit === "visitor" ? sql`
|
|
43
|
+
u as (
|
|
44
|
+
select x.anon_id as unit, x.variant_id, x.first_at, coalesce(x.segment, 'main') as seg,
|
|
45
|
+
i.user_id, (x.first_at + ${win} <= ${asOfTs}) as mature
|
|
46
|
+
from evolve.exposures x
|
|
47
|
+
left join evolve.identities i on i.anon_id = x.anon_id
|
|
48
|
+
where x.experiment_id = ${input.experimentId}::text
|
|
49
|
+
and x.variant_id = any(${variants})
|
|
50
|
+
and x.first_at >= ${from}::timestamptz and x.first_at < ${asOfTs}),
|
|
51
|
+
hits as (
|
|
52
|
+
select u.unit, ${val} as v from u
|
|
53
|
+
join evolve.events e on e.anon_id = u.unit
|
|
54
|
+
where ${match} and ${inWindow}
|
|
55
|
+
union all
|
|
56
|
+
select u.unit, ${val} from u
|
|
57
|
+
join evolve.events e on e.user_id = u.user_id
|
|
58
|
+
where u.user_id is not null and (e.anon_id is null or e.anon_id <> u.unit)
|
|
59
|
+
and ${match} and ${inWindow})` : sql`
|
|
60
|
+
firsts as (
|
|
61
|
+
-- Earliest exposure per user over all cohorts, so a user exposed again on
|
|
62
|
+
-- another device never becomes a second unit in a later cohort.
|
|
63
|
+
select distinct on (i.user_id) i.user_id as unit, x.variant_id, x.first_at,
|
|
64
|
+
coalesce(x.segment, 'main') as seg
|
|
65
|
+
from evolve.exposures x
|
|
66
|
+
join evolve.identities i on i.anon_id = x.anon_id
|
|
67
|
+
where x.experiment_id = ${input.experimentId}::text and x.first_at < ${asOfTs}
|
|
68
|
+
order by i.user_id, x.first_at, x.anon_id),
|
|
69
|
+
u as (
|
|
70
|
+
select unit, variant_id, first_at, seg, (first_at + ${win} <= ${asOfTs}) as mature
|
|
71
|
+
from firsts
|
|
72
|
+
where variant_id = any(${variants}) and first_at >= ${from}::timestamptz),
|
|
73
|
+
hits as (
|
|
74
|
+
select u.unit, ${val} as v from u
|
|
75
|
+
join evolve.events e on e.user_id = u.unit
|
|
76
|
+
where ${match} and ${inWindow}
|
|
77
|
+
union all
|
|
78
|
+
select u.unit, ${val} from u
|
|
79
|
+
join evolve.identities ia on ia.user_id = u.unit
|
|
80
|
+
join evolve.events e on e.anon_id = ia.anon_id
|
|
81
|
+
where (e.user_id is null or e.user_id <> u.unit) and ${match} and ${inWindow})`},
|
|
82
|
+
per_unit as (select unit, sum(coalesce(v, 0)) as v from hits group by unit)
|
|
83
|
+
select u.variant_id, u.seg,
|
|
84
|
+
to_char((u.first_at at time zone 'UTC')::date, 'YYYY-MM-DD') as day,
|
|
85
|
+
count(*)::int as n,
|
|
86
|
+
count(p.unit)::int as c,
|
|
87
|
+
coalesce(sum(p.v), 0)::float8 as s1,
|
|
88
|
+
coalesce(sum(p.v * p.v), 0)::float8 as s2,
|
|
89
|
+
(count(*) filter (where u.mature))::int as mn,
|
|
90
|
+
(count(p.unit) filter (where u.mature))::int as mc,
|
|
91
|
+
coalesce(sum(p.v) filter (where u.mature), 0)::float8 as ms1,
|
|
92
|
+
coalesce(sum(p.v * p.v) filter (where u.mature), 0)::float8 as ms2
|
|
93
|
+
from u left join per_unit p on p.unit = u.unit
|
|
94
|
+
group by 1, 2, 3
|
|
95
|
+
order by 3, 1, 2`;
|
|
96
|
+
}
|
|
97
|
+
/** Runs the attribution query and returns protocol rows. */
|
|
98
|
+
async function runAttribution(sql, input) {
|
|
99
|
+
return (await attributionQuery(sql, input)).map((r) => ({
|
|
100
|
+
exp: input.experimentId,
|
|
101
|
+
var: r.variant_id,
|
|
102
|
+
goal: input.goalKey,
|
|
103
|
+
seg: r.seg,
|
|
104
|
+
day: r.day,
|
|
105
|
+
n: r.n,
|
|
106
|
+
c: r.c,
|
|
107
|
+
s1: r.s1,
|
|
108
|
+
s2: r.s2,
|
|
109
|
+
mn: r.mn,
|
|
110
|
+
mc: r.mc,
|
|
111
|
+
ms1: r.ms1,
|
|
112
|
+
ms2: r.ms2
|
|
113
|
+
}));
|
|
114
|
+
}
|
|
115
|
+
export { attributionQuery, runAttribution };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Sql } from "../db/client.js";
|
|
2
|
+
import { AggregateRequest, AggregateResponse } from "@next-evolve/protocol";
|
|
3
|
+
export type RunAggregateOptions = {
|
|
4
|
+
/** Whole-request budget (spec: 25 s). */
|
|
5
|
+
maxMs: number;
|
|
6
|
+
/** Per-query cap (spec: 20 s). */
|
|
7
|
+
statementTimeoutMs: number;
|
|
8
|
+
sdkVersion: string;
|
|
9
|
+
now?: () => number;
|
|
10
|
+
};
|
|
11
|
+
export type RunAggregateResult = {
|
|
12
|
+
kind: "ok";
|
|
13
|
+
response: AggregateResponse;
|
|
14
|
+
cached: boolean;
|
|
15
|
+
} | {
|
|
16
|
+
kind: "busy";
|
|
17
|
+
retryAfter: number;
|
|
18
|
+
};
|
|
19
|
+
/** Answer `429 { busy, retryAfter }` while another aggregate holds the lock. */
|
|
20
|
+
export declare const BUSY_RETRY_AFTER_SECONDS = 30;
|
|
21
|
+
export declare function runAggregate(sql: Sql, req: AggregateRequest, opts: RunAggregateOptions): Promise<RunAggregateResult>;
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { runAttribution } from "./attribution.js";
|
|
2
|
+
import { MAX_RESPONSE_ROWS } from "@next-evolve/protocol";
|
|
3
|
+
import { createHash } from "node:crypto";
|
|
4
|
+
// src/aggregate/run.ts
|
|
5
|
+
/** Answer `429 { busy, retryAfter }` while another aggregate holds the lock. */
|
|
6
|
+
const BUSY_RETRY_AFTER_SECONDS = 30;
|
|
7
|
+
/** Below this much budget left, don't start another query. */
|
|
8
|
+
const MIN_QUERY_MS = 500;
|
|
9
|
+
const QUERY_CANCELED = "57014";
|
|
10
|
+
/** Stored responses are replayed for this long (spec §9.3). */
|
|
11
|
+
const RUN_TTL = "24 hours";
|
|
12
|
+
async function storedResponse(sql, requestId) {
|
|
13
|
+
const [hit] = await sql`
|
|
14
|
+
select response from evolve.aggregate_runs
|
|
15
|
+
where request_id = ${requestId} and status = 'done'
|
|
16
|
+
and received_at > now() - ${RUN_TTL}::interval`;
|
|
17
|
+
return hit?.response ?? null;
|
|
18
|
+
}
|
|
19
|
+
async function runAggregate(sql, req, opts) {
|
|
20
|
+
const now = opts.now ?? Date.now;
|
|
21
|
+
const started = now();
|
|
22
|
+
const cached = await storedResponse(sql, req.requestId);
|
|
23
|
+
if (cached) return {
|
|
24
|
+
kind: "ok",
|
|
25
|
+
response: cached,
|
|
26
|
+
cached: true
|
|
27
|
+
};
|
|
28
|
+
const conn = await sql.reserve();
|
|
29
|
+
let locked = false;
|
|
30
|
+
try {
|
|
31
|
+
const [l] = await conn`
|
|
32
|
+
select pg_try_advisory_lock(hashtext('evolve.aggregate')) as ok`;
|
|
33
|
+
locked = Boolean(l?.ok);
|
|
34
|
+
if (!locked) return {
|
|
35
|
+
kind: "busy",
|
|
36
|
+
retryAfter: 30
|
|
37
|
+
};
|
|
38
|
+
const raced = await storedResponse(conn, req.requestId);
|
|
39
|
+
if (raced) return {
|
|
40
|
+
kind: "ok",
|
|
41
|
+
response: raced,
|
|
42
|
+
cached: true
|
|
43
|
+
};
|
|
44
|
+
const deadline = started + opts.maxMs;
|
|
45
|
+
const asOf = new Date(req.asOf);
|
|
46
|
+
const rows = [];
|
|
47
|
+
const errors = [];
|
|
48
|
+
const remaining = [];
|
|
49
|
+
for (const exp of req.experiments) {
|
|
50
|
+
if (remaining.length) {
|
|
51
|
+
remaining.push(exp.id);
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
const expRows = [];
|
|
55
|
+
const expErrors = [];
|
|
56
|
+
let outOfBudget = false;
|
|
57
|
+
for (const g of exp.goals) {
|
|
58
|
+
const left = deadline - now();
|
|
59
|
+
if (left < MIN_QUERY_MS) {
|
|
60
|
+
outOfBudget = true;
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
const timeout = Math.floor(Math.min(opts.statementTimeoutMs, left));
|
|
64
|
+
await conn.unsafe(`set statement_timeout = ${timeout}`);
|
|
65
|
+
try {
|
|
66
|
+
expRows.push(...await runAttribution(conn, {
|
|
67
|
+
experimentId: exp.id,
|
|
68
|
+
variants: exp.variants,
|
|
69
|
+
goalKey: g.key,
|
|
70
|
+
goal: g.def,
|
|
71
|
+
asOf,
|
|
72
|
+
cohortsFrom: exp.cohortsFrom
|
|
73
|
+
}));
|
|
74
|
+
} catch (e) {
|
|
75
|
+
const code = e.code;
|
|
76
|
+
if (code === QUERY_CANCELED && now() >= deadline - MIN_QUERY_MS) {
|
|
77
|
+
outOfBudget = true;
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
expErrors.push({
|
|
81
|
+
exp: exp.id,
|
|
82
|
+
goal: g.key,
|
|
83
|
+
message: code === QUERY_CANCELED ? "statement timeout" : errorMessage(e)
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (outOfBudget || rows.length + expRows.length > MAX_RESPONSE_ROWS) {
|
|
88
|
+
remaining.push(exp.id);
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
rows.push(...expRows);
|
|
92
|
+
errors.push(...expErrors);
|
|
93
|
+
}
|
|
94
|
+
await conn.unsafe("set statement_timeout = 0");
|
|
95
|
+
const usage = req.usageFrom ? (await conn`
|
|
96
|
+
select to_char(day, 'YYYY-MM-DD') as day, tested_pageviews::text as n
|
|
97
|
+
from evolve.usage_daily where day >= ${req.usageFrom}::date order by day`).map((u) => ({
|
|
98
|
+
day: u.day,
|
|
99
|
+
testedPageviews: Number(u.n)
|
|
100
|
+
})) : [];
|
|
101
|
+
const response = {
|
|
102
|
+
pv: req.pv,
|
|
103
|
+
requestId: req.requestId,
|
|
104
|
+
site: req.site,
|
|
105
|
+
asOf: req.asOf,
|
|
106
|
+
sdk: opts.sdkVersion,
|
|
107
|
+
mode: req.mode,
|
|
108
|
+
durationMs: Math.max(0, Math.round(now() - started)),
|
|
109
|
+
partial: remaining.length > 0,
|
|
110
|
+
remaining,
|
|
111
|
+
rows,
|
|
112
|
+
usage,
|
|
113
|
+
errors
|
|
114
|
+
};
|
|
115
|
+
const body = JSON.stringify(response);
|
|
116
|
+
await conn`
|
|
117
|
+
insert into evolve.aggregate_runs
|
|
118
|
+
(request_id, received_at, mode, status, duration_ms, response_hash, response)
|
|
119
|
+
values (${req.requestId}, ${new Date(started)}, ${req.mode}, 'done', ${response.durationMs},
|
|
120
|
+
${createHash("sha256").update(body).digest("hex")}, (${body}::text)::jsonb)
|
|
121
|
+
on conflict (request_id) do nothing`;
|
|
122
|
+
await conn`delete from evolve.aggregate_runs where received_at < now() - ${RUN_TTL}::interval`;
|
|
123
|
+
return {
|
|
124
|
+
kind: "ok",
|
|
125
|
+
response,
|
|
126
|
+
cached: false
|
|
127
|
+
};
|
|
128
|
+
} finally {
|
|
129
|
+
if (locked) {
|
|
130
|
+
await conn.unsafe("set statement_timeout = 0").catch(() => {});
|
|
131
|
+
await conn`select pg_advisory_unlock(hashtext('evolve.aggregate'))`.catch(() => {});
|
|
132
|
+
}
|
|
133
|
+
conn.release();
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
function errorMessage(e) {
|
|
137
|
+
return String(e instanceof Error ? e.message : e).slice(0, 500);
|
|
138
|
+
}
|
|
139
|
+
export { BUSY_RETRY_AFTER_SECONDS, runAggregate };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
type Env = Record<string, string | undefined>;
|
|
2
|
+
export type AggregateCommandDeps = {
|
|
3
|
+
env: Env;
|
|
4
|
+
fetch?: typeof fetch;
|
|
5
|
+
out?: (s: string) => void;
|
|
6
|
+
err?: (s: string) => void;
|
|
7
|
+
};
|
|
8
|
+
export declare function aggregateCommand(args: string[], deps: AggregateCommandDeps): Promise<number>;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { runAggregate } from "../aggregate/run.js";
|
|
2
|
+
import { sdkVersion } from "../cloud/manifest.js";
|
|
3
|
+
import { createSql } from "../db/client.js";
|
|
4
|
+
import "../store/runtime.js";
|
|
5
|
+
import { SIGNATURE_HEADER, aggregateRequestSchema, isSupportedProtocol, parseSecretKey, signBody } from "@next-evolve/protocol";
|
|
6
|
+
// src/cli/aggregate.ts
|
|
7
|
+
async function aggregateCommand(args, deps) {
|
|
8
|
+
const { env } = deps;
|
|
9
|
+
const out = deps.out ?? ((s) => process.stdout.write(`${s}\n`));
|
|
10
|
+
const err = deps.err ?? ((s) => console.error(s));
|
|
11
|
+
const doFetch = deps.fetch ?? fetch;
|
|
12
|
+
const upload = args.includes("--upload");
|
|
13
|
+
const key = env.EVOLVE_SECRET_KEY;
|
|
14
|
+
const secret = env.EVOLVE_WEBHOOK_SECRET;
|
|
15
|
+
const dbUrl = env.EVOLVE_DATABASE_URL;
|
|
16
|
+
const site = parseSecretKey(key ?? "")?.siteId;
|
|
17
|
+
const missing = [
|
|
18
|
+
!site && "EVOLVE_SECRET_KEY",
|
|
19
|
+
!dbUrl && "EVOLVE_DATABASE_URL",
|
|
20
|
+
upload && !secret && "EVOLVE_WEBHOOK_SECRET"
|
|
21
|
+
].filter(Boolean);
|
|
22
|
+
if (missing.length) {
|
|
23
|
+
err(`next-evolve aggregate: set ${missing.join(", ")}`);
|
|
24
|
+
return 1;
|
|
25
|
+
}
|
|
26
|
+
const cloud = env.EVOLVE_CLOUD_URL ?? "https://api.evolveab.com";
|
|
27
|
+
const auth = { authorization: `Bearer ${key}` };
|
|
28
|
+
let request;
|
|
29
|
+
try {
|
|
30
|
+
const res = await doFetch(new URL("/v1/aggregates/request", cloud), {
|
|
31
|
+
method: "POST",
|
|
32
|
+
headers: auth
|
|
33
|
+
});
|
|
34
|
+
if (res.status === 404) {
|
|
35
|
+
out("Nothing to aggregate: no running experiments.");
|
|
36
|
+
return 0;
|
|
37
|
+
}
|
|
38
|
+
if (!res.ok) {
|
|
39
|
+
err(`next-evolve aggregate: the cloud answered ${res.status} for the request`);
|
|
40
|
+
return 1;
|
|
41
|
+
}
|
|
42
|
+
const body = await res.json();
|
|
43
|
+
if (!isSupportedProtocol(body.request?.pv)) {
|
|
44
|
+
err("next-evolve aggregate: unsupported protocol version; upgrade next-evolve");
|
|
45
|
+
return 1;
|
|
46
|
+
}
|
|
47
|
+
const parsed = aggregateRequestSchema.safeParse(body.request);
|
|
48
|
+
if (!parsed.success || parsed.data.site !== site) {
|
|
49
|
+
err("next-evolve aggregate: the cloud sent an invalid request");
|
|
50
|
+
return 1;
|
|
51
|
+
}
|
|
52
|
+
request = parsed.data;
|
|
53
|
+
} catch (e) {
|
|
54
|
+
err(`next-evolve aggregate: can't reach ${cloud} (${e instanceof Error ? e.message : e})`);
|
|
55
|
+
return 1;
|
|
56
|
+
}
|
|
57
|
+
const sql = createSql(dbUrl, { max: 1 });
|
|
58
|
+
let result;
|
|
59
|
+
try {
|
|
60
|
+
result = await runAggregate(sql, request, {
|
|
61
|
+
maxMs: 12e4,
|
|
62
|
+
statementTimeoutMs: 6e4,
|
|
63
|
+
sdkVersion: sdkVersion()
|
|
64
|
+
});
|
|
65
|
+
} catch (e) {
|
|
66
|
+
err(`next-evolve aggregate: database error (${e instanceof Error ? e.message : e})`);
|
|
67
|
+
return 1;
|
|
68
|
+
} finally {
|
|
69
|
+
await sql.end({ timeout: 5 });
|
|
70
|
+
}
|
|
71
|
+
if (result.kind === "busy") {
|
|
72
|
+
err("next-evolve aggregate: another aggregation is running; try again in 30 s");
|
|
73
|
+
return 1;
|
|
74
|
+
}
|
|
75
|
+
const r = result.response;
|
|
76
|
+
const summary = `${r.rows.length} rows for ${request.experiments.length} experiment(s) in ${r.durationMs} ms${r.partial ? `, partial (remaining: ${r.remaining.join(", ")})` : ""}${r.errors.length ? `, ${r.errors.length} error(s)` : ""}`;
|
|
77
|
+
if (!upload) {
|
|
78
|
+
out(JSON.stringify(r, null, 2));
|
|
79
|
+
err(summary);
|
|
80
|
+
return 0;
|
|
81
|
+
}
|
|
82
|
+
const body = JSON.stringify(r);
|
|
83
|
+
try {
|
|
84
|
+
const res = await doFetch(new URL("/v1/aggregates", cloud), {
|
|
85
|
+
method: "POST",
|
|
86
|
+
headers: {
|
|
87
|
+
...auth,
|
|
88
|
+
"content-type": "application/json",
|
|
89
|
+
[SIGNATURE_HEADER]: signBody(secret, body)
|
|
90
|
+
},
|
|
91
|
+
body
|
|
92
|
+
});
|
|
93
|
+
if (!res.ok) {
|
|
94
|
+
err(`next-evolve aggregate: upload failed (${res.status} ${await res.text()})`);
|
|
95
|
+
return 1;
|
|
96
|
+
}
|
|
97
|
+
} catch (e) {
|
|
98
|
+
err(`next-evolve aggregate: upload failed (${e instanceof Error ? e.message : e})`);
|
|
99
|
+
return 1;
|
|
100
|
+
}
|
|
101
|
+
out(`Uploaded ${summary}.`);
|
|
102
|
+
return 0;
|
|
103
|
+
}
|
|
104
|
+
export { aggregateCommand };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type FlagSpec = Record<string, {
|
|
2
|
+
type: "string" | "boolean";
|
|
3
|
+
short?: string;
|
|
4
|
+
}>;
|
|
5
|
+
/** Parses `--flag value` / `--flag`; unknown flags are an error (returned, not thrown). */
|
|
6
|
+
export declare function parseFlags<S extends FlagSpec>(args: string[], spec: S): {
|
|
7
|
+
ok: true;
|
|
8
|
+
flags: { [K in keyof S]?: S[K]["type"] extends "boolean" ? boolean : string; };
|
|
9
|
+
} | {
|
|
10
|
+
ok: false;
|
|
11
|
+
error: string;
|
|
12
|
+
};
|
package/dist/cli/args.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { parseArgs } from "node:util";
|
|
2
|
+
// src/cli/args.ts
|
|
3
|
+
/** Parses `--flag value` / `--flag`; unknown flags are an error (returned, not thrown). */
|
|
4
|
+
function parseFlags(args, spec) {
|
|
5
|
+
try {
|
|
6
|
+
const { values } = parseArgs({
|
|
7
|
+
args,
|
|
8
|
+
options: spec,
|
|
9
|
+
strict: true,
|
|
10
|
+
allowPositionals: false
|
|
11
|
+
});
|
|
12
|
+
return {
|
|
13
|
+
ok: true,
|
|
14
|
+
flags: values
|
|
15
|
+
};
|
|
16
|
+
} catch (e) {
|
|
17
|
+
return {
|
|
18
|
+
ok: false,
|
|
19
|
+
error: e instanceof Error ? e.message : String(e)
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export { parseFlags };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
type Env = Record<string, string | undefined>;
|
|
2
|
+
export type CloudDeps = {
|
|
3
|
+
env: Env;
|
|
4
|
+
fetch?: typeof fetch;
|
|
5
|
+
};
|
|
6
|
+
export type SiteInfo = {
|
|
7
|
+
id: string;
|
|
8
|
+
name: string;
|
|
9
|
+
appUrl: string | null;
|
|
10
|
+
};
|
|
11
|
+
export declare const cloudUrl: (env: Env) => string;
|
|
12
|
+
/** `GET /v1/site`: which site this key belongs to. Accepts the cloud's and the fake cloud's shapes. */
|
|
13
|
+
export declare function getSiteInfo(deps: CloudDeps, key: string): Promise<{
|
|
14
|
+
ok: true;
|
|
15
|
+
site: SiteInfo;
|
|
16
|
+
} | {
|
|
17
|
+
ok: false;
|
|
18
|
+
error: string;
|
|
19
|
+
}>;
|
|
20
|
+
/** `PUT /v1/site/app-url`. */
|
|
21
|
+
export declare function putAppUrl(deps: CloudDeps, key: string, appUrl: string): Promise<{
|
|
22
|
+
ok: true;
|
|
23
|
+
appUrl: string;
|
|
24
|
+
} | {
|
|
25
|
+
ok: false;
|
|
26
|
+
error: string;
|
|
27
|
+
}>;
|
|
28
|
+
export type CheckResult = {
|
|
29
|
+
ok: boolean;
|
|
30
|
+
detail?: string;
|
|
31
|
+
};
|
|
32
|
+
/** `POST /v1/site/test`: the cloud pings the sync route and dry-runs an aggregate. */
|
|
33
|
+
export declare function runCloudTest(deps: CloudDeps, key: string): Promise<{
|
|
34
|
+
ok: true;
|
|
35
|
+
webhook: CheckResult;
|
|
36
|
+
aggregate: CheckResult;
|
|
37
|
+
} | {
|
|
38
|
+
ok: false;
|
|
39
|
+
error: string;
|
|
40
|
+
}>;
|
|
41
|
+
export {};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import "../store/runtime.js";
|
|
2
|
+
// src/cli/cloud.ts
|
|
3
|
+
const cloudUrl = (env) => env.EVOLVE_CLOUD_URL ?? "https://api.evolveab.com";
|
|
4
|
+
async function call(deps, key, method, path, body) {
|
|
5
|
+
try {
|
|
6
|
+
const res = await (deps.fetch ?? fetch)(new URL(path, cloudUrl(deps.env)), {
|
|
7
|
+
method,
|
|
8
|
+
headers: {
|
|
9
|
+
authorization: `Bearer ${key}`,
|
|
10
|
+
...body === void 0 ? {} : { "content-type": "application/json" }
|
|
11
|
+
},
|
|
12
|
+
body: body === void 0 ? void 0 : JSON.stringify(body),
|
|
13
|
+
signal: AbortSignal.timeout(3e4)
|
|
14
|
+
});
|
|
15
|
+
const text = await res.text();
|
|
16
|
+
let json = null;
|
|
17
|
+
try {
|
|
18
|
+
json = JSON.parse(text);
|
|
19
|
+
} catch {}
|
|
20
|
+
if (!res.ok) {
|
|
21
|
+
const err = json ?? {};
|
|
22
|
+
return {
|
|
23
|
+
ok: false,
|
|
24
|
+
status: res.status,
|
|
25
|
+
error: res.status === 401 ? "the secret key was rejected (check EVOLVE_SECRET_KEY)" : `HTTP ${res.status}${err.message ? `: ${err.message}` : err.error ? `: ${err.error}` : ""}`
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
return {
|
|
29
|
+
ok: true,
|
|
30
|
+
status: res.status,
|
|
31
|
+
json
|
|
32
|
+
};
|
|
33
|
+
} catch (e) {
|
|
34
|
+
return {
|
|
35
|
+
ok: false,
|
|
36
|
+
error: `can't reach ${cloudUrl(deps.env)} (${e instanceof Error ? e.message : e})`
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/** `GET /v1/site`: which site this key belongs to. Accepts the cloud's and the fake cloud's shapes. */
|
|
41
|
+
async function getSiteInfo(deps, key) {
|
|
42
|
+
const r = await call(deps, key, "GET", "/v1/site");
|
|
43
|
+
if (!r.ok) return r;
|
|
44
|
+
const j = r.json;
|
|
45
|
+
return {
|
|
46
|
+
ok: true,
|
|
47
|
+
site: j.site && typeof j.site === "object" ? j.site : {
|
|
48
|
+
id: String(j.site ?? ""),
|
|
49
|
+
name: j.name ?? "",
|
|
50
|
+
appUrl: j.appUrl ?? null
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/** `PUT /v1/site/app-url`. */
|
|
55
|
+
async function putAppUrl(deps, key, appUrl) {
|
|
56
|
+
const r = await call(deps, key, "PUT", "/v1/site/app-url", { appUrl });
|
|
57
|
+
if (!r.ok) return r;
|
|
58
|
+
return {
|
|
59
|
+
ok: true,
|
|
60
|
+
appUrl: r.json.appUrl
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/** `POST /v1/site/test`: the cloud pings the sync route and dry-runs an aggregate. */
|
|
64
|
+
async function runCloudTest(deps, key) {
|
|
65
|
+
const r = await call(deps, key, "POST", "/v1/site/test");
|
|
66
|
+
if (!r.ok) return r;
|
|
67
|
+
const j = r.json;
|
|
68
|
+
return {
|
|
69
|
+
ok: true,
|
|
70
|
+
webhook: j.webhook,
|
|
71
|
+
aggregate: j.aggregate
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
export { cloudUrl, getSiteInfo, putAppUrl, runCloudTest };
|