Files
dsh-infstarapi/src/totals.js
T
大阔andClaude Opus 5.5 f4f4ce55b3 fix: review fixes, GitHub Releases update check (0.1.1)
- Wallet/subscription/window exhaustion skips a key for 10 minutes instead
  of disabling it; a balance read showing money brings it back. Only the
  gateway's own key refusals and a used-up key cap still disable a key.
- Classify by sub2api's exact wording; a bare 401 or upstream text no
  longer retires a key; 'No available accounts' is capacity, not coverage.
- Hosts keep only an https origin (no doubled /v1); timeouts and HTTP
  answers no longer set a host aside; hand edits are re-probed.
- Balance: inactive subscriptions, per-plan subscription pooling, key caps
  kept out of the total, rate windows shown.
- Endpoint editor no longer empties itself after Save.
- Update check reads GitHub Releases and installs the release tarball;
  release workflow replaces npm publishing.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-15 01:15:24 +08:00

68 lines
2.5 KiB
JavaScript

/**
* Totals across the keys a balance read returned.
*
* In sub2api money belongs to the account, not the key:
* - every wallet key of one account spends the same wallet, counted once —
* the same wallet when the gateway names the same account (`accountRef`)
* or, when it names none, when the balances agree to the micro-dollar;
* - a subscription belongs to the user and its group, so keys on the same
* plan count it once;
* - a key quota is a cap on top of the wallet, not money of its own, so it
* is left out of the sums and reported as `capped`.
*
* @module dsh-geek2api/totals
*/
const amount = value => (typeof value === 'number' && Number.isFinite(value) ? value : undefined)
/** Which pool of money a row draws on; rows with the same pool count once. */
function poolOf(row, usage) {
if (usage.mode === 'subscription') return `subscription:${usage.planName ?? row?.id}`
if (usage.mode !== 'wallet') return `key:${row?.id}`
if (typeof usage.accountRef === 'string') return `account:${usage.accountRef}`
const remaining = amount(usage.availableUsd)
return remaining === undefined ? `key:${row?.id}` : `wallet:${remaining.toFixed(6)}`
}
/**
* Add up the balance rows the host returned.
* @param rows - the `keys` array of a balance projection.
* @returns summed amounts plus the conditions that make a sum incomplete:
* keys whose read failed, keys with no limit at all, rows folded into a
* pool already counted, and key caps left out of the sum.
*/
export function sumBalances(rows) {
const total = { keys: 0, counted: 0, unread: 0, unlimited: 0, shared: 0, capped: 0, remainingUsd: 0, usedUsd: 0, limitUsd: 0 }
const seen = new Set()
for (const row of Array.isArray(rows) ? rows : []) {
total.keys += 1
const usage = row?.usage
if (usage === undefined) {
total.unread += 1
continue
}
total.counted += 1
if (usage.mode === 'quota') {
total.capped += 1
continue
}
const pool = poolOf(row, usage)
if (seen.has(pool)) {
total.shared += 1
continue
}
seen.add(pool)
if (usage.unlimitedQuota === true) {
total.unlimited += 1
continue
}
const remaining = amount(usage.availableUsd)
const used = amount(usage.usedUsd)
const limit = amount(usage.limitUsd)
if (remaining !== undefined) total.remainingUsd += remaining
if (used !== undefined) total.usedUsd += used
if (limit !== undefined) total.limitUsd += limit
}
return total
}