Переглянути джерело

perf(online_log): add index for range search of last_time

Irohaede 2 роки тому
батько
коміт
47dc05806f

+ 6 - 6
composer.lock

@@ -4352,16 +4352,16 @@
         },
         {
             "name": "smarty/smarty",
-            "version": "v4.3.0",
+            "version": "v4.3.1",
             "source": {
                 "type": "git",
                 "url": "https://github.com/smarty-php/smarty.git",
-                "reference": "c02e9e135ea719b91f457a0072748ded0e852e7d"
+                "reference": "e28cb0915b4e3749bf57d4ebae2984e25395cfe5"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/smarty-php/smarty/zipball/c02e9e135ea719b91f457a0072748ded0e852e7d",
-                "reference": "c02e9e135ea719b91f457a0072748ded0e852e7d",
+                "url": "https://api.github.com/repos/smarty-php/smarty/zipball/e28cb0915b4e3749bf57d4ebae2984e25395cfe5",
+                "reference": "e28cb0915b4e3749bf57d4ebae2984e25395cfe5",
                 "shasum": ""
             },
             "require": {
@@ -4412,9 +4412,9 @@
             "support": {
                 "forum": "https://github.com/smarty-php/smarty/discussions",
                 "issues": "https://github.com/smarty-php/smarty/issues",
-                "source": "https://github.com/smarty-php/smarty/tree/v4.3.0"
+                "source": "https://github.com/smarty-php/smarty/tree/v4.3.1"
             },
-            "time": "2022-11-22T21:47:32+00:00"
+            "time": "2023-03-28T19:47:03+00:00"
         },
         {
             "name": "starkbank/ecdsa",

+ 2 - 1
db/migrations/2023032600-online_log_per_user-ip.php

@@ -18,7 +18,8 @@ return new class() implements MigrationInterface {
                 first_time INT UNSIGNED NOT NULL,
                 last_time INT UNSIGNED NOT NULL,
                 PRIMARY KEY (id),
-                UNIQUE KEY (user_id, ip)
+                UNIQUE KEY (user_id, ip),
+                KEY (last_time)
             )
         ');
 

+ 14 - 7
src/Models/OnlineLog.php

@@ -4,21 +4,26 @@ declare(strict_types=1);
 
 namespace App\Models;
 
+use function inet_pton;
 use function substr;
 
 /**
  * Online Log
  *
- * @property int    $id         INT UNSIGNED NOT NULL AUTO_INCREMENT, PRIMARY KEY
- * @property int    $user_id    INT UNSIGNED NOT NULL, UNIQUE KEY(A0)
- * @property string $ip         INET6 NOT NULL, UNIQUE KEY(A1) \
+ * PRIMARY KEY (id) \
+ * UNIQUE KEY (user_id, ip) \
+ * KEY (last_time)
+ *
+ * @property int    $id         INT UNSIGNED NOT NULL AUTO_INCREMENT
+ * @property int    $user_id    INT UNSIGNED NOT NULL
+ * @property string $ip         INET6 NOT NULL \
  *      Human-readable IPv6 address. \
  *      IPv4 Address would be IPv4-mapped IPv6 Address like `::ffff:1.1.1.1`.
  * @property int    $node_id    INT UNSIGNED NOT NULL
  * @property int    $first_time INT UNSIGNED NOT NULL \
- *      The time when $ip fisrt time connect.
+ *      The time when $ip fisrt being seen.
  * @property int    $last_time  INT UNSIGNED NOT NULL \
- *      The time when $ip last time connect.
+ *      The time when $ip last being seen.
  *
  * @see https://mariadb.com/kb/en/inet6/ MariaDB INET6 data type
  * @see https://www.rfc-editor.org/rfc/rfc4291.html#section-2.5.5.2 IPv4-mapped IPv6 Address
@@ -31,13 +36,15 @@ final class OnlineLog extends Model
     /**
      * Get human-readable IPv4 or IPv6 address
      *
+     * Unlike `$this->ip`, this method would convert IPv4-mapped IPv6 Address to IPv4 Address.
+     *
      * @return string Example: IPv4 Address: `1.1.1.1`; IPv6 Address: `2606:4700:4700::1111`
      */
     public function ip(): string
     {
         $ip = $this->attributes['ip'];
-        if (str_starts_with($ip, '::ffff:')) {
-            return substr($ip, 6);
+        if (substr(inet_pton($ip), 0, 12) === "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff") {
+            return substr($ip, 7);
         }
         return $ip;
     }