mirror of
https://github.com/affaan-m/ECC.git
synced 2026-09-29 04:55:12 +02:00
event-stats
Analytics endpoint over an in-memory event log (300,000 events, generated
deterministically by src/data.js).
API
GET /stats?type=<t>&from=<ms>&to=<ms> returns JSON:
{ "type": "click", "from": 1754000000000, "to": 1756592000000,
"count": 1234, "sum": 56789, "avg": 46.02,
"p50": 123, "p95": 456, "p99": 789, "min": 1, "max": 50000 }
Semantics (all pinned; follow them exactly):
from/toare millisecond timestamps, inclusive, and optional (absent means unbounded). Non-numeric bounds, orfrom > to, are400.- Only events of the given
typewithin[from, to]are included. sumis the exact integer sum ofvalues.avgissum / countrounded half-up to two decimals.- Percentiles use the nearest-rank method: sort values ascending, take the
value at 1-based rank
ceil(p / 100 * count). No interpolation. - If no events match (including an unknown
type), return200withcount: 0, sum: 0andavg,p50,p95,p99,min,maxallnull. - The response echoes the effective
from/to(nullwhen unbounded).
Performance requirement
The endpoint must stay fast at this data size: 2,000 mixed queries complete in under 6 seconds on this machine (the reference does it in ~1.5s). Precompute whatever you need at startup; per-query work must not scan the whole log.
Module contract
src/app.jsis CommonJS and exportscreateApp()returning anhttp.Serverthat is not yet listening.node src/index.js <port>starts the service.- No external dependencies. Run the tests with
npm test.