diff --git a/src/Appwrite/Platform/Workers/Usage.php b/src/Appwrite/Platform/Workers/Usage.php index d8da3cd16..5537fae50 100644 --- a/src/Appwrite/Platform/Workers/Usage.php +++ b/src/Appwrite/Platform/Workers/Usage.php @@ -5,6 +5,7 @@ namespace Appwrite\Platform\Workers; use Exception; use Utopia\CLI\Console; use Utopia\Database\Database; +use Utopia\Database\DateTime; use Utopia\Database\Document; use Utopia\Platform\Action; use Utopia\Queue\Message; @@ -19,7 +20,7 @@ class Usage extends Action ]; protected const INFINITY_PERIOD = '_inf_'; - + protected const DEBUG_PROJECT_ID = 85293; public static function getName(): string { return 'usage'; @@ -50,7 +51,6 @@ class Usage extends Action public function action(Message $message, callable $getProjectDB): void { $payload = $message->getPayload() ?? []; - if (empty($payload)) { throw new Exception('Missing payload'); } @@ -70,6 +70,16 @@ class Usage extends Action getProjectDB: $getProjectDB ); } + if ($project->getInternalId() == self::DEBUG_PROJECT_ID) { + var_dump([ + 'type' => 'payload', + 'project' => $project->getInternalId(), + 'database' => $project['database'] ?? '', + $payload['metrics'] + ]); + + var_dump('=========================='); + } self::$stats[$projectId]['project'] = $project; foreach ($payload['metrics'] ?? [] as $metric) { diff --git a/src/Appwrite/Platform/Workers/UsageHook.php b/src/Appwrite/Platform/Workers/UsageHook.php index 3125a0d93..68e6a3fed 100644 --- a/src/Appwrite/Platform/Workers/UsageHook.php +++ b/src/Appwrite/Platform/Workers/UsageHook.php @@ -46,17 +46,25 @@ class UsageHook extends Usage 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'); + $projectInternalId = $data['project']->getInternalId(); + $database = $data['project']['database'] ?? ''; + + console::warning('Ticker started ' . DateTime::now()); if ($numberOfKeys === 0) { continue; } - $projectInternalId = $data['project']->getInternalId(); - try { $dbForProject = $getProjectDB($data['project']); - + if ($projectInternalId == 85293) { + var_dump([ + 'project' => $projectInternalId, + 'database' => $database, + 'time' => DateTime::now(), + 'data' => $data['keys'] + ]); + } foreach ($data['keys'] ?? [] as $key => $value) { if ($value == 0) { continue; @@ -67,6 +75,15 @@ class UsageHook extends Usage $id = \md5("{$time}_{$period}_{$key}"); try { + if ($projectInternalId == self::DEBUG_PROJECT_ID) { + var_dump([ + 'type' => 'create', + 'period' => $period, + 'metric' => $key, + 'id' => $id, + 'value' => $value + ]); + } $dbForProject->createDocument('stats_v2', new Document([ '$id' => $id, 'period' => $period, @@ -77,6 +94,15 @@ class UsageHook extends Usage ])); } catch (Duplicate $th) { if ($value < 0) { + if ($projectInternalId == self::DEBUG_PROJECT_ID) { + var_dump([ + 'type' => 'decrease', + 'period' => $period, + 'metric' => $key, + 'id' => $id, + 'value' => $value + ]); + } $dbForProject->decreaseDocumentAttribute( 'stats_v2', $id, @@ -84,6 +110,15 @@ class UsageHook extends Usage abs($value) ); } else { + if ($projectInternalId == self::DEBUG_PROJECT_ID) { + var_dump([ + 'type' => 'increase', + 'period' => $period, + 'metric' => $key, + 'id' => $id, + 'value' => $value + ]); + } $dbForProject->increaseDocumentAttribute( 'stats_v2', $id,