StatisticsLogic.php
9.1 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
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;
use Throwable;
class StatisticsLogic extends BaseLogic
{
public $trafficStatistics;
private $customerVisit;
public $date;
public $time = 60 * 60 * 2;
public function __construct()
{
parent::__construct();
$this->customerVisit = new CustomerVisit();
$this->trafficStatistics = new TrafficStatistics();
try {
$this->date = request()->get('date') ?? null;
// 判断传入日期是否大于当月
if (checkIsGreaterMonth($this->date)) {
$this->date = null;
}
// 判断传入日期是否是当月
if (checkIsMonth($this->date)) {
$this->date = null;
}
} catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) {
$this->date = null;
}
}
/**
* @param $type
* @return array|Repository|mixed
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface|Throwable
*/
public function getStatisticsData($type)
{
$function = debug_backtrace()[1]['function'];
// 判断是否查询当月数据
if (!is_null($this->date)) {
// 获取缓存名
$key = $this->cacheName($type, $this->date);
$response = cache()->get($key);
if (empty($response)) {
$dd = explode('-', $this->date);
$year = $dd[0] ?? null;
$month = str_pad($dd[1], 2, '0', STR_PAD_LEFT) ?? null;
// 获取当前数据是否存在
$data = $this->trafficStatistics->getData($type, $year, $month);
if (is_null($data)) {
$response = $this->$function();
// 存在则直接返回
$response_data = array_filter($response);
if ($response_data) {
// 不存在则请求接口获取数据并保存
$this->create($response_data);
}
} else {
$response = $data->toArray();
}
// 缓存数据
cache()->put($key, $response, $this->time);
}
} else {
$response = $this->$function();
}
return $response;
}
/**
* 获取当月的每天的流量趋势
* @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
*/
public function create($response)
{
DB::beginTransaction();
$keys = array_flip($this->trafficStatistics::KEY_VALUE);
foreach ($response as $k => $v) {
if (is_array($v)) {
$v = json_encode($v);
}
$field = $keys[$k];
$this->trafficStatistics->$field = $v;
}
$isRes = $this->trafficStatistics->save();
if (!$isRes) {
DB::rollBack();
return false;
}
DB::commit();
return true;
}
/**
* 访问来源
* @return array
*/
public function source()
{
$response = $this->getPvIp();
$response['lists'] = $this->customerVisit->getUrlCount();
return $response;
}
public function getPvIp()
{
$response['pv_count'] = $this->customerVisit->getMonthPVCount();
$response['ip_count'] = $this->customerVisit->getMonthIPCount();
return $response;
}
/**
* 地域分布
* @return array
*/
public function distribution()
{
$response = $this->getPvIp();
$response['lists'] = $this->customerVisit->getCountryCount();
return $response;
}
/**
* 流量趋势
* @return array
* @throws ContainerExceptionInterface
* @throws GuzzleException
* @throws NotFoundExceptionInterface
*/
public function trend()
{
$response = $this->getPvIp();
$inquiry = $this->getCache('inquiry_information', $this->date);
$response['in_count'] = $inquiry['count'] ?? 0;
$response['lists'] = $inquiry['lists'] ?? [];
return $response;
}
/**
* 访问终端
* @return array
*/
public function terminal()
{
$response = $this->getPvIp();
$response['pc_count'] = $this->customerVisit->getTerminalPcCount();
$response['mobile_count'] = $this->customerVisit->getTerminalMobileCount();
$response['lists'] = $this->customerVisit->getCountryCount();
return $response;
}
/**
* 受访页面
* @return array
*/
public function page()
{
$response = $this->getPvIp();
$response['lists'] = $this->customerVisit->getPageCount();
return $response;
}
/**
* 获取缓存
* @param string $key 缓存键名
* @param string|null $date 日期 格式:Y-m
* @param float|int $time 缓存时间,默认2小时
* @return mixed
* @throws ContainerExceptionInterface
* @throws GuzzleException
* @throws NotFoundExceptionInterface
*/
public function getCache(string $key, string $date = null, $time = 60 * 60 * 2)
{
$domain = request()->getHttpHost(); //'www.wowstainless.com';
$sta_date = !is_null($date) ? $date . '-01' : date('Y-m-01');
$key = $key . '_' . stringUnderlineLowercase($domain) . '_' . str_replace('-', '_', $sta_date);
$value = cache()->get($key);
if (!$value) {
$value = getInquiryInformation($domain, $sta_date);
cache()->put($key, $value, $time);
}
return $value;
}
/**
* @param int $type
* @param string|null $date 日期 格式:Y-m-d
* @return string
*/
public function cacheName(int $type = TrafficStatistics::TYPE_SOURCE, string $date = null)
{
return 'statistics_' . $type . '_' . str_replace('-', '_', $date);
}
/**
* 判断传入日期是否是当日
* @param $date
* @return bool
*/
public function checkIsDay($date)
{
// 传入日期的时间戳
$timestamp = strtotime($date);
// 当日的日期格式,如:2021-10-13
$today = date('Y-m-d');
// 传入日期的日期格式,如:2021-10-12
$date = date('Y-m-d', $timestamp);
// 判断传入日期是否是当日
return $date == $today;
}
/**
* 判断传入日期是否大于当日
* @param $date
* @return bool
*/
public function checkIsGreaterDay($date)
{
// 传入日期的时间戳
$timestamp = strtotime($date);
// 当前日期的时间戳
$now = strtotime(date('Y-m-d'));
// 判断传入日期是否大于当前日期
return $timestamp > $now;
}
}