From 4b7a7496ff33e34bf3652b18d2b07ea7ecf20e41 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Sun, 9 Nov 2025 07:17:40 +0000 Subject: [PATCH 1/2] Feat: stats sites and functions runtimes and frameworks - Sites frameworks count - Functions runtimes count --- app/init/constants.php | 2 ++ .../Platform/Workers/StatsResources.php | 30 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/app/init/constants.php b/app/init/constants.php index afed64f798..3b81785690 100644 --- a/app/init/constants.php +++ b/app/init/constants.php @@ -271,6 +271,8 @@ const METRIC_SITES_ID_REQUESTS = 'sites.{siteInternalId}.requests'; const METRIC_SITES_ID_INBOUND = 'sites.{siteInternalId}.inbound'; const METRIC_SITES_ID_OUTBOUND = 'sites.{siteInternalId}.outbound'; const METRIC_AVATARS_SCREENSHOTS_GENERATED = 'avatars.screenshotsGenerated'; +const METRIC_FUNCTIONS_RUNTIME = 'functions.runtimes.{runtime}'; +const METRIC_SITES_FRAMEWORK = 'sites.frameworks.{framework}'; // Resource types const RESOURCE_TYPE_PROJECTS = 'projects'; diff --git a/src/Appwrite/Platform/Workers/StatsResources.php b/src/Appwrite/Platform/Workers/StatsResources.php index 5ec306c5bb..d4a4b4a56c 100644 --- a/src/Appwrite/Platform/Workers/StatsResources.php +++ b/src/Appwrite/Platform/Workers/StatsResources.php @@ -335,6 +335,10 @@ class StatsResources extends Action $this->createStatsDocuments($region, str_replace("{resourceType}", RESOURCE_TYPE_FUNCTIONS, METRIC_RESOURCE_TYPE_DEPLOYMENTS), $deployments); $this->createStatsDocuments($region, str_replace("{resourceType}", RESOURCE_TYPE_FUNCTIONS, METRIC_RESOURCE_TYPE_BUILDS), $deployments); + + // count runtimes + $runtimes = []; + $this->foreachDocument($dbForProject, 'functions', [], function (Document $function) use ($dbForProject, $region) { $functionDeploymentsStorage = $dbForProject->sum('deployments', 'sourceSize', [ Query::equal('resourceInternalId', [$function->getSequence()]), @@ -364,7 +368,19 @@ class StatsResources extends Action }); $this->createStatsDocuments($region, str_replace(['{resourceType}','{resourceInternalId}'], [RESOURCE_TYPE_FUNCTIONS,$function->getSequence()], METRIC_RESOURCE_TYPE_ID_BUILDS_STORAGE), $functionBuildsStorage); + + // Runtimes count + $runtime = $function->getAttribute('runtime'); + if (!empty($runtime)) { + $runtimes[$runtime] = ($runtimes[$runtime] ?? 0) + 1; + } }); + + // Write runtimes counts + foreach ($runtimes as $runtime => $count) { + $this->createStatsDocuments($region, str_replace('{runtime}', $runtime, METRIC_FUNCTIONS_RUNTIME), $count); + } + } protected function countForSites(Database $dbForProject, string $region) @@ -385,6 +401,9 @@ class StatsResources extends Action $this->createStatsDocuments($region, str_replace("{resourceType}", RESOURCE_TYPE_SITES, METRIC_RESOURCE_TYPE_DEPLOYMENTS), $deployments); $this->createStatsDocuments($region, str_replace("{resourceType}", RESOURCE_TYPE_SITES, METRIC_RESOURCE_TYPE_BUILDS), $deployments); + // Count frameworks + $frameworks = []; + $this->foreachDocument($dbForProject, 'sites', [], function (Document $site) use ($dbForProject, $region) { $siteDeploymentsStorage = $dbForProject->sum('deployments', 'sourceSize', [ Query::equal('resourceInternalId', [$site->getSequence()]), @@ -410,7 +429,18 @@ class StatsResources extends Action ]); $this->createStatsDocuments($region, str_replace(['{resourceType}','{resourceInternalId}'], [RESOURCE_TYPE_SITES,$site->getSequence()], METRIC_RESOURCE_TYPE_ID_BUILDS_STORAGE), $siteBuildsStorage); + + // Frameworks count + $framework = $site->getAttribute('framework'); + if (!empty($framework)) { + $frameworks[$framework] = ($frameworks[$framework] ?? 0) + 1; + } }); + + // Write frameworks counts + foreach ($frameworks as $framework => $count) { + $this->createStatsDocuments($region, str_replace('{framework}', $framework, METRIC_SITES_FRAMEWORK), $count); + } } protected function createStatsDocuments(string $region, string $metric, int $value) From 4fd4af312d7024e85e0b46ec7df5daaf8778bbf8 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Sun, 9 Nov 2025 08:18:37 +0000 Subject: [PATCH 2/2] Fix array wrong reference --- src/Appwrite/Platform/Workers/StatsResources.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Appwrite/Platform/Workers/StatsResources.php b/src/Appwrite/Platform/Workers/StatsResources.php index d4a4b4a56c..1ef348091a 100644 --- a/src/Appwrite/Platform/Workers/StatsResources.php +++ b/src/Appwrite/Platform/Workers/StatsResources.php @@ -336,10 +336,10 @@ class StatsResources extends Action $this->createStatsDocuments($region, str_replace("{resourceType}", RESOURCE_TYPE_FUNCTIONS, METRIC_RESOURCE_TYPE_BUILDS), $deployments); - // count runtimes + // Count runtimes $runtimes = []; - $this->foreachDocument($dbForProject, 'functions', [], function (Document $function) use ($dbForProject, $region) { + $this->foreachDocument($dbForProject, 'functions', [], function (Document $function) use ($dbForProject, $region, &$runtimes) { $functionDeploymentsStorage = $dbForProject->sum('deployments', 'sourceSize', [ Query::equal('resourceInternalId', [$function->getSequence()]), Query::equal('resourceType', [RESOURCE_TYPE_FUNCTIONS]), @@ -404,7 +404,7 @@ class StatsResources extends Action // Count frameworks $frameworks = []; - $this->foreachDocument($dbForProject, 'sites', [], function (Document $site) use ($dbForProject, $region) { + $this->foreachDocument($dbForProject, 'sites', [], function (Document $site) use ($dbForProject, $region, &$frameworks) { $siteDeploymentsStorage = $dbForProject->sum('deployments', 'sourceSize', [ Query::equal('resourceInternalId', [$site->getSequence()]), Query::equal('resourceType', [RESOURCE_TYPE_SITES]),