From 5f92312419d3f044e077a9a1c51baa01e35056ed Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 2 Sep 2026 16:47:06 +0100 Subject: [PATCH] perf: cache geo and audit attribution lookups --- src/Appwrite/Event/Message/Audit.php | 2 ++ src/Appwrite/Geo/Geo.php | 5 +-- tests/unit/Geo/GeoTest.php | 48 ++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 tests/unit/Geo/GeoTest.php diff --git a/src/Appwrite/Event/Message/Audit.php b/src/Appwrite/Event/Message/Audit.php index 63d7842bfe..1305b87e92 100644 --- a/src/Appwrite/Event/Message/Audit.php +++ b/src/Appwrite/Event/Message/Audit.php @@ -30,6 +30,8 @@ final class Audit extends Base '$id' => $this->project->getId(), '$sequence' => $this->project->getSequence(), 'database' => $this->project->getAttribute('database', ''), + 'teamId' => $this->project->getAttribute('teamId', ''), + 'teamInternalId' => $this->project->getAttribute('teamInternalId', ''), ], 'user' => $this->user->getArrayCopy(), 'impersonatorUser' => $this->impersonatorUser->getArrayCopy(), diff --git a/src/Appwrite/Geo/Geo.php b/src/Appwrite/Geo/Geo.php index 9dce01cd05..ac9292a76d 100644 --- a/src/Appwrite/Geo/Geo.php +++ b/src/Appwrite/Geo/Geo.php @@ -11,7 +11,7 @@ use Utopia\Locale\Locale; class Geo { public const CACHE_SIZE = 10_000; - public const CACHE_VALUE_SIZE = 512; + public const CACHE_VALUE_SIZE = 1024; public function __construct( private ?Client $client, @@ -77,7 +77,8 @@ class Geo 'weatherCode' => $record['weatherCode'] ?? null, 'postalCode' => $record['postalCode'] ?? null, 'city' => $record['city']['en'] ?? null, - 'state' => $record['subdivision']['en'] ?? $record['subdivisions'][0]['en'] ?? null, + 'state' => $record['subdivision']['en'] ?? $record['subdivisions'][0]['en'] ?? $record['subdivisions'][0]['names']['en'] ?? null, + 'subdivisions' => $record['subdivisions'] ?? null, 'autonomousSystemNumber' => $autonomousSystemNumber === null ? null : (string) $autonomousSystemNumber, 'autonomousSystemOrganization' => $record['autonomousSystemOrganization'] ?? null, 'connectionType' => $record['connection'] ?? null, diff --git a/tests/unit/Geo/GeoTest.php b/tests/unit/Geo/GeoTest.php new file mode 100644 index 0000000000..44caf7f490 --- /dev/null +++ b/tests/unit/Geo/GeoTest.php @@ -0,0 +1,48 @@ +createMock(Client::class); + $client->expects($this->once()) + ->method('lookup') + ->with('203.0.113.10') + ->willReturn([ + 'countryCode' => 'DE', + 'continentCode' => 'EU', + 'city' => ['en' => 'Frankfurt'], + 'subdivisions' => [ + ['iso_code' => 'HE', 'names' => ['en' => 'Hesse']], + ], + 'isp' => 'Example ISP', + ]); + + $cache = new Table(16); + $cache->column('value', Table::TYPE_STRING, Geo::CACHE_VALUE_SIZE); + $cache->create(); + + $geo = new Geo($client, new Locale('en'), $cache); + $geo->get('203.0.113.10'); + $cached = $geo->get('203.0.113.10'); + + $this->assertSame('Frankfurt', $cached->getAttribute('city')); + $this->assertSame('Hesse', $cached->getAttribute('subdivisions')[0]['names']['en']); + $this->assertSame('Example ISP', $cached->getAttribute('isp')); + } +}