opencode-quota-gui 1.0.1 → 1.1.0
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 +32 -18
- package/dist/contracts/analytics-entity.d.ts +32 -0
- package/dist/contracts/analytics-entity.d.ts.map +1 -0
- package/dist/contracts/analytics-entity.js +123 -0
- package/dist/contracts/analytics-entity.js.map +1 -0
- package/dist/contracts/build-identity.js +1 -1
- package/dist/contracts/collision-safe-key.d.ts +14 -0
- package/dist/contracts/collision-safe-key.d.ts.map +1 -0
- package/dist/contracts/collision-safe-key.js +28 -0
- package/dist/contracts/collision-safe-key.js.map +1 -0
- package/dist/contracts/dashboard.d.ts +139 -0
- package/dist/contracts/dashboard.d.ts.map +1 -1
- package/dist/data/modelsdev-pricing.min.json +1 -1064
- package/dist/gui/aggregation.d.ts.map +1 -1
- package/dist/gui/aggregation.js +6 -0
- package/dist/gui/aggregation.js.map +1 -1
- package/dist/gui/analytics-bridge.d.ts.map +1 -1
- package/dist/gui/analytics-bridge.js +14 -0
- package/dist/gui/analytics-bridge.js.map +1 -1
- package/dist/gui/analytics-engine.d.ts +3 -1
- package/dist/gui/analytics-engine.d.ts.map +1 -1
- package/dist/gui/analytics-engine.js +198 -7
- package/dist/gui/analytics-engine.js.map +1 -1
- package/dist/gui/analytics-model.d.ts +7 -5
- package/dist/gui/analytics-model.d.ts.map +1 -1
- package/dist/gui/analytics-model.js +444 -38
- package/dist/gui/analytics-model.js.map +1 -1
- package/dist/gui/analytics-protocol.d.ts +6 -2
- package/dist/gui/analytics-protocol.d.ts.map +1 -1
- package/dist/gui/analytics-protocol.js.map +1 -1
- package/dist/gui/analytics-service.d.ts +3 -1
- package/dist/gui/analytics-service.d.ts.map +1 -1
- package/dist/gui/analytics-service.js +14 -0
- package/dist/gui/analytics-service.js.map +1 -1
- package/dist/gui/analytics-state.d.ts +8 -0
- package/dist/gui/analytics-state.d.ts.map +1 -1
- package/dist/gui/analytics-state.js +13 -0
- package/dist/gui/analytics-state.js.map +1 -1
- package/dist/gui/analytics-worker-runtime.d.ts +5 -0
- package/dist/gui/analytics-worker-runtime.d.ts.map +1 -1
- package/dist/gui/analytics-worker-runtime.js +6 -0
- package/dist/gui/analytics-worker-runtime.js.map +1 -1
- package/dist/gui/lifecycle.d.ts +1 -1
- package/dist/gui/lifecycle.d.ts.map +1 -1
- package/dist/gui/lifecycle.js +1 -0
- package/dist/gui/lifecycle.js.map +1 -1
- package/dist/gui/server.d.ts.map +1 -1
- package/dist/gui/server.js +62 -0
- package/dist/gui/server.js.map +1 -1
- package/dist/gui/snapshot-service.d.ts +3 -1
- package/dist/gui/snapshot-service.d.ts.map +1 -1
- package/dist/gui/snapshot-service.js +10 -0
- package/dist/gui/snapshot-service.js.map +1 -1
- package/dist/gui/sources.d.ts +1 -1
- package/dist/gui/sources.d.ts.map +1 -1
- package/dist/gui/sources.js +25 -4
- package/dist/gui/sources.js.map +1 -1
- package/dist/lib/cursor-usage.d.ts.map +1 -1
- package/dist/lib/cursor-usage.js +8 -2
- package/dist/lib/cursor-usage.js.map +1 -1
- package/dist/lib/modelsdev-pricing.d.ts +54 -1
- package/dist/lib/modelsdev-pricing.d.ts.map +1 -1
- package/dist/lib/modelsdev-pricing.js +284 -43
- package/dist/lib/modelsdev-pricing.js.map +1 -1
- package/dist/lib/opencode-console-auth.d.ts +19 -0
- package/dist/lib/opencode-console-auth.d.ts.map +1 -1
- package/dist/lib/opencode-console-auth.js +263 -3
- package/dist/lib/opencode-console-auth.js.map +1 -1
- package/dist/lib/opencode-storage.d.ts +26 -11
- package/dist/lib/opencode-storage.d.ts.map +1 -1
- package/dist/lib/opencode-storage.js +33 -56
- package/dist/lib/opencode-storage.js.map +1 -1
- package/dist/lib/quota-providers-local.d.ts.map +1 -1
- package/dist/lib/quota-providers-local.js +13 -3
- package/dist/lib/quota-providers-local.js.map +1 -1
- package/dist/lib/quota-stats-format.d.ts.map +1 -1
- package/dist/lib/quota-stats-format.js +28 -6
- package/dist/lib/quota-stats-format.js.map +1 -1
- package/dist/lib/quota-stats.d.ts +49 -6
- package/dist/lib/quota-stats.d.ts.map +1 -1
- package/dist/lib/quota-stats.js +319 -20
- package/dist/lib/quota-stats.js.map +1 -1
- package/dist/lib/scoped-query-chunks.d.ts +15 -0
- package/dist/lib/scoped-query-chunks.d.ts.map +1 -0
- package/dist/lib/scoped-query-chunks.js +48 -0
- package/dist/lib/scoped-query-chunks.js.map +1 -0
- package/dist/lib/token-cost.d.ts +29 -0
- package/dist/lib/token-cost.d.ts.map +1 -1
- package/dist/lib/token-cost.js +74 -10
- package/dist/lib/token-cost.js.map +1 -1
- package/dist/provider-logos/NOTICE.md +12 -0
- package/dist/provider-logos/OPENCODE-MIT.txt +21 -0
- package/dist/provider-logos/OPENCODE-NOTICE.md +12 -0
- package/dist/provider-logos/UPSTREAM-MIT.txt +21 -0
- package/dist/providers/cache-policies.d.ts.map +1 -1
- package/dist/providers/cache-policies.js +4 -3
- package/dist/providers/cache-policies.js.map +1 -1
- package/dist/providers/opencode-go.d.ts.map +1 -1
- package/dist/providers/opencode-go.js +2 -0
- package/dist/providers/opencode-go.js.map +1 -1
- package/dist/providers/opencode-zen.d.ts.map +1 -1
- package/dist/providers/opencode-zen.js +2 -0
- package/dist/providers/opencode-zen.js.map +1 -1
- package/dist/ui/assets/{index-D3-Yn_WA.css → index-BZwMJ6Ax.css} +1 -1
- package/dist/ui/assets/index-CjNjqsse.js +8 -0
- package/dist/ui/assets/providers/302ai.svg +7 -0
- package/dist/ui/assets/providers/abacus.svg +14 -0
- package/dist/ui/assets/providers/abliteration-ai.svg +6 -0
- package/dist/ui/assets/providers/above.svg +3 -0
- package/dist/ui/assets/providers/agentrouter.svg +4 -0
- package/dist/ui/assets/providers/agnes.svg +1 -0
- package/dist/ui/assets/providers/ai-router.svg +5 -0
- package/dist/ui/assets/providers/ai21.svg +3 -0
- package/dist/ui/assets/providers/aiand.svg +4 -0
- package/dist/ui/assets/providers/aihubmix.svg +3 -0
- package/dist/ui/assets/providers/aimlapi.svg +3 -0
- package/dist/ui/assets/providers/ainetcafe.svg +1 -0
- package/dist/ui/assets/providers/aixy.svg +4 -0
- package/dist/ui/assets/providers/aki-io.svg +7 -0
- package/dist/ui/assets/providers/alibaba-cn.svg +3 -0
- package/dist/ui/assets/providers/alibaba-coding-plan-cn.svg +3 -0
- package/dist/ui/assets/providers/alibaba-coding-plan.svg +3 -0
- package/dist/ui/assets/providers/alibaba-token-plan.svg +3 -0
- package/dist/ui/assets/providers/alibaba.svg +3 -0
- package/dist/ui/assets/providers/amazon-bedrock.svg +3 -0
- package/dist/ui/assets/providers/ambient.svg +3 -0
- package/dist/ui/assets/providers/amd.svg +3 -0
- package/dist/ui/assets/providers/anthropic.svg +3 -0
- package/dist/ui/assets/providers/anyapi.svg +3 -0
- package/dist/ui/assets/providers/arcee.svg +3 -0
- package/dist/ui/assets/providers/atomic-chat.svg +1 -0
- package/dist/ui/assets/providers/auriko.svg +6 -0
- package/dist/ui/assets/providers/azure-cognitive-services.svg +3 -0
- package/dist/ui/assets/providers/azure.svg +3 -0
- package/dist/ui/assets/providers/bailing.svg +3 -0
- package/dist/ui/assets/providers/baseten.svg +3 -0
- package/dist/ui/assets/providers/bee.svg +1 -0
- package/dist/ui/assets/providers/berget.svg +3 -0
- package/dist/ui/assets/providers/blueclaw.svg +3 -0
- package/dist/ui/assets/providers/bothub.svg +33 -0
- package/dist/ui/assets/providers/boundless.svg +12 -0
- package/dist/ui/assets/providers/cerebras.svg +4 -0
- package/dist/ui/assets/providers/clarifai.svg +3 -0
- package/dist/ui/assets/providers/claudinio.svg +19 -0
- package/dist/ui/assets/providers/cloudferro-sherlock.svg +5 -0
- package/dist/ui/assets/providers/cloudflare-ai-gateway.svg +4 -0
- package/dist/ui/assets/providers/cloudflare-workers-ai.svg +4 -0
- package/dist/ui/assets/providers/cohere.svg +5 -0
- package/dist/ui/assets/providers/commandcode.svg +11 -0
- package/dist/ui/assets/providers/coralbricks.svg +6 -0
- package/dist/ui/assets/providers/crossmodel.svg +5 -0
- package/dist/ui/assets/providers/crusoe.svg +23 -0
- package/dist/ui/assets/providers/daoxe.svg +3 -0
- package/dist/ui/assets/providers/databricks.svg +3 -0
- package/dist/ui/assets/providers/deepinfra.svg +4 -0
- package/dist/ui/assets/providers/deepseek.svg +3 -0
- package/dist/ui/assets/providers/digitalocean.svg +6 -0
- package/dist/ui/assets/providers/dinference.svg +1 -0
- package/dist/ui/assets/providers/drun.svg +8 -0
- package/dist/ui/assets/providers/ebcloud.svg +8 -0
- package/dist/ui/assets/providers/echo.svg +7 -0
- package/dist/ui/assets/providers/edenai.svg +8 -0
- package/dist/ui/assets/providers/empiriolabs.svg +1 -0
- package/dist/ui/assets/providers/engy.svg +1 -0
- package/dist/ui/assets/providers/evroc.svg +3 -0
- package/dist/ui/assets/providers/fastrouter.svg +3 -0
- package/dist/ui/assets/providers/fireworks-ai.svg +3 -0
- package/dist/ui/assets/providers/fluence.svg +3 -0
- package/dist/ui/assets/providers/freemodel.svg +3 -0
- package/dist/ui/assets/providers/friendli.svg +3 -0
- package/dist/ui/assets/providers/frogbot.svg +36 -0
- package/dist/ui/assets/providers/generic.svg +4 -0
- package/dist/ui/assets/providers/github-copilot.svg +3 -0
- package/dist/ui/assets/providers/gitlab.svg +3 -0
- package/dist/ui/assets/providers/google-vertex-anthropic.svg +10 -0
- package/dist/ui/assets/providers/google-vertex.svg +10 -0
- package/dist/ui/assets/providers/google.svg +3 -0
- package/dist/ui/assets/providers/greenpt.svg +6 -0
- package/dist/ui/assets/providers/groq.svg +3 -0
- package/dist/ui/assets/providers/helicone.svg +6 -0
- package/dist/ui/assets/providers/hetzner.svg +3 -0
- package/dist/ui/assets/providers/hpc-ai.svg +444 -0
- package/dist/ui/assets/providers/huggingface.svg +3 -0
- package/dist/ui/assets/providers/hyper.svg +20 -0
- package/dist/ui/assets/providers/impossibl.svg +3 -0
- package/dist/ui/assets/providers/inception.svg +14 -0
- package/dist/ui/assets/providers/inceptron.svg +14 -0
- package/dist/ui/assets/providers/inco.svg +3 -0
- package/dist/ui/assets/providers/infer.svg +4 -0
- package/dist/ui/assets/providers/inference.svg +3 -0
- package/dist/ui/assets/providers/inferx.svg +5 -0
- package/dist/ui/assets/providers/infomaniak.svg +3 -0
- package/dist/ui/assets/providers/io-net.svg +5 -0
- package/dist/ui/assets/providers/iteracompute.svg +6 -0
- package/dist/ui/assets/providers/jalapeno.svg +8 -0
- package/dist/ui/assets/providers/jiekou.svg +4 -0
- package/dist/ui/assets/providers/kenari.svg +4 -0
- package/dist/ui/assets/providers/kilo.svg +4 -0
- package/dist/ui/assets/providers/kimi-code-plan-cn.svg +3 -0
- package/dist/ui/assets/providers/kimi-code-plan-global.svg +3 -0
- package/dist/ui/assets/providers/klokintegration.svg +11 -0
- package/dist/ui/assets/providers/kosmik.svg +28 -0
- package/dist/ui/assets/providers/kuae-cloud-coding-plan.svg +3 -0
- package/dist/ui/assets/providers/lilac.svg +6 -0
- package/dist/ui/assets/providers/liquid.svg +1 -0
- package/dist/ui/assets/providers/llmgateway-providers.svg +6 -0
- package/dist/ui/assets/providers/llmgateway.svg +6 -0
- package/dist/ui/assets/providers/llmtech.svg +6 -0
- package/dist/ui/assets/providers/llmtr.svg +12 -0
- package/dist/ui/assets/providers/lmstudio.svg +3 -0
- package/dist/ui/assets/providers/longcat.svg +6 -0
- package/dist/ui/assets/providers/lowrouter.svg +8 -0
- package/dist/ui/assets/providers/lucidquery.svg +3 -0
- package/dist/ui/assets/providers/lynkr.svg +1 -0
- package/dist/ui/assets/providers/meganova.svg +3 -0
- package/dist/ui/assets/providers/melious.svg +3 -0
- package/dist/ui/assets/providers/merge-gateway.svg +4 -0
- package/dist/ui/assets/providers/meta.svg +3 -0
- package/dist/ui/assets/providers/minimax-cn-coding-plan.svg +3 -0
- package/dist/ui/assets/providers/minimax-cn.svg +3 -0
- package/dist/ui/assets/providers/minimax-coding-plan.svg +3 -0
- package/dist/ui/assets/providers/minimax.svg +3 -0
- package/dist/ui/assets/providers/mistral.svg +3 -0
- package/dist/ui/assets/providers/mixlayer.svg +8 -0
- package/dist/ui/assets/providers/moark.svg +3 -0
- package/dist/ui/assets/providers/modal.svg +3 -0
- package/dist/ui/assets/providers/model-oracle-ai.svg +11 -0
- package/dist/ui/assets/providers/modelis.svg +1 -0
- package/dist/ui/assets/providers/modelscope.svg +5 -0
- package/dist/ui/assets/providers/moonshotai-cn.svg +3 -0
- package/dist/ui/assets/providers/moonshotai.svg +3 -0
- package/dist/ui/assets/providers/munito-eu.svg +1 -0
- package/dist/ui/assets/providers/munito.svg +1 -0
- package/dist/ui/assets/providers/nan.svg +1 -0
- package/dist/ui/assets/providers/nano-gpt.svg +3 -0
- package/dist/ui/assets/providers/nearai.svg +1 -0
- package/dist/ui/assets/providers/nebius.svg +4 -0
- package/dist/ui/assets/providers/neon.svg +3 -0
- package/dist/ui/assets/providers/neosmith.svg +6 -0
- package/dist/ui/assets/providers/neuralwatt.svg +27 -0
- package/dist/ui/assets/providers/nova.svg +3 -0
- package/dist/ui/assets/providers/novita-ai.svg +10 -0
- package/dist/ui/assets/providers/nvidia.svg +3 -0
- package/dist/ui/assets/providers/oci.svg +3 -0
- package/dist/ui/assets/providers/ofox.svg +5 -0
- package/dist/ui/assets/providers/ollama-cloud.svg +7 -0
- package/dist/ui/assets/providers/openai.svg +3 -0
- package/dist/ui/assets/providers/opencode-go-g.svg +3 -0
- package/dist/ui/assets/providers/opencode-go.svg +4 -0
- package/dist/ui/assets/providers/opencode.svg +4 -0
- package/dist/ui/assets/providers/openreason.svg +8 -0
- package/dist/ui/assets/providers/openrouter.svg +3 -0
- package/dist/ui/assets/providers/opper.svg +3 -0
- package/dist/ui/assets/providers/orcarouter.svg +2 -0
- package/dist/ui/assets/providers/ovhcloud.svg +3 -0
- package/dist/ui/assets/providers/pareto.svg +5 -0
- package/dist/ui/assets/providers/pendra.svg +10 -0
- package/dist/ui/assets/providers/perplexity-agent.svg +3 -0
- package/dist/ui/assets/providers/perplexity.svg +3 -0
- package/dist/ui/assets/providers/pioneer.svg +3 -0
- package/dist/ui/assets/providers/poe.svg +7 -0
- package/dist/ui/assets/providers/poolside.svg +3 -0
- package/dist/ui/assets/providers/prime.svg +7 -0
- package/dist/ui/assets/providers/privatemode-ai.svg +5 -0
- package/dist/ui/assets/providers/qihang-ai.svg +9 -0
- package/dist/ui/assets/providers/qiniu-ai.svg +7 -0
- package/dist/ui/assets/providers/qvac.svg +6 -0
- package/dist/ui/assets/providers/reflection.svg +3 -0
- package/dist/ui/assets/providers/regolo-ai.svg +45 -0
- package/dist/ui/assets/providers/routing-run.svg +4 -0
- package/dist/ui/assets/providers/runinfra.svg +10 -0
- package/dist/ui/assets/providers/salad-cloud.svg +15 -0
- package/dist/ui/assets/providers/sambanova.svg +26 -0
- package/dist/ui/assets/providers/scaleway.svg +5 -0
- package/dist/ui/assets/providers/scnet-token-plan.svg +8 -0
- package/dist/ui/assets/providers/scx-ai.svg +1 -0
- package/dist/ui/assets/providers/sensenova.svg +18 -0
- package/dist/ui/assets/providers/siliconflow-cn.svg +3 -0
- package/dist/ui/assets/providers/siliconflow.svg +3 -0
- package/dist/ui/assets/providers/snowflake-cortex.svg +3 -0
- package/dist/ui/assets/providers/stackit.svg +3 -0
- package/dist/ui/assets/providers/standardcompute.svg +5 -0
- package/dist/ui/assets/providers/stepfun-ai-step-plan.svg +16 -0
- package/dist/ui/assets/providers/stepfun-ai.svg +16 -0
- package/dist/ui/assets/providers/stepfun-step-plan.svg +16 -0
- package/dist/ui/assets/providers/stepfun.svg +16 -0
- package/dist/ui/assets/providers/subconscious.svg +3 -0
- package/dist/ui/assets/providers/submodel.svg +3 -0
- package/dist/ui/assets/providers/teamorouter.svg +3 -0
- package/dist/ui/assets/providers/tempr.svg +4 -0
- package/dist/ui/assets/providers/tencent-coding-plan.svg +5 -0
- package/dist/ui/assets/providers/tencent-token-plan.svg +3 -0
- package/dist/ui/assets/providers/tencent-tokenhub.svg +3 -0
- package/dist/ui/assets/providers/tensorx.svg +6 -0
- package/dist/ui/assets/providers/the-grid-ai.svg +14 -0
- package/dist/ui/assets/providers/thinkingmachines.svg +3 -0
- package/dist/ui/assets/providers/tinfoil.svg +3 -0
- package/dist/ui/assets/providers/togetherai.svg +4 -0
- package/dist/ui/assets/providers/tokengo.svg +3 -0
- package/dist/ui/assets/providers/tokenrouter.svg +4 -0
- package/dist/ui/assets/providers/trustedrouter.svg +4 -0
- package/dist/ui/assets/providers/umans-ai-coding-plan.svg +29 -0
- package/dist/ui/assets/providers/umans-ai.svg +29 -0
- package/dist/ui/assets/providers/unorouter.svg +7 -0
- package/dist/ui/assets/providers/vancine.svg +6 -0
- package/dist/ui/assets/providers/venice.svg +4 -0
- package/dist/ui/assets/providers/vercel.svg +3 -0
- package/dist/ui/assets/providers/vispark.svg +3 -0
- package/dist/ui/assets/providers/vivgrid.svg +4 -0
- package/dist/ui/assets/providers/volcengine-coding-plan.svg +3 -0
- package/dist/ui/assets/providers/volcengine.svg +3 -0
- package/dist/ui/assets/providers/vultr.svg +6 -0
- package/dist/ui/assets/providers/wafer.ai.svg +3 -0
- package/dist/ui/assets/providers/wallaby.svg +4 -0
- package/dist/ui/assets/providers/wandb.svg +6 -0
- package/dist/ui/assets/providers/watsonx.svg +5 -0
- package/dist/ui/assets/providers/xai.svg +3 -0
- package/dist/ui/assets/providers/xiaomi-token-plan-ams.svg +3 -0
- package/dist/ui/assets/providers/xiaomi-token-plan-cn.svg +3 -0
- package/dist/ui/assets/providers/xiaomi-token-plan-sgp.svg +3 -0
- package/dist/ui/assets/providers/xiaomi.svg +3 -0
- package/dist/ui/assets/providers/xpersona.svg +8 -0
- package/dist/ui/assets/providers/zai-coding-plan.svg +3 -0
- package/dist/ui/assets/providers/zai.svg +3 -0
- package/dist/ui/assets/providers/zeldoc.svg +3 -0
- package/dist/ui/assets/providers/zenifra.svg +4 -0
- package/dist/ui/assets/providers/zenmux.svg +4 -0
- package/dist/ui/assets/providers/zhipuai-coding-plan.svg +3 -0
- package/dist/ui/assets/providers/zhipuai.svg +3 -0
- package/dist/ui/index.html +2 -2
- package/package.json +4 -1
- package/dist/ui/assets/index-CPEiQt-C.js +0 -8
|
@@ -1,6 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Worker-only rollup model.
|
|
3
|
+
*
|
|
4
|
+
* Owns deterministic fork ownership over compact fingerprint groups plus
|
|
5
|
+
* per-hour rollups (global / project / session) and derives grouped
|
|
6
|
+
* model/provider views from the same owned steps. Full hours use
|
|
7
|
+
* pre-aggregated buckets; only the partial edge hours of a request touch the
|
|
8
|
+
* owned step list, so bucket boundaries stay exact without rebuilding a raw
|
|
9
|
+
* model per view.
|
|
10
|
+
*/
|
|
1
11
|
import { sumTokenStack } from "../contracts/guards.js";
|
|
2
12
|
import { planRangeBuckets, rangeBucketUnit, rangeWindow, } from "../contracts/ranges.js";
|
|
3
|
-
import { rawAssistantRowFingerprint, } from "../lib/opencode-storage.js";
|
|
13
|
+
import { rawAssistantRowFingerprint, rawAssistantRowHasTokenUsage, } from "../lib/opencode-storage.js";
|
|
4
14
|
import { priceTokenBuckets } from "../lib/quota-stats.js";
|
|
5
15
|
import { selectForkOwners } from "../lib/usage-ownership.js";
|
|
6
16
|
import { UNKNOWN_PROJECT_ID, UNTITLED_SESSION_TITLE } from "./aggregation.js";
|
|
@@ -18,13 +28,6 @@ function createAccumulator() {
|
|
|
18
28
|
unknownTokens: 0,
|
|
19
29
|
};
|
|
20
30
|
}
|
|
21
|
-
function isZeroTokens(accumulator) {
|
|
22
|
-
return (accumulator.input === 0 &&
|
|
23
|
-
accumulator.cacheRead === 0 &&
|
|
24
|
-
accumulator.cacheWrite === 0 &&
|
|
25
|
-
accumulator.output === 0 &&
|
|
26
|
-
accumulator.reasoning === 0);
|
|
27
|
-
}
|
|
28
31
|
function addStepToAccumulator(accumulator, step) {
|
|
29
32
|
accumulator.input += step.tokens.input;
|
|
30
33
|
accumulator.cacheRead += step.tokens.cacheRead;
|
|
@@ -76,6 +79,177 @@ function finalize(accumulator) {
|
|
|
76
79
|
unknownTokens: accumulator.unknownTokens,
|
|
77
80
|
};
|
|
78
81
|
}
|
|
82
|
+
function createGroupNode() {
|
|
83
|
+
return {
|
|
84
|
+
totals: createAccumulator(),
|
|
85
|
+
sessions: new Set(),
|
|
86
|
+
stepCount: 0,
|
|
87
|
+
children: new Map(),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
function identityOf(raw) {
|
|
91
|
+
return raw === undefined ? { kind: "unknown" } : { kind: "known", id: raw };
|
|
92
|
+
}
|
|
93
|
+
function identityTotal(identity) {
|
|
94
|
+
return identity.kind === "known" ? identity.id : undefined;
|
|
95
|
+
}
|
|
96
|
+
/** Known raw ids sort before the explicit unknown identity, then by raw id. */
|
|
97
|
+
function compareIdentities(a, b) {
|
|
98
|
+
if (a.kind !== b.kind)
|
|
99
|
+
return a.kind === "known" ? -1 : 1;
|
|
100
|
+
if (a.kind === "known" && b.kind === "known")
|
|
101
|
+
return compareIds(a.id, b.id);
|
|
102
|
+
return 0;
|
|
103
|
+
}
|
|
104
|
+
function accumulatorTotal(accumulator) {
|
|
105
|
+
return (accumulator.input +
|
|
106
|
+
accumulator.cacheRead +
|
|
107
|
+
accumulator.cacheWrite +
|
|
108
|
+
accumulator.output +
|
|
109
|
+
accumulator.reasoning);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Deterministic token rank for the selected range: token volume descending,
|
|
113
|
+
* then raw identity ascending with the unknown identity last. The rank never
|
|
114
|
+
* depends on key insertion order or on cost.
|
|
115
|
+
*/
|
|
116
|
+
function compareGroupEntries(a, b) {
|
|
117
|
+
const byTokens = accumulatorTotal(b.node.totals) - accumulatorTotal(a.node.totals);
|
|
118
|
+
if (byTokens !== 0)
|
|
119
|
+
return byTokens;
|
|
120
|
+
return compareIdentities(identityOf(a.raw), identityOf(b.raw));
|
|
121
|
+
}
|
|
122
|
+
function sortedChildEntries(node) {
|
|
123
|
+
return [...node.children].map(([raw, child]) => ({ raw, node: child })).sort(compareGroupEntries);
|
|
124
|
+
}
|
|
125
|
+
function materializeBuckets(bucketTotals, bucketCount) {
|
|
126
|
+
const buckets = new Array(bucketCount);
|
|
127
|
+
for (let index = 0; index < bucketCount; index += 1) {
|
|
128
|
+
const accumulator = bucketTotals.get(index);
|
|
129
|
+
buckets[index] = finalize(accumulator ?? createAccumulator());
|
|
130
|
+
}
|
|
131
|
+
return buckets;
|
|
132
|
+
}
|
|
133
|
+
function addStepToGroupNode(node, step) {
|
|
134
|
+
addStepToAccumulator(node.totals, step);
|
|
135
|
+
node.sessions.add(step.sessionID);
|
|
136
|
+
node.stepCount += 1;
|
|
137
|
+
}
|
|
138
|
+
function materializeGroupRow(raw, node) {
|
|
139
|
+
return {
|
|
140
|
+
identity: identityOf(raw),
|
|
141
|
+
tokens: finalize(node.totals),
|
|
142
|
+
sessionCount: node.sessions.size,
|
|
143
|
+
stepCount: node.stepCount,
|
|
144
|
+
children: sortedChildEntries(node).map((entry) => materializeGroupRow(entry.raw, entry.node)),
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
function addStepToBucket(bucketTotals, step, bucketIndex) {
|
|
148
|
+
if (bucketIndex < 0)
|
|
149
|
+
return;
|
|
150
|
+
let bucket = bucketTotals.get(bucketIndex);
|
|
151
|
+
if (!bucket) {
|
|
152
|
+
bucket = createAccumulator();
|
|
153
|
+
bucketTotals.set(bucketIndex, bucket);
|
|
154
|
+
}
|
|
155
|
+
addStepToAccumulator(bucket, step);
|
|
156
|
+
}
|
|
157
|
+
function addStepToEntityBucket(entityBuckets, raw, step, bucketIndex) {
|
|
158
|
+
if (bucketIndex < 0)
|
|
159
|
+
return;
|
|
160
|
+
let bucketTotals = entityBuckets.get(raw);
|
|
161
|
+
if (!bucketTotals) {
|
|
162
|
+
bucketTotals = new Map();
|
|
163
|
+
entityBuckets.set(raw, bucketTotals);
|
|
164
|
+
}
|
|
165
|
+
addStepToBucket(bucketTotals, step, bucketIndex);
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Sparse per-bucket entity breakdown aligned with the snapshot boundaries.
|
|
169
|
+
* Entities are ranked by their own bucket tokens, then exact identity with the
|
|
170
|
+
* unknown identity last, so output order never depends on map insertion.
|
|
171
|
+
*/
|
|
172
|
+
function materializeEntityBuckets(entityBuckets, bucketCount) {
|
|
173
|
+
const buckets = [];
|
|
174
|
+
for (let index = 0; index < bucketCount; index += 1)
|
|
175
|
+
buckets.push([]);
|
|
176
|
+
for (const [raw, bucketTotals] of entityBuckets) {
|
|
177
|
+
for (const [bucketIndex, accumulator] of bucketTotals) {
|
|
178
|
+
const bucket = buckets[bucketIndex];
|
|
179
|
+
if (bucket === undefined)
|
|
180
|
+
continue;
|
|
181
|
+
bucket.push({ identity: identityOf(raw), tokens: finalize(accumulator) });
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
for (const entries of buckets) {
|
|
185
|
+
entries.sort((a, b) => {
|
|
186
|
+
const byTokens = b.tokens.total - a.tokens.total;
|
|
187
|
+
if (byTokens !== 0)
|
|
188
|
+
return byTokens;
|
|
189
|
+
return compareIdentities(a.identity, b.identity);
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
return buckets;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Top five entities by selected-range token rank plus one chart-only Other
|
|
196
|
+
* series. Only these sources carry per-bucket accumulators, so the returned
|
|
197
|
+
* bucket arrays are bounded by series × buckets and their breakdowns by the
|
|
198
|
+
* visible entity cardinality, never by a parent × child cross-product.
|
|
199
|
+
*/
|
|
200
|
+
function buildSeries(top, other, bucketCount) {
|
|
201
|
+
const series = top.map((source) => ({
|
|
202
|
+
kind: "entity",
|
|
203
|
+
identity: identityOf(source.raw),
|
|
204
|
+
tokens: finalize(source.totals),
|
|
205
|
+
buckets: materializeBuckets(source.bucketTotals, bucketCount),
|
|
206
|
+
childBuckets: materializeEntityBuckets(source.childBuckets, bucketCount),
|
|
207
|
+
}));
|
|
208
|
+
if (other !== undefined) {
|
|
209
|
+
series.push({
|
|
210
|
+
kind: "other",
|
|
211
|
+
tokens: finalize(other.totals),
|
|
212
|
+
buckets: materializeBuckets(other.bucketTotals, bucketCount),
|
|
213
|
+
childBuckets: materializeEntityBuckets(other.childBuckets, bucketCount),
|
|
214
|
+
...(other.parentBuckets === undefined
|
|
215
|
+
? {}
|
|
216
|
+
: { parentBuckets: materializeEntityBuckets(other.parentBuckets, bucketCount) }),
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
return series;
|
|
220
|
+
}
|
|
221
|
+
function rawIdForDimension(step, dimension) {
|
|
222
|
+
return dimension === "model" ? step.modelID : step.providerID;
|
|
223
|
+
}
|
|
224
|
+
function oppositeDimension(dimension) {
|
|
225
|
+
return dimension === "model" ? "provider" : "model";
|
|
226
|
+
}
|
|
227
|
+
/** Filter matches the entity's own dimension, independent of grouping. */
|
|
228
|
+
function matchesFilter(step, filter) {
|
|
229
|
+
if (filter === undefined)
|
|
230
|
+
return true;
|
|
231
|
+
return rawIdForDimension(step, filter.dimension) === identityTotal(filter.identity);
|
|
232
|
+
}
|
|
233
|
+
/** Last bucket whose start is at or before `timeMs`; boundaries are contiguous. */
|
|
234
|
+
function bucketIndexAt(bucketStarts, timeMs) {
|
|
235
|
+
let low = 0;
|
|
236
|
+
let high = bucketStarts.length - 1;
|
|
237
|
+
let found = -1;
|
|
238
|
+
while (low <= high) {
|
|
239
|
+
const middle = (low + high) >>> 1;
|
|
240
|
+
const start = bucketStarts[middle];
|
|
241
|
+
if (start === undefined)
|
|
242
|
+
return found;
|
|
243
|
+
if (start <= timeMs) {
|
|
244
|
+
found = middle;
|
|
245
|
+
low = middle + 1;
|
|
246
|
+
}
|
|
247
|
+
else {
|
|
248
|
+
high = middle - 1;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return found;
|
|
252
|
+
}
|
|
79
253
|
function compareIds(a, b) {
|
|
80
254
|
return a < b ? -1 : a > b ? 1 : 0;
|
|
81
255
|
}
|
|
@@ -121,8 +295,6 @@ export function createAnalyticsModel(options = {}) {
|
|
|
121
295
|
const ownedByFp = new Map();
|
|
122
296
|
const ownedBySession = new Map();
|
|
123
297
|
const hours = new Map();
|
|
124
|
-
/** Lifetime model-dimension rollup (small: one entry per provider/model). */
|
|
125
|
-
const modelLifetime = new Map();
|
|
126
298
|
const latestStepBySession = new Map();
|
|
127
299
|
const earliestStepBySession = new Map();
|
|
128
300
|
let earliestHourMs;
|
|
@@ -152,6 +324,14 @@ export function createAnalyticsModel(options = {}) {
|
|
|
152
324
|
},
|
|
153
325
|
cache: pricingCache,
|
|
154
326
|
});
|
|
327
|
+
if (outcome.status === "partial") {
|
|
328
|
+
return {
|
|
329
|
+
costUsd: outcome.costUsd,
|
|
330
|
+
pricedAny: true,
|
|
331
|
+
unpricedTokens: outcome.unpricedTokens,
|
|
332
|
+
unknownTokens: 0,
|
|
333
|
+
};
|
|
334
|
+
}
|
|
155
335
|
if (outcome.status === "priced") {
|
|
156
336
|
return { costUsd: outcome.costUsd, pricedAny: true, unpricedTokens: 0, unknownTokens: 0 };
|
|
157
337
|
}
|
|
@@ -172,21 +352,12 @@ export function createAnalyticsModel(options = {}) {
|
|
|
172
352
|
}
|
|
173
353
|
return bucket;
|
|
174
354
|
}
|
|
175
|
-
function keyedAccumulator(map, key) {
|
|
176
|
-
let accumulator = map.get(key);
|
|
177
|
-
if (!accumulator) {
|
|
178
|
-
accumulator = createAccumulator();
|
|
179
|
-
map.set(key, accumulator);
|
|
180
|
-
}
|
|
181
|
-
return accumulator;
|
|
182
|
-
}
|
|
183
355
|
function addStep(step) {
|
|
184
356
|
const startMs = Math.floor(step.completedAtMs / HOUR_MS) * HOUR_MS;
|
|
185
357
|
if (earliestHourMs === undefined || startMs < earliestHourMs)
|
|
186
358
|
earliestHourMs = startMs;
|
|
187
359
|
const bucket = bucketFor(startMs);
|
|
188
360
|
addStepToAccumulator(bucket.totals, step);
|
|
189
|
-
addStepToAccumulator(keyedAccumulator(modelLifetime, `${step.providerID ?? ""}|||${step.modelID ?? ""}`), step);
|
|
190
361
|
bucket.steps.set(`${step.sessionID}\u0000${step.messageID}`, step);
|
|
191
362
|
let sessionSteps = ownedBySession.get(step.sessionID);
|
|
192
363
|
if (!sessionSteps) {
|
|
@@ -210,13 +381,6 @@ export function createAnalyticsModel(options = {}) {
|
|
|
210
381
|
const bucket = hours.get(startMs);
|
|
211
382
|
if (bucket) {
|
|
212
383
|
subtractStepFromAccumulator(bucket.totals, step);
|
|
213
|
-
const modelKey = `${step.providerID ?? ""}|||${step.modelID ?? ""}`;
|
|
214
|
-
const model = modelLifetime.get(modelKey);
|
|
215
|
-
if (model) {
|
|
216
|
-
subtractStepFromAccumulator(model, step);
|
|
217
|
-
if (isZeroTokens(model))
|
|
218
|
-
modelLifetime.delete(modelKey);
|
|
219
|
-
}
|
|
220
384
|
bucket.steps.delete(`${step.sessionID}\u0000${step.messageID}`);
|
|
221
385
|
if (bucket.steps.size <= 0) {
|
|
222
386
|
hours.delete(startMs);
|
|
@@ -368,8 +532,43 @@ export function createAnalyticsModel(options = {}) {
|
|
|
368
532
|
else
|
|
369
533
|
ownedByFp.set(fingerprint, next);
|
|
370
534
|
}
|
|
535
|
+
function removeOccurrenceAt(fingerprint, messageID) {
|
|
536
|
+
const occurrences = groups.get(fingerprint);
|
|
537
|
+
if (!occurrences)
|
|
538
|
+
return false;
|
|
539
|
+
const index = occurrences.findIndex((entry) => entry.messageID === messageID);
|
|
540
|
+
if (index < 0)
|
|
541
|
+
return false;
|
|
542
|
+
occurrences.splice(index, 1);
|
|
543
|
+
if (occurrences.length === 0)
|
|
544
|
+
groups.delete(fingerprint);
|
|
545
|
+
return true;
|
|
546
|
+
}
|
|
547
|
+
/**
|
|
548
|
+
* Removes one message across fingerprints. Only used for a positive→zero
|
|
549
|
+
* upsert (rare) or a removal row whose payload no longer matches its owned
|
|
550
|
+
* fingerprint, never for the steady positive insert path.
|
|
551
|
+
*/
|
|
552
|
+
function removeOccurrenceByMessage(messageID) {
|
|
553
|
+
for (const fingerprint of groups.keys()) {
|
|
554
|
+
if (!removeOccurrenceAt(fingerprint, messageID))
|
|
555
|
+
continue;
|
|
556
|
+
return fingerprint;
|
|
557
|
+
}
|
|
558
|
+
return undefined;
|
|
559
|
+
}
|
|
560
|
+
/**
|
|
561
|
+
* Add or replace one physical contribution. Zero-token rows never enter the
|
|
562
|
+
* ownership groups: they carry metadata/activity but no usage, so they are
|
|
563
|
+
* excluded from fork ownership, pricing, groups, and activity extents. An
|
|
564
|
+
* update from positive to zero therefore removes the previously owned copy.
|
|
565
|
+
*/
|
|
371
566
|
function upsertOccurrence(row) {
|
|
372
567
|
const fingerprint = rawAssistantRowFingerprint(row);
|
|
568
|
+
if (!rawAssistantRowHasTokenUsage(row)) {
|
|
569
|
+
const previous = removeOccurrenceByMessage(row.messageID);
|
|
570
|
+
return { fingerprint, ...(previous === undefined ? {} : { previous }) };
|
|
571
|
+
}
|
|
373
572
|
let occurrences = groups.get(fingerprint);
|
|
374
573
|
if (!occurrences) {
|
|
375
574
|
occurrences = [];
|
|
@@ -399,17 +598,13 @@ export function createAnalyticsModel(options = {}) {
|
|
|
399
598
|
occurrences[index] = occurrence;
|
|
400
599
|
else
|
|
401
600
|
occurrences.push(occurrence);
|
|
601
|
+
return { fingerprint };
|
|
402
602
|
}
|
|
403
603
|
function removeOccurrence(row) {
|
|
404
604
|
const fingerprint = rawAssistantRowFingerprint(row);
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
const index = occurrences.findIndex((entry) => entry.messageID === row.messageID);
|
|
409
|
-
if (index >= 0)
|
|
410
|
-
occurrences.splice(index, 1);
|
|
411
|
-
if (occurrences.length === 0)
|
|
412
|
-
groups.delete(fingerprint);
|
|
605
|
+
if (!removeOccurrenceAt(fingerprint, row.messageID)) {
|
|
606
|
+
removeOccurrenceByMessage(row.messageID);
|
|
607
|
+
}
|
|
413
608
|
}
|
|
414
609
|
function setMetadata(nextSessions, nextProjects) {
|
|
415
610
|
const previousProjectBySession = projectBySession;
|
|
@@ -603,6 +798,179 @@ export function createAnalyticsModel(options = {}) {
|
|
|
603
798
|
}
|
|
604
799
|
return { totals, stepCount, sessions: sessionsSet };
|
|
605
800
|
}
|
|
801
|
+
/** Populated hour starts intersecting the window, chronologically ordered. */
|
|
802
|
+
function populatedHourStarts(window) {
|
|
803
|
+
const starts = [];
|
|
804
|
+
for (const startMs of hours.keys()) {
|
|
805
|
+
if (startMs + HOUR_MS <= (window.sinceMs ?? Number.NEGATIVE_INFINITY))
|
|
806
|
+
continue;
|
|
807
|
+
if (startMs >= window.untilMs)
|
|
808
|
+
continue;
|
|
809
|
+
starts.push(startMs);
|
|
810
|
+
}
|
|
811
|
+
starts.sort((a, b) => a - b);
|
|
812
|
+
return starts;
|
|
813
|
+
}
|
|
814
|
+
/**
|
|
815
|
+
* Two passes over owned steps already retained by the hour buckets.
|
|
816
|
+
*
|
|
817
|
+
* Pass 1 accumulates flat totals, distinct sessions, pair children, and the
|
|
818
|
+
* earliest positive owned step matching the query filter. Groups are keyed
|
|
819
|
+
* by nested maps of the exact raw id (`undefined` is its own key), so a pair
|
|
820
|
+
* cannot collide across a delimiter and missing ids stay distinct from empty
|
|
821
|
+
* ones. Pass 2 ranks the visible groups, then fills per-bucket accumulators
|
|
822
|
+
* for just the top five series and Other, their opposite-dimension child
|
|
823
|
+
* breakdown, and Other's hidden same-dimension parents. One visit per owned
|
|
824
|
+
* step feeds every level, so inspection adds no raw scan and no price lookup.
|
|
825
|
+
* Bucket arrays never scale with parent/child cardinality, and no parent ×
|
|
826
|
+
* child cross-product or duplicate raw contribution is emitted.
|
|
827
|
+
*/
|
|
828
|
+
function groupedSnapshot(query, nowMs) {
|
|
829
|
+
const { range, dimension } = query;
|
|
830
|
+
const filter = query.filter;
|
|
831
|
+
const window = rangeWindow(range, nowMs);
|
|
832
|
+
const hourStarts = populatedHourStarts(window);
|
|
833
|
+
function eachOwnedStep(visit) {
|
|
834
|
+
for (const startMs of hourStarts) {
|
|
835
|
+
const hour = hours.get(startMs);
|
|
836
|
+
if (!hour)
|
|
837
|
+
continue;
|
|
838
|
+
const hourEnd = startMs + HOUR_MS;
|
|
839
|
+
for (const step of hour.steps.values()) {
|
|
840
|
+
if (step.completedAtMs < startMs || step.completedAtMs >= hourEnd)
|
|
841
|
+
continue;
|
|
842
|
+
if (window.sinceMs !== undefined && step.completedAtMs < window.sinceMs)
|
|
843
|
+
continue;
|
|
844
|
+
if (step.completedAtMs >= window.untilMs)
|
|
845
|
+
continue;
|
|
846
|
+
visit(step);
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
const totals = createAccumulator();
|
|
851
|
+
const sessions = new Set();
|
|
852
|
+
const parentGroups = new Map();
|
|
853
|
+
const childDimension = oppositeDimension(dimension);
|
|
854
|
+
const lifetime = range === "lifetime";
|
|
855
|
+
let stepCount = 0;
|
|
856
|
+
let earliestMatchedMs;
|
|
857
|
+
eachOwnedStep((step) => {
|
|
858
|
+
if (!matchesFilter(step, filter))
|
|
859
|
+
return;
|
|
860
|
+
if (lifetime && (earliestMatchedMs === undefined || step.completedAtMs < earliestMatchedMs)) {
|
|
861
|
+
earliestMatchedMs = step.completedAtMs;
|
|
862
|
+
}
|
|
863
|
+
const parentRaw = rawIdForDimension(step, dimension);
|
|
864
|
+
const childRaw = rawIdForDimension(step, childDimension);
|
|
865
|
+
addStepToAccumulator(totals, step);
|
|
866
|
+
sessions.add(step.sessionID);
|
|
867
|
+
stepCount += 1;
|
|
868
|
+
let parent = parentGroups.get(parentRaw);
|
|
869
|
+
if (!parent) {
|
|
870
|
+
parent = createGroupNode();
|
|
871
|
+
parentGroups.set(parentRaw, parent);
|
|
872
|
+
}
|
|
873
|
+
addStepToGroupNode(parent, step);
|
|
874
|
+
let child = parent.children.get(childRaw);
|
|
875
|
+
if (!child) {
|
|
876
|
+
child = createGroupNode();
|
|
877
|
+
parent.children.set(childRaw, child);
|
|
878
|
+
}
|
|
879
|
+
addStepToGroupNode(child, step);
|
|
880
|
+
});
|
|
881
|
+
// Lifetime buckets start at the exact first positive owned step for this
|
|
882
|
+
// query filter — never session metadata, and never an older unrelated
|
|
883
|
+
// model/provider. Finite ranges anchor on their rolling window and ignore
|
|
884
|
+
// this value. An empty lifetime still renders its fallback current bucket.
|
|
885
|
+
const boundaries = planRangeBuckets(range, nowMs, lifetime ? earliestMatchedMs : undefined, {
|
|
886
|
+
timeZone,
|
|
887
|
+
});
|
|
888
|
+
const bucketStarts = boundaries.map((boundary) => boundary.startMs);
|
|
889
|
+
const ranked = [...parentGroups]
|
|
890
|
+
.map(([raw, node]) => ({ raw, node }))
|
|
891
|
+
.sort(compareGroupEntries);
|
|
892
|
+
const rows = ranked.map((entry) => materializeGroupRow(entry.raw, entry.node));
|
|
893
|
+
const topEntries = ranked.slice(0, 5);
|
|
894
|
+
const otherEntries = ranked.slice(5);
|
|
895
|
+
const topBucketTotals = topEntries.map(() => new Map());
|
|
896
|
+
const topChildBuckets = topEntries.map(() => new Map());
|
|
897
|
+
const otherBucketTotals = otherEntries.length > 0 ? new Map() : undefined;
|
|
898
|
+
const otherChildBuckets = otherEntries.length > 0 ? new Map() : undefined;
|
|
899
|
+
const otherParentBuckets = otherEntries.length > 0 ? new Map() : undefined;
|
|
900
|
+
const seriesIndexByParent = new Map();
|
|
901
|
+
topEntries.forEach((entry, index) => {
|
|
902
|
+
seriesIndexByParent.set(entry.raw, index);
|
|
903
|
+
});
|
|
904
|
+
if (ranked.length > 0) {
|
|
905
|
+
eachOwnedStep((step) => {
|
|
906
|
+
if (!matchesFilter(step, filter))
|
|
907
|
+
return;
|
|
908
|
+
const bucketIndex = bucketIndexAt(bucketStarts, step.completedAtMs);
|
|
909
|
+
if (bucketIndex < 0)
|
|
910
|
+
return;
|
|
911
|
+
const parentRaw = rawIdForDimension(step, dimension);
|
|
912
|
+
const childRaw = rawIdForDimension(step, childDimension);
|
|
913
|
+
const seriesIndex = seriesIndexByParent.get(parentRaw);
|
|
914
|
+
if (seriesIndex !== undefined) {
|
|
915
|
+
const bucketTotals = topBucketTotals[seriesIndex];
|
|
916
|
+
if (bucketTotals)
|
|
917
|
+
addStepToBucket(bucketTotals, step, bucketIndex);
|
|
918
|
+
const childBuckets = topChildBuckets[seriesIndex];
|
|
919
|
+
if (childBuckets) {
|
|
920
|
+
addStepToEntityBucket(childBuckets, childRaw, step, bucketIndex);
|
|
921
|
+
}
|
|
922
|
+
return;
|
|
923
|
+
}
|
|
924
|
+
if (otherBucketTotals)
|
|
925
|
+
addStepToBucket(otherBucketTotals, step, bucketIndex);
|
|
926
|
+
if (otherChildBuckets) {
|
|
927
|
+
addStepToEntityBucket(otherChildBuckets, childRaw, step, bucketIndex);
|
|
928
|
+
}
|
|
929
|
+
if (otherParentBuckets) {
|
|
930
|
+
addStepToEntityBucket(otherParentBuckets, parentRaw, step, bucketIndex);
|
|
931
|
+
}
|
|
932
|
+
});
|
|
933
|
+
}
|
|
934
|
+
const topSources = topEntries.map((entry, index) => ({
|
|
935
|
+
raw: entry.raw,
|
|
936
|
+
totals: entry.node.totals,
|
|
937
|
+
bucketTotals: topBucketTotals[index] ?? new Map(),
|
|
938
|
+
childBuckets: topChildBuckets[index] ?? new Map(),
|
|
939
|
+
}));
|
|
940
|
+
let otherSource;
|
|
941
|
+
if (otherEntries.length > 0) {
|
|
942
|
+
const otherTotals = createAccumulator();
|
|
943
|
+
for (const entry of otherEntries)
|
|
944
|
+
mergeAccumulator(otherTotals, entry.node.totals);
|
|
945
|
+
otherSource = {
|
|
946
|
+
raw: undefined,
|
|
947
|
+
totals: otherTotals,
|
|
948
|
+
bucketTotals: otherBucketTotals ?? new Map(),
|
|
949
|
+
childBuckets: otherChildBuckets ?? new Map(),
|
|
950
|
+
parentBuckets: otherParentBuckets ?? new Map(),
|
|
951
|
+
};
|
|
952
|
+
}
|
|
953
|
+
const snapshot = {
|
|
954
|
+
dimension,
|
|
955
|
+
range,
|
|
956
|
+
window,
|
|
957
|
+
bucketUnit: rangeBucketUnit(range),
|
|
958
|
+
timeZone,
|
|
959
|
+
buckets: boundaries.map((boundary) => ({
|
|
960
|
+
startMs: boundary.startMs,
|
|
961
|
+
endMs: boundary.endMs,
|
|
962
|
+
})),
|
|
963
|
+
totals: finalize(totals),
|
|
964
|
+
sessionCount: sessions.size,
|
|
965
|
+
stepCount,
|
|
966
|
+
rows,
|
|
967
|
+
series: buildSeries(topSources, otherSource, boundaries.length),
|
|
968
|
+
stale: false,
|
|
969
|
+
};
|
|
970
|
+
if (filter !== undefined)
|
|
971
|
+
snapshot.filter = filter;
|
|
972
|
+
return snapshot;
|
|
973
|
+
}
|
|
606
974
|
function buildUsage(range, nowMs, options = {}, earliestMs = earliestActivity()) {
|
|
607
975
|
const window = rangeWindow(range, nowMs);
|
|
608
976
|
const boundaries = planRangeBuckets(range, nowMs, earliestMs, { timeZone });
|
|
@@ -788,7 +1156,6 @@ export function createAnalyticsModel(options = {}) {
|
|
|
788
1156
|
ownedByFp.clear();
|
|
789
1157
|
ownedBySession.clear();
|
|
790
1158
|
hours.clear();
|
|
791
|
-
modelLifetime.clear();
|
|
792
1159
|
latestStepBySession.clear();
|
|
793
1160
|
earliestStepBySession.clear();
|
|
794
1161
|
earliestHourMs = undefined;
|
|
@@ -797,13 +1164,51 @@ export function createAnalyticsModel(options = {}) {
|
|
|
797
1164
|
},
|
|
798
1165
|
apply(upserts, removals) {
|
|
799
1166
|
const affected = new Set();
|
|
1167
|
+
const applyUpsert = (row) => {
|
|
1168
|
+
const result = upsertOccurrence(row);
|
|
1169
|
+
affected.add(result.fingerprint);
|
|
1170
|
+
if (result.previous !== undefined)
|
|
1171
|
+
affected.add(result.previous);
|
|
1172
|
+
};
|
|
800
1173
|
for (const row of removals) {
|
|
801
1174
|
affected.add(rawAssistantRowFingerprint(row));
|
|
802
1175
|
removeOccurrence(row);
|
|
803
1176
|
}
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
1177
|
+
// `upserts` may be a one-shot generator (the engine hydrates the model
|
|
1178
|
+
// this way), so materialize references before the zero-row scan. The
|
|
1179
|
+
// rows themselves are already retained by the caller.
|
|
1180
|
+
const rows = Array.isArray(upserts) ? upserts : [...upserts];
|
|
1181
|
+
let hasZeroRows = false;
|
|
1182
|
+
for (const row of rows) {
|
|
1183
|
+
if (!rawAssistantRowHasTokenUsage(row)) {
|
|
1184
|
+
hasZeroRows = true;
|
|
1185
|
+
break;
|
|
1186
|
+
}
|
|
1187
|
+
}
|
|
1188
|
+
if (!hasZeroRows) {
|
|
1189
|
+
for (const row of rows)
|
|
1190
|
+
applyUpsert(row);
|
|
1191
|
+
}
|
|
1192
|
+
else {
|
|
1193
|
+
// A batch that contains zero-token rows is reconciled in two phases:
|
|
1194
|
+
// last occurrence per message id wins, then zero rows remove prior
|
|
1195
|
+
// ownership before positive rows are inserted. This keeps bulk
|
|
1196
|
+
// hydration linear instead of scanning all groups per zero row.
|
|
1197
|
+
const lastByMessage = new Map();
|
|
1198
|
+
for (const row of rows)
|
|
1199
|
+
lastByMessage.set(row.messageID, row);
|
|
1200
|
+
// With no owned groups there is nothing a zero row can remove. This is
|
|
1201
|
+
// the cold-hydration shape; skipping fingerprint work keeps it linear.
|
|
1202
|
+
if (groups.size > 0) {
|
|
1203
|
+
for (const row of lastByMessage.values()) {
|
|
1204
|
+
if (!rawAssistantRowHasTokenUsage(row))
|
|
1205
|
+
applyUpsert(row);
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
1208
|
+
for (const row of lastByMessage.values()) {
|
|
1209
|
+
if (rawAssistantRowHasTokenUsage(row))
|
|
1210
|
+
applyUpsert(row);
|
|
1211
|
+
}
|
|
807
1212
|
}
|
|
808
1213
|
for (const fingerprint of affected)
|
|
809
1214
|
recomputeFingerprint(fingerprint);
|
|
@@ -812,6 +1217,7 @@ export function createAnalyticsModel(options = {}) {
|
|
|
812
1217
|
hasProject: (projectId) => Object.hasOwn(projects, projectId) || sessionsByProject.has(projectId),
|
|
813
1218
|
home,
|
|
814
1219
|
project,
|
|
1220
|
+
groups: groupedSnapshot,
|
|
815
1221
|
earliestActivityMs: earliestActivity,
|
|
816
1222
|
ownedStepCount() {
|
|
817
1223
|
let count = 0;
|