From 1397bfdc76681a9b91c0ef3b584cb3d695c2de4d Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Wed, 5 Mar 2025 06:22:16 +0000 Subject: [PATCH] Feat-batch writes in stats usage dump --- .../Platform/Workers/StatsUsageDump.php | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/Appwrite/Platform/Workers/StatsUsageDump.php b/src/Appwrite/Platform/Workers/StatsUsageDump.php index 10b8900b92..9aa551ab96 100644 --- a/src/Appwrite/Platform/Workers/StatsUsageDump.php +++ b/src/Appwrite/Platform/Workers/StatsUsageDump.php @@ -79,6 +79,8 @@ class StatsUsageDump extends Action try { /** @var \Utopia\Database\Database $dbForProject */ $dbForProject = $getProjectDB($project); + $documents = []; + $documentsClone = []; foreach ($stats['keys'] ?? [] as $key => $value) { if ($value == 0) { continue; @@ -100,25 +102,33 @@ class StatsUsageDump extends Action 'value' => $value, 'region' => System::getEnv('_APP_REGION', 'default'), ]); + $documents[] = $document; $documentClone = new Document($document->getArrayCopy()); - - $dbForProject->createOrUpdateDocumentsWithIncrease( - 'stats', - 'value', - [$document] - ); - - $this->writeToLogsDB($project, $documentClone); + $documentsClone[] = $documentClone; } } + + $dbForProject->createOrUpdateDocumentsWithIncrease( + 'stats', + 'value', + $documents + ); + $this->writeToLogsDB($project, $documentsClone); } catch (\Exception $e) { console::error('[' . DateTime::now() . '] project [' . $project->getInternalId() . '] database [' . $project['database'] . '] ' . ' ' . $e->getMessage()); } } } - protected function writeToLogsDB(Document $project, Document $document): void + /** + * Write to logs DB + * + * @param Document $project + * @param array $documents + * @return void + */ + protected function writeToLogsDB(Document $project, array $documents): void { if (System::getEnv('_APP_STATS_USAGE_DUAL_WRITING', 'disabled') === 'disabled') { Console::log('Dual Writing is disabled. Skipping...'); @@ -132,7 +142,7 @@ class StatsUsageDump extends Action $dbForLogs->createOrUpdateDocumentsWithIncrease( 'stats', 'value', - [$document] + $documents ); Console::success('Usage logs pushed to Logs DB'); } catch (\Throwable $th) {