dsh-whale-widget 0.3.16 → 0.3.18
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 +12 -2
- package/assets/whale-widget.js +508 -67
- package/lib/accounting.mjs +39 -2
- package/lib/index.js +286 -41
- package/package.json +1 -1
package/lib/accounting.mjs
CHANGED
|
@@ -29,6 +29,41 @@ export function dayOffset(day, offset) {
|
|
|
29
29
|
return beijingDay(Date.parse(day + 'T00:00:00+08:00') + offset * 86400000)
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
// v789(issue #184):partialDay 改为**按实际观测缺口就地计算**,不再是硬编码 true。
|
|
33
|
+
// 为什么要有这个:记账只发生在「客户端界面开着 + 余额能取到」的时候,任一条不成立就完全不观测;
|
|
34
|
+
// 跨零点的那一段(新建当日行时 openingUnits 直接取"此刻余额")会被新当天的起点吞掉,
|
|
35
|
+
// 于是「今日已用」长期显示一个明显偏小的数字,而界面上**没有任何"这是部分数据"的提示**。
|
|
36
|
+
// 判据(issue 里给出的算法):观测起点距当日 00:00、或(对已经过去的日子)最后一次观测距次日 00:00,
|
|
37
|
+
// 超过 GAP_MS 就视为部分数据。正常运行时两个缺口都只有几分钟 ⇒ false,不会误伤正常日。
|
|
38
|
+
const PARTIAL_GAP_MS = 10 * 60 * 1000
|
|
39
|
+
function partialDayOf(day, row, now) {
|
|
40
|
+
try {
|
|
41
|
+
const start = Date.parse(String(day) + 'T00:00:00+08:00')
|
|
42
|
+
if (!Number.isFinite(start)) return true
|
|
43
|
+
const end = start + 86400000
|
|
44
|
+
const first = Number(row && row.firstAt)
|
|
45
|
+
const last = Number(row && row.lastAt)
|
|
46
|
+
if (!Number.isFinite(first) || !Number.isFinite(last)) return true
|
|
47
|
+
const beforeGap = first - start
|
|
48
|
+
const afterGap = Number(now) >= end ? (end - last) : 0
|
|
49
|
+
return beforeGap > PARTIAL_GAP_MS || afterGap > PARTIAL_GAP_MS
|
|
50
|
+
} catch (err) { return true }
|
|
51
|
+
}
|
|
52
|
+
export function partialDayInfo(day, row, now = Date.now()) {
|
|
53
|
+
const start = Date.parse(String(day) + 'T00:00:00+08:00')
|
|
54
|
+
const partialDay = partialDayOf(day, row, now)
|
|
55
|
+
const first = Number(row && row.firstAt)
|
|
56
|
+
const last = Number(row && row.lastAt)
|
|
57
|
+
return {
|
|
58
|
+
partialDay,
|
|
59
|
+
// 给界面用:从几点才开始有观测(缺口在起点)/ 那天几点之后就没再观测(缺口在末尾)
|
|
60
|
+
observedFromMs: Number.isFinite(first) ? first : null,
|
|
61
|
+
observedToMs: Number.isFinite(last) ? last : null,
|
|
62
|
+
leadingGapMs: Number.isFinite(first) && Number.isFinite(start) ? Math.max(0, first - start) : null,
|
|
63
|
+
trailingGapMs: Number.isFinite(last) && Number.isFinite(start) && now >= start + 86400000 ? Math.max(0, start + 86400000 - last) : 0,
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
32
67
|
function currentBook(ledger) {
|
|
33
68
|
const a = ledger.accounting
|
|
34
69
|
return a && a.version === ACCOUNTING_VERSION && a.books && a.books[a.active]
|
|
@@ -98,7 +133,8 @@ export function balanceSummary(ledger, day = beijingDay()) {
|
|
|
98
133
|
firstObservedAt: row.firstAt, lastObservedAt: row.lastAt,
|
|
99
134
|
openingBalance: row.openingUnits / SCALE, currentBalance: row.lastUnits / SCALE,
|
|
100
135
|
observedDecrease: row.debitUnits / SCALE, observedIncrease: row.creditUnits / SCALE,
|
|
101
|
-
needsReview,
|
|
136
|
+
needsReview, revision: revisionOf(ledger, row),
|
|
137
|
+
...partialDayInfo(day, row),
|
|
102
138
|
credits: correction ? correction.creditsUnits / SCALE : null,
|
|
103
139
|
otherDebits: correction ? correction.otherDebitsUnits / SCALE : null,
|
|
104
140
|
correctedAt: correction ? correction.at : null,
|
|
@@ -237,7 +273,8 @@ export function daySummary(ledger, day) {
|
|
|
237
273
|
day, amount: fromOther.amount, currency: fromOther.currency,
|
|
238
274
|
source: 'balance-observed-other-account', label: '已观测消费 · 历史账户',
|
|
239
275
|
firstObservedAt: fromOther.firstObservedAt, lastObservedAt: fromOther.lastObservedAt,
|
|
240
|
-
needsReview: fromOther.needsReview,
|
|
276
|
+
needsReview: fromOther.needsReview, ...common,
|
|
277
|
+
...partialDayInfo(day, { firstAt: fromOther.firstObservedAt, lastAt: fromOther.lastObservedAt }),
|
|
241
278
|
}
|
|
242
279
|
}
|
|
243
280
|
// An explicit historical total (even zero) wins over a conflicting estimate.
|