stanley-king 3 سال پیش
والد
کامیت
d403890dc5
2فایلهای تغییر یافته به همراه183 افزوده شده و 81 حذف شده
  1. 177 81
      test/TestOrderErr.php
  2. 6 0
      test/TestRefillClear.php

+ 177 - 81
test/TestOrderErr.php

@@ -20,7 +20,7 @@ class TestOrderErr extends TestCase
     //docker-compose run -d phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestOrderErr::testRecoverPdlog)( .*)?$/" --test-suffix TestOrderErr.php /var/www/html/test
     public function testRecoverPdlog()
     {
-        $names = ['4','45','55','81','116'];
+        $names = ['4', '45', '55', '81', '116'];
         foreach ($names as $name) {
             $filename = BASE_DATA_PATH . "/log/recover/{$name}.log";
             $this->recover($filename);
@@ -29,10 +29,8 @@ class TestOrderErr extends TestCase
 
     private function recover($filename)
     {
-        $reader = function ($fp)
-        {
-            while(!feof($fp))
-            {
+        $reader = function ($fp) {
+            while (!feof($fp)) {
                 $logs = [];
                 for ($index = 0; $index < 100 && !feof($fp); $index++) {
                     $log = fgets($fp);
@@ -43,25 +41,23 @@ class TestOrderErr extends TestCase
             }
         };
 
-        $filter = function ($lines)
-        {
+        $filter = function ($lines) {
             $logs = [];
-            foreach ($lines as $line)
-            {
+            foreach ($lines as $line) {
                 $ret = preg_match('/lrlz_pd_log[^(]+[(]{1}(?P<key>[^)]+)[)]{1}[\s]+VALUES[^(][(]{1}(?P<value>[^)]+)[)]{1}/u', $line, $matches);
-                if($ret) {
+                if ($ret) {
                     $skey = $matches['key'];
                     $svalue = $matches['value'];
-                    $keys = explode(',',$skey);
-                    $values = explode(',',$svalue);
+                    $keys = explode(',', $skey);
+                    $values = explode(',', $svalue);
 
                     $conveter = function ($value) {
                         $val = trim($value, '\'');
                         return $val;
                     };
 
-                    $values = array_map($conveter,$values);
-                    $log = array_combine($keys,$values);
+                    $values = array_map($conveter, $values);
+                    $log = array_combine($keys, $values);
                     $logs[] = $log;
                 }
             }
@@ -70,19 +66,17 @@ class TestOrderErr extends TestCase
         };
 
         $mod_log = Model();
-        $inserter = function ($logs) use ($mod_log)
-        {
+        $inserter = function ($logs) use ($mod_log) {
 //            $mod_log->table('acclog')->insertAll($logs);
-            foreach ($logs as $log)
-            {
+            foreach ($logs as $log) {
                 $ret = $mod_log->table('acclog')->insert($log);
-                if(!$ret) {
-                    Log::record("import error",Log::ERR);
+                if (!$ret) {
+                    Log::record("import error", Log::ERR);
                 }
             }
         };
 
-        $file = fopen($filename,'r');
+        $file = fopen($filename, 'r');
         $batches = $reader($file);
         foreach ($batches as $batch) {
             $logs = $filter($batch);
@@ -103,27 +97,26 @@ class TestOrderErr extends TestCase
 line;
 
         $ret = preg_match('/lrlz_pd_log[^(]+[(]{1}(?P<key>[^)]+)[)]{1}[\s]+VALUES[^(][(]{1}(?P<value>[^)]+)[)]{1}/u', $line, $matches);
-        if($ret) {
+        if ($ret) {
             $skey = $matches['key'];
             $svalue = $matches['value'];
-            $keys = explode(',',$skey);
-            $values = explode(',',$svalue);
+            $keys = explode(',', $skey);
+            $values = explode(',', $svalue);
 
-            $conveter = function ($value)
-            {
-                $val = trim($value,'\'');
+            $conveter = function ($value) {
+                $val = trim($value, '\'');
                 return $val;
             };
 
-            $values = array_map($conveter,$values);
-            $log = array_combine($keys,$values);
+            $values = array_map($conveter, $values);
+            $log = array_combine($keys, $values);
         }
     }
 
     public function testGetLatestSending()
     {
         $mod = Model();
-        $item = $mod->table('refill_detail')->field('min(order_time) as mintime')->where(['order_time' => ['egt',time()-5 * 86400],'order_state'=>30])->find();
+        $item = $mod->table('refill_detail')->field('min(order_time) as mintime')->where(['order_time' => ['egt', time() - 5 * 86400], 'order_state' => 30])->find();
     }
 
 
@@ -134,7 +127,7 @@ line;
 line;
         //        $ret = preg_match('/lrlz_pd_log[^(]+[(]{1}(?P<key>[^)]+)[)]{1}[\s]+VALUES[^(][(]{1}(?P<value>[^)]+)[)]{1}/u', $line, $matches);
         $ret = preg_match('/2021-12-28[\s]+(?P<time>[\d:^\]]+)]{1}[\s\S]+available_predeposit=[\']{1}(?P<amount>[\d\.]+)[\']{1}[\s\S]+member_id[\s=]+(?P<member>[\d]+)/u', $line, $matches);
-        if($ret) {
+        if ($ret) {
             $time = $matches['time'];
             $amount = $matches['amount'];
             $member = $matches['member'];
@@ -142,9 +135,8 @@ line;
             $add_time = "2021-12-28 {$time}";
             $add_time = strtotime($add_time);
 
-            $log = ['member_id' => $member,'available_predeposit' => $amount,'add_time' => $add_time];
-        }
-        else {
+            $log = ['member_id' => $member, 'available_predeposit' => $amount, 'add_time' => $add_time];
+        } else {
 
         }
     }
@@ -158,10 +150,8 @@ line;
 
     private function recover_acclog($filename)
     {
-        $reader = function ($fp)
-        {
-            while(!feof($fp))
-            {
+        $reader = function ($fp) {
+            while (!feof($fp)) {
                 $logs = [];
                 for ($index = 0; $index < 100 && !feof($fp); $index++) {
                     $log = fgets($fp);
@@ -172,13 +162,11 @@ line;
             }
         };
 
-        $filter = function ($lines)
-        {
+        $filter = function ($lines) {
             $logs = [];
-            foreach ($lines as $line)
-            {
+            foreach ($lines as $line) {
                 $ret = preg_match('/2021-12-28[\s]+(?P<time>[\d:^\]]+)]{1}[\s\S]+available_predeposit=[\']{1}(?P<amount>[-\d\.]+)[\']{1}[\s\S]+member_id[\s=]+(?P<member>[\d]+)/u', $line, $matches);
-                if($ret) {
+                if ($ret) {
                     $time = $matches['time'];
                     $amount = $matches['amount'];
                     $member = $matches['member'];
@@ -186,11 +174,10 @@ line;
                     $add_time = "2021-12-28 {$time}";
                     $add_time = strtotime($add_time);
 
-                    $log = ['member_id' => $member,'available_predeposit' => $amount,'add_time' => $add_time];
+                    $log = ['member_id' => $member, 'available_predeposit' => $amount, 'add_time' => $add_time];
                     $logs[] = $log;
-                }
-                else {
-                    Log::record("{$line}",Log::ERR);
+                } else {
+                    Log::record("{$line}", Log::ERR);
                 }
             }
 
@@ -198,18 +185,16 @@ line;
         };
 
         $mod_log = Model();
-        $inserter = function ($logs) use ($mod_log)
-        {
-            foreach ($logs as $log)
-            {
+        $inserter = function ($logs) use ($mod_log) {
+            foreach ($logs as $log) {
                 $ret = $mod_log->table('macclog')->insert($log);
-                if(!$ret) {
-                    Log::record("import error",Log::ERR);
+                if (!$ret) {
+                    Log::record("import error", Log::ERR);
                 }
             }
         };
 
-        $file = fopen($filename,'r');
+        $file = fopen($filename, 'r');
         $batches = $reader($file);
         foreach ($batches as $batch) {
             $logs = $filter($batch);
@@ -219,8 +204,8 @@ line;
         fclose($file);
     }
 
-    //docker-compose run -d phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestOrderErr::testFilter)( .*)?$/" --test-suffix TestOrderErr.php /var/www/html/test
-    public function testFilter()
+    //docker-compose run -d phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestOrderErr::testFilterErrorLog)( .*)?$/" --test-suffix TestOrderErr.php /var/www/html/test
+    public function testFilterErrorLog()
     {
         $start_time = '2021-12-28 15:22:00';
         $end_time = '2021-12-28 15:41:00';
@@ -230,40 +215,33 @@ line;
         foreach ($files as $path => $name) {
             $input = "{$prefix}/{$path}/{$name}";
             $outpath = "{$prefix}/{$path}";
-            $this->filter_time($input,$outpath,$start_time,$end_time);
+            $this->filter_time($input, $outpath, $start_time, $end_time);
         }
     }
 
-    private function filter_time($input_file,$outpath,$start_time,$end_time)
+    private function filter_time($input_file, $outpath, $start_time, $end_time)
     {
         $start = strtotime($start_time);
         $end = strtotime($end_time);
 
-        $reader = function ($fp) use ($start, $end)
-        {
-            while(!feof($fp))
-            {
+        $reader = function ($fp) use ($start, $end) {
+            while (!feof($fp)) {
                 $line = fgets($fp);
                 $ret = preg_match('/\[cordispatcher[\s]+(?P<pid>[\d]+)-[\d]+[\s]+(?P<time>[\d-]+[\s]{1}[\d:]+)[\s]{1}/u', $line, $matches);
-                if ($ret)
-                {
+                if ($ret) {
                     $time = strtotime($matches['time']);
                     if ($time < $start) {
                         continue;
-                    }
-                    elseif($time > $end) {
+                    } elseif ($time > $end) {
                         break;
-                    }
-                    else {
+                    } else {
                         yield $line;
                     }
                 }
             }
         };
 
-
-        $filter_pid = function ($line)
-        {
+        $filter_pid = function ($line) {
             $ret = preg_match('/\[cordispatcher[\s]+(?P<pid>[\d]+)-[\d]+[\s]+(?P<time>[\d-]+[\s]{1}[\d:]+)[\s]{1}/u', $line, $matches);
             if ($ret) {
                 $pid = intval($matches['pid']);
@@ -274,26 +252,24 @@ line;
         };
 
         $pid_fp = [];
-        $spliter = function ($pid,$line) use ($outpath,&$pid_fp)
-        {
-            if(array_key_exists($pid,$pid_fp)) {
+        $spliter = function ($pid, $line) use ($outpath, &$pid_fp) {
+            if (array_key_exists($pid, $pid_fp)) {
                 $fp = $pid_fp[$pid];
             } else {
                 $name = "{$outpath}/{$pid}.log";
-                $fp = fopen($name,'w');
+                $fp = fopen($name, 'w');
                 $pid_fp[$pid] = $fp;
             }
-            fwrite($fp,$line);
+            fwrite($fp, $line);
         };
 
-        $input = fopen($input_file,'r');
+        $input = fopen($input_file, 'r');
 
         $lines = $reader($input);
-        foreach ($lines as $line)
-        {
+        foreach ($lines as $line) {
             $pid = $filter_pid($line);
-            if($pid > 0) {
-                $spliter($pid,$line);
+            if ($pid > 0) {
+                $spliter($pid, $line);
             }
         }
 
@@ -302,4 +278,124 @@ line;
             fclose($fp);
         }
     }
+
+    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+    public function testFindErrAcc()
+    {
+        $files = ['45' => ['7.log']];
+        $prefix = BASE_DATA_PATH . "/log/pdlog";
+        foreach ($files as $path => $names)
+        {
+            foreach ($names as $name)
+            {
+                $input = "{$prefix}/{$path}/{$name}";
+                $this->filter_error($input);
+            }
+        }
+    }
+
+    private function filter_error($file)
+    {
+        $task_reader = function ($fp)
+        {
+            $task = [];
+            $time = '';
+            $pid = 0;
+            $cid = 0;
+
+            while (!feof($fp))
+            {
+                $line = fgets($fp);
+                $ret = preg_match('/\[cordispatcher[\s]+(?P<pid>[\d]+)-(?P<cid>[\d]+)[\s]+(?P<time>[\d-]+[\s]{1}[\d:]+)[\s]{1}[\s\S]+BeginGoFunction[\s]+/u', $line, $matches);
+                if ($ret)
+                {
+                    if(!empty($task)) {
+                        yield [$time,$pid,$cid,$task];
+                        $task = [];
+                    }
+
+                    $time = $matches['time'];
+                    $pid = intval($matches['pid']);
+                    $cid = intval($matches['cid']);
+                    $task[] = $line;
+                }
+                else {
+                    $task[] = $line;
+                }
+            }
+
+            if(!empty($task)) {
+                yield [$time,$pid,$cid,$task];
+            }
+        };
+
+        $methoder = function ($task)
+        {
+            foreach ($task as $line)
+            {
+                $ret = preg_match('/[\s]+method[=]{1}(?P<method>[\S]+)/u', $line, $matches);
+                if ($ret) {
+                    $method = $matches['method'];
+                    return $method;
+                }
+            }
+
+            return false;
+        };
+
+        $trans_filter = function ($task)
+        {
+            $tran = [];
+            foreach ($task as $line)
+            {
+                $ret = preg_match('/[\s]+transaction[\s]+(?P<type>[\S]+)/u', $line, $matches);
+                if ($ret)
+                {
+                    $type = $matches['type'];
+                    if($type == 'begin') {
+                        $tran = [];
+                        $tran[] = $line;
+                    }
+                    elseif($type == 'commit') {
+                        $tran[] = $line;
+                        yield [$type,$tran];
+
+                        $tran = [];
+                    }
+                    elseif($type == 'rollback') {
+                        $tran[] = $line;
+                        yield [$type,$tran];
+
+                        $tran = [];
+                    }
+                    else {
+                        $tran[] = $line;
+                        yield [$type,$tran];
+
+                        $tran = [];
+                    }
+                }
+                else {
+                    $tran[] = $line;
+                }
+            }
+        };
+
+        $input = fopen($file, 'r');
+
+        $tasks = $task_reader($input);
+        foreach ($tasks as $time_task)
+        {
+            [$time,$pid,$cid,$task] = $time_task;
+            $method = $methoder($task);
+            Log::record("{$pid}-{$cid} method={$method} time={$time}",Log::DEBUG);
+            $trans = $trans_filter($task);
+            foreach ($trans as $type_tran) {
+                [$type,$tran] = $type_tran;
+                Log::record("\ttrans type={$type}",Log::DEBUG);
+            }
+            Log::record("\r\n",Log::DEBUG);
+        }
+        fclose($input);
+    }
 }

+ 6 - 0
test/TestRefillClear.php

@@ -500,6 +500,12 @@ class TestRefillClear extends TestCase
         $this->delete_log(619026072,strtotime('2021-12-01'));
     }
 
+    //docker-compose run -d phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestRefillClear::testLogDec28)( .*)?$/" --test-suffix TestRefillClear.php /var/www/html/test
+    public function testLogDec28()
+    {
+        $this->delete_log(1079047575,strtotime('2021-12-28'));
+    }
+
     private function delete_log($start_id,$endtime)
     {
         //需要的函数