Skip to content

feat: Accept and save measurements - #10

Open
seanxjin wants to merge 2 commits into
mainfrom
Sean/accept-save-measurements
Open

seanxjin wants to merge 2 commits into
mainfrom
Sean/accept-save-measurements

Conversation

@seanxjin

@seanxjin seanxjin commented Nov 26, 2026 •

Copy link
Copy Markdown

TLDR: I Read the code. Did the tests, it correctly saves ping data + verifies token (checks if that token hash in database with SHA0256) + checks if the test that ran ran against a existing revision id, checks that the test id in the data measurement json is the same as the test id retrieved from db of the hashed token id. Makes sure that body of the request matches the example json in #4 . Parses it.

Summary

New route POST /functions/v1/probe-measurements. Probe run test, probe send result here, server save result. One run = one row in measurements, and the run ID is the row ID.

How it work

  1. The route accepts only POST, and the body must be 64 KiB or less.
  2. The route checks the JSON: exact fields, schema_version 1, UUIDs, RFC 3339 timestamps, finished_at not before started_at, and nothing more than 5 minutes in the future.
  3. It checks the X-Probe-Token header against the probe_id in the body. The token must match an unrevoked credential, and the probe must not be disabled.
  4. The token check runs before the revision lookup, so a stranger learns nothing about which revisions exist.
  5. It looks up the revision that actually ran (test_revision_id), not the probe's current assignment.
  6. Run rules:
    • completed needs exit code 0 and exactly the metric keys in definition.metrics. Values must be numbers. null is allowed only for a nullable metric, for example Ping latency when all packets are lost.
    • failed and timeout need result_data {}. result_code can be an integer or null.
    • location is null or {latitude, longitude, accuracy_m?}.
  7. The save is insert-only (ON CONFLICT (id) DO NOTHING). The server sets received_at, never the probe.
  8. If the run ID already exists, an exact repeat from the same probe gets 200 and the same ID. Anything else gets 409. Rows are never overwritten, and the response never shows another probe's data.

Responses (error body is always {"error":"<code>"} and never echoes the request)

When Status Body
Saved 201 {"id":"<run id>"}
Identical repeat 200 {"id":"<run id>"}
Bad JSON or bad data 400 invalid_request
Token rejected, revoked, or probe disabled 401 unauthorized
Not POST 405 method_not_allowed
Same ID with different data or from another probe 409 conflict
Body too big 413 payload_too_large
Unknown revision 422 unknown_revision
Server broke 500 internal_error

Files

  • functions/probe-measurements/: index.ts (wiring), handler.ts (request flow), validate.ts (data rules), store.ts (database), and tests.
  • functions/_shared/: probe-auth.ts (token check), http.ts (JSON and error helpers), supabase.ts (service-role client).
  • config.toml: [functions.probe-measurements] verify_jwt = false, because probes have no Supabase JWT. The handler checks the token itself.
  • CI: new edge-functions job runs deno lint, deno check and deno test --frozen.
  • Docs: README.md, server/README.md (contract, test commands, local curl check) and AGENTS.md.
  • deno.json and deno.lock added.
  • No schema change, so no migration.

Tokens, request bodies and keys are never logged or returned.

Still to agree before merge

Related issue

Closes #4

Testing

From server/supabase/functions/, Deno 2.9.6:

  • deno lint: pass, 9 files.
  • deno check probe-measurements/index.ts: pass.
  • deno test --frozen: 22 passed, 0 failed. The handler tests use a stand-in token check and an in-memory store, so no backend is needed.

From server/:

  • npx supabase db reset, then npx supabase test db: 3 files, 85 tests, PASS.
  • Ran npx supabase functions serve and posted to the real route with the seeded token:
Case Result
Valid Ping example 201, row saved
Same upload again 200, same ID, received_at unchanged
Same ID with changed metrics 409, original row unchanged
Wrong token, missing token 401, no row
Bad metric keys 400, no row
Unknown revision 422, no row
Ping with all packets lost (rtt_avg_ms: null) 201
Timeout with {} and null code 201
Bad JSON 400
GET 405
  • Checked the measurements table with psql. Only the three accepted runs were saved, and the original row kept its first values and receipt time.
  • The function logs showed no token or request body.
  • Not tested: a hosted deployment.

Checklist

  • I ran all tests and they passed, and I have recorded the results above.
  • I have updated all docs where necessary.
  • I have reviewed the code and can explain the changes.

@gaida123 gaida123 mentioned this pull request Nov 26, 2026
3 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Accept and save measurements

1 participant