Replace broadcast backfill with per-entry re-fetch for running_balance

This commit is contained in:
Keith Solomon
2026-08-07 17:39:22 -05:00
parent 5b8abd8bbc
commit 63c6cc6972
+30 -24
View File
@@ -290,21 +290,24 @@ function consoleLog( $data ) {
} }
/** /**
* Backfill the running_balance column for any user rows that have NULL. * Re-fetch the running_balance for each entry of a user by walking the
* v2 /log paginated endpoint.
* *
* The v2 API's `data.balance` field is the vault balance after each entry. * For each entry the API returns, we run a single UPDATE setting that
* If the column is missing for a row, treating it as NULL means * entry's `running_balance` to its `data.balance`. The pre-existing
* MAX(running_balance) ignores it. This routine fetches the latest entry's * broadcast approach gave every row of a user the same value, which
* balance from the API and broadcasts it to all NULL rows of the user, * made the historical view misleading; this restores per-entry accuracy.
* so the displayed balance becomes accurate after the first sync following *
* the schema upgrade. * If the user has zero rows with `running_balance IS NULL`, returns
* without making any HTTP call. On API failure, the underlying
* exception propagates to the caller's try/catch in index.php.
* *
* @param PDO $pdo Database connection. * @param PDO $pdo Database connection.
* @param string $user The user whose NULL rows should be backfilled. * @param string $user The user whose NULL rows should be refilled.
* *
* @return void * @return void
*/ */
function backfillRunningBalances($pdo, $user) { function refetchRunningBalances($pdo, $user) {
if (!array_key_exists($user, USER_KEYS)) { if (!array_key_exists($user, USER_KEYS)) {
throw new ApiKeyMissingException("User does not have an API key configured."); throw new ApiKeyMissingException("User does not have an API key configured.");
} }
@@ -317,25 +320,28 @@ function backfillRunningBalances($pdo, $user) {
return; return;
} }
$url = 'https://api.torn.com/v2/user?selections=log&log=5850,5851&limit=1&sort=DESC'; $updateStmt = $pdo->prepare(
$responseData = executeApiCall($url, USER_KEYS[$user]); 'UPDATE vault SET running_balance = :balance WHERE id = :id'
validateApiResponse($responseData); );
if (empty($responseData['log'])) { $url = 'https://api.torn.com/v2/user?selections=log&log=5850,5851';
return; do {
} $responseData = executeApiCall($url, USER_KEYS[$user]);
validateApiResponse($responseData);
$latest = $responseData['log'][0]; foreach ($responseData['log'] as $entry) {
if (!isset($latest['data']['balance'])) { $id = $entry['id'] ?? null;
return; if (!$id || !isset($entry['data']['balance'])) {
} continue;
}
$balance = (int)$latest['data']['balance']; $updateStmt->bindValue(':balance', (int)$entry['data']['balance']);
$updateStmt->bindValue(':id', $id);
$updateStmt->execute();
}
$updateStmt = $pdo->prepare('UPDATE vault SET running_balance = :balance WHERE user = :user AND running_balance IS NULL'); $url = $responseData['_metadata']['links']['next'] ?? null;
$updateStmt->bindValue(':balance', $balance); } while ($url !== null);
$updateStmt->bindValue(':user', $user);
$updateStmt->execute();
} }
/** /**