Revert vaultLoop to per-user SQL MAX(running_balance)

This commit is contained in:
Keith Solomon
2026-08-07 17:34:37 -05:00
parent 3c798cee88
commit 5b8abd8bbc
+10 -27
View File
@@ -205,39 +205,22 @@ function fetchVaultRecords($user = null) {
* @return int The total balance of the given user (or all users if no user is given). * @return int The total balance of the given user (or all users if no user is given).
*/ */
function vaultLoop ($name=null) { function vaultLoop ($name=null) {
if ($name !== null) { $pdo = getDatabaseConnection();
// Per-user path: prefer live /money balance, fall back to MAX(running_balance).
$liveBalance = fetchLiveVaultBalance($name);
if ($liveBalance !== null) {
return $liveBalance;
}
$pdo = getDatabaseConnection(); if ($name === null) {
$stmt = $pdo->prepare('SELECT MAX(running_balance) FROM vault WHERE user = :user'); $stmt = $pdo->query(
$stmt->bindValue(':user', $name); 'SELECT COALESCE(SUM(max_balance), 0) FROM '
$stmt->execute(); . '(SELECT MAX(running_balance) AS max_balance FROM vault GROUP BY user)'
);
return (int)$stmt->fetchColumn(); return (int)$stmt->fetchColumn();
} }
// All-users path: sum the live /money value for each user. For any user $stmt = $pdo->prepare('SELECT MAX(running_balance) FROM vault WHERE user = :user');
// whose /money call failed, fall back to MAX(running_balance) for that user. $stmt->bindValue(':user', $name);
$pdo = getDatabaseConnection(); $stmt->execute();
$sum = 0;
foreach (USER_KEYS as $user => $apiKey) {
$live = fetchLiveVaultBalance($user);
if ($live !== null) {
$sum += $live;
continue;
}
$stmt = $pdo->prepare('SELECT MAX(running_balance) FROM vault WHERE user = :user'); return (int)$stmt->fetchColumn();
$stmt->bindValue(':user', $user);
$stmt->execute();
$sum += (int)$stmt->fetchColumn();
}
return $sum;
} }
/** /**