Skip to content

Commit 4b7a749

Browse files
committed
Feat: stats sites and functions runtimes and frameworks
- Sites frameworks count - Functions runtimes count
1 parent 4a96d89 commit 4b7a749

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

app/init/constants.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@
271271
const METRIC_SITES_ID_INBOUND = 'sites.{siteInternalId}.inbound';
272272
const METRIC_SITES_ID_OUTBOUND = 'sites.{siteInternalId}.outbound';
273273
const METRIC_AVATARS_SCREENSHOTS_GENERATED = 'avatars.screenshotsGenerated';
274+
const METRIC_FUNCTIONS_RUNTIME = 'functions.runtimes.{runtime}';
275+
const METRIC_SITES_FRAMEWORK = 'sites.frameworks.{framework}';
274276

275277
// Resource types
276278
const RESOURCE_TYPE_PROJECTS = 'projects';

src/Appwrite/Platform/Workers/StatsResources.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,10 @@ protected function countForFunctions(Database $dbForProject, string $region)
335335
$this->createStatsDocuments($region, str_replace("{resourceType}", RESOURCE_TYPE_FUNCTIONS, METRIC_RESOURCE_TYPE_DEPLOYMENTS), $deployments);
336336
$this->createStatsDocuments($region, str_replace("{resourceType}", RESOURCE_TYPE_FUNCTIONS, METRIC_RESOURCE_TYPE_BUILDS), $deployments);
337337

338+
339+
// count runtimes
340+
$runtimes = [];
341+
338342
$this->foreachDocument($dbForProject, 'functions', [], function (Document $function) use ($dbForProject, $region) {
339343
$functionDeploymentsStorage = $dbForProject->sum('deployments', 'sourceSize', [
340344
Query::equal('resourceInternalId', [$function->getSequence()]),
@@ -364,7 +368,19 @@ protected function countForFunctions(Database $dbForProject, string $region)
364368
});
365369

366370
$this->createStatsDocuments($region, str_replace(['{resourceType}','{resourceInternalId}'], [RESOURCE_TYPE_FUNCTIONS,$function->getSequence()], METRIC_RESOURCE_TYPE_ID_BUILDS_STORAGE), $functionBuildsStorage);
371+
372+
// Runtimes count
373+
$runtime = $function->getAttribute('runtime');
374+
if (!empty($runtime)) {
375+
$runtimes[$runtime] = ($runtimes[$runtime] ?? 0) + 1;
376+
}
367377
});
378+
379+
// Write runtimes counts
380+
foreach ($runtimes as $runtime => $count) {
381+
$this->createStatsDocuments($region, str_replace('{runtime}', $runtime, METRIC_FUNCTIONS_RUNTIME), $count);
382+
}
383+
368384
}
369385

370386
protected function countForSites(Database $dbForProject, string $region)
@@ -385,6 +401,9 @@ protected function countForSites(Database $dbForProject, string $region)
385401
$this->createStatsDocuments($region, str_replace("{resourceType}", RESOURCE_TYPE_SITES, METRIC_RESOURCE_TYPE_DEPLOYMENTS), $deployments);
386402
$this->createStatsDocuments($region, str_replace("{resourceType}", RESOURCE_TYPE_SITES, METRIC_RESOURCE_TYPE_BUILDS), $deployments);
387403

404+
// Count frameworks
405+
$frameworks = [];
406+
388407
$this->foreachDocument($dbForProject, 'sites', [], function (Document $site) use ($dbForProject, $region) {
389408
$siteDeploymentsStorage = $dbForProject->sum('deployments', 'sourceSize', [
390409
Query::equal('resourceInternalId', [$site->getSequence()]),
@@ -410,7 +429,18 @@ protected function countForSites(Database $dbForProject, string $region)
410429
]);
411430

412431
$this->createStatsDocuments($region, str_replace(['{resourceType}','{resourceInternalId}'], [RESOURCE_TYPE_SITES,$site->getSequence()], METRIC_RESOURCE_TYPE_ID_BUILDS_STORAGE), $siteBuildsStorage);
432+
433+
// Frameworks count
434+
$framework = $site->getAttribute('framework');
435+
if (!empty($framework)) {
436+
$frameworks[$framework] = ($frameworks[$framework] ?? 0) + 1;
437+
}
413438
});
439+
440+
// Write frameworks counts
441+
foreach ($frameworks as $framework => $count) {
442+
$this->createStatsDocuments($region, str_replace('{framework}', $framework, METRIC_SITES_FRAMEWORK), $count);
443+
}
414444
}
415445

416446
protected function createStatsDocuments(string $region, string $metric, int $value)

0 commit comments

Comments
 (0)