@igorjpimenta/manifold 0.0.0-bootstrap.1
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/.migrations/ops/0000_futuristic_karma.sql +111 -0
- package/.migrations/ops/0001_low_maximus.sql +7 -0
- package/.migrations/ops/meta/0000_snapshot.json +763 -0
- package/.migrations/ops/meta/0001_snapshot.json +173 -0
- package/.migrations/ops/meta/_journal.json +20 -0
- package/.migrations/runtime/0000_real_boomer.sql +180 -0
- package/.migrations/runtime/0001_hot_dakota_north.sql +2 -0
- package/.migrations/runtime/0002_breezy_vertigo.sql +8 -0
- package/.migrations/runtime/0003_dashing_jack_flag.sql +5 -0
- package/.migrations/runtime/0004_panoramic_ego.sql +1 -0
- package/.migrations/runtime/0005_tidy_ben_grimm.sql +10 -0
- package/.migrations/runtime/0006_wet_nightmare.sql +57 -0
- package/.migrations/runtime/0007_ambiguous_chronomancer.sql +8 -0
- package/.migrations/runtime/0008_wealthy_thundra.sql +301 -0
- package/.migrations/runtime/0009_lying_shocker.sql +96 -0
- package/.migrations/runtime/0010_solid_phantom_reporter.sql +160 -0
- package/.migrations/runtime/0011_windy_demogoblin.sql +175 -0
- package/.migrations/runtime/0012_restore_harness_fks.sql +103 -0
- package/.migrations/runtime/meta/0000_snapshot.json +1181 -0
- package/.migrations/runtime/meta/0001_snapshot.json +1209 -0
- package/.migrations/runtime/meta/0002_snapshot.json +1261 -0
- package/.migrations/runtime/meta/0003_snapshot.json +1292 -0
- package/.migrations/runtime/meta/0004_snapshot.json +1240 -0
- package/.migrations/runtime/meta/0006_snapshot.json +1310 -0
- package/.migrations/runtime/meta/0007_snapshot.json +1362 -0
- package/.migrations/runtime/meta/0008_snapshot.json +1742 -0
- package/.migrations/runtime/meta/0009_snapshot.json +2413 -0
- package/.migrations/runtime/meta/0010_snapshot.json +2562 -0
- package/.migrations/runtime/meta/0011_snapshot.json +2812 -0
- package/.migrations/runtime/meta/0012_snapshot.json +2812 -0
- package/.migrations/runtime/meta/_journal.json +97 -0
- package/README.md +1584 -0
- package/RELEASING.md +89 -0
- package/agent/fleet.js +100 -0
- package/bin/cli.js +854 -0
- package/bin/manifold.js +4 -0
- package/boot/flow-revival.js +536 -0
- package/budget/index.js +495 -0
- package/budget/lib.js +59 -0
- package/dashboard/cost.js +528 -0
- package/dashboard/dist/assets/index-CUfp63sj.css +1 -0
- package/dashboard/dist/assets/index-Qy9JMpma.js +371 -0
- package/dashboard/dist/index.html +13 -0
- package/dashboard/event-log.js +267 -0
- package/dashboard/index.html +12 -0
- package/dashboard/package-lock.json +2877 -0
- package/dashboard/package.json +37 -0
- package/dashboard/pipeline-graph.js +131 -0
- package/dashboard/postcss.config.js +6 -0
- package/dashboard/search.js +183 -0
- package/dashboard/server.js +1015 -0
- package/dashboard/src/App.tsx +42 -0
- package/dashboard/src/api/client.ts +118 -0
- package/dashboard/src/api/types.ts +510 -0
- package/dashboard/src/api/ws.tsx +112 -0
- package/dashboard/src/components/CancelButton.tsx +63 -0
- package/dashboard/src/components/ClickToCopy.tsx +68 -0
- package/dashboard/src/components/CopyButton.tsx +61 -0
- package/dashboard/src/components/DailyCostTrend.tsx +85 -0
- package/dashboard/src/components/DailyTimeTrend.tsx +94 -0
- package/dashboard/src/components/Funnel.tsx +149 -0
- package/dashboard/src/components/GateForm.tsx +158 -0
- package/dashboard/src/components/GraphView.tsx +302 -0
- package/dashboard/src/components/GroupPathsSummary.tsx +47 -0
- package/dashboard/src/components/Histogram.tsx +292 -0
- package/dashboard/src/components/Layout.tsx +28 -0
- package/dashboard/src/components/NeedsAttention.tsx +98 -0
- package/dashboard/src/components/NodeContextCard.tsx +132 -0
- package/dashboard/src/components/PathTrail.tsx +35 -0
- package/dashboard/src/components/PipelineCostBreakdown.tsx +277 -0
- package/dashboard/src/components/RerunButton.tsx +46 -0
- package/dashboard/src/components/RunComparison.tsx +53 -0
- package/dashboard/src/components/Sparkline.tsx +94 -0
- package/dashboard/src/components/StatStrip.tsx +147 -0
- package/dashboard/src/components/StateTree.tsx +84 -0
- package/dashboard/src/components/StatusBadge.tsx +18 -0
- package/dashboard/src/components/SubflowPathPills.tsx +38 -0
- package/dashboard/src/components/SubflowSummary.tsx +24 -0
- package/dashboard/src/components/SubflowsCard.tsx +327 -0
- package/dashboard/src/components/TodayStrip.tsx +86 -0
- package/dashboard/src/components/TranscriptPane.tsx +99 -0
- package/dashboard/src/components/TranscriptRow.tsx +69 -0
- package/dashboard/src/components/ui/badge.tsx +38 -0
- package/dashboard/src/components/ui/button.tsx +28 -0
- package/dashboard/src/components/ui/card.tsx +42 -0
- package/dashboard/src/components/ui/skeleton.tsx +9 -0
- package/dashboard/src/components/ui/table.tsx +62 -0
- package/dashboard/src/index.css +26 -0
- package/dashboard/src/lib/anomaly.ts +42 -0
- package/dashboard/src/lib/format.ts +30 -0
- package/dashboard/src/lib/step-history.ts +13 -0
- package/dashboard/src/lib/subflow.ts +154 -0
- package/dashboard/src/lib/use-click-outside.ts +28 -0
- package/dashboard/src/lib/use-ignored-clusters.ts +67 -0
- package/dashboard/src/lib/utils.ts +6 -0
- package/dashboard/src/main.tsx +10 -0
- package/dashboard/src/types/shims.d.ts +4 -0
- package/dashboard/src/views/Home.tsx +478 -0
- package/dashboard/src/views/PipelineDetail.tsx +1185 -0
- package/dashboard/src/views/PipelinesList.tsx +111 -0
- package/dashboard/src/views/RunDetail.tsx +248 -0
- package/dashboard/src/views/Search.tsx +196 -0
- package/dashboard/stats.js +668 -0
- package/dashboard/tailwind.config.ts +43 -0
- package/dashboard/tree-stats.js +12 -0
- package/dashboard/tsconfig.json +25 -0
- package/dashboard/views.js +231 -0
- package/dashboard/vite.config.ts +34 -0
- package/drizzle.config.js +12 -0
- package/drizzle.config.ops.js +12 -0
- package/elevation.js +116 -0
- package/engine/admission-promotion-resume.js +127 -0
- package/engine/agent-dispatch.js +64 -0
- package/engine/child-resume-trigger.js +315 -0
- package/engine/executor-budget.js +87 -0
- package/engine/executor-cancel.js +83 -0
- package/engine/executor-errors.js +122 -0
- package/engine/executor-helpers.js +254 -0
- package/engine/executor-nodes.js +292 -0
- package/engine/executor-persist.js +212 -0
- package/engine/executor-subflow-children.js +275 -0
- package/engine/executor.js +1013 -0
- package/engine/global-concurrency.js +33 -0
- package/engine/loader.js +528 -0
- package/engine/needs-resolver.js +229 -0
- package/engine/pipelines-watcher.js +133 -0
- package/engine/result-replay.js +34 -0
- package/engine/revival-messages.js +11 -0
- package/engine/spawn-child.js +55 -0
- package/engine/wait-for-flow-settlement.js +109 -0
- package/events/catalog.js +26 -0
- package/events/emit.js +44 -0
- package/events/kinds.js +118 -0
- package/events/serve-ws.js +74 -0
- package/events/stream.js +35 -0
- package/events/ws-dialog.js +257 -0
- package/events/ws-frame.js +194 -0
- package/events/ws-run-events.js +70 -0
- package/events.js +32 -0
- package/gates/gate-resolver.js +576 -0
- package/gates/paused-flows.js +55 -0
- package/graphql/columns.js +67 -0
- package/graphql/schema.js +333 -0
- package/index.js +49 -0
- package/mcp/http.js +159 -0
- package/mcp/identity.js +94 -0
- package/mcp/stdio.js +80 -0
- package/mcp/tools.js +245 -0
- package/mcp/wire-ops.js +178 -0
- package/package.json +51 -0
- package/routes/bearer-auth.js +57 -0
- package/routes/discuss-audio.js +87 -0
- package/routes/discuss-component.js +39 -0
- package/routes/discuss-draft.js +159 -0
- package/routes/discuss-helpers.js +20 -0
- package/routes/discuss.js +848 -0
- package/routes/failures.js +104 -0
- package/routes/graphql.js +61 -0
- package/routes/health.js +54 -0
- package/routes/operator-discuss.js +193 -0
- package/routes/operator-resume.js +32 -0
- package/routes/operator-retry.js +301 -0
- package/routes/operator.js +539 -0
- package/routes/pipelines.js +118 -0
- package/routes/runs.js +1129 -0
- package/scripts/ci/import-check.mjs +50 -0
- package/scripts/link-core-self.mjs +31 -0
- package/scripts/with-guard.sh +24 -0
- package/server/app.js +503 -0
- package/server/registry.js +53 -0
- package/server/serve.js +163 -0
- package/state/admission-queue-store.js +325 -0
- package/state/attribution-registries.js +102 -0
- package/state/batch-writer.js +162 -0
- package/state/db-guard.js +63 -0
- package/state/db-health.js +27 -0
- package/state/db.js +122 -0
- package/state/discuss-store.js +299 -0
- package/state/failure-store.js +427 -0
- package/state/migrate.js +104 -0
- package/state/node-cost.js +67 -0
- package/state/node-executions.js +775 -0
- package/state/node-health-store.js +124 -0
- package/state/ops-merge.js +93 -0
- package/state/paths.js +146 -0
- package/state/pipeline-dirs.js +99 -0
- package/state/pipeline-registry.js +449 -0
- package/state/run-store.js +697 -0
- package/state/schema.js +438 -0
- package/state/schema.ops.js +44 -0
- package/state/session-index.js +177 -0
- package/state/session-keys.js +39 -0
- package/state/state-store.js +311 -0
- package/state/subflow-state.js +126 -0
- package/state/tool-handoff.js +169 -0
- package/telemetry/budget-alert.js +353 -0
- package/telemetry/cli.js +331 -0
- package/telemetry/grammar.js +119 -0
- package/telemetry/http-budget.js +132 -0
- package/telemetry/http-cost.js +670 -0
- package/telemetry/http-helpers.js +134 -0
- package/telemetry/http-tree.js +349 -0
- package/telemetry/http.js +44 -0
- package/telemetry/index.js +307 -0
- package/telemetry/ingest.js +134 -0
- package/telemetry/labels.js +115 -0
- package/telemetry/pricer.js +77 -0
- package/telemetry/resolver.js +314 -0
- package/telemetry/store.js +147 -0
- package/utils/run-patch.js +43 -0
|
@@ -0,0 +1,670 @@
|
|
|
1
|
+
// /manifold/telemetry/cost/* routes — aggregate and drill-down cost views.
|
|
2
|
+
import {
|
|
3
|
+
displayRootLabel, displayRootKind, stripTeamPrefix,
|
|
4
|
+
UNATTRIBUTED_LABEL, UNATTRIBUTED_FILTER,
|
|
5
|
+
} from "./labels.js";
|
|
6
|
+
import { currentCapUSD } from "./budget-alert.js";
|
|
7
|
+
import {
|
|
8
|
+
ATTR_JOIN, ATTR_ROOT_KIND, ATTR_ROOT_LABEL,
|
|
9
|
+
bootstrapForecast, hasFlowRuns, execAll, execGet,
|
|
10
|
+
} from "./http-helpers.js";
|
|
11
|
+
|
|
12
|
+
export function registerCostRoutes({ api, getStore, logger, json, bad, parseRange }) {
|
|
13
|
+
const makeGroupHandler = (groupExpr, label, extraCols = []) => async (req, res) => {
|
|
14
|
+
const store = getStore();
|
|
15
|
+
if (!store) return bad(res, "lineage store not ready");
|
|
16
|
+
try {
|
|
17
|
+
const url = new URL(req.url, "http://x");
|
|
18
|
+
const { where, args } = parseRange(url.searchParams);
|
|
19
|
+
const limit = Math.min(Number(url.searchParams.get("limit") || 50), 500);
|
|
20
|
+
for (const col of ["pipeline_id", "flow_id", "node_id", "root_kind", "root_label", "model", "provider"]) {
|
|
21
|
+
const v = url.searchParams.get(col);
|
|
22
|
+
if (v != null && v !== "") { where.push(`${col} = ?`); args.push(v); }
|
|
23
|
+
}
|
|
24
|
+
const w = where.length ? `WHERE ${where.join(" AND ")}` : "";
|
|
25
|
+
const extra = extraCols.length ? `, ${extraCols.join(", ")}` : "";
|
|
26
|
+
const sql = `
|
|
27
|
+
SELECT ${groupExpr} AS ${label}${extra},
|
|
28
|
+
COUNT(*) AS calls,
|
|
29
|
+
SUM(input_tokens) AS input_tokens,
|
|
30
|
+
SUM(output_tokens) AS output_tokens,
|
|
31
|
+
SUM(cache_read_tokens) AS cache_read_tokens,
|
|
32
|
+
SUM(cache_write_tokens) AS cache_write_tokens,
|
|
33
|
+
ROUND(SUM(cost_total), 6) AS cost
|
|
34
|
+
FROM cost_by_call
|
|
35
|
+
${w}
|
|
36
|
+
GROUP BY 1${extraCols.length ? ", 2" : ""}
|
|
37
|
+
ORDER BY cost DESC NULLS LAST
|
|
38
|
+
LIMIT ${limit}
|
|
39
|
+
`;
|
|
40
|
+
const rows = await execAll(store._client, sql, args);
|
|
41
|
+
json(res, { dimension: label, rows });
|
|
42
|
+
} catch (err) {
|
|
43
|
+
logger?.warn?.(`lineage[http ${label}]: ${err?.message ?? err}`);
|
|
44
|
+
json(res, { error: String(err?.message ?? err) }, 500);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// ─── /cost/today ───────────────────────────────────────────────────────────
|
|
49
|
+
api.registerHttpRoute({
|
|
50
|
+
method: "GET",
|
|
51
|
+
path: "/manifold/telemetry/cost/today",
|
|
52
|
+
auth: "plugin",
|
|
53
|
+
handler: async (req, res) => {
|
|
54
|
+
const store = getStore();
|
|
55
|
+
if (!store) return bad(res, "lineage store not ready");
|
|
56
|
+
const client = store._client;
|
|
57
|
+
const dayClause = `date(ts/1000, 'unixepoch', 'localtime') = date('now', 'localtime')`;
|
|
58
|
+
const summary = await execGet(client, `
|
|
59
|
+
SELECT COUNT(*) AS calls, ROUND(SUM(cost_total), 6) AS cost
|
|
60
|
+
FROM cost_by_call WHERE ${dayClause}
|
|
61
|
+
`);
|
|
62
|
+
const byRoot = await execAll(client, `
|
|
63
|
+
SELECT root_label, root_kind, COUNT(*) AS calls, ROUND(SUM(cost_total), 6) AS cost
|
|
64
|
+
FROM cost_by_call WHERE ${dayClause} AND root_label IS NOT NULL
|
|
65
|
+
GROUP BY root_label, root_kind ORDER BY cost DESC LIMIT 10
|
|
66
|
+
`);
|
|
67
|
+
const byModel = await execAll(client, `
|
|
68
|
+
SELECT model, provider, COUNT(*) AS calls, ROUND(SUM(cost_total), 6) AS cost
|
|
69
|
+
FROM cost_by_call WHERE ${dayClause} AND model IS NOT NULL
|
|
70
|
+
GROUP BY model, provider ORDER BY cost DESC
|
|
71
|
+
`);
|
|
72
|
+
const _n = new Date();
|
|
73
|
+
const _today = `${_n.getFullYear()}-${String(_n.getMonth()+1).padStart(2,'0')}-${String(_n.getDate()).padStart(2,'0')}`;
|
|
74
|
+
json(res, { today: _today, summary, byRoot, byModel });
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
// ─── uniform group-by handlers ─────────────────────────────────────────────
|
|
79
|
+
api.registerHttpRoute({ method: "GET", path: "/manifold/telemetry/cost/by-root", auth: "plugin", handler: makeGroupHandler("root_label", "root_label", ["root_kind"]) });
|
|
80
|
+
api.registerHttpRoute({ method: "GET", path: "/manifold/telemetry/cost/by-trigger", auth: "plugin", handler: makeGroupHandler("root_kind", "root_kind") });
|
|
81
|
+
api.registerHttpRoute({ method: "GET", path: "/manifold/telemetry/cost/by-pipeline",auth: "plugin", handler: makeGroupHandler("pipeline_id","pipeline_id") });
|
|
82
|
+
api.registerHttpRoute({ method: "GET", path: "/manifold/telemetry/cost/by-flow", auth: "plugin", handler: makeGroupHandler("flow_id", "flow_id", ["pipeline_id"]) });
|
|
83
|
+
api.registerHttpRoute({ method: "GET", path: "/manifold/telemetry/cost/by-node", auth: "plugin", handler: makeGroupHandler("node_id", "node_id", ["flow_id"]) });
|
|
84
|
+
|
|
85
|
+
// ─── /cost/by-session ──────────────────────────────────────────────────────
|
|
86
|
+
api.registerHttpRoute({
|
|
87
|
+
method: "GET", path: "/manifold/telemetry/cost/by-session", auth: "plugin",
|
|
88
|
+
handler: async (req, res) => {
|
|
89
|
+
const store = getStore();
|
|
90
|
+
if (!store) return bad(res, "lineage store not ready");
|
|
91
|
+
try {
|
|
92
|
+
const url = new URL(req.url, "http://x");
|
|
93
|
+
const flowId = url.searchParams.get("flow_id");
|
|
94
|
+
const nodeId = url.searchParams.get("node_id");
|
|
95
|
+
if (!flowId || !nodeId) return bad(res, "flow_id and node_id required");
|
|
96
|
+
const rows = await execAll(store._client, `
|
|
97
|
+
SELECT c.session_key, c.item_key,
|
|
98
|
+
(SELECT sl.agent_id FROM session_lineage sl
|
|
99
|
+
WHERE sl.child_session_key = c.session_key LIMIT 1) AS agent_id,
|
|
100
|
+
COUNT(*) AS calls,
|
|
101
|
+
ROUND(SUM(c.cost_total), 6) AS cost,
|
|
102
|
+
datetime(MIN(c.ts)/1000, 'unixepoch', 'localtime') AS started_at
|
|
103
|
+
FROM cost_by_call c
|
|
104
|
+
WHERE c.flow_id = ? AND c.node_id = ?
|
|
105
|
+
GROUP BY c.session_key
|
|
106
|
+
ORDER BY MIN(c.ts) ASC
|
|
107
|
+
`, [flowId, nodeId]);
|
|
108
|
+
json(res, { rows });
|
|
109
|
+
} catch (err) {
|
|
110
|
+
logger?.warn?.(`lineage[http by-session]: ${err?.message ?? err}`);
|
|
111
|
+
json(res, { error: String(err?.message ?? err) }, 500);
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
// ─── /cost/runs ────────────────────────────────────────────────────────────
|
|
117
|
+
api.registerHttpRoute({
|
|
118
|
+
method: "GET", path: "/manifold/telemetry/cost/runs", auth: "plugin",
|
|
119
|
+
handler: async (req, res) => {
|
|
120
|
+
const store = getStore();
|
|
121
|
+
if (!store) return bad(res, "lineage store not ready");
|
|
122
|
+
try {
|
|
123
|
+
const url = new URL(req.url, "http://x");
|
|
124
|
+
const limit = Math.min(Number(url.searchParams.get("limit") || 50), 200);
|
|
125
|
+
const pipelineId = url.searchParams.get("pipeline_id");
|
|
126
|
+
const { where: rangeWhere, args } = parseRange(url.searchParams);
|
|
127
|
+
if (await hasFlowRuns(store._client)) {
|
|
128
|
+
const where = ["c.flow_id IS NOT NULL", ...rangeWhere.map((w) => `c.${w}`)];
|
|
129
|
+
if (pipelineId) { where.push("c.pipeline_id = ?"); args.push(pipelineId); }
|
|
130
|
+
const sql = `
|
|
131
|
+
SELECT c.flow_id, c.pipeline_id,
|
|
132
|
+
COUNT(*) AS calls,
|
|
133
|
+
ROUND(SUM(c.cost_total), 6) AS cost,
|
|
134
|
+
f.status,
|
|
135
|
+
datetime(MIN(c.ts)/1000, 'unixepoch', 'localtime') AS started_at,
|
|
136
|
+
MIN(c.ts) AS ts_first, MAX(c.ts) AS ts_last
|
|
137
|
+
FROM cost_by_call c
|
|
138
|
+
LEFT JOIN flow_runs f ON f.flow_id = c.flow_id
|
|
139
|
+
WHERE ${where.join(" AND ")}
|
|
140
|
+
GROUP BY c.flow_id ORDER BY ts_first DESC LIMIT ${limit}
|
|
141
|
+
`;
|
|
142
|
+
json(res, { rows: await execAll(store._client, sql, args) });
|
|
143
|
+
} else {
|
|
144
|
+
const where = ["flow_id IS NOT NULL", ...rangeWhere];
|
|
145
|
+
if (pipelineId) { where.push("pipeline_id = ?"); args.push(pipelineId); }
|
|
146
|
+
const sql = `
|
|
147
|
+
SELECT flow_id, pipeline_id,
|
|
148
|
+
COUNT(*) AS calls, ROUND(SUM(cost_total), 6) AS cost,
|
|
149
|
+
NULL AS status,
|
|
150
|
+
datetime(MIN(ts)/1000, 'unixepoch', 'localtime') AS started_at,
|
|
151
|
+
MIN(ts) AS ts_first, MAX(ts) AS ts_last
|
|
152
|
+
FROM cost_by_call
|
|
153
|
+
WHERE ${where.join(" AND ")}
|
|
154
|
+
GROUP BY flow_id ORDER BY ts_first DESC LIMIT ${limit}
|
|
155
|
+
`;
|
|
156
|
+
json(res, { rows: await execAll(store._client, sql, args) });
|
|
157
|
+
}
|
|
158
|
+
} catch (err) {
|
|
159
|
+
logger?.warn?.(`lineage[http /runs]: ${err?.message ?? err}`);
|
|
160
|
+
json(res, { error: String(err?.message ?? err) }, 500);
|
|
161
|
+
}
|
|
162
|
+
},
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
api.registerHttpRoute({ method: "GET", path: "/manifold/telemetry/cost/by-model", auth: "plugin", handler: makeGroupHandler("model", "model", ["provider"]) });
|
|
166
|
+
|
|
167
|
+
// ─── /cost/by-cron-job ─────────────────────────────────────────────────────
|
|
168
|
+
api.registerHttpRoute({ method: "GET", path: "/manifold/telemetry/cost/by-cron-job", auth: "plugin", handler: async (req, res) => {
|
|
169
|
+
const store = getStore();
|
|
170
|
+
if (!store) return bad(res, "lineage store not ready");
|
|
171
|
+
try {
|
|
172
|
+
const url = new URL(req.url, "http://x");
|
|
173
|
+
const { where, args } = parseRange(url.searchParams);
|
|
174
|
+
where.push(`root_kind = 'cron-owner'`);
|
|
175
|
+
const sql = `
|
|
176
|
+
SELECT root_label AS cron_job, COUNT(*) AS calls, ROUND(SUM(cost_total), 6) AS cost
|
|
177
|
+
FROM cost_by_call WHERE ${where.join(" AND ")}
|
|
178
|
+
GROUP BY 1 ORDER BY cost DESC NULLS LAST LIMIT 50
|
|
179
|
+
`;
|
|
180
|
+
json(res, { dimension: "cron_job", rows: await execAll(store._client, sql, args) });
|
|
181
|
+
} catch (err) { json(res, { error: String(err?.message ?? err) }, 500); }
|
|
182
|
+
} });
|
|
183
|
+
|
|
184
|
+
// ─── /cost/by-day ──────────────────────────────────────────────────────────
|
|
185
|
+
api.registerHttpRoute({
|
|
186
|
+
method: "GET", path: "/manifold/telemetry/cost/by-day", auth: "plugin",
|
|
187
|
+
handler: async (req, res) => {
|
|
188
|
+
const store = getStore();
|
|
189
|
+
if (!store) return bad(res, "lineage store not ready");
|
|
190
|
+
try {
|
|
191
|
+
const sp = new URL(req.url, "http://x").searchParams;
|
|
192
|
+
const since = sp.get("since");
|
|
193
|
+
const until = sp.get("until");
|
|
194
|
+
let rows, days;
|
|
195
|
+
if (since) {
|
|
196
|
+
const sinceMs = new Date(since).getTime();
|
|
197
|
+
const untilMs = until ? new Date(until).getTime() : Date.now();
|
|
198
|
+
days = Math.round((untilMs - sinceMs) / (24 * 3600 * 1000));
|
|
199
|
+
rows = await execAll(store._client, `
|
|
200
|
+
SELECT date(ts/1000, 'unixepoch', 'localtime') AS day,
|
|
201
|
+
COUNT(*) AS calls, ROUND(SUM(cost_total), 4) AS cost
|
|
202
|
+
FROM cost_by_call WHERE ts >= ? AND ts < ?
|
|
203
|
+
GROUP BY day ORDER BY day ASC
|
|
204
|
+
`, [sinceMs, untilMs]);
|
|
205
|
+
} else {
|
|
206
|
+
days = Math.min(Number(sp.get("days") || 90), 90);
|
|
207
|
+
rows = await execAll(store._client, `
|
|
208
|
+
SELECT date(ts/1000, 'unixepoch', 'localtime') AS day,
|
|
209
|
+
COUNT(*) AS calls, ROUND(SUM(cost_total), 4) AS cost
|
|
210
|
+
FROM cost_by_call
|
|
211
|
+
WHERE ts >= strftime('%s', date('now', 'localtime', '-' || ? || ' days')) * 1000
|
|
212
|
+
GROUP BY day ORDER BY day ASC
|
|
213
|
+
`, [days]);
|
|
214
|
+
}
|
|
215
|
+
json(res, { days, rows, cap: currentCapUSD() });
|
|
216
|
+
} catch (err) {
|
|
217
|
+
logger?.warn?.(`lineage[http /by-day]: ${err?.message ?? err}`);
|
|
218
|
+
json(res, { error: String(err?.message ?? err) }, 500);
|
|
219
|
+
}
|
|
220
|
+
},
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
// ─── /cost/by-hour ─────────────────────────────────────────────────────────
|
|
224
|
+
api.registerHttpRoute({
|
|
225
|
+
method: "GET", path: "/manifold/telemetry/cost/by-hour", auth: "plugin", match: "exact",
|
|
226
|
+
handler: async (req, res) => {
|
|
227
|
+
const store = getStore();
|
|
228
|
+
if (!store) return bad(res, "lineage store not ready");
|
|
229
|
+
try {
|
|
230
|
+
const now = new Date();
|
|
231
|
+
const sodMs = new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime();
|
|
232
|
+
const fullDayMs = 24 * 3600 * 1000;
|
|
233
|
+
const currentHour = now.getHours();
|
|
234
|
+
|
|
235
|
+
const rawRows = await execAll(store._client, `
|
|
236
|
+
SELECT CAST(strftime('%H', ts/1000, 'unixepoch', 'localtime') AS INTEGER) AS hour,
|
|
237
|
+
COUNT(*) AS calls,
|
|
238
|
+
ROUND(SUM(cost_total), 6) AS cost
|
|
239
|
+
FROM cost_by_call
|
|
240
|
+
WHERE date(ts/1000, 'unixepoch', 'localtime') = date('now', 'localtime')
|
|
241
|
+
GROUP BY hour ORDER BY hour ASC
|
|
242
|
+
`);
|
|
243
|
+
const rowMap = new Map(rawRows.map((r) => [r.hour, r]));
|
|
244
|
+
const rows = [];
|
|
245
|
+
for (let h = 0; h <= currentHour; h++) {
|
|
246
|
+
rows.push(rowMap.get(h) ?? { hour: h, calls: 0, cost: 0 });
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const rawHist = await execAll(store._client, `
|
|
250
|
+
SELECT
|
|
251
|
+
date(ts/1000, 'unixepoch', 'localtime') AS day,
|
|
252
|
+
CAST(strftime('%H', ts/1000, 'unixepoch', 'localtime') AS INTEGER) AS hour,
|
|
253
|
+
SUM(cost_total) AS hourly_cost
|
|
254
|
+
FROM cost_by_call
|
|
255
|
+
WHERE ts >= ? AND ts < ?
|
|
256
|
+
GROUP BY day, hour ORDER BY day, hour
|
|
257
|
+
`, [sodMs - 14 * fullDayMs, sodMs]);
|
|
258
|
+
|
|
259
|
+
const byDay = new Map();
|
|
260
|
+
for (const r of rawHist) {
|
|
261
|
+
if (!byDay.has(r.day)) byDay.set(r.day, new Map());
|
|
262
|
+
byDay.get(r.day).set(r.hour, r.hourly_cost);
|
|
263
|
+
}
|
|
264
|
+
const dayCumuls = [];
|
|
265
|
+
for (const [, hourMap] of byDay) {
|
|
266
|
+
let sum = 0;
|
|
267
|
+
const cumul = [];
|
|
268
|
+
for (let h = 0; h <= 23; h++) { sum += hourMap.get(h) ?? 0; cumul.push(sum); }
|
|
269
|
+
dayCumuls.push(cumul);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const histBands = [];
|
|
273
|
+
for (let h = 0; h <= 23; h++) {
|
|
274
|
+
const vals = dayCumuls.map((c) => c[h]).sort((a, b) => a - b);
|
|
275
|
+
if (!vals.length) { histBands.push({ hour: h, p50: 0, p95: 0 }); continue; }
|
|
276
|
+
const p50 = vals[Math.floor((vals.length - 1) * 0.50)];
|
|
277
|
+
const p95 = vals[Math.floor((vals.length - 1) * 0.95)];
|
|
278
|
+
histBands.push({ hour: h, p50: Number(p50.toFixed(6)), p95: Number(p95.toFixed(6)) });
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const rawBand = bootstrapForecast(byDay, currentHour, rows);
|
|
282
|
+
const currentCumul = rows.reduce((s, r) => s + r.cost, 0);
|
|
283
|
+
const forecastBand = rawBand ? [
|
|
284
|
+
{ hour: currentHour, p10: currentCumul, p50: currentCumul, p90: currentCumul },
|
|
285
|
+
...rawBand,
|
|
286
|
+
] : null;
|
|
287
|
+
|
|
288
|
+
json(res, { rows, currentHour, histBands, cap: currentCapUSD(), forecastBand });
|
|
289
|
+
} catch (err) {
|
|
290
|
+
logger?.warn?.(`lineage[http /by-hour]: ${err?.message ?? err}`);
|
|
291
|
+
json(res, { error: String(err?.message ?? err) }, 500);
|
|
292
|
+
}
|
|
293
|
+
},
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
// ─── /cost/top-calls ───────────────────────────────────────────────────────
|
|
297
|
+
api.registerHttpRoute({
|
|
298
|
+
method: "GET", path: "/manifold/telemetry/cost/top-calls", auth: "plugin",
|
|
299
|
+
handler: async (req, res) => {
|
|
300
|
+
const store = getStore();
|
|
301
|
+
if (!store) return bad(res, "lineage store not ready");
|
|
302
|
+
try {
|
|
303
|
+
const url = new URL(req.url, "http://x");
|
|
304
|
+
const limit = Math.min(Number(url.searchParams.get("limit") || 20), 500);
|
|
305
|
+
const { where, args } = parseRange(url.searchParams);
|
|
306
|
+
const filterCols = ["pipeline_id", "flow_id", "node_id", "session_key", "root_kind", "root_label", "model", "provider"];
|
|
307
|
+
for (const col of filterCols) {
|
|
308
|
+
const v = url.searchParams.get(col);
|
|
309
|
+
if (v == null || v === "") continue;
|
|
310
|
+
// "__null__" sentinel = IS NULL filter (used for direct-pipeline leaf drill-down)
|
|
311
|
+
if (v === "__null__") { where.push(`${col} IS NULL`); }
|
|
312
|
+
else { where.push(`${col} = ?`); args.push(v); }
|
|
313
|
+
}
|
|
314
|
+
const w = where.length ? `WHERE ${where.join(" AND ")}` : "";
|
|
315
|
+
const sql = `
|
|
316
|
+
WITH attributed AS (
|
|
317
|
+
SELECT cbc.ts, cbc.cost_total, cbc.flow_id, cbc.pipeline_id,
|
|
318
|
+
cbc.node_id, cbc.model, cbc.provider,
|
|
319
|
+
cbc.input_tokens, cbc.output_tokens,
|
|
320
|
+
cbc.cache_read_tokens, cbc.cache_write_tokens,
|
|
321
|
+
cbc.total_tokens,
|
|
322
|
+
cbc.cost_input, cbc.cost_output,
|
|
323
|
+
cbc.cost_cache_read, cbc.cost_cache_write,
|
|
324
|
+
cbc.session_id, cbc.session_key, cbc.agent_id,
|
|
325
|
+
cbc.item_key, cbc.msg_id,
|
|
326
|
+
${ATTR_ROOT_KIND} AS root_kind,
|
|
327
|
+
${ATTR_ROOT_LABEL} AS root_label
|
|
328
|
+
FROM cost_by_call cbc ${ATTR_JOIN}
|
|
329
|
+
)
|
|
330
|
+
SELECT datetime(ts/1000, 'unixepoch', 'localtime') AS local_ts,
|
|
331
|
+
provider, model, total_tokens,
|
|
332
|
+
input_tokens, output_tokens,
|
|
333
|
+
cache_read_tokens, cache_write_tokens,
|
|
334
|
+
ROUND(cost_total, 6) AS cost,
|
|
335
|
+
ROUND(cost_input, 6) AS cost_input,
|
|
336
|
+
ROUND(cost_output, 6) AS cost_output,
|
|
337
|
+
ROUND(cost_cache_read, 6) AS cost_cache_read,
|
|
338
|
+
ROUND(cost_cache_write, 6) AS cost_cache_write,
|
|
339
|
+
root_kind, root_label, pipeline_id, flow_id, node_id,
|
|
340
|
+
session_id, session_key, item_key, msg_id
|
|
341
|
+
FROM attributed
|
|
342
|
+
${w}
|
|
343
|
+
ORDER BY cost_total DESC NULLS LAST LIMIT ?
|
|
344
|
+
`;
|
|
345
|
+
const rows = await execAll(store._client, sql, [...args, limit]);
|
|
346
|
+
for (const r of rows) {
|
|
347
|
+
r.display_root_label = displayRootLabel(r.root_kind, r.root_label);
|
|
348
|
+
r.display_root_kind = displayRootKind(r.root_kind);
|
|
349
|
+
}
|
|
350
|
+
json(res, { limit, rows });
|
|
351
|
+
} catch (err) {
|
|
352
|
+
logger?.warn?.(`lineage[http /top-calls]: ${err?.message ?? err}`);
|
|
353
|
+
json(res, { error: String(err?.message ?? err) }, 500);
|
|
354
|
+
}
|
|
355
|
+
},
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
// ─── /cost/breakdown ───────────────────────────────────────────────────────
|
|
359
|
+
api.registerHttpRoute({
|
|
360
|
+
method: "GET", path: "/manifold/telemetry/cost/breakdown", auth: "plugin", match: "exact",
|
|
361
|
+
handler: async (req, res) => {
|
|
362
|
+
const store = getStore();
|
|
363
|
+
if (!store) return bad(res, "lineage store not ready");
|
|
364
|
+
try {
|
|
365
|
+
const url = new URL(req.url, "http://x");
|
|
366
|
+
const dim = url.searchParams.get("dim") || "trigger";
|
|
367
|
+
const { where, args } = parseRange(url.searchParams);
|
|
368
|
+
const limit = Math.min(Number(url.searchParams.get("limit") || 100), 500);
|
|
369
|
+
for (const col of ["pipeline_id", "flow_id", "node_id", "root_kind", "root_label", "model", "provider"]) {
|
|
370
|
+
const v = url.searchParams.get(col);
|
|
371
|
+
if (v == null || v === "") continue;
|
|
372
|
+
// "__null__" sentinel = IS NULL, mirroring /cost/top-calls above.
|
|
373
|
+
// The pipeline dim's transform emits this for its (unattributed)
|
|
374
|
+
// row, so a drill-through on that row selects the NULL rows it was
|
|
375
|
+
// computed from instead of matching the synthetic label literally.
|
|
376
|
+
if (v === "__null__") { where.push(`${col} IS NULL`); }
|
|
377
|
+
else { where.push(`${col} = ?`); args.push(v); }
|
|
378
|
+
}
|
|
379
|
+
const w = where.length ? `AND ${where.join(" AND ")}` : "";
|
|
380
|
+
|
|
381
|
+
const ATTRIBUTED = `(
|
|
382
|
+
SELECT cbc.ts, cbc.cost_total, cbc.flow_id, cbc.pipeline_id,
|
|
383
|
+
cbc.node_id, cbc.model, cbc.provider,
|
|
384
|
+
cbc.input_tokens, cbc.output_tokens,
|
|
385
|
+
cbc.cache_read_tokens, cbc.cache_write_tokens,
|
|
386
|
+
cbc.session_id, cbc.session_key, cbc.agent_id, cbc.msg_id,
|
|
387
|
+
${ATTR_ROOT_KIND} AS root_kind,
|
|
388
|
+
${ATTR_ROOT_LABEL} AS root_label
|
|
389
|
+
FROM cost_by_call cbc ${ATTR_JOIN}
|
|
390
|
+
)`;
|
|
391
|
+
|
|
392
|
+
const flowsAttached = await hasFlowRuns(store._client);
|
|
393
|
+
const dims = {
|
|
394
|
+
trigger: {
|
|
395
|
+
sql: `SELECT root_kind AS raw_label, root_kind AS kind_for_label,
|
|
396
|
+
COUNT(*) AS calls, ROUND(SUM(cost_total), 6) AS cost
|
|
397
|
+
FROM ${ATTRIBUTED}
|
|
398
|
+
WHERE root_kind IS NOT NULL ${w}
|
|
399
|
+
GROUP BY 1 ORDER BY cost DESC NULLS LAST LIMIT ${limit}`,
|
|
400
|
+
transform: (r) => ({
|
|
401
|
+
label: displayRootKind(r.raw_label),
|
|
402
|
+
kind: null,
|
|
403
|
+
calls: r.calls, cost: r.cost,
|
|
404
|
+
filter: { root_kind: r.raw_label },
|
|
405
|
+
}),
|
|
406
|
+
},
|
|
407
|
+
root: {
|
|
408
|
+
sql: `SELECT root_label AS raw_label, root_kind AS kind_for_label,
|
|
409
|
+
COUNT(*) AS calls, ROUND(SUM(cost_total), 6) AS cost
|
|
410
|
+
FROM ${ATTRIBUTED}
|
|
411
|
+
WHERE root_label IS NOT NULL ${w}
|
|
412
|
+
GROUP BY 1, 2 ORDER BY cost DESC NULLS LAST LIMIT ${limit}`,
|
|
413
|
+
transform: (r) => ({
|
|
414
|
+
label: displayRootLabel(r.kind_for_label, r.raw_label),
|
|
415
|
+
kind: displayRootKind(r.kind_for_label),
|
|
416
|
+
calls: r.calls, cost: r.cost,
|
|
417
|
+
filter: { root_label: r.raw_label, root_kind: r.kind_for_label },
|
|
418
|
+
}),
|
|
419
|
+
},
|
|
420
|
+
pipeline: {
|
|
421
|
+
// COALESCE rather than a `pipeline_id IS NOT NULL` filter. A
|
|
422
|
+
// cost_by_call row can legitimately have no pipeline_id: its
|
|
423
|
+
// lineage walk landed on a root that isn't a flow (cron-owned
|
|
424
|
+
// agent turns, operator ad-hoc sessions, spawned subagents,
|
|
425
|
+
// Telegram DMs). Filtering those out made them vanish from this
|
|
426
|
+
// breakdown while still counting toward /cost/home's
|
|
427
|
+
// unconditional SUM(cost_total) — so the top card showed a total
|
|
428
|
+
// that silently exceeded the sum of this list, and the operator
|
|
429
|
+
// had no way to account for the difference. Surfacing them as a
|
|
430
|
+
// labeled bucket keeps the two numbers reconcilable and makes any
|
|
431
|
+
// future unattributed spend self-diagnosing.
|
|
432
|
+
sql: flowsAttached
|
|
433
|
+
? `SELECT COALESCE(c.pipeline_id, '${UNATTRIBUTED_LABEL}') AS raw_label,
|
|
434
|
+
COUNT(*) AS calls,
|
|
435
|
+
ROUND(SUM(c.cost_total), 6) AS cost,
|
|
436
|
+
COUNT(DISTINCT CASE WHEN f.status = 'succeeded' THEN c.flow_id END) AS succeeded_runs,
|
|
437
|
+
COUNT(DISTINCT CASE WHEN f.status = 'failed' THEN c.flow_id END) AS failed_runs,
|
|
438
|
+
COUNT(DISTINCT CASE WHEN f.status NOT IN ('succeeded','failed') AND f.status IS NOT NULL THEN c.flow_id END) AS other_runs,
|
|
439
|
+
COUNT(DISTINCT c.flow_id) AS total_runs
|
|
440
|
+
FROM ${ATTRIBUTED} c
|
|
441
|
+
LEFT JOIN flow_runs f ON f.flow_id = c.flow_id
|
|
442
|
+
WHERE 1=1 ${w.replace(/\b(ts|cost_total|root_kind|root_label|model|provider|input_tokens|output_tokens|cache_read_tokens|cache_write_tokens|pipeline_id|flow_id|node_id|item_key|session_id|session_key|agent_id)\b/g, "c.$1")}
|
|
443
|
+
GROUP BY 1 ORDER BY cost DESC NULLS LAST LIMIT ${limit}`
|
|
444
|
+
: `SELECT COALESCE(pipeline_id, '${UNATTRIBUTED_LABEL}') AS raw_label,
|
|
445
|
+
COUNT(*) AS calls,
|
|
446
|
+
ROUND(SUM(cost_total), 6) AS cost,
|
|
447
|
+
NULL AS succeeded_runs, NULL AS failed_runs,
|
|
448
|
+
NULL AS other_runs, NULL AS total_runs
|
|
449
|
+
FROM ${ATTRIBUTED}
|
|
450
|
+
WHERE 1=1 ${w}
|
|
451
|
+
GROUP BY 1 ORDER BY cost DESC NULLS LAST LIMIT ${limit}`,
|
|
452
|
+
transform: (r) => {
|
|
453
|
+
// The COALESCE above synthesizes UNATTRIBUTED_LABEL for NULL
|
|
454
|
+
// pipeline_ids; stripTeamPrefix is a no-op on it (no underscore),
|
|
455
|
+
// so pass it through verbatim rather than deriving a label from
|
|
456
|
+
// a value that was never stored.
|
|
457
|
+
const unattributed = r.raw_label === UNATTRIBUTED_LABEL;
|
|
458
|
+
const out = {
|
|
459
|
+
label: unattributed ? UNATTRIBUTED_LABEL : stripTeamPrefix(r.raw_label),
|
|
460
|
+
kind: "Pipeline",
|
|
461
|
+
calls: r.calls, cost: r.cost,
|
|
462
|
+
// Not `{ pipeline_id: '(unattributed)' }` — no row stores that
|
|
463
|
+
// string, so it would select nothing. The __null__ sentinel is
|
|
464
|
+
// what actually resolves back to these rows, and the filter
|
|
465
|
+
// loop above translates it to `pipeline_id IS NULL`.
|
|
466
|
+
filter: unattributed ? { ...UNATTRIBUTED_FILTER } : { pipeline_id: r.raw_label },
|
|
467
|
+
};
|
|
468
|
+
// Outcome counters are meaningless for the unattributed bucket:
|
|
469
|
+
// its rows carry no flow_id, so total_runs is 0 and the guard
|
|
470
|
+
// already excludes it. Stated explicitly so a future change that
|
|
471
|
+
// gives those rows a flow_id can't start emitting a success_rate
|
|
472
|
+
// a client ROI filter would read as a real pipeline signal.
|
|
473
|
+
if (!unattributed && r.succeeded_runs != null && r.total_runs > 0) {
|
|
474
|
+
out.succeeded_runs = r.succeeded_runs;
|
|
475
|
+
out.failed_runs = r.failed_runs;
|
|
476
|
+
out.other_runs = r.other_runs;
|
|
477
|
+
out.total_runs = r.total_runs;
|
|
478
|
+
if (r.succeeded_runs > 0) {
|
|
479
|
+
out.cost_per_success = Number((r.cost / r.succeeded_runs).toFixed(6));
|
|
480
|
+
}
|
|
481
|
+
if ((r.succeeded_runs + r.failed_runs) > 0) {
|
|
482
|
+
out.success_rate = Number(
|
|
483
|
+
(r.succeeded_runs / (r.succeeded_runs + r.failed_runs)).toFixed(4)
|
|
484
|
+
);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
return out;
|
|
488
|
+
},
|
|
489
|
+
},
|
|
490
|
+
"cron-job": {
|
|
491
|
+
sql: `SELECT root_label AS raw_label,
|
|
492
|
+
COUNT(*) AS calls, ROUND(SUM(cost_total), 6) AS cost
|
|
493
|
+
FROM ${ATTRIBUTED}
|
|
494
|
+
WHERE root_kind = 'cron-owner' AND root_label IS NOT NULL ${w}
|
|
495
|
+
GROUP BY 1 ORDER BY cost DESC NULLS LAST LIMIT ${limit}`,
|
|
496
|
+
transform: (r) => ({
|
|
497
|
+
// raw_label is the cron job's own free-text name (opaque, no
|
|
498
|
+
// shape contract) — shown as-is, see labels.js's cron-owner case.
|
|
499
|
+
label: r.raw_label,
|
|
500
|
+
kind: "Cron",
|
|
501
|
+
calls: r.calls, cost: r.cost,
|
|
502
|
+
filter: { root_label: r.raw_label, root_kind: "cron-owner" },
|
|
503
|
+
}),
|
|
504
|
+
},
|
|
505
|
+
model: {
|
|
506
|
+
sql: `SELECT model AS raw_label, provider AS extra,
|
|
507
|
+
COUNT(*) AS calls, ROUND(SUM(cost_total), 6) AS cost
|
|
508
|
+
FROM ${ATTRIBUTED}
|
|
509
|
+
WHERE model IS NOT NULL ${w}
|
|
510
|
+
GROUP BY 1, 2 ORDER BY cost DESC NULLS LAST LIMIT ${limit}`,
|
|
511
|
+
transform: (r) => ({
|
|
512
|
+
label: r.raw_label,
|
|
513
|
+
kind: r.extra,
|
|
514
|
+
calls: r.calls, cost: r.cost,
|
|
515
|
+
filter: { model: r.raw_label, ...(r.extra ? { provider: r.extra } : {}) },
|
|
516
|
+
}),
|
|
517
|
+
},
|
|
518
|
+
};
|
|
519
|
+
const def = dims[dim];
|
|
520
|
+
if (!def) return bad(res, `unknown dim: ${dim} (expected: trigger|root|pipeline|cron-job|model)`, 400);
|
|
521
|
+
const raw = await execAll(store._client, def.sql, args);
|
|
522
|
+
let rows = raw.map(def.transform);
|
|
523
|
+
if (dim === "trigger") {
|
|
524
|
+
const byLabel = new Map();
|
|
525
|
+
for (const r of rows) {
|
|
526
|
+
if (byLabel.has(r.label)) {
|
|
527
|
+
const m = byLabel.get(r.label);
|
|
528
|
+
m.calls += r.calls;
|
|
529
|
+
m.cost = Number((m.cost + (r.cost || 0)).toFixed(6));
|
|
530
|
+
delete m.filter;
|
|
531
|
+
} else {
|
|
532
|
+
byLabel.set(r.label, { ...r });
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
rows = [...byLabel.values()].sort((a, b) => (b.cost || 0) - (a.cost || 0));
|
|
536
|
+
}
|
|
537
|
+
const total = rows.reduce((a, r) => a + (r.cost || 0), 0);
|
|
538
|
+
json(res, { dimension: dim, total, rows });
|
|
539
|
+
} catch (err) {
|
|
540
|
+
logger?.warn?.(`lineage[http /breakdown]: ${err?.message ?? err}`);
|
|
541
|
+
json(res, { error: String(err?.message ?? err) }, 500);
|
|
542
|
+
}
|
|
543
|
+
},
|
|
544
|
+
});
|
|
545
|
+
|
|
546
|
+
// ─── /cost/home ────────────────────────────────────────────────────────────
|
|
547
|
+
api.registerHttpRoute({
|
|
548
|
+
method: "GET", path: "/manifold/telemetry/cost/home", auth: "plugin", match: "exact",
|
|
549
|
+
handler: async (req, res) => {
|
|
550
|
+
const store = getStore();
|
|
551
|
+
if (!store) return bad(res, "lineage store not ready");
|
|
552
|
+
try {
|
|
553
|
+
const client = store._client;
|
|
554
|
+
const cap = currentCapUSD();
|
|
555
|
+
const dayClause = `date(ts/1000, 'unixepoch', 'localtime') = date('now', 'localtime')`;
|
|
556
|
+
const yClause = `date(ts/1000, 'unixepoch', 'localtime') = date('now', 'localtime', '-1 day')`;
|
|
557
|
+
|
|
558
|
+
const today = await execGet(client, `
|
|
559
|
+
SELECT COUNT(*) AS calls, COALESCE(ROUND(SUM(cost_total), 6), 0) AS cost
|
|
560
|
+
FROM cost_by_call WHERE ${dayClause}
|
|
561
|
+
`);
|
|
562
|
+
const yest = await execGet(client, `
|
|
563
|
+
SELECT COUNT(*) AS calls, COALESCE(ROUND(SUM(cost_total), 6), 0) AS cost
|
|
564
|
+
FROM cost_by_call WHERE ${yClause}
|
|
565
|
+
`);
|
|
566
|
+
const byRootRaw = await execAll(client, `
|
|
567
|
+
SELECT root_label, root_kind, COUNT(*) AS calls,
|
|
568
|
+
ROUND(SUM(cost_total), 6) AS cost
|
|
569
|
+
FROM cost_by_call
|
|
570
|
+
WHERE ${dayClause} AND root_label IS NOT NULL
|
|
571
|
+
GROUP BY root_label, root_kind ORDER BY cost DESC LIMIT 5
|
|
572
|
+
`);
|
|
573
|
+
const byRoot = byRootRaw.map((r) => ({
|
|
574
|
+
...r,
|
|
575
|
+
display_label: displayRootLabel(r.root_kind, r.root_label),
|
|
576
|
+
display_kind: displayRootKind(r.root_kind),
|
|
577
|
+
}));
|
|
578
|
+
const byDay = await execAll(client, `
|
|
579
|
+
SELECT date(ts/1000, 'unixepoch', 'localtime') AS day,
|
|
580
|
+
ROUND(SUM(cost_total), 6) AS cost
|
|
581
|
+
FROM cost_by_call
|
|
582
|
+
WHERE ts >= strftime('%s', date('now', 'localtime', '-14 days')) * 1000
|
|
583
|
+
GROUP BY day ORDER BY day ASC
|
|
584
|
+
`);
|
|
585
|
+
const topCall = await execGet(client, `
|
|
586
|
+
SELECT datetime(ts/1000, 'unixepoch', 'localtime') AS local_ts,
|
|
587
|
+
provider, model, total_tokens,
|
|
588
|
+
ROUND(cost_total, 6) AS cost,
|
|
589
|
+
root_kind, root_label, pipeline_id, flow_id, node_id, session_id
|
|
590
|
+
FROM cost_by_call
|
|
591
|
+
WHERE ${dayClause} AND cost_total IS NOT NULL
|
|
592
|
+
ORDER BY cost_total DESC LIMIT 1
|
|
593
|
+
`);
|
|
594
|
+
if (topCall) {
|
|
595
|
+
topCall.display_root_label = displayRootLabel(topCall.root_kind, topCall.root_label);
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
const now = new Date();
|
|
599
|
+
const sodMs = new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime();
|
|
600
|
+
const elapsedMs = Date.now() - sodMs;
|
|
601
|
+
const fullDayMs = 24 * 3600 * 1000;
|
|
602
|
+
const elapsedFrac = elapsedMs / fullDayMs;
|
|
603
|
+
let forecast = null;
|
|
604
|
+
if (elapsedMs > 30 * 60 * 1000 && today.cost > 0) {
|
|
605
|
+
forecast = Number((today.cost * (fullDayMs / elapsedMs)).toFixed(4));
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
const comparison = yest.cost > 0
|
|
609
|
+
? Number((today.cost / yest.cost).toFixed(2))
|
|
610
|
+
: null;
|
|
611
|
+
|
|
612
|
+
const yestSameHour = await execGet(client, `
|
|
613
|
+
SELECT COALESCE(ROUND(SUM(cost_total), 6), 0) AS cost
|
|
614
|
+
FROM cost_by_call WHERE ts >= ? AND ts < ?
|
|
615
|
+
`, [sodMs - fullDayMs, Date.now() - fullDayMs]);
|
|
616
|
+
const compareAtSameHour = (yestSameHour?.cost > 0)
|
|
617
|
+
? Number((today.cost / yestSameHour.cost).toFixed(2))
|
|
618
|
+
: null;
|
|
619
|
+
|
|
620
|
+
const avg7Day = await execGet(client, `
|
|
621
|
+
SELECT COUNT(*) AS day_count, AVG(daily_cost) AS avg
|
|
622
|
+
FROM (
|
|
623
|
+
SELECT SUM(cost_total) AS daily_cost
|
|
624
|
+
FROM cost_by_call WHERE ts >= ? AND ts < ?
|
|
625
|
+
GROUP BY date(ts/1000, 'unixepoch', 'localtime')
|
|
626
|
+
)
|
|
627
|
+
`, [sodMs - 7 * fullDayMs, sodMs]);
|
|
628
|
+
const expectedAtThisHour = (avg7Day?.avg != null && avg7Day.avg > 0 && avg7Day.day_count >= 3)
|
|
629
|
+
? Number((avg7Day.avg * elapsedFrac).toFixed(4))
|
|
630
|
+
: null;
|
|
631
|
+
|
|
632
|
+
const currentHour = now.getHours();
|
|
633
|
+
const rawHist = await execAll(client, `
|
|
634
|
+
SELECT date(ts/1000, 'unixepoch', 'localtime') AS day,
|
|
635
|
+
CAST(strftime('%H', ts/1000, 'unixepoch', 'localtime') AS INTEGER) AS hour,
|
|
636
|
+
SUM(cost_total) AS hourly_cost
|
|
637
|
+
FROM cost_by_call WHERE ts >= ? AND ts < ?
|
|
638
|
+
GROUP BY day, hour ORDER BY day, hour
|
|
639
|
+
`, [sodMs - 14 * fullDayMs, sodMs]);
|
|
640
|
+
const histByDay = new Map();
|
|
641
|
+
for (const r of rawHist) {
|
|
642
|
+
if (!histByDay.has(r.day)) histByDay.set(r.day, new Map());
|
|
643
|
+
histByDay.get(r.day).set(r.hour, r.hourly_cost);
|
|
644
|
+
}
|
|
645
|
+
const fcBand = bootstrapForecast(histByDay, currentHour, [{ cost: today.cost }]);
|
|
646
|
+
const midnight = fcBand?.[fcBand.length - 1] ?? null;
|
|
647
|
+
|
|
648
|
+
json(res, {
|
|
649
|
+
today: `${now.getFullYear()}-${String(now.getMonth()+1).padStart(2,'0')}-${String(now.getDate()).padStart(2,'0')}`,
|
|
650
|
+
cap,
|
|
651
|
+
summary: today,
|
|
652
|
+
yesterday: yest,
|
|
653
|
+
byRoot,
|
|
654
|
+
byDay,
|
|
655
|
+
topCall,
|
|
656
|
+
forecastUntilMidnight: forecast,
|
|
657
|
+
forecastP10: midnight ? Number(midnight.p10.toFixed(4)) : null,
|
|
658
|
+
forecastP50: midnight ? Number(midnight.p50.toFixed(4)) : null,
|
|
659
|
+
forecastP90: midnight ? Number(midnight.p90.toFixed(4)) : null,
|
|
660
|
+
compareYesterday: comparison,
|
|
661
|
+
compareAtSameHour,
|
|
662
|
+
expectedAtThisHour,
|
|
663
|
+
});
|
|
664
|
+
} catch (err) {
|
|
665
|
+
logger?.warn?.(`lineage[http /home]: ${err?.message ?? err}`);
|
|
666
|
+
json(res, { error: String(err?.message ?? err) }, 500);
|
|
667
|
+
}
|
|
668
|
+
},
|
|
669
|
+
});
|
|
670
|
+
}
|