From de9e177bc84cb5ddc1a31fb10ede3d5260161e6d Mon Sep 17 00:00:00 2001 From: fogelito Date: Wed, 10 Sep 2025 17:23:33 +0300 Subject: [PATCH] sort and hardcode bachSize --- .../Platform/Workers/StatsResources.php | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/Appwrite/Platform/Workers/StatsResources.php b/src/Appwrite/Platform/Workers/StatsResources.php index 655ebfbd52..d26f4c06f3 100644 --- a/src/Appwrite/Platform/Workers/StatsResources.php +++ b/src/Appwrite/Platform/Workers/StatsResources.php @@ -206,8 +206,6 @@ class StatsResources extends Action $this->writeDocuments($dbForLogs, $project); } catch (Throwable $th) { call_user_func_array($this->logError, [$th, "StatsResources", "count_for_project_{$project->getId()}"]); - } finally { - $this->documents = []; } Console::info('End of count for: ' . $project->getId()); @@ -436,18 +434,32 @@ class StatsResources extends Action { $message = 'Stats writeDocuments project: ' . $project->getId() . '(' . $project->getSequence() . ')'; + // sort by unique index key to make + usort($this->documents, function($a, $b) { + // metric DESC + $cmp = strcmp($b['metric'], $a['metric']); + if ($cmp !== 0) return $cmp; + + // period ASC + $cmp = strcmp($a['period'], $b['period']); + if ($cmp !== 0) return $cmp; + + // time ASC + if ($a['time'] === $b['time']) return 0; + return ($a['time'] < $b['time']) ? -1 : 1; + }); + try { $dbForLogs->createOrUpdateDocuments( 'stats', - $this->documents + $this->documents, + 10 // See if this make an effect ); Console::success($message . ' | Documents: ' . count($this->documents)); } catch (\Throwable $e) { Console::error('Error: ' . $message . ' | Exception: ' . $e->getMessage()); throw $e; - } finally { - $this->documents = []; } } }