From 00473d47440f6779f7579ec80ccca15941c7d584 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Wed, 12 Feb 2025 10:57:43 +0530 Subject: [PATCH] feat: batch create abuse logs --- .cursorignore | 7 +++ composer.json | 2 +- composer.lock | 18 +++---- src/Appwrite/Platform/Workers/Audits.php | 60 +++++++++++++++++++----- 4 files changed, 65 insertions(+), 22 deletions(-) create mode 100644 .cursorignore diff --git a/.cursorignore b/.cursorignore new file mode 100644 index 0000000000..95ce4247a6 --- /dev/null +++ b/.cursorignore @@ -0,0 +1,7 @@ +docs/* +public/* +vendor/* +dev/* +.vscode/* +app/assets/* +app/sdks/* \ No newline at end of file diff --git a/composer.json b/composer.json index b7c3711410..6e25f5f912 100644 --- a/composer.json +++ b/composer.json @@ -47,7 +47,7 @@ "appwrite/php-clamav": "2.0.*", "utopia-php/abuse": "0.49.*", "utopia-php/analytics": "0.10.*", - "utopia-php/audit": "0.49.*", + "utopia-php/audit": "dev-add-batch-logging-method", "utopia-php/cache": "0.11.*", "utopia-php/cli": "0.15.*", "utopia-php/config": "0.2.*", diff --git a/composer.lock b/composer.lock index 2fcea6b3e5..e77db78d2f 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "4a54d0bd5973ed68082970f317664df3", + "content-hash": "9b7ba7990ac224daffbe50b377bacb0a", "packages": [ { "name": "adhocore/jwt", @@ -3474,16 +3474,16 @@ }, { "name": "utopia-php/audit", - "version": "0.49.0", + "version": "dev-add-batch-logging-method", "source": { "type": "git", "url": "https://github.com/utopia-php/audit.git", - "reference": "9d5c5e0cf0f6d9157b911fc3971da4331d71c96d" + "reference": "41d87370f1559656c8695d8376d6ac8406c5bd8e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/audit/zipball/9d5c5e0cf0f6d9157b911fc3971da4331d71c96d", - "reference": "9d5c5e0cf0f6d9157b911fc3971da4331d71c96d", + "url": "https://api.github.com/repos/utopia-php/audit/zipball/41d87370f1559656c8695d8376d6ac8406c5bd8e", + "reference": "41d87370f1559656c8695d8376d6ac8406c5bd8e", "shasum": "" }, "require": { @@ -3515,9 +3515,9 @@ ], "support": { "issues": "https://github.com/utopia-php/audit/issues", - "source": "https://github.com/utopia-php/audit/tree/0.49.0" + "source": "https://github.com/utopia-php/audit/tree/add-batch-logging-method" }, - "time": "2025-02-04T07:27:18+00:00" + "time": "2025-02-12T05:16:50+00:00" }, { "name": "utopia-php/cache", @@ -8747,7 +8747,9 @@ ], "aliases": [], "minimum-stability": "stable", - "stability-flags": {}, + "stability-flags": { + "utopia-php/audit": 20 + }, "prefer-stable": false, "prefer-lowest": false, "platform": { diff --git a/src/Appwrite/Platform/Workers/Audits.php b/src/Appwrite/Platform/Workers/Audits.php index c0bcab1c3a..795900f9da 100644 --- a/src/Appwrite/Platform/Workers/Audits.php +++ b/src/Appwrite/Platform/Workers/Audits.php @@ -15,6 +15,11 @@ use Utopia\Queue\Message; class Audits extends Action { + private const BATCH_SIZE = 5_000; + private const BATCH_TIME_WINDOW = 60; + + private static array $pendingEvents = []; + public static function getName(): string { return 'audits'; @@ -44,7 +49,6 @@ class Audits extends Action */ public function action(Message $message, Database $dbForProject): void { - $payload = $message->getPayload() ?? []; if (empty($payload)) { @@ -63,23 +67,53 @@ class Audits extends Action $userEmail = $user->getAttribute('email', ''); $userType = $user->getAttribute('type', Auth::ACTIVITY_TYPE_USER); - $audit = new Audit($dbForProject); - $audit->log( - userId: $user->getInternalId(), - // Pass first, most verbose event pattern - event: $event, - resource: $resource, - userAgent: $userAgent, - ip: $ip, - location: '', - data: [ + // Create event data + $eventData = [ + 'userId' => $user->getInternalId(), + 'event' => $event, + 'resource' => $resource, + 'userAgent' => $userAgent, + 'ip' => $ip, + 'location' => '', + 'data' => [ 'userId' => $user->getId(), 'userName' => $userName, 'userEmail' => $userEmail, 'userType' => $userType, 'mode' => $mode, 'data' => $auditPayload, - ] - ); + ], + 'timestamp' => time() + ]; + + self::$pendingEvents[] = $eventData; + + // Check if we should process the batch by checking both for the batch size and the elapsed time + $shouldProcessBatch = count(self::$pendingEvents) >= self::BATCH_SIZE; + if (!$shouldProcessBatch && count(self::$pendingEvents) > 0) { + $oldestEventTime = self::$pendingEvents[0]['timestamp']; + $shouldProcessBatch = (time() - $oldestEventTime) >= self::BATCH_TIME_WINDOW; + } + + if ($shouldProcessBatch) { + $audit = new Audit($dbForProject); + $batchEvents = array_map(function($event) { + return [ + 'userId' => $event['userId'], + 'event' => $event['event'], + 'resource' => $event['resource'], + 'userAgent' => $event['userAgent'], + 'ip' => $event['ip'], + 'location' => $event['location'], + 'data' => $event['data'], + 'timestamp' => $event['timestamp'] + ]; + }, self::$pendingEvents); + + $audit->logByBatch($batchEvents); + + // Clear the pending events after successful batch processing + self::$pendingEvents = []; + } } }