@angelyeye/dsh-cost-tracker 1.11.0 → 1.11.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/CHANGELOG.md +52 -0
- package/client.js +79 -14
- package/index.js +56 -3
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,58 @@
|
|
|
2
2
|
|
|
3
3
|
本文件用中文记录 dsh-cost-tracker 的版本变更。
|
|
4
4
|
|
|
5
|
+
## v1.11.1(2026-11-16)
|
|
6
|
+
|
|
7
|
+
**时间区间口径修复:卡片主值跟随筛选 ・ 新增「今天」・ 云端区间真正生效 ・ 设备 × Agent 矩阵恢复**
|
|
8
|
+
|
|
9
|
+
这一版修的全是**静默错值**——不报错、界面照常渲染,但数字是错的。四个问题互相缠绕,都源于「界面上的时间范围」和「实际取数的区间」不是同一件事。
|
|
10
|
+
|
|
11
|
+
### 一、三张统计卡(花费 / API 请求次数 / Token)主值跟随时间筛选
|
|
12
|
+
|
|
13
|
+
**问题**:这三张卡直接读 `dash.all`(全时段累计),与顶部「近 N 天 / 全部」下拉完全无关。切到「近 7 天」时,图表与明细表都收窄了,卡片却依旧显示开天辟地以来的累计,同一屏内两套口径。
|
|
14
|
+
|
|
15
|
+
**改法**:主值改取窗口聚合 `dash.realCost / realCalls / realTokens / subEquivalent / subCalls / subTokens`(`buildDashboard({days})` 已按 cutoff 过滤),累计值降到副行并显式标注「累计 ¥X」,标题追加「· 近 7 天」后缀。
|
|
16
|
+
|
|
17
|
+
`今日费用`/`本月费用` 两卡保持原样——它们是固定日历口径,与滚动窗口无关是正确语义。
|
|
18
|
+
|
|
19
|
+
### 二、新增「今天」选项(按北京日历日,不是近 24 小时)
|
|
20
|
+
|
|
21
|
+
时间下拉新增首项「今天」。**不能简单加一个 `days=1`**:`buildDashboard` 原本的约定是 `cutoff = now - days*86400000`,`days=1` 会变成「近 24 小时」,把昨天晚间的消费算进今天,与旁边「今日费用」卡(北京日历日)对不上,屏幕上出现两个互相矛盾的「今天」。
|
|
22
|
+
|
|
23
|
+
改法:抽出 `windowCutoff(days, now)` 作为**唯一**的区间换算入口,`days=1` 特判为北京时间当日 00:00:
|
|
24
|
+
|
|
25
|
+
| days | cutoff |
|
|
26
|
+
|---|---|
|
|
27
|
+
| 1 | 北京时间当日 00:00(今天) |
|
|
28
|
+
| >1 | `now - days*86400000`(滚动窗口) |
|
|
29
|
+
| ≤0 | 0(不限起点,全部) |
|
|
30
|
+
|
|
31
|
+
`buildDashboard` 与本版新增的云端 `fetchCloud` 共用它,确保「本机 + 云端」的两半落在同一区间。
|
|
32
|
+
|
|
33
|
+
### 三、云端区间真正生效(此前恒定近 7 天)
|
|
34
|
+
|
|
35
|
+
**问题**:`fetchCloud()` 拼查询串时只发了 `range` 和 `days`,**从未发 `from`/`to`**;而 `cloud` 路由又把 `range` 默认值写死成 `'7d'`。结果:界面切「近 30 天 / 全部」,云端那一半纹丝不动,固定按近 7 天算。
|
|
36
|
+
|
|
37
|
+
**为什么不能靠 `range` 枚举解决**:云端 `resolveRange()` 只认 `today / 7d / 30d / 90d / month / year / all`,**其余值静默降级成 `all`**。实测传 `1d`、`14d` 会直接返回全时段累计且无任何报错——这比报错更难发现。所以凡有精确区间的地方一律走云端已支持的 `from`/`to` 自定义区间,不受枚举限制。
|
|
38
|
+
|
|
39
|
+
**改法**:
|
|
40
|
+
- `fetchCloud` 在 `days>0` 时用 `windowCutoff` 推导起点并写入 `from`/`to`;
|
|
41
|
+
- `cloud` 路由不再无条件写死 `range:'7d'`,改为 `a.range || (a.days === 0 ? 'all' : '7d')` 兜底。
|
|
42
|
+
|
|
43
|
+
**配套云端改动**(`dsh-cost-cloud` 同步发布):`readOverview` / `readPluginView` 的 union 分支透传 `from`/`to`。这两个函数此前用白名单式 `Object.assign` 转发参数,漏掉了 `from`/`to`,导致「仅云端」正常、「本机+云端」不跟随——一半对一半错。
|
|
44
|
+
|
|
45
|
+
### 四、设备 × Agent 矩阵恢复渲染
|
|
46
|
+
|
|
47
|
+
**问题**:`loadCloudMatrix()` 请求宿主 `matrix` 路由,但宿主 `ROUTES` 里**根本没有这个路由**,稳定 404 → `setCloudMatrix(null)` → 矩阵区块永不渲染,且无任何提示。
|
|
48
|
+
|
|
49
|
+
**改法**:不再依赖不存在的路由,改由云端 `overview`/`plugin-view` 已返回的 `devices[]`(含 `sources[]`)与 `sources[]` 在客户端就地派生矩阵(`buildDeviceMatrix`),用派生值而非 state,`viewDash`/`days`/`view` 变化时自动重算。
|
|
50
|
+
|
|
51
|
+
**数据诚实性**:`devices[].sources` 只说明「该设备用过哪些 agent」,**不提供按 agent 拆分的金额**。因此单 agent 设备将整台金额记入该列;多 agent 设备无法拆分时格子**留空**并在表头注明,合计列仍为精确总额——不编造数字。
|
|
52
|
+
|
|
53
|
+
### 测试
|
|
54
|
+
|
|
55
|
+
新增 `test/time-range.test.js`(9 项断言):捕获真实发出的云端 URL,断言必须含 `from`/`to`、`days=1` 落在北京零点(UTC 16:00 整)、不同 `days` 必须产生不同区间(锁死「恒定 7d」回归)、`days=0` 走 `range=all` 且不带 `from`。
|
|
56
|
+
|
|
5
57
|
## v1.11.0(2026-11-13)
|
|
6
58
|
|
|
7
59
|
**Token 数量改中文单位(万/亿)· GPT Fast 档单独计价 · 目录汇率 6.67 · 新模型与价格表刷新**
|
package/client.js
CHANGED
|
@@ -925,6 +925,7 @@ window.__ModuleLoader__.load({
|
|
|
925
925
|
const views = viewCtl && viewCtl.available ? BOARD_VIEWS : [BOARD_VIEWS[0]];
|
|
926
926
|
return e("div", { className: "cost-row" },
|
|
927
927
|
e("select", { className: "cost-select", value: String(days), onChange: ev => setDays(parseInt(ev.target.value, 10)) },
|
|
928
|
+
e("option", { value: "1" }, "今天"),
|
|
928
929
|
e("option", { value: "7" }, "近 7 天"),
|
|
929
930
|
e("option", { value: "14" }, "近 14 天"),
|
|
930
931
|
e("option", { value: "30" }, "近 30 天"),
|
|
@@ -995,7 +996,23 @@ window.__ModuleLoader__.load({
|
|
|
995
996
|
function statCards(dash, balance, viewCtl, planTotal) {
|
|
996
997
|
const today = dash.today || { real: 0, calls: 0, tokens: 0, sub: 0, subCalls: 0, subTokens: 0 };
|
|
997
998
|
const month = dash.month || { real: 0, calls: 0, tokens: 0, sub: 0, subCalls: 0, subTokens: 0 };
|
|
999
|
+
// all = 全时段累计(不随筛选变化),仅作副行参考
|
|
998
1000
|
const all = dash.all || { real: 0, calls: 0, tokens: 0, sub: 0, subCalls: 0, subTokens: 0 };
|
|
1001
|
+
// win = 当前时间窗口(顶部「近 N 天 / 全部」下拉)内的合计(v1.9.x)。
|
|
1002
|
+
// 此前这三张卡(总花费 / API 请求次数 / Token)直接读 `all`,与筛选框完全无关:
|
|
1003
|
+
// 切到「近 7 天」时费用却显示全时段累计,与下方图表口径打架。
|
|
1004
|
+
// 现在主值取窗口内金额,累计值降到副行,并显式标注「累计」。
|
|
1005
|
+
// dash 由 buildDashboard({days}) 计算,realCost/subEquivalent/calls/tokens
|
|
1006
|
+
// 都按 cutoff 过滤;云端经 normalizeCloudDash 后同名字段齐全,故本机/云端/并集同口径。
|
|
1007
|
+
const n = (v) => Number(v) || 0;
|
|
1008
|
+
const win = {
|
|
1009
|
+
real: n(dash.realCost), calls: n(dash.realCalls), tokens: n(dash.realTokens),
|
|
1010
|
+
sub: n(dash.subEquivalent), subCalls: n(dash.subCalls), subTokens: n(dash.subTokens),
|
|
1011
|
+
};
|
|
1012
|
+
const days = Number(dash.days) || 0;
|
|
1013
|
+
// days=1 是「今天」(北京时间日历日,见宿主 buildDashboard 的 cutoff 约定),
|
|
1014
|
+
// 不能拼成「近 1 天」,否则会与「今日费用」卡看起来是两个口径。
|
|
1015
|
+
const winLabel = days === 1 ? "今天" : (days > 0 ? "近 " + days + " 天" : "全部");
|
|
999
1016
|
// 金额卡:口径由「含 Plan 总额」开关决定
|
|
1000
1017
|
// off:主值 = 按量消费(不含订阅会员等效费用);副行附注订阅等效
|
|
1001
1018
|
// on :主值 = 按量 + 订阅等值(Plan 总额);副行说明拆分,避免看数不明口径
|
|
@@ -1034,14 +1051,27 @@ window.__ModuleLoader__.load({
|
|
|
1034
1051
|
: "本机 + 云端 " + Math.max(0, (viewCtl.devices || []).length - 1) + " 台其他设备")
|
|
1035
1052
|
: "本机";
|
|
1036
1053
|
const tag = (t) => scopeNote + (viewCtl && viewCtl.pending > 0 && viewCtl.view !== "local" ? " · 待同步 " + viewCtl.pending + " 条" : "");
|
|
1054
|
+
// 三张「窗口卡」:主值跟随顶部时间筛选,副行给累计值(并注明「累计」)
|
|
1055
|
+
// 金额卡副行的按量/订阅拆分同样取窗口值,累计单独一块,避免半窗口半累计。
|
|
1056
|
+
//
|
|
1057
|
+
// 单位说明:副行保留「N 次」写法(`按量 100 次 · 订阅 5 次`)——
|
|
1058
|
+
// 早先这里只有裸数字 `按量 100 · 订阅 5`,界面上看不出单位,
|
|
1059
|
+
// 且与「今日/本月」两卡的「调用 N 次」风格不一致。
|
|
1060
|
+
const moneyWin = money(win);
|
|
1061
|
+
const cnt = (n) => fmtInt(n) + " 次";
|
|
1062
|
+
const moneyWinSub = (planTotal
|
|
1063
|
+
? "按量 " + cnt(win.calls) + " · 订阅 " + cnt(win.subCalls)
|
|
1064
|
+
: (win.sub > 0 ? "按量 ¥" + fmtMoney(win.real) + " · 订阅 ¥" + fmtMoney(win.sub) : moneySub(win)));
|
|
1065
|
+
const allMoney = money(all);
|
|
1037
1066
|
return e("div", { className: "cost-cards" },
|
|
1038
1067
|
statCard("今日费用(CNY)" + planBadge + "[" + tag() + "]", "¥" + fmtMoney(money(today)), moneySub(today)),
|
|
1039
1068
|
statCard("本月费用(CNY)" + planBadge, "¥" + fmtMoney(money(month)), moneySub(month)),
|
|
1040
|
-
statCard("
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1069
|
+
statCard("花费(CNY)" + planBadge + " · " + winLabel, "¥" + fmtMoney(moneyWin),
|
|
1070
|
+
moneyWinSub + " · 累计 ¥" + fmtMoney(allMoney)),
|
|
1071
|
+
statCard("API 请求次数 · " + winLabel, cnt(win.calls + win.subCalls),
|
|
1072
|
+
"按量 " + cnt(win.calls) + " · 订阅 " + cnt(win.subCalls) + " · 累计 " + cnt(all.calls + all.subCalls)),
|
|
1073
|
+
statCard("Token · " + winLabel, fmtTokens(win.tokens + win.subTokens),
|
|
1074
|
+
"按量 " + fmtTokens(win.tokens) + " · 订阅 " + fmtTokens(win.subTokens) + " · 累计 " + fmtTokens(all.tokens + all.subTokens)),
|
|
1045
1075
|
statCard(balTitle, balValue, balSub));
|
|
1046
1076
|
}
|
|
1047
1077
|
|
|
@@ -2635,7 +2665,6 @@ window.__ModuleLoader__.load({
|
|
|
2635
2665
|
const [sync, setSync] = useState(null);
|
|
2636
2666
|
const [cloudDash, setCloudDash] = useState(null);
|
|
2637
2667
|
const [cloudErr, setCloudErr] = useState("");
|
|
2638
|
-
const [cloudMatrix, setCloudMatrix] = useState(null);
|
|
2639
2668
|
const [cloudUsage, setCloudUsage] = useState(null);
|
|
2640
2669
|
const [cloudUsageErr, setCloudUsageErr] = useState("");
|
|
2641
2670
|
const [view, setViewState] = useState(() => {
|
|
@@ -2703,10 +2732,41 @@ window.__ModuleLoader__.load({
|
|
|
2703
2732
|
else { setCloudDash(null); setCloudErr((v && v.error) || "云端不可用"); }
|
|
2704
2733
|
}).catch(err => { setCloudDash(null); setCloudErr(String(err && err.message ? err.message : err)); });
|
|
2705
2734
|
}
|
|
2706
|
-
|
|
2707
|
-
|
|
2708
|
-
|
|
2709
|
-
|
|
2735
|
+
/**
|
|
2736
|
+
* 「设备 × Agent 矩阵」数据源。
|
|
2737
|
+
*
|
|
2738
|
+
* 此前这里请求宿主 `matrix` 路由,但宿主 ROUTES 里**没有**该路由 ——
|
|
2739
|
+
* 稳定 404 → setCloudMatrix(null) → 矩阵区块永远不渲染(静默失效,
|
|
2740
|
+
* 界面上只表现为「这块没了」,没有任何报错提示)。
|
|
2741
|
+
*
|
|
2742
|
+
* 修复:不再依赖不存在的路由。云端 overview / plugin-view 本身已返回
|
|
2743
|
+
* devices:[{device,name,cost,calls,tokens,sources[]}] 与
|
|
2744
|
+
* sources:[{source,cost,calls,tokens,devices[]}]
|
|
2745
|
+
* 足以就地拼出矩阵,且不引入云端版本耦合(老云端同样可用)。
|
|
2746
|
+
*
|
|
2747
|
+
* 格子填充规则:devices[].sources 只说明「该设备用过哪些 agent」,
|
|
2748
|
+
* 并不提供按 agent 拆分的金额。因此**单 agent 设备**把整台设备的金额记到
|
|
2749
|
+
* 那一列;**多 agent 设备**无法拆分,格子留空并在表头标注「部分未拆分」,
|
|
2750
|
+
* 不编造数字(合计列仍按设备精确总额展示)。
|
|
2751
|
+
*/
|
|
2752
|
+
function buildDeviceMatrix(dash) {
|
|
2753
|
+
if (!dash) return null;
|
|
2754
|
+
const rows = Array.isArray(dash.devices) ? dash.devices : [];
|
|
2755
|
+
const srcs = Array.isArray(dash.sources) ? dash.sources : [];
|
|
2756
|
+
const cols = Array.from(new Set(srcs.map(s => s && s.source).filter(Boolean)));
|
|
2757
|
+
if (!rows.length || !cols.length) return null;
|
|
2758
|
+
const grid = rows.map(r => {
|
|
2759
|
+
const list = (r.sources || []).filter(Boolean);
|
|
2760
|
+
const cells = {};
|
|
2761
|
+
if (list.length === 1) cells[list[0]] = { cost: Number(r.cost) || 0 };
|
|
2762
|
+
return Object.assign({}, r, { cells });
|
|
2763
|
+
});
|
|
2764
|
+
return {
|
|
2765
|
+
cols,
|
|
2766
|
+
rows: grid,
|
|
2767
|
+
totals: { cost: rows.reduce((s, r) => s + (Number(r.cost) || 0), 0) },
|
|
2768
|
+
unsplit: grid.some(r => Object.keys(r.cells).length !== ((r.sources || []).filter(Boolean).length)),
|
|
2769
|
+
};
|
|
2710
2770
|
}
|
|
2711
2771
|
/**
|
|
2712
2772
|
* 云端「按天用量明细」(热力图数据源)。
|
|
@@ -2800,7 +2860,7 @@ window.__ModuleLoader__.load({
|
|
|
2800
2860
|
useEffect(() => { loadDash(days); }, [days]);
|
|
2801
2861
|
useEffect(() => { loadKimi(false); loadVolc(false); loadBalance(""); loadUsage(); loadSync(); }, []);
|
|
2802
2862
|
// 视图切换:local+cloud 需要额外拉「排除本机」的云端聚合、矩阵与按天用量明细
|
|
2803
|
-
useEffect(() => { loadCloud(days, view);
|
|
2863
|
+
useEffect(() => { loadCloud(days, view); loadCloudUsage(view); }, [view, days]);
|
|
2804
2864
|
useEffect(() => {
|
|
2805
2865
|
const id = setInterval(() => setNow(Date.now()), 30000);
|
|
2806
2866
|
return () => clearInterval(id);
|
|
@@ -2813,6 +2873,10 @@ window.__ModuleLoader__.load({
|
|
|
2813
2873
|
: view === "cloud"
|
|
2814
2874
|
? cloudNorm
|
|
2815
2875
|
: (cloudNorm ? mergeDash(dash, cloudNorm) : dash);
|
|
2876
|
+
// 设备 × Agent 矩阵:由 viewDash 的 devices/sources 就地派生(见 buildDeviceMatrix 注释),
|
|
2877
|
+
// 不再依赖宿主 `matrix` 路由 —— 该路由不存在,此前稳定 404 导致矩阵区块永不渲染。
|
|
2878
|
+
// 用派生值而非 state:viewDash / days / view 变化时自动重算,不必再手工触发了拉取。
|
|
2879
|
+
const viewMatrix = view === "local" ? null : buildDeviceMatrix(viewDash);
|
|
2816
2880
|
// 热力图同样跟随三态视图:本机 / 本机+云端 / 仅云端。
|
|
2817
2881
|
// 云端明细取不到时退回本机,并在标题右侧注明口径,避免与上方卡片数字打架。
|
|
2818
2882
|
const viewUsage = view === "local"
|
|
@@ -2863,7 +2927,7 @@ window.__ModuleLoader__.load({
|
|
|
2863
2927
|
sync && sync.enabled && sync.pending > 0 && view !== "local"
|
|
2864
2928
|
? e("div", { className: "cost-cloudnote" },
|
|
2865
2929
|
"本机还有 " + sync.pending + " 条尚未同步,云端数字会偏小 —— ",
|
|
2866
|
-
e("button", { className: "cost-btn", onClick: () => { apiCall("sync-now", {}).then(() => { loadSync(); loadCloud(days, view);
|
|
2930
|
+
e("button", { className: "cost-btn", onClick: () => { apiCall("sync-now", {}).then(() => { loadSync(); loadCloud(days, view); loadCloudUsage(view); }); } }, "立即同步"))
|
|
2867
2931
|
: null,
|
|
2868
2932
|
cloudErr && view !== "local"
|
|
2869
2933
|
? e("div", { className: "cost-err", style: { marginTop: "6px" } }, "云端数据不可用(已显示本机数据):" + cloudErr)
|
|
@@ -2874,7 +2938,7 @@ window.__ModuleLoader__.load({
|
|
|
2874
2938
|
dashErr ? e("div", { className: "cost-err", style: { marginTop: "8px" } }, dashErr) : null,
|
|
2875
2939
|
(!viewDash && !dashErr) ? e("div", { className: "cost-hint", style: { marginTop: "12px" } }, "加载中…") : null,
|
|
2876
2940
|
viewDash ? statCards(viewDash, balance, viewCtl, planTotal) : null,
|
|
2877
|
-
view !== "local" && dimension !== "total" && viewDash ? dimensionPanel(viewDash, dimension,
|
|
2941
|
+
view !== "local" && dimension !== "total" && viewDash ? dimensionPanel(viewDash, dimension, viewMatrix, loadingCloud => loadingCloud) : null,
|
|
2878
2942
|
viewDash ? mainPanel(viewDash, tab, setTab, scheme, setScheme, planTotal) : null,
|
|
2879
2943
|
e("div", { className: "cost-panel" },
|
|
2880
2944
|
e("div", { className: "cost-row" },
|
|
@@ -2932,7 +2996,8 @@ window.__ModuleLoader__.load({
|
|
|
2932
2996
|
e("span", { className: "cost-matrix-num" }, fmtTokens(r.tokens)),
|
|
2933
2997
|
e("span", { className: "cost-matrix-note" }, r.tag || "")))),
|
|
2934
2998
|
matrix && matrix.rows && e("div", { style: { marginTop: "10px" } },
|
|
2935
|
-
e("div", { className: "cost-hint" }, "设备 × Agent 矩阵(行=设备,列=Agent,点击云端「设备 × Agent」页可下钻到记录)"
|
|
2999
|
+
e("div", { className: "cost-hint" }, "设备 × Agent 矩阵(行=设备,列=Agent,点击云端「设备 × Agent」页可下钻到记录)"
|
|
3000
|
+
+ (matrix.unsplit ? " · 多 Agent 设备无法按 Agent 拆分金额,未拆分格子留空(合计列仍为精确总额)" : "")),
|
|
2936
3001
|
e("div", { className: "cost-matrix" },
|
|
2937
3002
|
e("div", { className: "cost-matrix-row cost-matrix-head" },
|
|
2938
3003
|
e("span", { className: "cost-matrix-key" }, "设备"),
|
package/index.js
CHANGED
|
@@ -41,7 +41,7 @@ import { readSettingsSection } from './settings.js'
|
|
|
41
41
|
import { listSessionLogs, planAndBuildImports, loadManifest, IMPORT_SOURCE } from './import.js'
|
|
42
42
|
|
|
43
43
|
/** 插件版本(写入上报信封,便于云端排查版本差异) */
|
|
44
|
-
const PLUGIN_VERSION = '1.11.
|
|
44
|
+
const PLUGIN_VERSION = '1.11.1'
|
|
45
45
|
setPluginVersion(PLUGIN_VERSION)
|
|
46
46
|
|
|
47
47
|
/** 配置面板可写的字段(与 settings 命名空间一致)。
|
|
@@ -1308,11 +1308,30 @@ export default {
|
|
|
1308
1308
|
return out
|
|
1309
1309
|
}
|
|
1310
1310
|
|
|
1311
|
+
/**
|
|
1312
|
+
* 把界面的「天数」换算成查询区间起点(毫秒)。
|
|
1313
|
+
* 本机 buildDashboard 与云端 fetchCloud 必须共用这一份规则,
|
|
1314
|
+
* 否则「本机 + 云端」视图的两半会落在不同区间,卡片数字自相矛盾。
|
|
1315
|
+
*
|
|
1316
|
+
* · days = 1 → 北京时间当日 00:00(「今天」,与「今日费用」卡同口径)
|
|
1317
|
+
* · days > 1 → now - days * 86400000 滚动窗口
|
|
1318
|
+
* · days <= 0 → 0(不限起点,即全部)
|
|
1319
|
+
*/
|
|
1320
|
+
function windowCutoff(days, now) {
|
|
1321
|
+
const n = typeof days === 'number' && isFinite(days) ? Math.max(0, Math.floor(days)) : 7
|
|
1322
|
+
if (n <= 0) return 0
|
|
1323
|
+
if (n === 1) {
|
|
1324
|
+
const k = dayKey(now)
|
|
1325
|
+
return Date.UTC(toInt(k.slice(0, 4)), toInt(k.slice(5, 7)) - 1, toInt(k.slice(8, 10))) - 28800000
|
|
1326
|
+
}
|
|
1327
|
+
return now - n * 86400000
|
|
1328
|
+
}
|
|
1329
|
+
|
|
1311
1330
|
function buildDashboard(args) {
|
|
1312
1331
|
const days = args && typeof args.days === 'number' && isFinite(args.days) ? Math.max(0, Math.floor(args.days)) : 7
|
|
1313
1332
|
const now = Date.now()
|
|
1314
1333
|
const todayKey = dayKey(now)
|
|
1315
|
-
const cutoff = days
|
|
1334
|
+
const cutoff = windowCutoff(days, now)
|
|
1316
1335
|
const filt = []
|
|
1317
1336
|
for (const r of records) if (r.ts >= cutoff) filt.push(r)
|
|
1318
1337
|
// 保留窗口外的日汇总(按天+模型),仅取落在查询范围内的天
|
|
@@ -1968,6 +1987,29 @@ export default {
|
|
|
1968
1987
|
const qs = new URLSearchParams()
|
|
1969
1988
|
qs.set('range', query.range || '7d')
|
|
1970
1989
|
if (query.days) qs.set('days', String(query.days))
|
|
1990
|
+
// 自定义区间:云端 resolveRange() 优先识别 from=… & to=…(可写 from),
|
|
1991
|
+
// 一旦命中就返回 { range:'custom', fromMs, toMs },不受 range 枚举限制。
|
|
1992
|
+
//
|
|
1993
|
+
// 为什么必须走 from/to 而不是 range 枚举:云端只认 today / 7d / 30d / 90d /
|
|
1994
|
+
// month / year / all,**其余值静默降级成 all**(实测 1d、14d 都变全时段)。
|
|
1995
|
+
// 界面上「近 14 天」若只传 range=14d,会拿到全时段累计且没有任何报错 —— 这种
|
|
1996
|
+
// 静默错值比报错更难发现,所以凡是有精确区间的地方一律传 from/to。
|
|
1997
|
+
//
|
|
1998
|
+
// 语义必须与本地 buildDashboard 的 cutoff 保持一致(否则本机 + 云端两半对不齐):
|
|
1999
|
+
// · days=1 → 北京时间当日 00:00 起(「今天」,与「今日费用」卡同口径)
|
|
2000
|
+
// · days>1 → now - days*86400000 滚动窗口
|
|
2001
|
+
// · days<=0 → 不传,交给 range(调用方会传 all)
|
|
2002
|
+
// 调用方显式给了 from 就用 from;否则若给了 days>0,用 windowCutoff 自动推导,
|
|
2003
|
+
// 这样 client 只需继续传 days,不必关心云端的 range 枚举。
|
|
2004
|
+
let fromMs = typeof query.from === 'number' && isFinite(query.from) ? query.from : null
|
|
2005
|
+
if (fromMs === null && typeof query.days === 'number' && isFinite(query.days) && query.days > 0) {
|
|
2006
|
+
fromMs = windowCutoff(query.days, Date.now())
|
|
2007
|
+
}
|
|
2008
|
+
if (fromMs !== null) {
|
|
2009
|
+
qs.set('from', String(Math.max(0, Math.floor(fromMs))))
|
|
2010
|
+
const toMs = typeof query.to === 'number' && isFinite(query.to) ? query.to : Date.now()
|
|
2011
|
+
qs.set('to', String(Math.floor(toMs)))
|
|
2012
|
+
}
|
|
1971
2013
|
if (query.groupBy) qs.set('groupBy', query.groupBy)
|
|
1972
2014
|
if (query.bucket) qs.set('bucket', query.bucket)
|
|
1973
2015
|
if (Array.isArray(query.sources) && query.sources.length) qs.set('sources', query.sources.join(','))
|
|
@@ -2153,7 +2195,16 @@ export default {
|
|
|
2153
2195
|
if (rest.cloudEnabled === true) scheduleSync(500)
|
|
2154
2196
|
return Object.assign({ ok: true }, syncEngine.status())
|
|
2155
2197
|
},
|
|
2156
|
-
|
|
2198
|
+
// range 默认值只在调用方两者都没给时才兜底成 7d:
|
|
2199
|
+
// 调用方给了 days>0 → fetchCloud 会用 windowCutoff 推导 from/to(精确区间,优先);
|
|
2200
|
+
// 调用方给了 days=0 → 这里传 all。
|
|
2201
|
+
// 此前无条件写死 range:'7d',导致客户端只传 days 时云端永远按近 7 天算,
|
|
2202
|
+
// 界面切「近 30 天 / 全部」云端那一半却纹丝不动。
|
|
2203
|
+
cloud: (args) => {
|
|
2204
|
+
const a = args || {}
|
|
2205
|
+
const range = a.range || (a.days === 0 ? 'all' : '7d')
|
|
2206
|
+
return fetchCloud(Object.assign({ route: 'overview' }, a, { range }))
|
|
2207
|
+
},
|
|
2157
2208
|
}
|
|
2158
2209
|
|
|
2159
2210
|
async function handleRoute(req, res) {
|
|
@@ -2392,6 +2443,8 @@ export default {
|
|
|
2392
2443
|
}
|
|
2393
2444
|
if (scope === 'cloud' || scope === 'both') {
|
|
2394
2445
|
const c = await fetchCloud({ route: 'overview', range: days === 0 ? 'all' : '7d', days })
|
|
2446
|
+
// 注:days>0 时 fetchCloud 会用 windowCutoff 推导 from/to,range 只作兜底展示;
|
|
2447
|
+
// days===0 时走 range=all。两条路都与本地 buildDashboard 的窗口语义一致。
|
|
2395
2448
|
// 云端响应有两种形状,必须都认:
|
|
2396
2449
|
// · /api/v1/overview → 金额摊在 c.summary.{realCost,subEquivalent,...}
|
|
2397
2450
|
// · /api/v1/plugin-view → 没有 summary,字段直接摊在顶层(宿主在云端支持
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@angelyeye/dsh-cost-tracker",
|
|
3
|
-
"version": "1.11.
|
|
3
|
+
"version": "1.11.1",
|
|
4
4
|
"description": "DeepSeek Harness (DSH) plugin: LLM token usage & cost tracking with a settings dashboard, agent tools, HTTP API, persistent storage, multi-device cloud sync, session-log history import, official price sync and a multi-vendor price catalog.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"./package.json": "./package.json"
|
|
38
38
|
},
|
|
39
39
|
"scripts": {
|
|
40
|
-
"test": "node test/storage.test.js && node test/pricing.test.js && node test/peak-holidays.test.js && node test/volcengine-plan.test.js && node test/volcengine-host.test.js && node test/config.test.js && node test/recompute.test.js && node test/credstore.test.js && node test/vendor-catalog.test.js && node test/price-sync.test.js && node test/import.test.js && node test/balance-account.test.js && node test/session-log-v4.test.js && node test/settings-compat.test.js && node test/client-registration.test.js && node test/ui-display.test.js && node test/client-render.test.js && node test/cloud-read.test.js && node --no-warnings test/cloud-view-e2e.test.js && node test/sync.test.js && node test/view.test.js && node test/release-notes.test.js"
|
|
40
|
+
"test": "node test/storage.test.js && node test/pricing.test.js && node test/peak-holidays.test.js && node test/volcengine-plan.test.js && node test/volcengine-host.test.js && node test/config.test.js && node test/recompute.test.js && node test/credstore.test.js && node test/vendor-catalog.test.js && node test/price-sync.test.js && node test/import.test.js && node test/balance-account.test.js && node test/session-log-v4.test.js && node test/settings-compat.test.js && node test/client-registration.test.js && node test/ui-display.test.js && node test/client-render.test.js && node test/cloud-read.test.js && node --no-warnings test/cloud-view-e2e.test.js && node test/sync.test.js && node test/view.test.js && node test/time-range.test.js && node test/release-notes.test.js"
|
|
41
41
|
},
|
|
42
42
|
"keywords": [
|
|
43
43
|
"dsh-plugin",
|