`usage.get` and `simlock stats`: the figures from the event history, on a worker and a gateway
维护者通常 1 天内回复
评估
这个 Issue 还没有评估数据。
描述
Part of #329.
Scope
After this PR, simlock stats prints the usage figures for a window, as a table and as --json, on a worker and on a gateway, from the event history and nothing else. The programmatic client gets usage.get. ADR 0016 §1, §2, §5, §6, §7 and §8 fix how the figures are computed.
Technical spec
Modules touched
src/core/usage/(new, pure, platform-agnostic) —computeUsage(events, window, options)takes event envelopes in time order, including the carried steps below, and returns the figures.options.fleetselects the gateway rule (ADR 0016 §6);options.labelsis a map from requester id to label the caller fills.- Which window a fact belongs to: a request belongs to the window its
lease.requestedfalls in; its grant, rejection and end are joined from events up toto, so an end aftertois unseen and the lease is open attowith no held or turnaround sample. Provisioning, boot and incident counts go by the device event's timestamp. Utilisation and queue depth come from the steps; where no step precedes the window, the series points before the first step in it arenulland excluded from peak and mean.requestscountslease.requestedin the window (on a worker, only those withoutfleetRequestId; those with it count underprobes, ADR 0021 §5);rejectedcounts the rejections of those requests, plus rejections refused before admission (nolease.requested), which belong to the window theirlease.rejectedfalls in; sogranted + rejectedmay exceedrequestsanddocs/CLI.mdsays so. A request from beforefromthat is granted or rejected inside the window is in no count. - Incidents:
quarantinedcountsdevice.quarantined;crashRecoveredcountsdevice.recovered;quarantineRecoveredcountsdevice.quarantine-recovered;lostcountsdevice.recovery-failed,device.quarantine-abandonedanddevice.quarantine-stranded. - Fleet rule (ADR 0016 §6 as amended by ADR 0021 §5): a gateway's own events are those with no
payload.workerId. A fleet request's outcome is the gateway's ownrequest.grantedorlease.rejectedfor its request id; with neither, a later owndaemon.startedof the gateway ends it as rejecteddaemon-restarted, else it is open. Its wait runs from the gateway'slease.requestedto that outcome, by the gateway's clock. The grant source and held time come from the relayedlease.grantedwhoseworkerIdis therequest.granted'sworkerand whoseleaseIdis itsworkerLeaseId, and that lease's relayed end, by the worker's clock; with no relayed grant the source isunknownand there is no held sample; turnaround is wait plus held. A grant counts for itsrequest.granted'sworker, aworker-failedrejection for its ownworker. Relayedlease.declinedcount as events per worker and per platform (fromrequestSpec) underdeclined, in the window their timestamp falls in. Relayedlease.granted,lease.rejected,lease.declined,request.dispatchedanddaemon.startednever decide an outcome. This PR does not reword ADR 0016 §6; ADR 0021 replaced its join. Per worker on a gateway (ADR 0021 §5): a worker's entry has its device facts, the fleet requests itsrequest.grantednames and itsworker-failedrejections (with their waits and turnarounds), and itsdeclinedevents; a fleet request that ended any other way, or is open, counts in totals and per platform under no worker. - Series: the bucket width is the smallest of 1 minute, 5 minutes, 15 minutes, 1 hour, 6 hours and 1 day that keeps the series at or under 200 points.
src/bus/event-file.tsandEventHistory.replay— acarryoption: for each named event (capacity.changed,queue.changed), the latest envelope at or beforesinceTsis kept in the result, one perworkerIdwhere present. The reader scans every line already, so this adds no I/O.src/contract/errors.ts—HISTORY_NOT_KEPTjoins the error table as adomainerror withcliExitCode: 12,httpStatus: 422and details{ oldestTs }, so the CLI and the HTTP route take their behaviour from the table.src/contract/operations.ts—usage.get, roleadmin, effectread, input{ from, to }as epoch milliseconds withfrom < toandto - frombounded (at most 90 days).src/contract/schemas.tsfor the output shape.src/contract/operations.test.tsrole matrix.src/daemon/dispatcher.ts— worker handler: reads the history for[from, to]throughEventHistory(file plus ring, asevents.replaywithsinceTs, withcarryfor the two step events), fills labels from the token store, callscomputeUsagewithfleet: false, and answersHISTORY_NOT_KEPTwhen the oldest held timestamp is later thanto. It roundsfromandtodown tobucketMs, memoises the last answer by (rounded window, newest event id), and answers a call inside the same bucket while no event arrived from the memo without reading the history; the answer'swindowis the rounded one.src/gateway/dispatcher.ts— the same from the gateway's merged history withfleet: true, stripping its owngw:<instance>:prefix before the label lookup, with the same memo. The memo exists once, shared by both handlers.src/daemon/server.tssocket switch;src/simlock-client/client.tsandtypes.ts—usage(window).src/cli/index.ts—simlock stats [--since <duration> | --from <ISO> [--to <ISO>]] [--json]; default window the last 24 hours;--sinceand--fromtogether is a usage error. Human output: a header with the window andcoversFrom, the "partial" note when set, then the totals, the per-platform rows, the per-worker rows, the per-requester rows.docs/CLI.md,docs/CLIENT.md,docs/HTTP-API.mderror code list if it is shared.
Contract and event changes
- New operation
usage.get. Output, one object:window { from, to },coversFrom,partial: boolean,bucketMs.totals:requests,granted,bySource { warm, booted, provisioned },rejected { total, byReason },declined(lease.declinedevents in the window by their timestamp: the worker's own on a worker, relayed ones on a gateway; not requests, ADR 0021 §5),probes(a worker's own figure, absent from a gateway's answer in totals and in every worker entry; on a worker:lease.requestedcarryingfleetRequestId;requestscounts only those without it; grants, source and held time count both; a probe gives no wait or turnaround sample; inrequestersa probe counts under itsgw:requester ingrantedandheldTotalMsonly),bySourcegainsunknownon a gateway,wait { p50, p95, max, count },held { p50, p95, max, count },turnaround { p50, p95, max, count },provisioning { p50, p95, max, count },boot { p50, p95, max, count },utilisation { slots { peak, mean, max }, ram? { peakBytes, meanBytes, limitBytes } },queue { peakDepth, meanDepth },incidents { quarantined, crashRecovered, quarantineRecovered, lost },failures { byEvent }: a count per failure event name (device.purge-failed,device.recovery-failed,component.install-failed, and any other*-failedevent) in the window by the event's timestamp.platforms: the same figures keyed byiosandandroid.workers: one entry per worker id with the same figures and alabel; on a worker, one entry for itself.requesters:{ id, label?, requests, granted, rejected, heldTotalMs }[], byrequestsdescending.series:{ at, slotsUsed, slotsMax, ramUsedBytes?, queueDepth, waiting }[], one point per bucket,waitingthe number of requests waiting at the bucket's end; on a worker a probe is never counted in it (ADR 0021 §5).- Durations and percentiles in milliseconds; a percentile over an empty set is
null.
- New error code
HISTORY_NOT_KEPTin the contract's error table (domain,cliExitCode: 12,httpStatus: 422) witholdestTsin its details. - Protocol range unchanged: the gateway does not call workers for this.
Rules in play
architecture.mdrules 1 and 10: the figures module has no platform or transport imports; the fleet dedupe rule and the plan-source map exist once.safety.mdrule 10:fromandtoare wire input, bounded before use.testing.mdrule 1: each percentile test states the input set and the expected number.
Tests
computeUsagecounts a request granted, held and released as one request, one grant, and one sample in wait, held and turnaround with the expected millisecondscomputeUsagereports p50, p95 and max of a set of twenty known waitscomputeUsagecounts a rejected request under its reason and not under grantscomputeUsageattributes grants towarm,bootedandprovisionedfromlease.granted.sourcecomputeUsagereads provisioning and boot durations fromdevice.provisionedanddevice.readycomputeUsagederives peak and time-weighted mean slot utilisation fromcapacity.changedsteps, including the step in force before the window startscomputeUsagereports RAM utilisation whencapacity.changedcarriesramBudgetand omits it otherwisecomputeUsagederives peak and mean queue depth fromqueue.changedcomputeUsagecountsquarantined,crashRecovered,quarantineRecoveredandlostfrom the named device events and nothing elsecomputeUsagecounts a request whoselease.requestedis beforefromand whose grant is inside the window under neither requests nor grantscomputeUsagegives no held or turnaround sample for a lease still open atto, and counts its request and grantcomputeUsagecounts a rejection with no precedinglease.requestedunderrejectedand not underrequestscomputeUsagecounts a request from beforefromthat is rejected inside the window under neither requests nor rejectionscomputeUsagereportsnullseries points and excludes them from peak and mean before the first step when no step precedes the windowcomputeUsagegroups by platform and byworkerIdcomputeUsagewithfleet: truecounts a fleet request under the worker itsrequest.grantedorworker-failedrejection names, with its wait and turnaround, and atimeoutrejection under no workercomputeUsageon a worker leaves a probe between itslease.requestedand its grant out ofseries[].waitingcomputeUsagewithfleet: trueanswers noprobesfield in totals or worker entriescomputeUsageon a worker gives a probe no turnaround sample and lists itsgw:requester with its grant and held time and no requestcomputeUsagewithfleet: truecounts a fleet request once, from the gateway's own events, and attributes its grant to theworkerof itsrequest.grantedcomputeUsagewithfleet: trueignores relayedlease.requested,lease.queued,queue.changed,lease.rejectedandlease.declinedas request factscomputeUsagewithfleet: truedoes not count a relayedlease.grantedcarrying a request'sfleetRequestIdas its outcome when the gateway rejected itcomputeUsagewithfleet: truedoes not close an open fleet request at a relayed workerdaemon.startedcomputeUsagewithfleet: truesettles a request as rejected from the gateway's ownlease.rejected, and aworker-failedone under itsworkercomputeUsagewithfleet: truemeasures a fleet wait from the gateway'slease.requestedto itsrequest.granted, ignoring a relayedlease.grantedstamped earliercomputeUsagewithfleet: truetakes the grant source and held time from the relayedlease.grantedwhoseworkerIdandleaseIdmatch therequest.granted'sworkerandworkerLeaseId, and counts sourceunknownwith no held sample when it is missingcomputeUsagewithfleet: truecounts each relayedlease.declinedunder its worker's and platform'sdeclined, two declines of one request as twocomputeUsagewithfleet: trueends a request with no outcome at the gateway's nextdaemon.startedas rejecteddaemon-restarted, and leaves one with no later start opencomputeUsageon a worker counts alease.requestedcarryingfleetRequestIdunderprobes, notrequests, its grant undergranted, itslease.declinedunderdeclinedby the decline's timestamp, and gives it no wait samplecomputeUsagesetspartialandcoversFromwhen the oldest event is inside the windowcomputeUsagepicks 1-minute buckets for one hour, 15-minute buckets for one day and 1-hour buckets for seven days, and never more than 200 points for a 90-day windowreadEventFilewithcarryreturns the latestcapacity.changedandqueue.changedat or beforesinceTs, one per worker id, and none when there is none- the worker handler answers a second call inside the same bucket from the memo without reading the history, and reads again after an event arrives or the bucket moves
computeUsagelists requesters by request count with the label the caller supplied- the worker handler answers
HISTORY_NOT_KEPTwith the oldest held timestamp when the window ends before it - the gateway handler strips its own
gw:prefix before the label lookup and leaves another prefix alone usage.getis anadminoperation anagenttoken cannot callsimlock statsrejects--sincewith--from, and--fromlater than--to, as usage errorssimlock stats --jsonprints the operation's output unchanged- (e2e) after a scripted run of leases against the fake driver,
simlock stats --since 1h --jsonreports the counts a test derives independently fromsimlock events --since 1h - (e2e)
simlock stats --since 1hreturns the same figures before and aftersimlock daemon stopandsimlock daemon start - (e2e) on a gateway with two workers,
simlock statshas fleet totals and one row per worker, and each worker's ownsimlock statscovers itself only
Done when
- After a scripted run against the fake driver with waits, a
--no-waitrejection and a timeout,simlock stats --since 1hprints request count, grants by source, held time and turnaround, wait p50, p95 and max, provisioning and boot durations, peak utilisation, queue depth, rejections by reason and incidents;--jsonprints the same figures; and the counts match what a reader counts by hand fromsimlock events --since 1h. - The same window prints the same figures after
simlock daemon stopandsimlock daemon start. - On a gateway with two workers, the output has fleet totals and one row per worker; each worker's own output has one row, itself.
- A request that waited shows in the wait figures; a request that timed out and one cancelled are counted under rejections, not grants.
- The per-requester rows show each requester's leases in the window, with the token label beside the id for a lease taken over HTTP.
- After a
--no-waitrefusal,simlock stats --since 1hshowsrequestsandrejected.byReason.no-waiteach one higher than before it andgrantedunchanged. simlock stats --from <a day ago> --to <two days ago>is a usage error;--since 30don a history whose oldest event is a minute old prints the figures with "Figures cover from " above them; a window that ends before the oldest held event prints theHISTORY_NOT_KEPTmessage and exits non-zero.docs/CLI.mddescribessimlock stats, its flags, the partial note and the error.
Out of scope
- The HTTP route and the CSV export (next task).
- The console view.
Depends on
- #345
- #346
- #431
Approval
- Approved for delivery
Written by an agent.
- 主要语言
- TypeScript
- 星标
- 20
- 派生
- 2
- 平均合并
- 7 小时 21 分钟
- 30 天内合并 PR
- 247
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
callstackincubator/simlock 的其他 Issue
-
bug:new
难度 2/5 1-3 小时 新手友好度 76/100
callstackincubator/simlock#673 ·
维护者通常 1 天内回复
-
bug:needs-info
难度 2/5 1-3 小时 新手友好度 76/100
callstackincubator/simlock#657 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 70/100
callstackincubator/simlock#639 ·
维护者通常 1 天内回复
-
bug:new
难度 3/5 1-2 天 新手友好度 70/100
callstackincubator/simlock#672 ·
维护者通常 1 天内回复
-
Flaky console lane: CLI auto-launch's fixed 5 s deadline expires while a daemon starts on a loaded runner (ENOENT daemon.sock)可能已有人在做 @V3RON 今天认领。 未关闭bug:ready flaky-test
callstackincubator/simlock#663 · 1 条评论 · 已指派 1 人 ·
维护者通常 1 天内回复
查看 callstackincubator/simlock 的全部 Issue
相似的 Issue
-
backend priority:P3 privacy-fix
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
bug desktop v3 windows
难度 2/5 1-3 小时 新手友好度 65/100
esengine/DeepSeek-Reasonix#13697 · 1 条评论 ·
维护者通常 1 天内回复
-
area:security documentation effort:S priority:P2
难度 2/5 1-3 小时 新手友好度 65/100
维护者通常 1 天内回复
-
app:web meta:ops
难度 2/5 半天 新手友好度 78/100
osu-tournament-rating/otr#1113 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 82/100