Authored by ziy

1、添加数据库强制主、从

2、添加简单统计方法
... ... @@ -57,6 +57,19 @@ class Root
protected $_delTags = array();
/**
* 强制使用从
* @var bool
*/
protected $_slave = false;
/**
* 强制使用主
* @var bool
*/
protected $_master = false;
public function __construct()
{
defined('APPLICATION_ENV') || define('APPLICATION_ENV', 'developer');
... ... @@ -112,4 +125,17 @@ class Root
$server->setFileName($serviceFile);
return $server;
}
public function master()
{
$this->_master = true;
return $this;
}
public function slave()
{
$this->_slave = true;
return $this;
}
}
\ No newline at end of file
... ...
... ... @@ -177,6 +177,26 @@ class PDOConnection extends Root
}
/**
* 强制设置主
* @return $this
*/
public function master()
{
$this->_modality = 'write';
return $this;
}
/**
* 强制设置从
* @return $this
*/
public function slave()
{
$this->_modality = 'read';
return $this;
}
/**
* 创建PDO数据库对象
* @param string $modality (write/read)
* @return \PDO
... ...
<?php
/**
* Created by PhpStorm.
* User: Zip
* Date: 16/1/8
* Time: 16:50
*/
namespace Hood\Utils\Statistic;
class Data
{
public $begin_time = 0;
public $uri = '';
public $ip = '';
public $time = 0;
public $post_params = array();
public $get_params = array();
public $statistic_path = '/tmp/statistic/';
public $data = array();
public $code = 200;
public $type = 'log';
public $memory = 0;
}
\ No newline at end of file
... ...
<?php
/**
* Created by PhpStorm.
* User: Zip
* Date: 16/1/8
* Time: 11:36
*/
namespace Hood\Utils\Statistic;
class Files extends WorkerAbstract
{
static public function logs(Data $data)
{
self::write($data);
}
static public function write(Data $data)
{
$statistData = self::makeData($data);
$uri = str_replace('/', '_', $data->uri);
if (empty($uri)) $uri = 'system';
if (!is_dir($data->statistic_path)) {
umask(0);
mkdir($data->statistic_path, 0777, true);
}
file_put_contents($data->statistic_path . $uri . "." . date('Y-m-d') . '.log', $statistData, FILE_APPEND | LOCK_EX);
}
static public function error($errno, $errstr, $errfile, $errline)
{
$data = new Data();
$data->code = 500;
$data->type = 'error';
$data->data = array(
'errno' => $errno,
'errstr' => $errstr,
'errfile' => $errfile,
'errline' => $errline,
'backtrace' => debug_backtrace()
);
self::write($data);
}
static public function exception(\Exception $exception)
{
$data = new Data();
$data->code = 500;
$data->type = 'exception';
$data->data = array(
'code' => $exception->getCode(),
'message' => $exception->getMessage(),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'trace' => $exception->getTrace(),
'backtrace' => debug_backtrace()
);
self::write($data);
}
}
\ No newline at end of file
... ...
<?php
/**
* Created by PhpStorm.
* User: Zip
* Date: 16/1/8
* Time: 11:36
*/
namespace Hood\Utils\Statistic;
class Redis extends WorkerAbstract
{
public function write(Data $data)
{
// TODO: Implement write() method.
}
public function reader()
{
// TODO: Implement reader() method.
}
public function error($errno, $errstr, $errfile, $errline)
{
// TODO: Implement error() method.
}
public function exception($exception)
{
// TODO: Implement exception() method.
}
}
\ No newline at end of file
... ...
<?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();
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($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);
}
}
\ No newline at end of file
... ...
<?php
/**
* Created by PhpStorm.
* User: Zip
* Date: 16/1/6
* Time: 15:29
*/
namespace Hood\Utils\Statistic;
/**
* Class Worker
* @package Hood\Utils\Statistic
*
* ip,time(),
*/
interface WorkerInterface
{
static public function write(Data $data);
static public function reader(Data $data);
}
\ No newline at end of file
... ...