StatisticsDayTrend.php
2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
namespace App\Console\Commands\Bside\Statistics;
use App\Models\Bside\Statistics\TrafficTrends;
use App\Models\CustomerVisit\CustomerVisit;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Throwable;
class StatisticsDayTrend extends Command
{
public $error = 0;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'statistics_day_trend';
/**
* The console command description.
*
* @var string
*/
protected $description = '统计当天流量趋势数据';
/**
* @return void
* @throws GuzzleException
* @throws Throwable
*/
public function handle()
{
$date = getPreviousDaysDate();
$trafficTrends = new TrafficTrends();
$lists = $trafficTrends->getDaysLists($date);
if (!empty($lists)) {
$date = array_diff($date, $lists->pluck('day')->all());
}
if (!empty($date)) {
foreach ($date as $value) {
echo $this->statistics_day_trend($value);
}
}
echo $this->statistics_day_trend();
}
/**
* 统计当天流量趋势数据PV|IP数量
* @return int|mixed
* @throws GuzzleException|Throwable
*/
protected function statistics_day_trend($date = null)
{
DB::beginTransaction();
$date = $date ?? date('Y-m-d');
// 获取当天的IP|PV数量
$customerVisit = new CustomerVisit();
$ip_count = $customerVisit->getDayIPCount($date);
$pv_count = $customerVisit->getDayPVCount($date);
// 统计数据并插入到数据库
$trafficTrends = new TrafficTrends();
$isRes = $trafficTrends->getDay($date);
if ($isRes) {
$trafficTrends = $isRes;
}
$trafficTrends->day = $date;
$trafficTrends->pvnum = $pv_count;
$trafficTrends->ipnum = $ip_count;
$res = $trafficTrends->save();
if ($res) {
DB::commit();
$this->info($date . ' - 统计当天流量趋势数据PV|IP数量成功');
} else {
DB::rollBack();
$this->error++;
$this->info($date . ' - 统计当天流量趋势数据PV|IP数量失败');
}
return $this->error;
}
}