From df5b1287b8d6ac969e8849d9be954252f1f7d3ef Mon Sep 17 00:00:00 2001 From: shimon Date: Mon, 6 May 2024 08:54:51 +0300 Subject: [PATCH 01/14] adding new usage metric --- CONTRIBUTING.md | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 529f6103d1..f934048d7a 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -301,6 +301,57 @@ This will allow the Appwrite community to sufficiently discuss the new feature v This is also important for the Appwrite lead developers to be able to provide technical input and potentially a different emphasis regarding the feature design and architecture. Some bigger features might need to go through our [RFC process](https://github.com/appwrite/rfc). +## Adding new usage metrics + +metrics are collected to 3 scopes : +Daily, monthly, an infinity. +Adding new usage metrics in order to aggregate usage stats is very simple but very much depends on where do you want to collect. +the statistics( via API or via background worker) +Here are the steps needs to be taken in both cases: + +For both cases you need to add a const variable in app/init.php under the usage metrics list. +```php +// Usage metrics +const METRIC_FUNCTIONS = 'functions'; +const METRIC_DEPLOYMENTS = 'deployments'; +const METRIC_DEPLOYMENTS_STORAGE = 'deployments.storage'; +const METRIC_BUILDS = 'builds'; +const METRIC_BUILDS_STORAGE = 'builds.storage'; +const METRIC_BUILDS_COMPUTE = 'builds.compute'; +``` + +**API** + +On database listener, Add to existing or create a new switch case. +Add a call to the usage worker with your new metric const like so: + +```php + case $document->getCollection() === 'functions': + $queueForUsage + ->addMetric(METRIC_FUNCTIONS, $value); + if ($event === Database::EVENT_DOCUMENT_DELETE) { + $queueForUsage + ->addReduce($document); + } + break; +``` + + +**Background worker** + +```php +$queueForUsage + ->addMetric(METRIC_BUILDS, 1) + ->addMetric(METRIC_BUILDS_STORAGE, $build->getAttribute('size', 0)) + ->addMetric(METRIC_BUILDS_COMPUTE, (int)$build->getAttribute('duration', 0) * 1000) + ->addMetric(str_replace('{functionInternalId}', $function->getInternalId(), METRIC_FUNCTION_ID_BUILDS), 1) + ->addMetric(str_replace('{functionInternalId}', $function->getInternalId(), METRIC_FUNCTION_ID_BUILDS_STORAGE), $build->getAttribute('size', 0)) + ->addMetric(str_replace('{functionInternalId}', $function->getInternalId(), METRIC_FUNCTION_ID_BUILDS_COMPUTE), (int)$build->getAttribute('duration', 0) * 1000) + ->setProject($project) + ->trigger(); +``` + + ## Build To build a new version of the Appwrite server, all you need to do is run the build.sh file like this: From 2f54446aee4bbf4ff91b92eaa4c9df19d67aa489 Mon Sep 17 00:00:00 2001 From: shimon Date: Mon, 6 May 2024 17:31:36 +0300 Subject: [PATCH 02/14] adding new usage metric --- CONTRIBUTING.md | 89 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 81 insertions(+), 8 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index f934048d7a..2cc4a7cd6a 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -301,12 +301,53 @@ This will allow the Appwrite community to sufficiently discuss the new feature v This is also important for the Appwrite lead developers to be able to provide technical input and potentially a different emphasis regarding the feature design and architecture. Some bigger features might need to go through our [RFC process](https://github.com/appwrite/rfc). -## Adding new usage metrics +## Adding New Usage Metrics + +These are the current metrics we collect usage stats for: + +| Metric | Description | +|--------|---------------------------------------------------| +| teams | Total number of teams per project | +| users | Total number of users per project | +| executions | Total number of executions per project | +| databases | Total number of databases per project | +| collections | Total number of collections per project | +| {databaseInternalId}.collections | Total number of collections per database | +| documents | Total number of documents per project | +| {databaseInternalId}.{collectionInternalId}.documents | Total number of documents per collection | +| buckets | Total number of buckets per project | +| files | Total number of files per project | +| files.storage | Sum of files storage per project (in bytes) | +| {bucketInternalId}.files.storage | Sum of files.storage per bucket | +| functions | Total number of functions per project | +| deployments | Total number of deployments per project | +| deployments.storage | Sum of deployments storage per project (in bytes) | +| builds | Total number of builds per project | +| builds.storage | Sum of builds storage per project (in bytes) | +| builds.compute | Sum of compute duration per project (in seconds) | +| {functionInternalId}.builds.storage | Sum of builds storage per function | +| {functionInternalId}.builds.compute | Sum of compute duration per function (in seconds) | +| {resourceType}.{resourceInternalId}.deployments | Total number of deployments per function | +| {resourceType}.{resourceInternalId}.deployments.storage | Sum of deployments storage per function | +| executions | Total number of executions per project | +| executions.compute | Sum of compute duration per project (in seconds) | +| {functionInternalId}.executions | Total number of executions per function | +| network.requests | Total number of network requests per project | +| network.inbound | Sum of network inbound traffic per project | +| network.outbound | Sum of network outbound traffic per project | + +* The curly brackets in the metric name act as placeholders and will be replaced with a value. + +Metrics are collected into 3 scopes: Daily, monthly, and infinity. Adding new usage metrics to aggregate usage stats is simple but depends on whether you want to collect the statistics via API or background worker. Here are the steps needed for both cases: + +For both cases, add a const variable in `app/init.php` under the usage metrics list: + +* The curly brackets in the metric name acts as a placeholder and will be replaced with a value. metrics are collected to 3 scopes : Daily, monthly, an infinity. -Adding new usage metrics in order to aggregate usage stats is very simple but very much depends on where do you want to collect. -the statistics( via API or via background worker) +Adding new usage metrics in order to aggregate usage stats is very simple but very much depends on where do you want to collect +the statistics(API or via background worker). Here are the steps needs to be taken in both cases: For both cases you need to add a const variable in app/init.php under the usage metrics list. @@ -315,16 +356,22 @@ For both cases you need to add a const variable in app/init.php under the usage const METRIC_FUNCTIONS = 'functions'; const METRIC_DEPLOYMENTS = 'deployments'; const METRIC_DEPLOYMENTS_STORAGE = 'deployments.storage'; -const METRIC_BUILDS = 'builds'; -const METRIC_BUILDS_STORAGE = 'builds.storage'; -const METRIC_BUILDS_COMPUTE = 'builds.compute'; ``` **API** -On database listener, Add to existing or create a new switch case. -Add a call to the usage worker with your new metric const like so: +In the database listener, add to an existing or create a new switch case. Add a call to the usage worker with your new metric const like so: +```php + case $document->getCollection() === 'teams': + $queueForUsage + ->addMetric(METRIC_TEAMS, $value); // per project + break; +``` +There are cases when you need to handle metric the is a parent entity, like buckets. +Files are linked to a parent bucket, you should verify you remove the files stats when you delete a bucket. + +In that case you need also to handle children removal using addReduce() method call. ```php case $document->getCollection() === 'functions': $queueForUsage @@ -336,9 +383,35 @@ Add a call to the usage worker with your new metric const like so: break; ``` +On top of that also adding logic on the usage worker located in /src/Appwrite/Platform/Workers/Usage.php, on the reduce method. +```php +private function reduce(Document $project, Document $document, array &$metrics, callable $getProjectDB): void +``` + **Background worker** +On that case you need to inject the usage queue to the desired worker +```php +/** +* @throws Exception +*/ +public function __construct() +{ +$this +->desc('Functions worker') +->groups(['functions']) +->inject('message') +->inject('dbForProject') +->inject('queueForFunctions') +->inject('queueForEvents') +->inject('queueForUsage') +->inject('log') +->callback(fn (Message $message, Database $dbForProject, Func $queueForFunctions, Event $queueForEvents, Usage $queueForUsage, Log $log) => $this->action($message, $dbForProject, $queueForFunctions, $queueForEvents, $queueForUsage, $log)); +``` + +and then trigger the queue with the new metric like so: + ```php $queueForUsage ->addMetric(METRIC_BUILDS, 1) From 2962fbf7d791f24fd4ac66b8f3a93ffc2bcfd4d9 Mon Sep 17 00:00:00 2001 From: shimon Date: Tue, 7 May 2024 20:12:58 +0300 Subject: [PATCH 03/14] adding new usage metric --- CONTRIBUTING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 2cc4a7cd6a..438d0da2c3 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -360,7 +360,7 @@ const METRIC_DEPLOYMENTS_STORAGE = 'deployments.storage'; **API** -In the database listener, add to an existing or create a new switch case. Add a call to the usage worker with your new metric const like so: +On file app/controllers/shared/api.php in database listener, add to an existing or create a new switch case. Add a call to the usage worker with your new metric const like so: ```php case $document->getCollection() === 'teams': From b8a2de84daebb9fe2cf94856abc0658550238743 Mon Sep 17 00:00:00 2001 From: Shimon Newman Date: Tue, 7 May 2024 20:57:42 +0300 Subject: [PATCH 04/14] Update CONTRIBUTING.md Co-authored-by: Christy Jacob --- CONTRIBUTING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 438d0da2c3..b4b1f32e64 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -336,7 +336,7 @@ These are the current metrics we collect usage stats for: | network.inbound | Sum of network inbound traffic per project | | network.outbound | Sum of network outbound traffic per project | -* The curly brackets in the metric name act as placeholders and will be replaced with a value. +> Note: The curly brackets in the metric name represents a template and is replaced with a value when the metric is processed. Metrics are collected into 3 scopes: Daily, monthly, and infinity. Adding new usage metrics to aggregate usage stats is simple but depends on whether you want to collect the statistics via API or background worker. Here are the steps needed for both cases: From d87df007075e6774e710792d49c0211230f563fb Mon Sep 17 00:00:00 2001 From: Shimon Newman Date: Tue, 7 May 2024 20:57:54 +0300 Subject: [PATCH 05/14] Update CONTRIBUTING.md Co-authored-by: Christy Jacob --- CONTRIBUTING.md | 1 - 1 file changed, 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b4b1f32e64..f2fb6b0683 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -342,7 +342,6 @@ Metrics are collected into 3 scopes: Daily, monthly, and infinity. Adding new us For both cases, add a const variable in `app/init.php` under the usage metrics list: -* The curly brackets in the metric name acts as a placeholder and will be replaced with a value. metrics are collected to 3 scopes : Daily, monthly, an infinity. From 7430cb48d052070efe249f678b84a90d7fe12938 Mon Sep 17 00:00:00 2001 From: Shimon Newman Date: Tue, 7 May 2024 20:58:04 +0300 Subject: [PATCH 06/14] Update CONTRIBUTING.md Co-authored-by: Christy Jacob --- CONTRIBUTING.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index f2fb6b0683..84fb340463 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -356,6 +356,7 @@ const METRIC_FUNCTIONS = 'functions'; const METRIC_DEPLOYMENTS = 'deployments'; const METRIC_DEPLOYMENTS_STORAGE = 'deployments.storage'; ``` +Next follow the appropriate steps below depending on whether you're adding the metric to the API or the worker. **API** From ad5c526ca0853e9c872494cfe8556bf4b7a96364 Mon Sep 17 00:00:00 2001 From: Shimon Newman Date: Tue, 7 May 2024 20:58:11 +0300 Subject: [PATCH 07/14] Update CONTRIBUTING.md Co-authored-by: Christy Jacob --- CONTRIBUTING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 84fb340463..dee13c4049 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -360,7 +360,7 @@ Next follow the appropriate steps below depending on whether you're adding the m **API** -On file app/controllers/shared/api.php in database listener, add to an existing or create a new switch case. Add a call to the usage worker with your new metric const like so: +On file `app/controllers/shared/api.php` in database listener, add to an existing or create a new switch case. Add a call to the usage worker with your new metric const like so: ```php case $document->getCollection() === 'teams': From ddda24e34d03620b86c389890fe64abc3d5106c4 Mon Sep 17 00:00:00 2001 From: Shimon Newman Date: Tue, 7 May 2024 20:59:12 +0300 Subject: [PATCH 08/14] Update CONTRIBUTING.md Co-authored-by: Christy Jacob --- CONTRIBUTING.md | 1 - 1 file changed, 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index dee13c4049..c895a6c666 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -349,7 +349,6 @@ Adding new usage metrics in order to aggregate usage stats is very simple but v the statistics(API or via background worker). Here are the steps needs to be taken in both cases: -For both cases you need to add a const variable in app/init.php under the usage metrics list. ```php // Usage metrics const METRIC_FUNCTIONS = 'functions'; From 243bdeb29c9c6f57e60840e5d48d06d73b13b972 Mon Sep 17 00:00:00 2001 From: Shimon Newman Date: Tue, 7 May 2024 20:59:19 +0300 Subject: [PATCH 09/14] Update CONTRIBUTING.md Co-authored-by: Christy Jacob --- CONTRIBUTING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index c895a6c666..bb828b1583 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -367,7 +367,7 @@ On file `app/controllers/shared/api.php` in database listener, add to an exist ->addMetric(METRIC_TEAMS, $value); // per project break; ``` -There are cases when you need to handle metric the is a parent entity, like buckets. +There are cases when you need to handle metric that has a parent entity, like buckets. Files are linked to a parent bucket, you should verify you remove the files stats when you delete a bucket. In that case you need also to handle children removal using addReduce() method call. From 508691a9a6ab1e3669266a190fd7d13325151e8d Mon Sep 17 00:00:00 2001 From: Shimon Newman Date: Tue, 7 May 2024 20:59:31 +0300 Subject: [PATCH 10/14] Update CONTRIBUTING.md Co-authored-by: Christy Jacob --- CONTRIBUTING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index bb828b1583..f764cc49c5 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -382,7 +382,7 @@ In that case you need also to handle children removal using addReduce() method c break; ``` -On top of that also adding logic on the usage worker located in /src/Appwrite/Platform/Workers/Usage.php, on the reduce method. +In addition, you also need to add some logic to the `reduce()` method of the Usage worker located in `/src/Appwrite/Platform/Workers/Usage.php`. ```php private function reduce(Document $project, Document $document, array &$metrics, callable $getProjectDB): void ``` From 8559753978bdb720a1eb250bd8f7888961cf4c86 Mon Sep 17 00:00:00 2001 From: shimon Date: Tue, 7 May 2024 21:16:18 +0300 Subject: [PATCH 11/14] adding new usage metric --- CONTRIBUTING.md | 100 ++++++++++++++++++++++++++---------------------- 1 file changed, 54 insertions(+), 46 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index f764cc49c5..7a642bc2db 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -306,48 +306,42 @@ This is also important for the Appwrite lead developers to be able to provide te These are the current metrics we collect usage stats for: | Metric | Description | -|--------|---------------------------------------------------| -| teams | Total number of teams per project | -| users | Total number of users per project | +|--------|-------------------------------------------------| +| teams | Total number of teams per project | +| users | Total number of users per project| | executions | Total number of executions per project | | databases | Total number of databases per project | -| collections | Total number of collections per project | -| {databaseInternalId}.collections | Total number of collections per database | +| collections | Total number of collections per project | +| {databaseInternalId}.collections | Total number of collections per database| | documents | Total number of documents per project | -| {databaseInternalId}.{collectionInternalId}.documents | Total number of documents per collection | +| {databaseInternalId}.{collectionInternalId}.documents | Total number of documents per collection | | buckets | Total number of buckets per project | -| files | Total number of files per project | -| files.storage | Sum of files storage per project (in bytes) | -| {bucketInternalId}.files.storage | Sum of files.storage per bucket | +| files | Total number of files per project | +| {bucketInternalId}.files.storage | Sum of files.storage per bucket (in bytes) | | functions | Total number of functions per project | | deployments | Total number of deployments per project | -| deployments.storage | Sum of deployments storage per project (in bytes) | | builds | Total number of builds per project | +| {resourceType}.{resourceInternalId}.deployments | Total number of deployments per function | +| executions | Total number of executions per project | +| {functionInternalId}.executions | Total number of executions per function | +| files.storage | Sum of files storage per project (in bytes) | +| deployments.storage | Sum of deployments storage per project (in bytes) | +| {resourceType}.{resourceInternalId}.deployments.storage | Sum of deployments storage per function (in bytes) | | builds.storage | Sum of builds storage per project (in bytes) | | builds.compute | Sum of compute duration per project (in seconds) | -| {functionInternalId}.builds.storage | Sum of builds storage per function | +| {functionInternalId}.builds.storage | Sum of builds storage per function (in bytes) | | {functionInternalId}.builds.compute | Sum of compute duration per function (in seconds) | -| {resourceType}.{resourceInternalId}.deployments | Total number of deployments per function | -| {resourceType}.{resourceInternalId}.deployments.storage | Sum of deployments storage per function | -| executions | Total number of executions per project | -| executions.compute | Sum of compute duration per project (in seconds) | -| {functionInternalId}.executions | Total number of executions per function | | network.requests | Total number of network requests per project | +| executions.compute | Sum of compute duration per project (in seconds) | | network.inbound | Sum of network inbound traffic per project | | network.outbound | Sum of network outbound traffic per project | > Note: The curly brackets in the metric name represents a template and is replaced with a value when the metric is processed. -Metrics are collected into 3 scopes: Daily, monthly, and infinity. Adding new usage metrics to aggregate usage stats is simple but depends on whether you want to collect the statistics via API or background worker. Here are the steps needed for both cases: - -For both cases, add a const variable in `app/init.php` under the usage metrics list: - - -metrics are collected to 3 scopes : -Daily, monthly, an infinity. -Adding new usage metrics in order to aggregate usage stats is very simple but very much depends on where do you want to collect -the statistics(API or via background worker). -Here are the steps needs to be taken in both cases: +Metrics are collected within 3 scopes Daily, monthly, an infinity. +Adding new usage metric in order to aggregate usage stats is very simple, but very much dependent on where do you want to collect +statistics ,via API or via background worker. +For both cases you will need to add a const variable in `app/init.php` under the usage metrics list. ```php // Usage metrics @@ -355,6 +349,7 @@ const METRIC_FUNCTIONS = 'functions'; const METRIC_DEPLOYMENTS = 'deployments'; const METRIC_DEPLOYMENTS_STORAGE = 'deployments.storage'; ``` + Next follow the appropriate steps below depending on whether you're adding the metric to the API or the worker. **API** @@ -372,21 +367,33 @@ Files are linked to a parent bucket, you should verify you remove the files stat In that case you need also to handle children removal using addReduce() method call. ```php - case $document->getCollection() === 'functions': - $queueForUsage - ->addMetric(METRIC_FUNCTIONS, $value); - if ($event === Database::EVENT_DOCUMENT_DELETE) { - $queueForUsage - ->addReduce($document); - } - break; + case $document->getCollection() === 'buckets': + $files = $dbForProject->getDocument('stats', md5(self::INFINITY_PERIOD . str_replace('{bucketInternalId}', $document->getInternalId(), METRIC_BUCKET_ID_FILES))); + $storage = $dbForProject->getDocument('stats', md5(self::INFINITY_PERIOD . str_replace('{bucketInternalId}', $document->getInternalId(), METRIC_BUCKET_ID_FILES_STORAGE))); + + if (!empty($files['value'])) { + $metrics[] = [ + 'key' => METRIC_FILES, + 'value' => ($files['value'] * -1), + ]; + } + + if (!empty($storage['value'])) { + $metrics[] = [ + 'key' => METRIC_FILES_STORAGE, + 'value' => ($storage['value'] * -1), + ]; + } + break; ``` In addition, you also need to add some logic to the `reduce()` method of the Usage worker located in `/src/Appwrite/Platform/Workers/Usage.php`. ```php -private function reduce(Document $project, Document $document, array &$metrics, callable $getProjectDB): void -``` +private function reduce(Document $project, Document $document, array &$metrics, callable $getProjectDB): void +{ +} +``` **Background worker** @@ -397,16 +404,17 @@ On that case you need to inject the usage queue to the desired worker */ public function __construct() { -$this -->desc('Functions worker') -->groups(['functions']) -->inject('message') -->inject('dbForProject') -->inject('queueForFunctions') -->inject('queueForEvents') -->inject('queueForUsage') -->inject('log') -->callback(fn (Message $message, Database $dbForProject, Func $queueForFunctions, Event $queueForEvents, Usage $queueForUsage, Log $log) => $this->action($message, $dbForProject, $queueForFunctions, $queueForEvents, $queueForUsage, $log)); + $this + ->desc('Functions worker') + ->groups(['functions']) + ->inject('message') + ->inject('dbForProject') + ->inject('queueForFunctions') + ->inject('queueForEvents') + ->inject('queueForUsage') + ->inject('log') + ->callback(fn (Message $message, Database $dbForProject, Func $queueForFunctions, Event $queueForEvents, Usage $queueForUsage, Log $log) => $this->action($message, $dbForProject, $queueForFunctions, $queueForEvents, $queueForUsage, $log)); +} ``` and then trigger the queue with the new metric like so: From b0912889c88870114c557011e62642d97005881b Mon Sep 17 00:00:00 2001 From: shimon Date: Tue, 7 May 2024 21:22:17 +0300 Subject: [PATCH 12/14] adding new usage metric --- CONTRIBUTING.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 7a642bc2db..118f5a83d5 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -333,8 +333,8 @@ These are the current metrics we collect usage stats for: | {functionInternalId}.builds.compute | Sum of compute duration per function (in seconds) | | network.requests | Total number of network requests per project | | executions.compute | Sum of compute duration per project (in seconds) | -| network.inbound | Sum of network inbound traffic per project | -| network.outbound | Sum of network outbound traffic per project | +| network.inbound | Sum of network inbound traffic per project (in bytes)| +| network.outbound | Sum of network outbound traffic per project (in bytes)| > Note: The curly brackets in the metric name represents a template and is replaced with a value when the metric is processed. @@ -354,7 +354,7 @@ Next follow the appropriate steps below depending on whether you're adding the m **API** -On file `app/controllers/shared/api.php` in database listener, add to an existing or create a new switch case. Add a call to the usage worker with your new metric const like so: +In file `app/controllers/shared/api.php` On the database listener, add to an existing or create a new switch case. Add a call to the usage worker with your new metric const like so: ```php case $document->getCollection() === 'teams': @@ -397,7 +397,7 @@ private function reduce(Document $project, Document $document, array &$metrics, **Background worker** -On that case you need to inject the usage queue to the desired worker +You need to inject the usage queue in the desired worker on the constructor method ```php /** * @throws Exception From 415b70585a3a458dc935306b0613da3c34ff18e3 Mon Sep 17 00:00:00 2001 From: shimon Date: Wed, 8 May 2024 10:01:24 +0300 Subject: [PATCH 13/14] adding new usage metric --- CONTRIBUTING.md | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 118f5a83d5..709d01ac52 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -366,8 +366,24 @@ There are cases when you need to handle metric that has a parent entity, like bu Files are linked to a parent bucket, you should verify you remove the files stats when you delete a bucket. In that case you need also to handle children removal using addReduce() method call. + ```php - case $document->getCollection() === 'buckets': + + case $document->getCollection() === 'buckets': //buckets + $queueForUsage + ->addMetric(METRIC_BUCKETS, $value); // per project + if ($event === Database::EVENT_DOCUMENT_DELETE) { + $queueForUsage + ->addReduce($document); + } + break; + +``` + +In addition, you will also need to add some logic to the `reduce()` method of the Usage worker located in `/src/Appwrite/Platform/Workers/Usage.php`, like so: + +```php +case $document->getCollection() === 'buckets': $files = $dbForProject->getDocument('stats', md5(self::INFINITY_PERIOD . str_replace('{bucketInternalId}', $document->getInternalId(), METRIC_BUCKET_ID_FILES))); $storage = $dbForProject->getDocument('stats', md5(self::INFINITY_PERIOD . str_replace('{bucketInternalId}', $document->getInternalId(), METRIC_BUCKET_ID_FILES_STORAGE))); @@ -387,14 +403,6 @@ In that case you need also to handle children removal using addReduce() method c break; ``` -In addition, you also need to add some logic to the `reduce()` method of the Usage worker located in `/src/Appwrite/Platform/Workers/Usage.php`. -```php -private function reduce(Document $project, Document $document, array &$metrics, callable $getProjectDB): void -{ - -} -``` - **Background worker** You need to inject the usage queue in the desired worker on the constructor method From 499236833da5d5ec03a7a120ea2ace09fd14aae6 Mon Sep 17 00:00:00 2001 From: shimon Date: Wed, 8 May 2024 19:47:55 +0300 Subject: [PATCH 14/14] getCountryCode --- CONTRIBUTING.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 709d01ac52..8326164c48 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -338,10 +338,8 @@ These are the current metrics we collect usage stats for: > Note: The curly brackets in the metric name represents a template and is replaced with a value when the metric is processed. -Metrics are collected within 3 scopes Daily, monthly, an infinity. -Adding new usage metric in order to aggregate usage stats is very simple, but very much dependent on where do you want to collect -statistics ,via API or via background worker. -For both cases you will need to add a const variable in `app/init.php` under the usage metrics list. +Metrics are collected within 3 scopes Daily, monthly, an infinity. Adding new usage metric in order to aggregate usage stats is very simple, but very much dependent on where do you want to collect +statistics ,via API or via background worker. For both cases you will need to add a `const` variable in `app/init.php` under the usage metrics list using the naming convention `METRIC_` as shown below. ```php // Usage metrics