WorkerAbstract.php
3.07 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
<?php
/**
* Created by PhpStorm.
* User: Zip
* Date: 16/1/8
* Time: 11:41
*/
namespace Hood\Utils\Statistic;
abstract class WorkerAbstract implements WorkerInterface
{
static public function makeData(Data $data)
{
$data->post_params = $_POST;
$data->get_params = $_GET;
$data->ip = empty($_SERVER['REMOTE_ADDR']) ? '' : $_SERVER['REMOTE_ADDR'];
$data->uri = empty($_SERVER['REQUEST_URI']) ? '' : $_SERVER['REQUEST_URI'];
$data->memory = memory_get_usage();
$error = error_get_last();
$params = array(
'post' => $data->post_params,
'get' => $data->get_params
);
if (isset($error['type'])) {
$data->code = 500;
$data->data = $error;
}
#ip 开始时间 执行时间 code 相关数据
return $data->ip . "\t" . $data->begin_time . "\t" . (microtime(true) - $data->begin_time) . "\t" . $data->memory . "\t" . $data->type . "\t" . $data->code . "\t" . json_encode($params) . "\t" . json_encode($data->data) . "\n";
}
static public function reader(Data $data)
{
$logFile = $data->statistic_path;
$handle = @fopen($logFile, 'r');
if (!$handle) {
return '';
}
$statisticsData = array();
while (!feof($handle)) {
$line = fgets($handle, 4096);
if ($line) {
$explode = explode("\t", $line);
if (count($explode) < 7) {
continue;
}
list($ip, $time, $suc_count, $suc_cost_time, $fail_count, $fail_cost_time, $code_map) = $explode;
$time = ceil($time / 300) * 300;
if (!isset($statistics_data[$time])) {
$statistics_data[$time] = array();
}
if (!isset($statistics_data[$time][$ip])) {
$statistics_data[$time][$ip] = array(
'suc_count' => 0,
'suc_cost_time' => 0,
'fail_count' => 0,
'fail_cost_time' => 0,
'code_map' => array(),
);
}
$statistics_data[$time][$ip]['suc_count'] += $suc_count;
$statistics_data[$time][$ip]['suc_cost_time'] += round($suc_cost_time, 5);
$statistics_data[$time][$ip]['fail_count'] += $fail_count;
$statistics_data[$time][$ip]['fail_cost_time'] += round($fail_cost_time, 5);
$code_map = json_decode(trim($code_map), true);
if ($code_map && is_array($code_map)) {
foreach ($code_map as $code => $count) {
if (!isset($statistics_data[$time][$ip]['code_map'][$code])) {
$statistics_data[$time][$ip]['code_map'][$code] = 0;
}
$statistics_data[$time][$ip]['code_map'][$code] += $count;
}
}
}
}
fclose($handle);
ksort($statisticsData);
}
}