作者 李美松

更新统计流量代码,新增流量趋势天数的获取并保存

... ... @@ -67,7 +67,7 @@ if (!function_exists('http_post')) {
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($ch);
if (curl_errno($ch)) {
\Illuminate\Support\Facades\Log::info(print_r(curl_errno($ch),1),'debug---1');
\Illuminate\Support\Facades\Log::info(print_r(curl_errno($ch), 1), 'debug---1');
}
curl_close($ch);
return json_decode($res, true);
... ... @@ -321,3 +321,86 @@ if (!function_exists('stringUnderlineLowercase')) {
return trim(strtolower(preg_replace('/[^a-zA-Z0-9]/', '_', $name)));
}
}
if (!function_exists('checkIsGreaterMonth')) {
/**
* 判断传入日期是否大于当月
* @param $date
* @return bool
*/
function checkIsGreaterMonth($date)
{
// 传入日期的时间戳
$timestamp = strtotime($date);
// 当前月份的时间戳
$nowMonth = strtotime(date('Y-m'));
// 判断传入日期是否大于当前月份
return $timestamp > $nowMonth;
}
}
if (!function_exists('checkIsMonth')) {
/**
* 判断传入日期是否是当月
* @param $date
* @return bool
*/
function checkIsMonth($date)
{
// 获取当前时间戳
$now = time();
// 获取当月的起始时间戳和结束时间戳
$firstDay = strtotime(date('Y-m-01', $now));
$lastDay = strtotime(date('Y-m-t', $now));
// 传入日期的时间戳
$timestamp = strtotime($date);
// 判断传入日期是否在当月范围内
return $timestamp >= $firstDay && $timestamp <= $lastDay;
}
}
if (!function_exists('getDateDays')) {
/**
* 返回当月到今天的天数
* @param string|null $date 日期,格式:Y-m
* @return array
*/
function getDateDays(string $date = null)
{
list($year, $month, $day) = explode('-', date('Y-m-d'));
// 获取当前月的第一天
$first_day_of_month = "{$year}-{$month}-01";
// 获取今天的日期
$today = "{$year}-{$month}-{$day}";
if (!is_null($date)) {
$dd = explode('-', $date);
if (!checkIsGreaterMonth($date) && !checkIsMonth($date)) {
$year = $dd[0];
$month = $dd[1];
$first_day_of_month = "{$year}-{$month}-01";
return getDateArray("{$year}-{$month}-" . date("t", strtotime($first_day_of_month)));
}
}
$day_timestamp = strtotime($today) - strtotime($first_day_of_month);
return getDateArray("{$year}-{$month}-" . date('d', $day_timestamp));
}
}
if (!function_exists('getDateArray')) {
/**
* 获取当月获取日期
* @param string $date 日期,格式:Y-m-d
* @return array
*/
function getDateArray(string $date)
{
list($year, $month, $day) = explode('-', date($date));
$i = 1;
$days = [];
while ($i <= $day) {
$days[] = "{$year}-{$month}-" . str_pad($i, 2, "0", STR_PAD_LEFT);
$i++;
}
return $days;
}
}
... ...
... ... @@ -89,6 +89,7 @@ class StatisticsController extends BaseController
{
$type = $this->trafficStatistics::TYPE_TREND;
$response = $this->statisticsLogic->getStatisticsData($type);
$response['days'] = $this->statisticsLogic->getDaysTrend();
return $this->success($response);
}
... ...
... ... @@ -4,9 +4,12 @@ namespace App\Http\Logic\Bside\Statistics;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\Bside\Statistics\TrafficStatistics;
use App\Models\Bside\Statistics\TrafficTrends;
use App\Models\CustomerVisit\CustomerVisit;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Contracts\Cache\Repository;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\DB;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
... ... @@ -30,11 +33,11 @@ class StatisticsLogic extends BaseLogic
try {
$this->date = request()->get('date') ?? null;
// 判断传入日期是否大于当月
if ($this->checkIsGreaterMonth($this->date)) {
if (checkIsGreaterMonth($this->date)) {
$this->date = null;
}
// 判断传入日期是否是当月
if ($this->checkIsMonth($this->date)) {
if (checkIsMonth($this->date)) {
$this->date = null;
}
} catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) {
... ... @@ -83,6 +86,57 @@ class StatisticsLogic extends BaseLogic
}
/**
* 获取当月的每天的流量趋势
* @return Builder[]|Collection
*/
public function getDaysTrend()
{
$trafficTrends = new TrafficTrends();
$data = $trafficTrends->getMonthsLists($this->date);
$original_date = $data->pluck('date')->all();
// 获取当月的每天的日期
$month_date = getDateDays($this->date);
// 判断是否存在缺失的日期
if (count($month_date) > 1) {
// 去除当天的日期
array_pop($month_date);
// 获取缺失的日期
$diff = array_diff($month_date, $original_date);
if ($diff) {
// 获取缺失的数据并保存
$data = array_merge($data->toArray(), $this->getTrendsLists($diff));
}
}
$date = date('Y-m-d');
$ip_count = $this->customerVisit->getDayIPCount();
$pv_count = $this->customerVisit->getDayPVCount();
$data[] = compact('date', 'ip_count', 'pv_count');
return $data;
}
/**
* 获取缺失的数据并保存
* @param array $dates 日期,格式:['y-m-d', 'y-m-d']
* @return array
*/
public function getTrendsLists(array $dates)
{
$data = [];
$trafficTrends = new TrafficTrends();
foreach ($dates as $item) {
$date = $item;
$ip_count = $this->customerVisit->getDayIPCount($item);
$pv_count = $this->customerVisit->getDayPVCount($item);
$trafficTrends->day = $item;
$trafficTrends->ipnum = $ip_count;
$trafficTrends->pvnum = $pv_count;
$trafficTrends->save();
$data[] = compact('date', 'ip_count', 'pv_count');
}
return $data;
}
/**
* @param $response
* @return bool
* @throws Throwable
... ... @@ -240,37 +294,4 @@ class StatisticsLogic extends BaseLogic
// 判断传入日期是否大于当前日期
return $timestamp > $now;
}
/**
* 判断传入日期是否大于当月
* @param $date
* @return bool
*/
public function checkIsGreaterMonth($date)
{
// 传入日期的时间戳
$timestamp = strtotime($date);
// 当前月份的时间戳
$nowMonth = strtotime(date('Y-m'));
// 判断传入日期是否大于当前月份
return $timestamp > $nowMonth;
}
/**
* 判断传入日期是否是当月
* @param $date
* @return bool
*/
public function checkIsMonth($date)
{
// 获取当前时间戳
$now = time();
// 获取当月的起始时间戳和结束时间戳
$firstDay = strtotime(date('Y-m-01', $now));
$lastDay = strtotime(date('Y-m-t', $now));
// 传入日期的时间戳
$timestamp = strtotime($date);
// 判断传入日期是否在当月范围内
return $timestamp >= $firstDay && $timestamp <= $lastDay;
}
}
... ...
... ... @@ -32,33 +32,50 @@ class TrafficTrends extends Base
protected $table = 'gl_traffic_trends';
/**
* * 根据时间获取数据
* @param array|string $date
* 根据时间获取数据
* @param string|null $date 日期,格式:Y-m-d
* @return TrafficTrends[]|Builder[]|Collection
*/
public function getDaysLists($date = null)
public function getDaysLists(string $date = null)
{
$query = $this->query();
if ($date != null) {
if (is_array($date)) {
if ($date != null && is_array($date)) {
$query->whereIn('day', $date);
} else {
$query->where('day', $date);
}
} else {
$query->whereYear('day', '=', date("Y"))
->whereMonth('day', '=', date("m"))
->whereDay('day', '=', date("d"));
}
return $query->get();
}
/**
* @param string|null $date
* @param string|null $date 日期,格式:Y-m-d
* @return TrafficTrends|Builder|Model|object|null
*/
public function getDay(string $date = null)
{
return $this->query()->where('day', $date ?? date('Y-m-d'))->first();
}
/**
* 获取当月的IP|PV数量
* @param string|null $date 日期,格式:Y-m
* @return Builder[]|Collection
*/
public function getMonthsLists(string $date = null)
{
if (!is_null($date)) {
$dd = explode('-', $date);
$year = $dd[0];
$month = $dd[1];
} else {
$year = date('Y');
$month = date('m');
}
return $this->query()
->selectRaw('day as date, pvnum as pv_count, ipnum as ip_count')
->whereYear('day', '=', $year)
->whereMonth('day', '=', $month)
->orderBy('day', 'asc')
->get();
}
}
... ...
... ... @@ -5,6 +5,20 @@
use Illuminate\Support\Facades\Route;
// 流量统计
Route::prefix('stat')->group(function () {
// 访问来源
Route::get('/source', [\App\Http\Controllers\Bside\StatisticsController::class, 'source'])->name('stat_source');
// 地域分布
Route::get('/distribution', [\App\Http\Controllers\Bside\StatisticsController::class, 'distribution'])->name('stat_distribution');
// 受访页面
Route::get('/page', [\App\Http\Controllers\Bside\StatisticsController::class, 'page'])->name('stat_page');
// 访问终端
Route::get('/terminal', [\App\Http\Controllers\Bside\StatisticsController::class, 'terminal'])->name('stat_terminal');
// 流量趋势
Route::get('/trend', [\App\Http\Controllers\Bside\StatisticsController::class, 'trend'])->name('stat_trend');
});
//必须登录验证的路由组
Route::middleware(['bloginauth'])->group(function () {
//登录用户编辑个人资料
... ... @@ -167,19 +181,7 @@ Route::middleware(['bloginauth'])->group(function () {
Route::any('describe/delete', [\App\Http\Controllers\Bside\Product\DescribeController::class, 'delete'])->name('product_describe_delete');
});
// 流量统计
Route::prefix('stat')->group(function () {
// 访问来源
Route::get('/source', [\App\Http\Controllers\Bside\StatisticsController::class, 'source'])->name('stat_source');
// 地域分布
Route::get('/distribution', [\App\Http\Controllers\Bside\StatisticsController::class, 'distribution'])->name('stat_distribution');
// 受访页面
Route::get('/page', [\App\Http\Controllers\Bside\StatisticsController::class, 'page'])->name('stat_page');
// 访问终端
Route::get('/terminal', [\App\Http\Controllers\Bside\StatisticsController::class, 'terminal'])->name('stat_terminal');
// 流量趋势
Route::get('/trend', [\App\Http\Controllers\Bside\StatisticsController::class, 'trend'])->name('stat_trend');
});
//组织架构
Route::prefix('dept')->group(function () {
... ...