Skip to content

Commit 726dddd

Browse files
author
izhangxm
committed
合并胖虎每天自动统计昨天所有用户的所有节点流量使用情况提交
2 parents d67b41d + ecef3b2 commit 726dddd

File tree

7 files changed

+263
-5
lines changed

7 files changed

+263
-5
lines changed

.gitignore

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
/node_modules
21
/config/database.php
3-
/public/hot
4-
/public/storage
5-
/storage/*.key
2+
/public/upload
3+
/storage
64
/vendor
75
/.idea
86
/.vagrant
@@ -12,4 +10,3 @@ Homestead.json
1210
Homestead.yaml
1311
npm-debug.log
1412
.DS_Store
15-
public/upload/image/qrcode/
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use App\Http\Models\User;
7+
use App\Http\Models\SsNode;
8+
use App\Http\Models\UserTrafficDaily;
9+
use App\Http\Models\UserTrafficLog;
10+
use Log;
11+
12+
class AutoStatisticsUserDailyTrafficJob extends Command
13+
{
14+
protected $signature = 'command:autoStatisticsUserDailyTrafficJob';
15+
protected $description = '用户每日流量自动统计';
16+
17+
public function __construct()
18+
{
19+
parent::__construct();
20+
}
21+
22+
public function handle()
23+
{
24+
$userList = User::get();
25+
foreach ($userList as $user) {
26+
// 统计一次所有节点的总和
27+
$this->statisticsByNode($user->id);
28+
29+
// 统计每个节点产生的流量
30+
$nodeList = SsNode::get();
31+
foreach ($nodeList as $node) {
32+
$this->statisticsByNode($user->id, $node->id);
33+
}
34+
}
35+
36+
Log::info('定时任务:' . $this->description);
37+
}
38+
39+
private function statisticsByNode($user_id, $node_id = 0) {
40+
$start_time = strtotime(date('Y-m-d 00:00:00', strtotime("-1 day")));
41+
$end_time = strtotime(date('Y-m-d 23:59:59', strtotime("-1 day")));
42+
43+
$query = UserTrafficLog::where('user_id', $user_id)->whereBetween('log_time', [$start_time, $end_time]);
44+
45+
if ($node_id) {
46+
$query->where('node_id', $node_id);
47+
}
48+
49+
$u = $query->sum('u');
50+
$d = $query->sum('d');
51+
$total = $u + $d;
52+
$traffic = $this->flowAutoShow($total);
53+
54+
$obj = new UserTrafficDaily();
55+
$obj->user_id = $user_id;
56+
$obj->node_id = $node_id;
57+
$obj->u = $u;
58+
$obj->d = $d;
59+
$obj->total = $total;
60+
$obj->traffic = $traffic;
61+
$obj->save();
62+
}
63+
64+
// 根据流量值自动转换单位输出
65+
private function flowAutoShow($value = 0)
66+
{
67+
$kb = 1024;
68+
$mb = 1048576;
69+
$gb = 1073741824;
70+
$tb = $gb * 1024;
71+
$pb = $tb * 1024;
72+
if (abs($value) > $pb) {
73+
return round($value / $pb, 2) . "PB";
74+
} elseif (abs($value) > $tb) {
75+
return round($value / $tb, 2) . "TB";
76+
} elseif (abs($value) > $gb) {
77+
return round($value / $gb, 2) . "GB";
78+
} elseif (abs($value) > $mb) {
79+
return round($value / $mb, 2) . "MB";
80+
} elseif (abs($value) > $kb) {
81+
return round($value / $kb, 2) . "KB";
82+
} else {
83+
return round($value, 2) . "B";
84+
}
85+
}
86+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use App\Http\Models\User;
7+
use App\Http\Models\SsNode;
8+
use App\Http\Models\UserTrafficLog;
9+
use App\Http\Models\UserTrafficHourly;
10+
use Log;
11+
12+
class AutoStatisticsUserHourlyTrafficJob extends Command
13+
{
14+
protected $signature = 'command:autoStatisticsUserHourlyTrafficJob';
15+
protected $description = '用户流量每小时自动统计';
16+
17+
public function __construct()
18+
{
19+
parent::__construct();
20+
}
21+
22+
public function handle()
23+
{
24+
$userList = User::get();
25+
foreach ($userList as $user) {
26+
// 统计一次所有节点的总和
27+
$this->statisticsByNode($user->id);
28+
29+
// 统计每个节点产生的流量
30+
$nodeList = SsNode::get();
31+
foreach ($nodeList as $node) {
32+
$this->statisticsByNode($user->id, $node->id);
33+
}
34+
}
35+
36+
Log::info('定时任务:' . $this->description);
37+
}
38+
39+
private function statisticsByNode($user_id, $node_id = 0) {
40+
$start_time = strtotime(date('Y-m-d H:i:s', strtotime("-1 hour")));
41+
$end_time = time();
42+
43+
$query = UserTrafficLog::where('user_id', $user_id)->whereBetween('log_time', [$start_time, $end_time]);
44+
45+
if ($node_id) {
46+
$query->where('node_id', $node_id);
47+
}
48+
49+
$u = $query->sum('u');
50+
$d = $query->sum('d');
51+
$total = $u + $d;
52+
$traffic = $this->flowAutoShow($total);
53+
54+
$obj = new UserTrafficHourly();
55+
$obj->user_id = $user_id;
56+
$obj->node_id = $node_id;
57+
$obj->u = $u;
58+
$obj->d = $d;
59+
$obj->total = $total;
60+
$obj->traffic = $traffic;
61+
$obj->save();
62+
}
63+
64+
// 根据流量值自动转换单位输出
65+
private function flowAutoShow($value = 0)
66+
{
67+
$kb = 1024;
68+
$mb = 1048576;
69+
$gb = 1073741824;
70+
$tb = $gb * 1024;
71+
$pb = $tb * 1024;
72+
if (abs($value) > $pb) {
73+
return round($value / $pb, 2) . "PB";
74+
} elseif (abs($value) > $tb) {
75+
return round($value / $tb, 2) . "TB";
76+
} elseif (abs($value) > $gb) {
77+
return round($value / $gb, 2) . "GB";
78+
} elseif (abs($value) > $mb) {
79+
return round($value / $mb, 2) . "MB";
80+
} elseif (abs($value) > $kb) {
81+
return round($value / $kb, 2) . "KB";
82+
} else {
83+
return round($value, 2) . "B";
84+
}
85+
}
86+
}

app/Console/Kernel.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class Kernel extends ConsoleKernel
1919
\App\Console\Commands\AutoExpireInviteJob::class,
2020
//\App\Console\Commands\AutoGetLocationInfoJob::class,
2121
\App\Console\Commands\AutoResetUserTrafficJob::class,
22+
\App\Console\Commands\AutoStatisticsUserDailyTrafficJob::class,
23+
\App\Console\Commands\AutoStatisticsUserHourlyTrafficJob::class,
2224
\App\Console\Commands\UserExpireWarningJob::class,
2325
\App\Console\Commands\UserTrafficWarningJob::class,
2426
];
@@ -37,6 +39,8 @@ protected function schedule(Schedule $schedule)
3739
$schedule->command('command:autoExpireInviteJob')->everyThirtyMinutes();
3840
//$schedule->command('command:autoGetLocationInfoJob')->everyMinute();
3941
$schedule->command('command:autoResetUserTrafficJob')->monthly();
42+
$schedule->command('command:autoStatisticsUserDailyTrafficJob')->dailyAt('04:00');
43+
$schedule->command('command:autoStatisticsUserHourlyTrafficJob')->hourly();
4044
$schedule->command('command:userExpireWarningJob')->daily();
4145
$schedule->command('command:userTrafficWarningJob')->daily();
4246
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace App\Http\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
/**
8+
* 用户每日流量统计
9+
* Class UserTrafficDaily
10+
* @package App\Http\Models
11+
*/
12+
class UserTrafficDaily extends Model
13+
{
14+
protected $table = 'user_traffic_daily';
15+
protected $primaryKey = 'id';
16+
protected $fillable = [
17+
'user_id',
18+
'node_id',
19+
'u',
20+
'd',
21+
'total',
22+
'traffic'
23+
];
24+
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace App\Http\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
/**
8+
* 用户流量每小时统计
9+
* Class Article
10+
* @package App\Http\Models
11+
*/
12+
class UserTrafficHourly extends Model
13+
{
14+
protected $table = 'user_traffic_hourly';
15+
protected $primaryKey = 'id';
16+
protected $fillable = [
17+
'user_id',
18+
'node_id',
19+
'u',
20+
'd',
21+
'total',
22+
'traffic'
23+
];
24+
25+
}

sql/db.sql

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,41 @@ CREATE TABLE `user_subscribe_log` (
598598
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
599599

600600

601+
-- ----------------------------
602+
-- Table structure for `user_traffic_daily`
603+
-- ----------------------------
604+
CREATE TABLE `user_traffic_daily` (
605+
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
606+
`user_id` int(11) NOT NULL DEFAULT '0' COMMENT '用户ID',
607+
`node_id` int(11) NOT NULL DEFAULT '0' COMMENT '节点ID,0表示统计全部节点',
608+
`u` bigint(20) NOT NULL DEFAULT '0' COMMENT '上传流量',
609+
`d` bigint(20) NOT NULL DEFAULT '0' COMMENT '下载流量',
610+
`total` bigint(20) NOT NULL DEFAULT '0' COMMENT '总流量',
611+
`traffic` varchar(255) DEFAULT '' COMMENT '总流量(带单位)',
612+
`created_at` datetime DEFAULT NULL COMMENT '创建时间',
613+
`updated_at` datetime DEFAULT NULL COMMENT '最后一起更新时间',
614+
PRIMARY KEY (`id`)
615+
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4;
616+
617+
618+
-- ----------------------------
619+
-- Table structure for `user_traffic_hourly`
620+
-- ----------------------------
621+
CREATE TABLE `user_traffic_hourly` (
622+
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
623+
`user_id` int(11) NOT NULL DEFAULT '0' COMMENT '用户ID',
624+
`node_id` int(11) NOT NULL DEFAULT '0' COMMENT '节点ID,0表示统计全部节点',
625+
`u` bigint(20) NOT NULL DEFAULT '0' COMMENT '上传流量',
626+
`d` bigint(20) NOT NULL DEFAULT '0' COMMENT '下载流量',
627+
`total` bigint(20) NOT NULL DEFAULT '0' COMMENT '总流量',
628+
`traffic` varchar(255) DEFAULT '' COMMENT '总流量(带单位)',
629+
`created_at` datetime DEFAULT NULL COMMENT '创建时间',
630+
`updated_at` datetime DEFAULT NULL COMMENT '最后一起更新时间',
631+
PRIMARY KEY (`id`)
632+
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4;
633+
634+
635+
601636
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
602637
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
603638
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;

0 commit comments

Comments
 (0)