diff --git a/src/Appwrite/Platform/Workers/UsageHook.php b/src/Appwrite/Platform/Workers/UsageHook.php index cc204f20ad..3125a0d93d 100644 --- a/src/Appwrite/Platform/Workers/UsageHook.php +++ b/src/Appwrite/Platform/Workers/UsageHook.php @@ -3,13 +3,12 @@ namespace Appwrite\Platform\Workers; use Utopia\App; -use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Exception\Duplicate; use Utopia\Platform\Action; use Utopia\CLI\Console; use Swoole\Timer; -use Utopia\Registry\Registry; +use Utopia\Database\DateTime; class UsageHook extends Usage { @@ -39,55 +38,66 @@ class UsageHook extends Usage public function action($register, $getProjectDB): void { - $interval = (int) App::getEnv('_APP_USAGE_AGGREGATION_INTERVAL', '60000'); - Timer::tick($interval, function () use ($register, $getProjectDB) { - $offset = count(self::$stats); - $projects = array_slice(self::$stats, 0, $offset, true); - array_splice(self::$stats, 0, $offset); - foreach ($projects as $data) { - try { - $dbForProject = $getProjectDB($data['project']); - foreach ($data['keys'] ?? [] as $key => $value) { - if ($value == 0) { - continue; - } + $interval = (int) App::getEnv('_APP_USAGE_AGGREGATION_INTERVAL', '60000'); + Timer::tick($interval, function () use ($register, $getProjectDB) { - foreach ($this->periods as $period => $format) { - $time = 'inf' === $period ? null : date($format, time()); - $id = \md5("{$time}_{$period}_{$key}"); + $offset = count(self::$stats); + $projects = array_slice(self::$stats, 0, $offset, true); + array_splice(self::$stats, 0, $offset); + foreach ($projects as $data) { + $numberOfKeys = !empty($data['keys']) ? count($data['keys']) : 0; + console::log(DateTime::now() . ' Iterating over ' . $numberOfKeys . ' keys'); - try { - $dbForProject->createDocument('stats_v2', new Document([ + if ($numberOfKeys === 0) { + continue; + } + + $projectInternalId = $data['project']->getInternalId(); + + try { + $dbForProject = $getProjectDB($data['project']); + + foreach ($data['keys'] ?? [] as $key => $value) { + if ($value == 0) { + continue; + } + + foreach ($this->periods as $period => $format) { + $time = 'inf' === $period ? null : date($format, time()); + $id = \md5("{$time}_{$period}_{$key}"); + + try { + $dbForProject->createDocument('stats_v2', new Document([ '$id' => $id, 'period' => $period, 'time' => $time, 'metric' => $key, 'value' => $value, 'region' => App::getEnv('_APP_REGION', 'default'), - ])); - } catch (Duplicate $th) { - if ($value < 0) { - $dbForProject->decreaseDocumentAttribute( - 'stats_v2', - $id, - 'value', - abs($value) - ); - } else { - $dbForProject->increaseDocumentAttribute( - 'stats_v2', - $id, - 'value', - $value - ); - } + ])); + } catch (Duplicate $th) { + if ($value < 0) { + $dbForProject->decreaseDocumentAttribute( + 'stats_v2', + $id, + 'value', + abs($value) + ); + } else { + $dbForProject->increaseDocumentAttribute( + 'stats_v2', + $id, + 'value', + $value + ); } } } - } catch (\Exception $e) { - console::error("[logger] " . " {DateTime::now()} " . " {$data['project']->getInternalId()} " . " {$e->getMessage()}"); } + } catch (\Exception $e) { + console::error(DateTime::now() . ' ' . $projectInternalId . ' ' . $e->getMessage()); } - }); + } + }); } }