Replace broadcast backfill with per-entry re-fetch for running_balance
This commit is contained in:
+28
-22
@@ -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.
|
||||
* If the column is missing for a row, treating it as NULL means
|
||||
* MAX(running_balance) ignores it. This routine fetches the latest entry's
|
||||
* balance from the API and broadcasts it to all NULL rows of the user,
|
||||
* so the displayed balance becomes accurate after the first sync following
|
||||
* the schema upgrade.
|
||||
* For each entry the API returns, we run a single UPDATE setting that
|
||||
* entry's `running_balance` to its `data.balance`. The pre-existing
|
||||
* broadcast approach gave every row of a user the same value, which
|
||||
* made the historical view misleading; this restores per-entry accuracy.
|
||||
*
|
||||
* 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 string $user The user whose NULL rows should be backfilled.
|
||||
* @param string $user The user whose NULL rows should be refilled.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function backfillRunningBalances($pdo, $user) {
|
||||
function refetchRunningBalances($pdo, $user) {
|
||||
if (!array_key_exists($user, USER_KEYS)) {
|
||||
throw new ApiKeyMissingException("User does not have an API key configured.");
|
||||
}
|
||||
@@ -317,25 +320,28 @@ function backfillRunningBalances($pdo, $user) {
|
||||
return;
|
||||
}
|
||||
|
||||
$url = 'https://api.torn.com/v2/user?selections=log&log=5850,5851&limit=1&sort=DESC';
|
||||
$updateStmt = $pdo->prepare(
|
||||
'UPDATE vault SET running_balance = :balance WHERE id = :id'
|
||||
);
|
||||
|
||||
$url = 'https://api.torn.com/v2/user?selections=log&log=5850,5851';
|
||||
do {
|
||||
$responseData = executeApiCall($url, USER_KEYS[$user]);
|
||||
validateApiResponse($responseData);
|
||||
|
||||
if (empty($responseData['log'])) {
|
||||
return;
|
||||
foreach ($responseData['log'] as $entry) {
|
||||
$id = $entry['id'] ?? null;
|
||||
if (!$id || !isset($entry['data']['balance'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$latest = $responseData['log'][0];
|
||||
if (!isset($latest['data']['balance'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$balance = (int)$latest['data']['balance'];
|
||||
|
||||
$updateStmt = $pdo->prepare('UPDATE vault SET running_balance = :balance WHERE user = :user AND running_balance IS NULL');
|
||||
$updateStmt->bindValue(':balance', $balance);
|
||||
$updateStmt->bindValue(':user', $user);
|
||||
$updateStmt->bindValue(':balance', (int)$entry['data']['balance']);
|
||||
$updateStmt->bindValue(':id', $id);
|
||||
$updateStmt->execute();
|
||||
}
|
||||
|
||||
$url = $responseData['_metadata']['links']['next'] ?? null;
|
||||
} while ($url !== null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user