From 9f1b356a79184a032bb4bee663422c14cd8a75ff Mon Sep 17 00:00:00 2001 From: shimon Date: Mon, 18 Dec 2023 11:37:54 +0200 Subject: [PATCH] combining network inbound and network outbound on hamster script --- src/Appwrite/Platform/Tasks/CalcTierStats.php | 15 ++++++-- src/Appwrite/Platform/Tasks/Hamster.php | 35 ++++++++++++------- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/src/Appwrite/Platform/Tasks/CalcTierStats.php b/src/Appwrite/Platform/Tasks/CalcTierStats.php index 4ac0dc0bb6..8d30a51ea3 100644 --- a/src/Appwrite/Platform/Tasks/CalcTierStats.php +++ b/src/Appwrite/Platform/Tasks/CalcTierStats.php @@ -49,8 +49,9 @@ class CalcTierStats extends Action protected string $date; private array $usageStats = [ - 'project.$all.network.requests' => 'Requests', - 'project.$all.network.bandwidth' => 'Bandwidth', + 'network.requests' => 'Requests', + 'network.inbound' => 'Inbound', + 'network.outbound' => 'Outbound', ]; @@ -171,6 +172,7 @@ class CalcTierStats extends Action $limit = $periods[$range]['limit']; $period = $periods[$range]['period']; + $requestDocs = $dbForProject->find('stats', [ Query::equal('metric', [$metric]), Query::equal('period', [$period]), @@ -199,6 +201,13 @@ class CalcTierStats extends Action $stats[$metrics[$key]] = $value; } + /** + * Workaround to combine network.inbound+network.outbound as network. + */ + $stats['Network'] = ($stats['Inbound'] ?? 0) + ($stats['Outbound'] ?? 0); + unset($stats['Inbound']); + unset($stats['Outbound']); + try { /** Get Domains */ $stats['Domains'] = $dbForConsole->count('rules', [ @@ -350,7 +359,7 @@ class CalcTierStats extends Action /** Content */ $mail->Subject = "Cloud Report for {$this->date}"; $mail->Body = "Please find the daily cloud report atttached"; - $mail->send(); + //$mail->send(); Console::success('Email has been sent!'); } catch (Exception $e) { Console::error("Message could not be sent. Mailer Error: {$mail->ErrorInfo}"); diff --git a/src/Appwrite/Platform/Tasks/Hamster.php b/src/Appwrite/Platform/Tasks/Hamster.php index c71b8d414b..fd4735146e 100644 --- a/src/Appwrite/Platform/Tasks/Hamster.php +++ b/src/Appwrite/Platform/Tasks/Hamster.php @@ -20,17 +20,18 @@ use Utopia\Pools\Group; class Hamster extends Action { private array $metrics = [ - 'usage_files' => 'files.$all.count.total', - 'usage_buckets' => 'buckets.$all.count.total', - 'usage_databases' => 'databases.$all.count.total', - 'usage_documents' => 'documents.$all.count.total', - 'usage_collections' => 'collections.$all.count.total', - 'usage_storage' => 'project.$all.storage.size', - 'usage_requests' => 'project.$all.network.requests', - 'usage_bandwidth' => 'project.$all.network.bandwidth', - 'usage_users' => 'users.$all.count.total', - 'usage_sessions' => 'sessions.email.requests.create', - 'usage_executions' => 'executions.$all.compute.total', + 'usage_files' => 'files', + 'usage_buckets' => 'buckets', + 'usage_databases' => 'databases', + 'usage_documents' => 'documents', + 'usage_collections' => 'collections', + 'usage_storage' => 'files.storage', + 'usage_requests' => 'network.requests', + 'usage_inbound' => 'network.inbound', + 'usage_outbound' => 'network.outbound', + 'usage_users' => 'users', + 'usage_sessions' => 'sessions', + 'usage_executions' => 'executions', ]; protected string $directory = '/usr/local'; @@ -261,6 +262,16 @@ class Hamster extends Action } }); + /** + * Workaround to combine network.inbound+network.outbound as network. + */ + $statsPerProject["usage_network_infinity"] = $statsPerProject["usage_inbound_infinity"] + $statsPerProject["usage_outbound_infinity"]; + $statsPerProject["usage_network_24h"] = $statsPerProject["usage_inbound_24h"] + $statsPerProject["usage_outbound_24h"]; + unset($statsPerProject["usage_outbound_24h"]); + unset($statsPerProject["usage_inbound_24h"]); + unset($statsPerProject["usage_outbound_infinity"]); + unset($statsPerProject["usage_inbound_infinity"]); + if (isset($statsPerProject['email'])) { /** Send data to mixpanel */ $res = $this->mixpanel->createProfile($statsPerProject['email'], '', [ @@ -300,7 +311,6 @@ class Hamster extends Action Console::success(APP_NAME . ' cloud hamster process has started'); $sleep = (int) App::getEnv('_APP_HAMSTER_INTERVAL', '30'); // 30 seconds (by default) - $jobInitTime = App::getEnv('_APP_HAMSTER_TIME', '22:00'); // (hour:minutes) $now = new \DateTime(); $now->setTimezone(new \DateTimeZone(date_default_timezone_get())); @@ -315,7 +325,6 @@ class Hamster extends Action $next->add(\DateInterval::createFromDateString('1 days')); $delay = $next->getTimestamp() - $now->getTimestamp(); } - Console::log('[' . $now->format("Y-m-d H:i:s.v") . '] Delaying for ' . $delay . ' setting loop to [' . $next->format("Y-m-d H:i:s.v") . ']'); Console::loop(function () use ($pools, $cache, $dbForConsole, $sleep) {