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.
|
* 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,27 +320,30 @@ 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(
|
||||||
|
'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]);
|
$responseData = executeApiCall($url, USER_KEYS[$user]);
|
||||||
validateApiResponse($responseData);
|
validateApiResponse($responseData);
|
||||||
|
|
||||||
if (empty($responseData['log'])) {
|
foreach ($responseData['log'] as $entry) {
|
||||||
return;
|
$id = $entry['id'] ?? null;
|
||||||
|
if (!$id || !isset($entry['data']['balance'])) {
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$latest = $responseData['log'][0];
|
$updateStmt->bindValue(':balance', (int)$entry['data']['balance']);
|
||||||
if (!isset($latest['data']['balance'])) {
|
$updateStmt->bindValue(':id', $id);
|
||||||
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->execute();
|
$updateStmt->execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$url = $responseData['_metadata']['links']['next'] ?? null;
|
||||||
|
} while ($url !== null);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetch the live vault balance for a user from the v2 /money endpoint.
|
* Fetch the live vault balance for a user from the v2 /money endpoint.
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user