feat: batch create abuse logs

This commit is contained in:
Christy Jacob 2025-02-12 10:57:43 +05:30
parent e140603890
commit 00473d4744
4 changed files with 65 additions and 22 deletions

7
.cursorignore Normal file
View file

@ -0,0 +1,7 @@
docs/*
public/*
vendor/*
dev/*
.vscode/*
app/assets/*
app/sdks/*

View file

@ -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.*",

18
composer.lock generated
View file

@ -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": {

View file

@ -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 = [];
}
}
}