Bläddra i källkod

Optimize user data chart

兔姬桑 4 år sedan
förälder
incheckning
9b27cd4bcd
2 ändrade filer med 94 tillägg och 96 borttagningar
  1. 53 0
      app/Http/Controllers/Admin/ToolsController.php
  2. 41 96
      app/Http/Controllers/Controller.php

+ 53 - 0
app/Http/Controllers/Admin/ToolsController.php

@@ -221,4 +221,57 @@ class ToolsController extends Controller
 
         return view('admin.tools.analysis', ['urlList' => array_unique($url ?? [])]);
     }
+
+    // 类似Linux中的tail命令
+    private function tail($file, $n, $base = 5)
+    {
+        $fileLines = $this->countLine($file);
+        if ($fileLines < 15000) {
+            return false;
+        }
+
+        $fp = fopen($file, 'rb+');
+        assert($n > 0);
+        $pos = $n + 1;
+        $lines = [];
+        while (count($lines) <= $n) {
+            try {
+                fseek($fp, -$pos, SEEK_END);
+            } catch (Exception $e) {
+                break;
+            }
+
+            $pos *= $base;
+            while (! feof($fp)) {
+                array_unshift($lines, fgets($fp));
+            }
+        }
+
+        return array_slice($lines, 0, $n);
+    }
+
+    /**
+     * 计算文件行数.
+     *
+     * @param $file
+     *
+     * @return int
+     */
+    private function countLine($file): int
+    {
+        $fp = fopen($file, 'rb');
+        $i = 0;
+        while (! feof($fp)) {
+            //每次读取2M
+            if ($data = fread($fp, 1024 * 1024 * 2)) {
+                //计算读取到的行数
+                $num = substr_count($data, "\n");
+                $i += $num;
+            }
+        }
+
+        fclose($fp);
+
+        return $i;
+    }
 }

+ 41 - 96
app/Http/Controllers/Controller.php

@@ -10,13 +10,10 @@ use App\Models\UserDailyDataFlow;
 use App\Models\UserDataFlowLog;
 use App\Models\UserHourlyDataFlow;
 use DB;
-use Exception;
 use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
 use Illuminate\Foundation\Bus\DispatchesJobs;
 use Illuminate\Foundation\Validation\ValidatesRequests;
 use Illuminate\Routing\Controller as BaseController;
-use RuntimeException;
-use Str;
 
 class Controller extends BaseController
 {
@@ -24,86 +21,6 @@ class Controller extends BaseController
     use DispatchesJobs;
     use ValidatesRequests;
 
-    // 类似Linux中的tail命令
-    public function tail($file, $n, $base = 5)
-    {
-        $fileLines = $this->countLine($file);
-        if ($fileLines < 15000) {
-            return false;
-        }
-
-        $fp = fopen($file, 'rb+');
-        assert($n > 0);
-        $pos = $n + 1;
-        $lines = [];
-        while (count($lines) <= $n) {
-            try {
-                fseek($fp, -$pos, SEEK_END);
-            } catch (Exception $e) {
-                break;
-            }
-
-            $pos *= $base;
-            while (! feof($fp)) {
-                array_unshift($lines, fgets($fp));
-            }
-        }
-
-        return array_slice($lines, 0, $n);
-    }
-
-    /**
-     * 计算文件行数.
-     *
-     * @param $file
-     *
-     * @return int
-     */
-    public function countLine($file): int
-    {
-        $fp = fopen($file, 'rb');
-        $i = 0;
-        while (! feof($fp)) {
-            //每次读取2M
-            if ($data = fread($fp, 1024 * 1024 * 2)) {
-                //计算读取到的行数
-                $num = substr_count($data, "\n");
-                $i += $num;
-            }
-        }
-
-        fclose($fp);
-
-        return $i;
-    }
-
-    // 将Base64图片转换为本地图片并保存
-    public function base64ImageSaver($base64_image_content): ?string
-    {
-        // 匹配出图片的格式
-        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
-            $type = $result[2];
-
-            $directory = date('Ymd');
-            $path = '/assets/images/qrcode/'.$directory.'/';
-            // 检查是否有该文件夹,如果没有就创建,并给予最高权限
-            if (! file_exists(public_path($path))
-                && ! mkdir($concurrentDirectory = public_path($path), 0755, true)
-                && ! is_dir($concurrentDirectory)) {
-                throw new RuntimeException(sprintf('Directory "%s" was not created', $concurrentDirectory));
-            }
-
-            $fileName = Str::random(18).".{$type}";
-            if (file_put_contents(public_path($path.$fileName), base64_decode(str_replace($result[1], '', $base64_image_content)))) {
-                chmod(public_path($path.$fileName), 0744);
-
-                return $path.$fileName;
-            }
-        }
-
-        return '';
-    }
-
     // 节点信息
     public function getUserNodeInfo(array $server, bool $is_url): ?string
     {
@@ -139,32 +56,60 @@ class Controller extends BaseController
             $hourlyFlow = UserHourlyDataFlow::userHourly($id);
             $dailyFlow = UserDailyDataFlow::userDaily($id);
         }
-        $currentFlow = $currentFlow->where('log_time', '>=', strtotime(date('Y-m-d H:00')))->sum(DB::raw('u + d'));
-        $hourlyFlow = $hourlyFlow->whereDate('created_at', date('Y-m-d'))->pluck('total', 'created_at')->toArray();
-        $dailyFlow = $dailyFlow->whereMonth('created_at', date('n'))->pluck('total', 'created_at')->toArray();
+        $currentFlow = $currentFlow->where('log_time', '>=', strtotime(date('Y-m-d H:0')))->sum(DB::raw('u + d'));
+        $hourlyFlow = $hourlyFlow->whereDate('created_at', date('Y-m-d'))->selectRaw('(DATE_FORMAT(user_hourly_data_flow.created_at, "%k")) as date, total')->pluck('total', 'date');
+        $dailyFlow = $dailyFlow->whereMonth('created_at', date('n'))->selectRaw('(DATE_FORMAT(user_daily_data_flow.created_at, "%e")) as date, total')->pluck('total', 'date');
 
         // 节点一天内的流量
         $hourlyData = array_fill(0, date('G') + 1, 0);
         foreach ($hourlyFlow as $date => $dataFlow) {
-            $date = date('G', strtotime($date));
-            if ($date) {
-                $hourlyData[$date] = round($dataFlow / GB, 3);
-            }
+            $hourlyData[$date] = round($dataFlow / GB, 3);
         }
         $hourlyData[date('G') + 1] = round($currentFlow / GB, 3);
 
         // 节点一个月内的流量
-        $dailyData = array_fill(0, date('j'), 0);
+        $dailyData = array_fill(0, date('j') - 1, 0);
+
         foreach ($dailyFlow as $date => $dataFlow) {
-            $dailyData[date('j', strtotime($date)) - 1] = round($dataFlow / GB, 3);
+            $dailyData[$date - 1] = round($dataFlow / GB, 3);
         }
-        $dailyData[date('j', strtotime(now())) - 1] = round((array_sum($hourlyFlow) + $currentFlow) / GB, 3);
+
+        $dailyData[date('j', strtotime(now())) - 1] = round(array_sum($hourlyData) + $currentFlow / GB, 3);
 
         return [
-            'trafficDaily' => $dailyData,
+            'trafficDaily'  => $dailyData,
             'trafficHourly' => $hourlyData,
-            'monthDays' => range(1, date('j')), // 本月天数
-            'dayHours' => range(0, date('G') + 1), // 本日小时
+            'monthDays'     => range(1, date('j')), // 本月天数
+            'dayHours'      => range(0, date('G') + 1), // 本日小时
         ];
     }
+
+    /*
+        // 将Base64图片转换为本地图片并保存
+        public function base64ImageSaver($base64_image_content): ?string
+        {
+            // 匹配出图片的格式
+            if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
+                $type = $result[2];
+
+                $directory = date('Ymd');
+                $path = '/assets/images/qrcode/'.$directory.'/';
+                // 检查是否有该文件夹,如果没有就创建,并给予最高权限
+                if (! file_exists(public_path($path))
+                    && ! mkdir($concurrentDirectory = public_path($path), 0755, true)
+                    && ! is_dir($concurrentDirectory)) {
+                    throw new RuntimeException(sprintf('Directory "%s" was not created', $concurrentDirectory));
+                }
+
+                $fileName = Str::random(18).".{$type}";
+                if (file_put_contents(public_path($path.$fileName), base64_decode(str_replace($result[1], '', $base64_image_content)))) {
+                    chmod(public_path($path.$fileName), 0744);
+
+                    return $path.$fileName;
+                }
+            }
+
+            return '';
+        }
+     */
 }