فهرست منبع

feat: shorten table clean time & add online_log to init

M1Screw 2 سال پیش
والد
کامیت
720c478caa
3فایلهای تغییر یافته به همراه24 افزوده شده و 52 حذف شده
  1. 13 10
      db/migrations/2023020100-init.php
  2. 6 37
      db/migrations/2023032600-online_log_per_user-ip.php
  3. 5 5
      src/Command/Job.php

+ 13 - 10
db/migrations/2023020100-init.php

@@ -9,16 +9,7 @@ return new class() implements MigrationInterface {
     public function up(): int
     public function up(): int
     {
     {
         DB::getPdo()->exec(
         DB::getPdo()->exec(
-            "CREATE TABLE `alive_ip` (
-                `id` bigint(20) NOT NULL AUTO_INCREMENT,
-                `nodeid` int(11) DEFAULT NULL,
-                `userid` int(11) DEFAULT NULL,
-                `ip` varchar(255) DEFAULT NULL,
-                `datetime` bigint(20) DEFAULT NULL,
-                PRIMARY KEY (`id`)
-            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-
-            CREATE TABLE `announcement` (
+            "CREATE TABLE `announcement` (
                 `id` int(11) NOT NULL AUTO_INCREMENT,
                 `id` int(11) NOT NULL AUTO_INCREMENT,
                 `date` datetime DEFAULT NULL,
                 `date` datetime DEFAULT NULL,
                 `content` text DEFAULT NULL,
                 `content` text DEFAULT NULL,
@@ -209,6 +200,18 @@ return new class() implements MigrationInterface {
                 `password` varchar(255) DEFAULT NULL,
                 `password` varchar(255) DEFAULT NULL,
                 PRIMARY KEY (`id`)
                 PRIMARY KEY (`id`)
             ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
             ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
+            
+            CREATE TABLE `online_log` (
+                `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
+                `user_id` INT UNSIGNED NOT NULL,
+                `ip` INET6 NOT NULL,
+                `node_id` INT UNSIGNED NOT NULL,
+                `first_time` INT UNSIGNED NOT NULL,
+                `last_time` INT UNSIGNED NOT NULL,
+                PRIMARY KEY (`id`),
+                UNIQUE KEY (`user_id`, `ip`),
+                KEY (`last_time`)
+            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
 
 
             CREATE TABLE `order` (
             CREATE TABLE `order` (
                 `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '订单ID',
                 `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '订单ID',

+ 6 - 37
db/migrations/2023032600-online_log_per_user-ip.php

@@ -10,7 +10,7 @@ return new class() implements MigrationInterface {
     {
     {
         $pdo = DB::getPdo();
         $pdo = DB::getPdo();
         $pdo->exec('
         $pdo->exec('
-            CREATE TABLE online_log (
+            CREATE TABLE IF NOT EXISTS online_log (
                 id INT UNSIGNED NOT NULL AUTO_INCREMENT,
                 id INT UNSIGNED NOT NULL AUTO_INCREMENT,
                 user_id INT UNSIGNED NOT NULL,
                 user_id INT UNSIGNED NOT NULL,
                 ip INET6 NOT NULL,
                 ip INET6 NOT NULL,
@@ -20,34 +20,10 @@ return new class() implements MigrationInterface {
                 PRIMARY KEY (id),
                 PRIMARY KEY (id),
                 UNIQUE KEY (user_id, ip),
                 UNIQUE KEY (user_id, ip),
                 KEY (last_time)
                 KEY (last_time)
-            )
+            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
         ');
         ');
 
 
-        $pdo->exec('
-            INSERT INTO online_log (user_id, ip, node_id, first_time, last_time)
-                SELECT
-                    userid,
-                    CASE
-                        WHEN IS_IPV4(ip) = 1 THEN CONCAT("::ffff:", ip)
-                        WHEN IS_IPV6(ip) = 1 THEN ip
-                        ELSE NULL
-                    END AS new_ip,
-                    nodeid,
-                    MIN(datetime) AS first_time,
-                    MAX(datetime) AS last_time
-                FROM
-                    alive_ip
-                WHERE
-                    userid IS NOT NULL
-                    AND nodeid IS NOT NULL
-                    AND datetime IS NOT NULL
-                GROUP BY
-                    userid, ip
-                HAVING
-                    new_ip IS NOT NULL
-        ');
-
-        $pdo->exec('DROP TABLE alive_ip');
+        $pdo->exec('DROP TABLE IF EXISTS alive_ip');
 
 
         return 2023032600;
         return 2023032600;
     }
     }
@@ -56,24 +32,17 @@ return new class() implements MigrationInterface {
     {
     {
         $pdo = DB::getPdo();
         $pdo = DB::getPdo();
         $pdo->exec('
         $pdo->exec('
-            CREATE TABLE alive_ip (
+            CREATE TABLE IF NOT EXISTS alive_ip (
                 id BIGINT(20) NOT NULL AUTO_INCREMENT,
                 id BIGINT(20) NOT NULL AUTO_INCREMENT,
                 nodeid INT(11) DEFAULT NULL,
                 nodeid INT(11) DEFAULT NULL,
                 userid INT(11) DEFAULT NULL,
                 userid INT(11) DEFAULT NULL,
                 ip VARCHAR(255) DEFAULT NULL,
                 ip VARCHAR(255) DEFAULT NULL,
                 datetime BIGINT(20) DEFAULT NULL,
                 datetime BIGINT(20) DEFAULT NULL,
                 PRIMARY KEY (id)
                 PRIMARY KEY (id)
-            ) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
-        ');
-
-        $pdo->exec('
-            INSERT INTO alive_ip (nodeid, userid, ip, datetime)
-                SELECT node_id, user_id, ip, first_time AS datetime FROM online_log
-                UNION
-                SELECT node_id, user_id, ip, last_time AS datetime FROM online_log
+            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
         ');
         ');
 
 
-        $pdo->exec('DROP TABLE online_log');
+        $pdo->exec('DROP TABLE IF EXISTS online_log');
 
 
         return 2023031701;
         return 2023031701;
     }
     }

+ 5 - 5
src/Command/Job.php

@@ -73,11 +73,11 @@ EOL;
         UserSubscribeLog::where('request_time', '<', date('Y-m-d H:i:s', time() - 86400 * (int) $_ENV['subscribeLog_keep_days']))->delete();
         UserSubscribeLog::where('request_time', '<', date('Y-m-d H:i:s', time() - 86400 * (int) $_ENV['subscribeLog_keep_days']))->delete();
         UserHourlyUsage::where('datetime', '<', time() - 86400 * (int) $_ENV['trafficLog_keep_days'])->delete();
         UserHourlyUsage::where('datetime', '<', time() - 86400 * (int) $_ENV['trafficLog_keep_days'])->delete();
         DetectLog::where('datetime', '<', time() - 86400 * 3)->delete();
         DetectLog::where('datetime', '<', time() - 86400 * 3)->delete();
-        EmailVerify::where('expire_in', '<', time() - 86400 * 3)->delete();
-        EmailQueue::where('time', '<', time() - 86400 * 3)->delete();
-        PasswordReset::where('expire_time', '<', time() - 86400 * 3)->delete();
-        OnlineLog::where('last_time', '<', time() - 86400 * 30)->delete();
-        StreamMedia::where('created_at', '<', time() - 86400 * 3)->delete();
+        EmailVerify::where('expire_in', '<', time() - 86400)->delete();
+        EmailQueue::where('time', '<', time() - 86400)->delete();
+        PasswordReset::where('expire_time', '<', time() - 86400)->delete();
+        OnlineLog::where('last_time', '<', time() - 86400)->delete();
+        StreamMedia::where('created_at', '<', time() - 86400)->delete();
         TelegramSession::where('datetime', '<', time() - 900)->delete();
         TelegramSession::where('datetime', '<', time() - 900)->delete();
         // ------- 清理各表记录
         // ------- 清理各表记录