Key-value store for ingest JS rules
Ingest JS rules (transform(event)) can keep state across events and batches through a per-project key-value store: drop duplicates, count per user, remember...
Ingest JS rules (transform(event)) can keep state across events and batches through a per-project key-value store: drop duplicates, count per user, remember the first value seen.
JS rules are their own resource, /v1/projects/{id}/js_rules (test one with POST .../js_rules/test). They are not the code kind of a transformation (POST /v1/projects/{id}/transformations with {"rule":{"kind":"code","source":...}}): that kind takes Lucida's statement language, not JavaScript. Its statements are if / else, set, unset, rename ... to, drop, log(...) and call, for example:
if properties.plan = 'trial' {
set properties.segment = concat('trial-', lower(properties.country));
} else if properties.internal = true {
drop;
}
rename properties.old to properties.new;API in rule code
function transform(event) {
const seen = "seen:" + event.properties.order_id;
if (kv.get(seen)) return null; // a duplicate order: drop it
kv.set(seen, true, 7 * 24 * 3600); // remember for 7 days (ttl in seconds)
event.properties.order_number = kv.incr("orders:" + event.distinct_id);
return event;
}Call | Returns |
|---|---|
| the stored JSON value, or |
| nothing; |
| the new number (a missing or non-number value counts as 0); without |
|
|
| seconds left, |
Keys are 1 to 256 bytes, values any JSON up to 64 KiB. A write past the project quota throws (the rule then fails open for that event, as any rule error does). Writes made before a rule throws are kept.
Consistency
Every rule and event of one batch sees the same store, in order, starting from the state when the batch began.
lc-worker applies the writes to Postgres (
rule_kv) after the batch's events are written to ClickHouse, once per batch (markerkv:{token}:{project}iningest_rule_batches). A redelivered batch neither repeats nor loses them; a dedupe rule then also drops events that were already written, which ClickHouse would dedupe anyway.Two workers on the same project merge counters (
incris applied as an increment, not a copy);setis last write wins. The quota is enforced in the sandbox and re-checked on apply.
Quota and admin
Default 10,000 keys and 4 MiB (key plus JSON value) per project; admins may raise it to 100,000 keys and 64 MiB.
Route | Access |
|---|---|
| read: live keys in key order |
| read: |
| admin: delete a key, a prefix or everything (audited) |
| admin (audited) |
The web app shows the store under Data -> Transformations -> Rule key-value store. POST /v1/projects/{id}/js_rules/test runs against the live store and returns the writes it would make as kv_ops without applying them.