From 0791b68149d8390f3ff928d01cfa5fc2dc04bcc8 Mon Sep 17 00:00:00 2001 From: shimon Date: Tue, 17 Sep 2024 11:45:07 +0300 Subject: [PATCH] adding external messages usage --- app/init.php | 4 ++++ src/Appwrite/Platform/Workers/Messaging.php | 18 ++++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/app/init.php b/app/init.php index 9540de57de..8f166f5703 100644 --- a/app/init.php +++ b/app/init.php @@ -223,8 +223,12 @@ const API_KEY_DYNAMIC = 'dynamic'; // Usage metrics const METRIC_TEAMS = 'teams'; const METRIC_USERS = 'users'; + const METRIC_AUTH_METHOD_PHONE = 'auth.method.phone'; const METRIC_AUTH_METHOD_PHONE_COUNTRY_CODE = METRIC_AUTH_METHOD_PHONE . '.{countryCode}'; +const METRIC_MESSAGES = 'messages'; +const METRIC_MESSAGES_SENT = METRIC_MESSAGES . '.{type}.{provider}.sent'; +const METRIC_MESSAGES_FAILED = METRIC_MESSAGES . '.{type}.{provider}.failed'; const METRIC_SESSIONS = 'sessions'; const METRIC_DATABASES = 'databases'; const METRIC_COLLECTIONS = 'collections'; diff --git a/src/Appwrite/Platform/Workers/Messaging.php b/src/Appwrite/Platform/Workers/Messaging.php index 271bbfedf1..c179218b92 100644 --- a/src/Appwrite/Platform/Workers/Messaging.php +++ b/src/Appwrite/Platform/Workers/Messaging.php @@ -113,6 +113,7 @@ class Messaging extends Action Document $message, Device $deviceForFiles, Document $project, + Usage $queueForUsage ): void { $topicIds = $message->getAttribute('topics', []); $targetIds = $message->getAttribute('targets', []); @@ -218,8 +219,8 @@ class Messaging extends Action /** * @var array $results */ - $results = batch(\array_map(function ($providerId) use ($identifiers, &$providers, $default, $message, $dbForProject, $deviceForFiles, $project) { - return function () use ($providerId, $identifiers, &$providers, $default, $message, $dbForProject, $deviceForFiles, $project) { + $results = batch(\array_map(function ($providerId) use ($identifiers, &$providers, $default, $message, $dbForProject, $deviceForFiles, $project, $queueForUsage) { + return function () use ($providerId, $identifiers, &$providers, $default, $message, $dbForProject, $deviceForFiles, $project, $queueForUsage) { if (\array_key_exists($providerId, $providers)) { $provider = $providers[$providerId]; } else { @@ -246,8 +247,8 @@ class Messaging extends Action $adapter->getMaxMessagesPerRequest() ); - return batch(\array_map(function ($batch) use ($message, $provider, $adapter, $dbForProject, $deviceForFiles, $project) { - return function () use ($batch, $message, $provider, $adapter, $dbForProject, $deviceForFiles, $project) { + return batch(\array_map(function ($batch) use ($message, $provider, $adapter, $dbForProject, $deviceForFiles, $project, $queueForUsage) { + return function () use ($batch, $message, $provider, $adapter, $dbForProject, $deviceForFiles, $project, $queueForUsage) { $deliveredTotal = 0; $deliveryErrors = []; $messageData = clone $message; @@ -286,6 +287,14 @@ class Messaging extends Action } catch (\Throwable $e) { $deliveryErrors[] = 'Failed sending to targets with error: ' . $e->getMessage(); } finally { + + $queueForUsage + ->addMetric(METRIC_MESSAGES, 1) + ->addMetric(str_replace(['{type}', '{provider}'], [$provider->getAttribute('type'), $this->getSmsAdapter($provider)], METRIC_MESSAGES_SENT), $deliveredTotal) + ->addMetric(str_replace(['{type}', '{provider}'], [$provider->getAttribute('type'), $this->getSmsAdapter($provider)], METRIC_MESSAGES_FAILED), $deliveryErrors) + ->setProject($project) + ->trigger(); + return [ 'deliveredTotal' => $deliveredTotal, 'deliveryErrors' => $deliveryErrors, @@ -318,6 +327,7 @@ class Messaging extends Action $message->setAttribute('status', MessageStatus::SENT); } + $message->removeAttribute('to'); foreach ($providers as $provider) {