WorkerAbstract.php 3.17 KB
<?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'];
        $serverAddr = empty($_SERVER['SERVER_ADDR']) ? '' : $_SERVER['SERVER_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" . $serverAddr . "\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);
    }
}