From 61286f75269536fa328817b8500c20c262f4f5fd Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Mon, 24 Feb 2025 13:43:09 +1300 Subject: [PATCH] Read logs DB --- app/controllers/api/account.php | 21 +++++++----- app/controllers/api/databases.php | 42 +++++++++++++---------- app/controllers/api/messaging.php | 57 ++++++++++++++++++------------- app/controllers/api/teams.php | 13 +++---- app/controllers/api/users.php | 10 +++--- 5 files changed, 82 insertions(+), 61 deletions(-) diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index bef26ad25c..a2af6b1ce4 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -2706,8 +2706,9 @@ App::get('/v1/account/logs') ->inject('user') ->inject('locale') ->inject('geodb') - ->inject('dbForProject') - ->action(function (array $queries, Response $response, Document $user, Locale $locale, Reader $geodb, Database $dbForProject) { + ->inject('project') + ->inject('getLogsDB') + ->action(function (array $queries, Response $response, Document $user, Locale $locale, Reader $geodb, Document $project, callable $getLogsDB) { try { $queries = Query::parseQueries($queries); @@ -2719,9 +2720,15 @@ App::get('/v1/account/logs') $limit = $grouped['limit'] ?? APP_LIMIT_COUNT; $offset = $grouped['offset'] ?? 0; - $audit = new EventAudit($dbForProject); + $logsDB = $getLogsDB($project); - $logs = $audit->getLogsByUser($user->getInternalId(), $limit, $offset); + $audit = new EventAudit($logsDB); + + $logs = $audit->getLogsByUser( + $user->getInternalId(), + $limit, + $offset + ); $output = []; @@ -2750,10 +2757,8 @@ App::get('/v1/account/logs') } $response->dynamic(new Document([ - //'total' => $audit->countLogsByUser($user->getInternalId()), - //'logs' => $output, - 'total' => 0, - 'logs' => [], + 'total' => $audit->countLogsByUser($user->getInternalId()), + 'logs' => $output, ]), Response::MODEL_LOG_LIST); }); diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index d9d62eac51..8761b35cbc 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -652,10 +652,14 @@ App::get('/v1/databases/:databaseId/logs') ->param('databaseId', '', new UID(), 'Database ID.') ->param('queries', [], new Queries([new Limit(), new Offset()]), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Only supported methods are limit and offset', true) ->inject('response') + ->inject('project') + ->inject('getLogsDB') ->inject('dbForProject') ->inject('locale') ->inject('geodb') - ->action(function (string $databaseId, array $queries, Response $response, Database $dbForProject, Locale $locale, Reader $geodb) { + ->action(function (string $databaseId, array $queries, Response $response, Document $project, callable $getLogsDB, Database $dbForProject, Locale $locale, Reader $geodb) { + + $logsDB = $getLogsDB($project); $database = $dbForProject->getDocument('databases', $databaseId); @@ -673,7 +677,7 @@ App::get('/v1/databases/:databaseId/logs') $limit = $grouped['limit'] ?? APP_LIMIT_COUNT; $offset = $grouped['offset'] ?? 0; - $audit = new Audit($dbForProject); + $audit = new Audit($logsDB); $resource = 'database/' . $databaseId; $logs = $audit->getLogsByResource($resource, $limit, $offset); @@ -723,10 +727,8 @@ App::get('/v1/databases/:databaseId/logs') } $response->dynamic(new Document([ - //'total' => $audit->countLogsByResource($resource), - //'logs' => $output, - 'total' => 0, - 'logs' => [], + 'total' => $audit->countLogsByResource($resource), + 'logs' => $output, ]), Response::MODEL_LOG_LIST); }); @@ -1041,10 +1043,14 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/logs') ->param('collectionId', '', new UID(), 'Collection ID.') ->param('queries', [], new Queries([new Limit(), new Offset()]), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Only supported methods are limit and offset', true) ->inject('response') + ->inject('project') + ->inject('getLogsDB') ->inject('dbForProject') ->inject('locale') ->inject('geodb') - ->action(function (string $databaseId, string $collectionId, array $queries, Response $response, Database $dbForProject, Locale $locale, Reader $geodb) { + ->action(function (string $databaseId, string $collectionId, array $queries, Response $response, Document $project, callable $getLogsDB, Database $dbForProject, Locale $locale, Reader $geodb) { + + $logsDB = $getLogsDB($project); $database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId)); @@ -1064,7 +1070,7 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/logs') $limit = $grouped['limit'] ?? APP_LIMIT_COUNT; $offset = $grouped['offset'] ?? 0; - $audit = new Audit($dbForProject); + $audit = new Audit($logsDB); $resource = 'database/' . $databaseId . '/collection/' . $collectionId; $logs = $audit->getLogsByResource($resource, $limit, $offset); @@ -1114,10 +1120,8 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/logs') } $response->dynamic(new Document([ - //'total' => $audit->countLogsByResource($resource), - //'logs' => $output, - 'total' => 0, - 'logs' => [], + 'total' => $audit->countLogsByResource($resource), + 'logs' => $output, ]), Response::MODEL_LOG_LIST); }); @@ -3684,10 +3688,14 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/documents/:documen ->param('documentId', '', new UID(), 'Document ID.') ->param('queries', [], new Queries([new Limit(), new Offset()]), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Only supported methods are limit and offset', true) ->inject('response') + ->inject('project') + ->inject('getLogsDB') ->inject('dbForProject') ->inject('locale') ->inject('geodb') - ->action(function (string $databaseId, string $collectionId, string $documentId, array $queries, Response $response, Database $dbForProject, Locale $locale, Reader $geodb) { + ->action(function (string $databaseId, string $collectionId, string $documentId, array $queries, Response $response, Document $project, callable $getLogsDB, Database $dbForProject, Locale $locale, Reader $geodb) { + + $logsDB = $getLogsDB($project); $database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId)); @@ -3717,7 +3725,7 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/documents/:documen $limit = $grouped['limit'] ?? APP_LIMIT_COUNT; $offset = $grouped['offset'] ?? 0; - $audit = new Audit($dbForProject); + $audit = new Audit($logsDB); $resource = 'database/' . $databaseId . '/collection/' . $collectionId . '/document/' . $document->getId(); $logs = $audit->getLogsByResource($resource, $limit, $offset); @@ -3767,10 +3775,8 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/documents/:documen } $response->dynamic(new Document([ - //'total' => $audit->countLogsByResource($resource), - //'logs' => $output, - 'total' => 0, - 'logs' => [], + 'total' => $audit->countLogsByResource($resource), + 'logs' => $output, ]), Response::MODEL_LOG_LIST); }); diff --git a/app/controllers/api/messaging.php b/app/controllers/api/messaging.php index 06d49394aa..76effc4987 100644 --- a/app/controllers/api/messaging.php +++ b/app/controllers/api/messaging.php @@ -978,10 +978,15 @@ App::get('/v1/messaging/providers/:providerId/logs') ->param('providerId', '', new UID(), 'Provider ID.') ->param('queries', [], new Queries([new Limit(), new Offset()]), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Only supported methods are limit and offset', true) ->inject('response') + ->inject('project') + ->inject('getLogsDB') ->inject('dbForProject') ->inject('locale') ->inject('geodb') - ->action(function (string $providerId, array $queries, Response $response, Database $dbForProject, Locale $locale, Reader $geodb) { + ->action(function (string $providerId, array $queries, Response $response, Document $project, callable $getLogsDB, Database $dbForProject, Locale $locale, Reader $geodb) { + + $logsDB = $getLogsDB($project); + $provider = $dbForProject->getDocument('providers', $providerId); if ($provider->isEmpty()) { @@ -998,7 +1003,7 @@ App::get('/v1/messaging/providers/:providerId/logs') $limit = $grouped['limit'] ?? APP_LIMIT_COUNT; $offset = $grouped['offset'] ?? 0; - $audit = new Audit($dbForProject); + $audit = new Audit($logsDB); $resource = 'provider/' . $providerId; $logs = $audit->getLogsByResource($resource, $limit, $offset); $output = []; @@ -1047,10 +1052,8 @@ App::get('/v1/messaging/providers/:providerId/logs') } $response->dynamic(new Document([ - //'total' => $audit->countLogsByResource($resource), - //'logs' => $output, - 'total' => 0, - 'logs' => [], + 'total' => $audit->countLogsByResource($resource), + 'logs' => $output, ]), Response::MODEL_LOG_LIST); }); @@ -2208,10 +2211,14 @@ App::get('/v1/messaging/topics/:topicId/logs') ->param('topicId', '', new UID(), 'Topic ID.') ->param('queries', [], new Queries([new Limit(), new Offset()]), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Only supported methods are limit and offset', true) ->inject('response') + ->inject('project') + ->inject('getLogsDB') ->inject('dbForProject') ->inject('locale') ->inject('geodb') - ->action(function (string $topicId, array $queries, Response $response, Database $dbForProject, Locale $locale, Reader $geodb) { + ->action(function (string $topicId, array $queries, Response $response, Document $project, callable $getLogsDB, Database $dbForProject, Locale $locale, Reader $geodb) { + $logsDB = $getLogsDB($project); + $topic = $dbForProject->getDocument('topics', $topicId); if ($topic->isEmpty()) { @@ -2228,7 +2235,7 @@ App::get('/v1/messaging/topics/:topicId/logs') $limit = $grouped['limit'] ?? APP_LIMIT_COUNT; $offset = $grouped['offset'] ?? 0; - $audit = new Audit($dbForProject); + $audit = new Audit($logsDB); $resource = 'topic/' . $topicId; $logs = $audit->getLogsByResource($resource, $limit, $offset); @@ -2278,10 +2285,8 @@ App::get('/v1/messaging/topics/:topicId/logs') } $response->dynamic(new Document([ - //'total' => $audit->countLogsByResource($resource), - //'logs' => $output, - 'total' => 0, - 'logs' => [], + 'total' => $audit->countLogsByResource($resource), + 'logs' => $output, ]), Response::MODEL_LOG_LIST); }); @@ -2620,10 +2625,14 @@ App::get('/v1/messaging/subscribers/:subscriberId/logs') ->param('subscriberId', '', new UID(), 'Subscriber ID.') ->param('queries', [], new Queries([new Limit(), new Offset()]), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Only supported methods are limit and offset', true) ->inject('response') + ->inject('project') + ->inject('getLogsDB') ->inject('dbForProject') ->inject('locale') ->inject('geodb') - ->action(function (string $subscriberId, array $queries, Response $response, Database $dbForProject, Locale $locale, Reader $geodb) { + ->action(function (string $subscriberId, array $queries, Response $response, Document $project, callable $getLogsDB, Database $dbForProject, Locale $locale, Reader $geodb) { + $logsDB = $getLogsDB($project); + $subscriber = $dbForProject->getDocument('subscribers', $subscriberId); if ($subscriber->isEmpty()) { @@ -2640,7 +2649,7 @@ App::get('/v1/messaging/subscribers/:subscriberId/logs') $limit = $grouped['limit'] ?? APP_LIMIT_COUNT; $offset = $grouped['offset'] ?? 0; - $audit = new Audit($dbForProject); + $audit = new Audit($logsDB); $resource = 'subscriber/' . $subscriberId; $logs = $audit->getLogsByResource($resource, $limit, $offset); @@ -2690,10 +2699,8 @@ App::get('/v1/messaging/subscribers/:subscriberId/logs') } $response->dynamic(new Document([ - //'total' => $audit->countLogsByResource($resource), - //'logs' => $output, - 'total' => 0, - 'logs' => [], + 'total' => $audit->countLogsByResource($resource), + 'logs' => $output, ]), Response::MODEL_LOG_LIST); }); @@ -3387,10 +3394,14 @@ App::get('/v1/messaging/messages/:messageId/logs') ->param('messageId', '', new UID(), 'Message ID.') ->param('queries', [], new Queries([new Limit(), new Offset()]), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Only supported methods are limit and offset', true) ->inject('response') + ->inject('project') + ->inject('getLogsDB') ->inject('dbForProject') ->inject('locale') ->inject('geodb') - ->action(function (string $messageId, array $queries, Response $response, Database $dbForProject, Locale $locale, Reader $geodb) { + ->action(function (string $messageId, array $queries, Response $response, Document $project, callable $getLogsDB, Database $dbForProject, Locale $locale, Reader $geodb) { + $logsDB = $getLogsDB($project); + $message = $dbForProject->getDocument('messages', $messageId); if ($message->isEmpty()) { @@ -3407,7 +3418,7 @@ App::get('/v1/messaging/messages/:messageId/logs') $limit = $grouped['limit'] ?? APP_LIMIT_COUNT; $offset = $grouped['offset'] ?? 0; - $audit = new Audit($dbForProject); + $audit = new Audit($logsDB); $resource = 'message/' . $messageId; $logs = $audit->getLogsByResource($resource, $limit, $offset); @@ -3457,10 +3468,8 @@ App::get('/v1/messaging/messages/:messageId/logs') } $response->dynamic(new Document([ - //'total' => $audit->countLogsByResource($resource), - //'logs' => $output, - 'total' => 0, - 'logs' => [], + 'total' => $audit->countLogsByResource($resource), + 'logs' => $output, ]), Response::MODEL_LOG_LIST); }); diff --git a/app/controllers/api/teams.php b/app/controllers/api/teams.php index 7a9f21d74b..e8f4930845 100644 --- a/app/controllers/api/teams.php +++ b/app/controllers/api/teams.php @@ -1345,10 +1345,13 @@ App::get('/v1/teams/:teamId/logs') ->param('teamId', '', new UID(), 'Team ID.') ->param('queries', [], new Queries([new Limit(), new Offset()]), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Only supported methods are limit and offset', true) ->inject('response') + ->inject('project') + ->inject('getLogsDB') ->inject('dbForProject') ->inject('locale') ->inject('geodb') - ->action(function (string $teamId, array $queries, Response $response, Database $dbForProject, Locale $locale, Reader $geodb) { + ->action(function (string $teamId, array $queries, Response $response, Documnt $project, callable $getLogsDB, Database $dbForProject, Locale $locale, Reader $geodb) { + $logsDB = $getLogsDB($project); $team = $dbForProject->getDocument('teams', $teamId); @@ -1366,7 +1369,7 @@ App::get('/v1/teams/:teamId/logs') $limit = $grouped['limit'] ?? APP_LIMIT_COUNT; $offset = $grouped['offset'] ?? 0; - $audit = new Audit($dbForProject); + $audit = new Audit($getLogsDB); $resource = 'team/' . $team->getId(); $logs = $audit->getLogsByResource($resource, $limit, $offset); @@ -1415,9 +1418,7 @@ App::get('/v1/teams/:teamId/logs') } } $response->dynamic(new Document([ - //'total' => $audit->countLogsByResource($resource), - //'logs' => $output, - 'total' => 0, - 'logs' => [], + 'total' => $audit->countLogsByResource($resource), + 'logs' => $output, ]), Response::MODEL_LOG_LIST); }); diff --git a/app/controllers/api/users.php b/app/controllers/api/users.php index ca3f042ba4..6b4536134e 100644 --- a/app/controllers/api/users.php +++ b/app/controllers/api/users.php @@ -845,10 +845,12 @@ App::get('/v1/users/:userId/logs') ->param('userId', '', new UID(), 'User ID.') ->param('queries', [], new Queries([new Limit(), new Offset()]), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Only supported methods are limit and offset', true) ->inject('response') + ->inject('project') + ->inject('getLogsDB') ->inject('dbForProject') ->inject('locale') ->inject('geodb') - ->action(function (string $userId, array $queries, Response $response, Database $dbForProject, Locale $locale, Reader $geodb) { + ->action(function (string $userId, array $queries, Response $response, Document $project, callable $getLogsDB, Database $dbForProject, Locale $locale, Reader $geodb) { $user = $dbForProject->getDocument('users', $userId); @@ -915,10 +917,8 @@ App::get('/v1/users/:userId/logs') } $response->dynamic(new Document([ - //'total' => $audit->countLogsByUser($user->getInternalId()), - //'logs' => $output, - 'total' => 0, - 'logs' => [], + 'total' => $audit->countLogsByUser($user->getInternalId()), + 'logs' => $output, ]), Response::MODEL_LOG_LIST); });