walkor 11 роки тому
батько
коміт
b6a9f3ac32

+ 7 - 0
applications/Statistics/Lib/Cache.php

@@ -0,0 +1,7 @@
+<?php
+class Cache
+{
+    public static $statisticDataCache = array();
+    public static $ServerIpList = array();
+    public static $modulesDataCache = array();
+}

+ 81 - 0
applications/Statistics/Lib/functions.php

@@ -0,0 +1,81 @@
+<?php
+
+/**
+ * 批量请求
+ * @param array $request_buffer_array ['ip:port'=>req_buf, 'ip:port'=>req_buf, ...]
+ * @return multitype:unknown string
+ */
+function multiRequest($request_buffer_array)
+{
+    $client_array = $sock_to_ip = $ip_list = array();
+    foreach($request_buffer_array as $address => $buffer)
+    {
+        list($ip, $port) = explode(':', $address);
+        $ip_list[$ip] = $ip;
+        $client = stream_socket_client("tcp://$address", $errno, $errmsg, 1);
+        if(!$client)
+        {
+            continue;
+        }
+        $client_array[$address] = $client;
+        stream_set_timeout($client_array[$address], 0, 100000);
+        fwrite($client_array[$address], $buffer);
+        stream_set_blocking($client_array[$address], 0);
+        $sock_to_address[(int)$client] = $address;
+    }
+    $read = $client_array;
+    $write = $except = $read_buffer = array();
+    $time_start = microtime(true);
+    $timeout = 0.99;
+    // 轮询处理数据
+    while(count($read) > 0)
+    {
+        if(stream_select($read, $write, $except, 0, 200000))
+        {
+            foreach($read as $socket)
+            {
+                $address = $sock_to_address[(int)$socket];
+                $buf = fread($socket, 8192);
+                if(!$buf)
+                {
+                    if(feof($socket))
+                    {
+                        unset($client_array[$address]);
+                    }
+                    continue;
+                }
+                if(!isset($read_buffer[$address]))
+                {
+                    $read_buffer[$address] = $buf;
+                }
+                else
+                {
+                    $read_buffer[$address] .= $buf;
+                }
+                // 数据接收完毕
+                if(0 === JMProtocol::checkInput($read_buffer[$address]))
+                {
+                    unset($client_array[$address]);
+                }
+            }
+        }
+        // 超时了
+        if(microtime(true) - $time_start > $timeout)
+        {
+            break;
+        }
+        $read = $client_array;
+    }
+
+    foreach($read_buffer as $address => $buf)
+    {
+        list($ip, $port) = explode(':', $address);
+        $this->lastSuccessIpArray[$ip] = $ip;
+    }
+
+    $this->lastFailedIpArray = array_diff($ip_list, $this->lastSuccessIpArray);
+
+    ksort($read_buffer);
+
+    return $read_buffer;
+}

+ 120 - 0
applications/Statistics/Modules/main.php

@@ -0,0 +1,120 @@
+<?php
+function main($module, $interface, $date, $date, $start_time, $offset)
+{
+    $module = 'WorkerMan';
+    $interface = 'Statistics';
+    multiRequestStAndModules($module, $interface, $date);
+    $all_st_str = '';
+    if(is_array(Cache::$statisticDataCache['statistic']))
+    {
+        foreach(Cache::$statisticDataCache['statistic'] as $ip=>$st_str)
+        {
+            $all_st_str .= $st_str;
+        }
+    }
+    $data = formatSt($all_st_str, $date);
+    
+    include ST_ROOT . '/Views/header.tpl.php';
+    include ST_ROOT . '/Views/main.tpl.php';
+    include ST_ROOT . '/Views/footer.tpl.php';
+}
+
+function multiRequestStAndModules($module, $interface, $date)
+{
+    Cache::$statisticDataCache['statistic'] = '';
+    $buffer = json_encode(array('module'=>$module, 'interface'=>$interface, 'date'=>$date))."\n";
+    $ip_list = (!empty($_GET['server_ip']) && is_array($_GET['server_ip'])) ? $_GET['server_ip'] : Cache::$ServerIpList;
+    $reqest_buffer_array = array();
+    $port = \Man\Core\Lib\Config::get('StatisticWorker.port') ? \Man\Core\Lib\Config::get('StatisticWorker.port') : 55656;
+    foreach($ip_list as $ip)
+    {
+        $reqest_buffer_array["$ip:$port"] = $buffer;
+    }
+    $read_buffer_array = multiRequest($reqest_buffer_array);
+    foreach($read_buffer_array as $address => $buf)
+    {
+        list($ip, $port) = explode(':',$address);
+        $body_data = json_decode(trim($buf), true);
+        $statistic_data = isset($body_data['statistic']) ? $body_data['statistic'] : '';
+        $modules_data = isset($body_data['modules']) ? $body_data['modules'] : array();
+        // 整理modules
+        foreach($modules_data as $mod => $interfaces)
+        {
+            if(!isset(Cache::$modulesDataCache[$mod]))
+            {
+                Cache::$modulesDataCache[$mod] = array();
+            }
+            foreach($interfaces as $if)
+            {
+                Cache::$modulesDataCache[$mod][$if] = $if;
+            }
+        }
+        Cache::$statisticDataCache['statistic'][$ip] = $statistic_data;
+    }
+}
+
+function formatSt($str, $date)
+{
+    // time:[suc_count:xx,suc_cost_time:xx,fail_count:xx,fail_cost_time:xx]
+    $st_data = array();
+    $st_explode = explode("\n", $str);
+    // 汇总计算
+    foreach($st_explode as $line)
+    {
+        // line = IP time suc_count suc_cost_time fail_count fail_cost_time code_json
+        $line_data = explode("\t", $line);
+        if(!isset($line_data[5]))
+        {
+            continue;
+        }
+        $time_line = $line_data[1];
+        $suc_count = $line_data[2];
+        $suc_cost_time = $line_data[3];
+        $fail_count = $line_data[4];
+        $fail_cost_time = $line_data[5];
+        if(!isset($st_data[$time_line]))
+        {
+            $st_data[$time_line] = array('suc_count'=>0, 'suc_cost_time'=>0, 'fail_count'=>0, 'fail_cost_time'=>0);
+        }
+        $st_data[$time_line]['suc_count'] += $suc_count;
+        $st_data[$time_line]['suc_cost_time'] += $suc_cost_time;
+        $st_data[$time_line]['fail_count'] += $fail_count;
+        $st_data[$time_line]['fail_cost_time'] += $fail_cost_time;
+    }
+    // 按照时间排序
+    ksort($st_data);
+    // time => [total_count:xx,suc_count:xx,suc_avg_time:xx,fail_count:xx,fail_avg_time:xx,percent:xx]
+    $data = array();
+    // 计算成功率 耗时
+    foreach($st_data as $time_line=>$item)
+    {
+        $data[$time_line] = array(
+                'time'          => date('Y-m-d H:i:s', $time_line),
+                'total_count'   => $item['suc_count']+$item['fail_count'],
+                'total_avg_time'=> $item['suc_count']+$item['fail_count'] == 0 ? 0 : round(($item['suc_cost_time']+$item['fail_cost_time'])/($item['suc_count']+$item['fail_count']), 4),
+                'suc_count'     => $item['suc_count'],
+                'suc_avg_time'  => $item['suc_count'] == 0 ? $item['suc_count'] : round($item['suc_cost_time']/$item['suc_count'], 4),
+                'fail_count'    => $item['fail_count'],
+                'fail_avg_time' => $item['fail_count'] == 0 ? 0 : round($item['fail_cost_time']/$item['fail_count'], 4),
+                'precent'       => $item['suc_count']+$item['fail_count'] == 0 ? 0 : round(($item['suc_count']*100/($item['suc_count']+$item['fail_count'])), 4),
+        );
+    }
+    $time_point =  strtotime($date);
+    for($i=0;$i<288;$i++)
+    {
+    $data[$time_point] = isset($data[$time_point]) ? $data[$time_point] :
+    array(
+            'time' => date('Y-m-d H:i:s', $time_point),
+            'total_count'   => 0,
+            'total_avg_time'=> 0,
+            'suc_count'     => 0,
+            'suc_avg_time'  => 0,
+            'fail_count'    => 0,
+            'fail_avg_time' => 0,
+            'precent'       => 100,
+            );
+            $time_point +=300;
+    }
+    ksort($data);
+    return $data;
+}

+ 5 - 0
applications/Statistics/Modules/statistic.php

@@ -0,0 +1,5 @@
+<?php
+function statistic($module, $interface, $date, $start_time, $offset)
+{
+    
+}

+ 2 - 0
applications/Statistics/Views/footer.tpl.php

@@ -0,0 +1,2 @@
+</body>
+</html>

+ 39 - 0
applications/Statistics/Views/header.tpl.php

@@ -0,0 +1,39 @@
+<!DOCTYPE html>
+<html lang="zh">
+<head>
+  <meta charset="utf-8">
+  <title>WorkerMan-集群统计与监控</title>
+  <meta name="viewport" content="width=device-width, initial-scale=1.0">
+  <meta name="description" content="">
+  <meta name="author" content="">
+
+	<!--link rel="stylesheet/less" href="/less/bootstrap.less" type="text/css" /-->
+	<!--link rel="stylesheet/less" href="/less/responsive.less" type="text/css" /-->
+	<!--script src="/js/less-1.3.3.min.js"></script-->
+	<!--append ‘#!watch’ to the browser URL, then refresh the page. -->
+	
+	<link href="/css/bootstrap.min.css" rel="stylesheet">
+	<link href="/css/style.css" rel="stylesheet">
+
+  <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
+  <!--[if lt IE 9]>
+    <script src="js/html5shiv.js"></script>
+  <![endif]-->
+
+  <!-- Fav and touch icons -->
+  <link rel="apple-touch-icon-precomposed" sizes="144x144" href="img/apple-touch-icon-144-precomposed.png">
+  <link rel="apple-touch-icon-precomposed" sizes="114x114" href="img/apple-touch-icon-114-precomposed.png">
+  <link rel="apple-touch-icon-precomposed" sizes="72x72" href="img/apple-touch-icon-72-precomposed.png">
+  <link rel="apple-touch-icon-precomposed" href="img/apple-touch-icon-57-precomposed.png">
+  <link rel="shortcut icon" href="img/favicon.png">
+  
+	<script type="text/javascript" src="/js/jquery.min.js"></script>
+	<script type="text/javascript" src="/js/bootstrap.min.js"></script>
+	<script type="text/javascript" src="/js/scripts.js"></script>
+	 <script type="text/javascript" src="/js/jquery.min.js"></script>
+	 <script type="text/javascript" src="/js/highcharts.js"></script>
+</head>
+<body>
+
+</body>
+</html>

+ 0 - 297
applications/Statistics/Views/home.php

@@ -1,297 +0,0 @@
-<!DOCTYPE html>
-<html lang="en">
-<head>
-  <meta charset="utf-8">
-  <title>Bootstrap 3, from LayoutIt!</title>
-  <meta name="viewport" content="width=device-width, initial-scale=1.0">
-  <meta name="description" content="">
-  <meta name="author" content="">
-
-	<!--link rel="stylesheet/less" href="less/bootstrap.less" type="text/css" /-->
-	<!--link rel="stylesheet/less" href="less/responsive.less" type="text/css" /-->
-	<!--script src="js/less-1.3.3.min.js"></script-->
-	<!--append ‘#!watch’ to the browser URL, then refresh the page. -->
-	
-	<link href="/css/bootstrap.min.css" rel="stylesheet">
-	<link href="/css/style.css" rel="stylesheet">
-
-  <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
-  <!--[if lt IE 9]>
-    <script src="js/html5shiv.js"></script>
-  <![endif]-->
-
-  <!-- Fav and touch icons -->
-  <link rel="apple-touch-icon-precomposed" sizes="144x144" href="img/apple-touch-icon-144-precomposed.png">
-  <link rel="apple-touch-icon-precomposed" sizes="114x114" href="img/apple-touch-icon-114-precomposed.png">
-  <link rel="apple-touch-icon-precomposed" sizes="72x72" href="img/apple-touch-icon-72-precomposed.png">
-  <link rel="apple-touch-icon-precomposed" href="img/apple-touch-icon-57-precomposed.png">
-  <link rel="shortcut icon" href="img/favicon.png">
-  
-	<script type="text/javascript" src="/js/jquery.min.js"></script>
-	<script type="text/javascript" src="/js/bootstrap.min.js"></script>
-	<script type="text/javascript" src="/js/scripts.js"></script>
-	 <script type="text/javascript" src="/js/jquery.min.js"></script>
-        <script type="text/javascript" src="/js/highcharts.js"></script>
-        <script type="text/javascript">
-$(function () {
-        $('#container').highcharts({
-            chart: {
-                type: 'area'
-            },
-            title: {
-                text: 'US and USSR nuclear stockpiles'
-            },
-            subtitle: {
-                text: 'Source: <a href="http://thebulletin.metapress.com/content/c4120650912x74k7/fulltext.pdf">'+
-                    'thebulletin.metapress.com</a>'
-            },
-            xAxis: {
-                labels: {
-                    formatter: function() {
-                        return this.value; // clean, unformatted number for year
-                    }
-                }
-            },
-            yAxis: {
-                title: {
-                    text: 'Nuclear weapon states'
-                },
-                labels: {
-                    formatter: function() {
-                        return this.value / 1000 +'k';
-                    }
-                }
-            },
-            tooltip: {
-                pointFormat: '{series.name} produced <b>{point.y:,.0f}</b><br/>warheads in {point.x}'
-            },
-            plotOptions: {
-                area: {
-                    pointStart: 1940,
-                    marker: {
-                        enabled: false,
-                        symbol: 'circle',
-                        radius: 2,
-                        states: {
-                            hover: {
-                                enabled: true
-                            }
-                        }
-                    }
-                }
-            },
-            series: [{
-                name: 'USA',
-                data: [null, null, null, null, null, 6 , 11, 32, 110, 235, 369, 640,
-                    1005, 1436, 2063, 3057, 4618, 6444, 9822, 15468, 20434, 24126,
-                    27387, 29459, 31056, 31982, 32040, 31233, 29224, 27342, 26662,
-                    26956, 27912, 28999, 28965, 27826, 25579, 25722, 24826, 24605,
-                    24304, 23464, 23708, 24099, 24357, 24237, 24401, 24344, 23586,
-                    22380, 21004, 17287, 14747, 13076, 12555, 12144, 11009, 10950,
-                    10871, 10824, 10577, 10527, 10475, 10421, 10358, 10295, 10104 ]
-            }, {
-                name: 'USSR/Russia',
-                data: [null, null, null, null, null, null, null , null , null ,null,
-                5, 25, 50, 120, 150, 200, 426, 660, 869, 1060, 1605, 2471, 3322,
-                4238, 5221, 6129, 7089, 8339, 9399, 10538, 11643, 13092, 14478,
-                15915, 17385, 19055, 21205, 23044, 25393, 27935, 30062, 32049,
-                33952, 35804, 37431, 39197, 45000, 43000, 41000, 39000, 37000,
-                35000, 33000, 31000, 29000, 27000, 25000, 24000, 23000, 22000,
-                21000, 20000, 19000, 18000, 18000, 17000, 16000]
-            }]
-        });
-    });
-    
-
-        </script>
-</head>
-
-<body>
-<div class="container">
-	<div class="row clearfix">
-		<div class="col-md-12 column">
-			<ul class="nav nav-tabs">
-				<li class="active">
-					<a href="#">概述</a>
-				</li>
-				<li>
-					<a href="#">监控</a>
-				</li>
-				<li class="disabled">
-					<a href="#">告警</a>
-				</li>
-				<li class="dropdown pull-right">
-					 <a href="#" data-toggle="dropdown" class="dropdown-toggle">其它<strong class="caret"></strong></a>
-					<ul class="dropdown-menu">
-						<li>
-							<a href="#">探测节点</a>
-						</li>
-						<li>
-							<a href="#">节点管理</a>
-						</li>
-					</ul>
-				</li>
-			</ul>
-		</div>
-	</div>
-	<div class="row clearfix">
-		<div class="col-md-3 column">
-			<div class="list-group">
-				 <a href="#" class="list-group-item active">HelloWorld</a>
-				 <a href="#" class="list-group-item">sayHello</a>
-				 <a href="#" class="list-group-item">sayHi</a>
-				 <a href="#" class="list-group-item active">User</a>
-				 <a href="#" class="list-group-item active">Blog</a>
-			</div>
-		</div>
-		<div class="col-md-9 column">
-			<div class="alert alert-dismissable alert-success">
-				 <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
-				<h4>
-					Alert!
-				</h4> <strong>Warning!</strong> Best check yo self, you're not looking too good. <a href="#" class="alert-link">alert link</a>
-			</div>
-			<div class="alert alert-dismissable alert-danger">
-				 <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
-				<h4>
-					Alert!
-				</h4> <strong>Warning!</strong> Best check yo self, you're not looking too good. <a href="#" class="alert-link">alert link</a>
-			</div>
-			<h3 class="text-primary text-center">
-				{$module}::{$interface}请求量曲线
-			</h3>
-			<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
-			<h3 class="text-danger text-center">
-				{$module}::{$interface}耗时曲线
-			</h3>
-			<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
-			<h3 class="text-primary text-center">
-				{$module}::{$interface}请求量曲线
-			</h3>
-			<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
-			<h3 class="text-danger text-center">
-				{$module}::{$interface}耗时曲线
-			</h3> 
-			<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
-			<div class="text-center">
-			<button class="btn btn-primary" type="button">分别统计</button>
-			<button class="btn btn-primary" type="button">汇总统计</button>
-			</div>
-			<table class="table table-hover table-condensed table-bordered">
-				<thead>
-					<tr>
-						<th>时间</th><th>调用总数</th><th>平均耗时</th><th>成功调用总数</th><th>成功平均耗时</th><th>失败调用总数</th><th>失败平均耗时</th><th>成功率</th>
-					</tr>
-				</thead>
-				<tbody>
-					<tr>
-						<td>
-							2014-02-09 00:05:00
-						</td>
-						<td>
-							13500
-						</td>
-						<td>
-							0.0268
-						</td>
-						<td>
-							13500
-						</td>
-						<td>
-							0.0268
-						</td>
-						<td>
-							0
-						</td>
-						<td>
-							0
-						</td>
-						<td>
-							100%
-						</td>
-					</tr>
-					<tr class="danger">
-						<td>
-							2014-02-09 00:10:00
-						</td>
-						<td>
-							13500
-						</td>
-						<td>
-							0.0268
-						</td>
-						<td>
-							13400
-						</td>
-						<td>
-							0.0268
-						</td>
-						<td>
-							100
-						</td>
-						<td>
-							0.0263
-						</td>
-						<td>
-							98.1%
-						</td>
-					</tr>
-					<tr>
-						<td>
-							2014-02-09 00:15:00
-						</td>
-						<td>
-							13500
-						</td>
-						<td>
-							0.0268
-						</td>
-						<td>
-							13500
-						</td>
-						<td>
-							0.0268
-						</td>
-						<td>
-							0
-						</td>
-						<td>
-							0
-						</td>
-						<td>
-							100%
-						</td>
-					</tr>
-					<tr>
-						<td>
-							2014-02-09 00:20:00
-						</td>
-						<td>
-							13500
-						</td>
-						<td>
-							0.0268
-						</td>
-						<td>
-							13500
-						</td>
-						<td>
-							0.0268
-						</td>
-						<td>
-							0
-						</td>
-						<td>
-							0
-						</td>
-						<td>
-							100%
-						</td>
-					</tr>
-				</tbody>
-			</table>
-		</div>
-	</div>
-</div>
-</body>
-</html>

+ 186 - 0
applications/Statistics/Views/log.tpl.php

@@ -0,0 +1,186 @@
+<div class="container">
+	<div class="row clearfix">
+		<div class="col-md-12 column">
+			<ul class="nav nav-tabs">
+				<li>
+					<a href="#">概述</a>
+				</li>
+				<li class="active">
+					<a href="#">监控</a>
+				</li>
+				<li class="disabled">
+					<a href="#">告警</a>
+				</li>
+				<li class="dropdown pull-right">
+					 <a href="#" data-toggle="dropdown" class="dropdown-toggle">其它<strong class="caret"></strong></a>
+					<ul class="dropdown-menu">
+						<li>
+							<a href="#">探测节点</a>
+						</li>
+						<li>
+							<a href="#">节点管理</a>
+						</li>
+					</ul>
+				</li>
+			</ul>
+		</div>
+	</div>
+	<div class="row clearfix">
+		<div class="col-md-3 column">
+			<div class="list-group">
+				 <a href="#" class="list-group-item active">HelloWorld</a>
+				 <a href="#" class="list-group-item">sayHello</a>
+				 <a href="#" class="list-group-item">sayHi</a>
+				 <a href="#" class="list-group-item active">User</a>
+				 <a href="#" class="list-group-item active">Blog</a>
+			</div>
+		</div>
+		<div class="col-md-9 column">
+			<div class="alert alert-dismissable alert-success">
+				 <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
+				<h4>
+					Alert!
+				</h4> <strong>Warning!</strong> Best check yo self, you're not looking too good. <a href="#" class="alert-link">alert link</a>
+			</div>
+			<div class="alert alert-dismissable alert-danger">
+				 <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
+				<h4>
+					Alert!
+				</h4> <strong>Warning!</strong> Best check yo self, you're not looking too good. <a href="#" class="alert-link">alert link</a>
+			</div>
+			<h3 class="text-primary text-center">
+				{$module}::{$interface}请求量曲线
+			</h3>
+			<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
+			<h3 class="text-danger text-center">
+				{$module}::{$interface}耗时曲线
+			</h3>
+			<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
+			<h3 class="text-primary text-center">
+				{$module}::{$interface}请求量曲线
+			</h3>
+			<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
+			<h3 class="text-danger text-center">
+				{$module}::{$interface}耗时曲线
+			</h3> 
+			<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
+			<div class="text-center">
+			<button class="btn btn-primary" type="button">分别统计</button>
+			<button class="btn btn-primary" type="button">汇总统计</button>
+			</div>
+			<table class="table table-hover table-condensed table-bordered">
+				<thead>
+					<tr>
+						<th>时间</th><th>调用总数</th><th>平均耗时</th><th>成功调用总数</th><th>成功平均耗时</th><th>失败调用总数</th><th>失败平均耗时</th><th>成功率</th>
+					</tr>
+				</thead>
+				<tbody>
+					<tr>
+						<td>
+							2014-02-09 00:05:00
+						</td>
+						<td>
+							13500
+						</td>
+						<td>
+							0.0268
+						</td>
+						<td>
+							13500
+						</td>
+						<td>
+							0.0268
+						</td>
+						<td>
+							0
+						</td>
+						<td>
+							0
+						</td>
+						<td>
+							100%
+						</td>
+					</tr>
+					<tr class="danger">
+						<td>
+							2014-02-09 00:10:00
+						</td>
+						<td>
+							13500
+						</td>
+						<td>
+							0.0268
+						</td>
+						<td>
+							13400
+						</td>
+						<td>
+							0.0268
+						</td>
+						<td>
+							100
+						</td>
+						<td>
+							0.0263
+						</td>
+						<td>
+							98.1%
+						</td>
+					</tr>
+					<tr>
+						<td>
+							2014-02-09 00:15:00
+						</td>
+						<td>
+							13500
+						</td>
+						<td>
+							0.0268
+						</td>
+						<td>
+							13500
+						</td>
+						<td>
+							0.0268
+						</td>
+						<td>
+							0
+						</td>
+						<td>
+							0
+						</td>
+						<td>
+							100%
+						</td>
+					</tr>
+					<tr>
+						<td>
+							2014-02-09 00:20:00
+						</td>
+						<td>
+							13500
+						</td>
+						<td>
+							0.0268
+						</td>
+						<td>
+							13500
+						</td>
+						<td>
+							0.0268
+						</td>
+						<td>
+							0
+						</td>
+						<td>
+							0
+						</td>
+						<td>
+							100%
+						</td>
+					</tr>
+				</tbody>
+			</table>
+		</div>
+	</div>
+</div>

+ 303 - 0
applications/Statistics/Views/main.tpl.php

@@ -0,0 +1,303 @@
+<div class="container">
+	<div class="row clearfix">
+		<div class="col-md-12 column">
+			<ul class="nav nav-tabs">
+				<li>
+					<a href="#">概述</a>
+				</li>
+				<li class="active">
+					<a href="#">监控</a>
+				</li>
+				<li class="disabled">
+					<a href="#">告警</a>
+				</li>
+				<li class="dropdown pull-right">
+					 <a href="#" data-toggle="dropdown" class="dropdown-toggle">其它<strong class="caret"></strong></a>
+					<ul class="dropdown-menu">
+						<li>
+							<a href="#">探测节点</a>
+						</li>
+						<li>
+							<a href="#">节点管理</a>
+						</li>
+					</ul>
+				</li>
+			</ul>
+		</div>
+	</div>
+	<div class="row clearfix">
+		<div class="col-md-3 column">
+			<div class="list-group">
+				 <a href="#" class="list-group-item active">HelloWorld</a>
+				 <a href="#" class="list-group-item">sayHello</a>
+				 <a href="#" class="list-group-item">sayHi</a>
+				 <a href="#" class="list-group-item active">User</a>
+				 <a href="#" class="list-group-item active">Blog</a>
+			</div>
+		</div>
+		<div class="col-md-9 column">
+			<div class="alert alert-dismissable alert-success">
+				 <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
+				<h4>
+					Alert!
+				</h4> <strong>Warning!</strong> Best check yo self, you're not looking too good. <a href="#" class="alert-link">alert link</a>
+			</div>
+			<div class="alert alert-dismissable alert-danger">
+				 <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
+				<h4>
+					Alert!
+				</h4> <strong>Warning!</strong> Best check yo self, you're not looking too good. <a href="#" class="alert-link">alert link</a>
+			</div>
+			<h3 class="text-primary text-center">
+				{$module}::{$interface}请求量曲线
+			</h3>
+			<div id="req-container" style="min-width:1100px;height:400px"></div>
+			<div id="time-container" style="min-width:1100px;height:400px"></div>
+			<div class="text-center">
+			<button class="btn btn-primary" type="button">分别统计</button>
+			<button class="btn btn-primary" type="button">汇总统计</button>
+			</div>
+			<table class="table table-hover table-condensed table-bordered">
+				<thead>
+					<tr>
+						<th>时间</th><th>调用总数</th><th>平均耗时</th><th>成功调用总数</th><th>成功平均耗时</th><th>失败调用总数</th><th>失败平均耗时</th><th>成功率</th>
+					</tr>
+				</thead>
+				<tbody>
+					<tr>
+						<td>
+							2014-02-09 00:05:00
+						</td>
+						<td>
+							13500
+						</td>
+						<td>
+							0.0268
+						</td>
+						<td>
+							13500
+						</td>
+						<td>
+							0.0268
+						</td>
+						<td>
+							0
+						</td>
+						<td>
+							0
+						</td>
+						<td>
+							100%
+						</td>
+					</tr>
+					<tr class="danger">
+						<td>
+							2014-02-09 00:10:00
+						</td>
+						<td>
+							13500
+						</td>
+						<td>
+							0.0268
+						</td>
+						<td>
+							13400
+						</td>
+						<td>
+							0.0268
+						</td>
+						<td>
+							100
+						</td>
+						<td>
+							0.0263
+						</td>
+						<td>
+							98.1%
+						</td>
+					</tr>
+					<tr>
+						<td>
+							2014-02-09 00:15:00
+						</td>
+						<td>
+							13500
+						</td>
+						<td>
+							0.0268
+						</td>
+						<td>
+							13500
+						</td>
+						<td>
+							0.0268
+						</td>
+						<td>
+							0
+						</td>
+						<td>
+							0
+						</td>
+						<td>
+							100%
+						</td>
+					</tr>
+					<tr>
+						<td>
+							2014-02-09 00:20:00
+						</td>
+						<td>
+							13500
+						</td>
+						<td>
+							0.0268
+						</td>
+						<td>
+							13500
+						</td>
+						<td>
+							0.0268
+						</td>
+						<td>
+							0
+						</td>
+						<td>
+							0
+						</td>
+						<td>
+							100%
+						</td>
+					</tr>
+				</tbody>
+			</table>
+		</div>
+	</div>
+</div>
+<script>
+Highcharts.setOptions({
+    global: {
+        useUTC: false
+    }
+});
+$(function () {
+    $('#req-container').highcharts({
+        chart: {
+            type: 'spline'
+        },
+        title: {
+            text: '<?php echo "$date $interface_name";?>  请求量曲线'
+        },
+        subtitle: {
+            text: ''
+        },
+        xAxis: {
+            type: 'datetime',
+            dateTimeLabelFormats: { 
+                hour: '%H:%M'
+            }
+        },
+        yAxis: {
+            title: {
+                text: '请求量(次/5分钟)'
+            },
+            min: 0
+        },
+        tooltip: {
+            formatter: function() {
+                return '<p style="color:'+this.series.color+';font-weight:bold;">'
+                 + this.series.name + 
+                 '</p><br /><p style="color:'+this.series.color+';font-weight:bold;">时间:' + Highcharts.dateFormat('%m月%d日 %H:%M', this.x) + 
+                 '</p><br /><p style="color:'+this.series.color+';font-weight:bold;">数量:'+ this.y + '</p>';
+            }
+        },
+        credits: {
+            enabled: false,
+        },
+        series: [        {
+            name: '成功曲线',
+            data: [
+                <?php echo $req_suc_series;?>
+            ],
+            lineWidth: 2,
+            marker:{
+                radius: 1
+            },
+            
+            pointInterval: 300*1000
+        },
+        {
+            name: '失败曲线',
+            data: [
+                <?php echo $req_fail_series;?>
+            ],
+            lineWidth: 2,
+            marker:{
+                radius: 1
+            },
+            pointInterval: 300*1000,
+            color : '#9C0D0D'
+        }]
+    });
+});
+$(function () {
+    $('#time-container').highcharts({
+        chart: {
+            type: 'spline'
+        },
+        title: {
+            text: '<?php echo "$date $interface_name";?>  请求耗时曲线'
+        },
+        subtitle: {
+            text: ''
+        },
+        xAxis: {
+            type: 'datetime',
+            dateTimeLabelFormats: { 
+                hour: '%H:%M'
+            }
+        },
+        yAxis: {
+            title: {
+                text: '平均耗时(单位:秒)'
+            },
+            min: 0
+        },
+        tooltip: {
+            formatter: function() {
+                return '<p style="color:'+this.series.color+';font-weight:bold;">'
+                 + this.series.name + 
+                 '</p><br /><p style="color:'+this.series.color+';font-weight:bold;">时间:' + Highcharts.dateFormat('%m月%d日 %H:%M', this.x) + 
+                 '</p><br /><p style="color:'+this.series.color+';font-weight:bold;">平均耗时:'+ this.y + '</p>';
+            }
+        },
+        credits: {
+            enabled: false,
+            text: "jumei.com",
+            href: "http://www.jumei.com"
+        },
+        series: [        {
+            name: '成功曲线',
+            data: [
+                <?php echo $cost_suc_series;?>
+            ],
+            lineWidth: 2,
+            marker:{
+                radius: 1
+            },
+            pointInterval: 300*1000
+        },
+        {
+            name: '失败曲线',
+            data: [
+                   <?php echo $cost_fail_series;?>
+            ],
+            lineWidth: 2,
+            marker:{
+                radius: 1
+            },
+            pointInterval: 300*1000,
+            color : '#9C0D0D'
+        }            ]
+    });
+});
+</script>

+ 193 - 0
applications/Statistics/Views/statistic.tpl.php

@@ -0,0 +1,193 @@
+<div class="container">
+	<div class="row clearfix">
+		<div class="col-md-12 column">
+			<ul class="nav nav-tabs">
+				<li>
+					<a href="#">概述</a>
+				</li>
+				<li class="active">
+					<a href="#">监控</a>
+				</li>
+				<li>
+					<a href="#">日志</a>
+				</li>
+				<li class="disabled">
+					<a href="#">告警</a>
+				</li>
+				<li class="dropdown pull-right">
+					 <a href="#" data-toggle="dropdown" class="dropdown-toggle">其它<strong class="caret"></strong></a>
+					<ul class="dropdown-menu">
+						<li>
+							<a href="#">探测节点</a>
+						</li>
+						<li>
+							<a href="#">节点管理</a>
+						</li>
+					</ul>
+				</li>
+			</ul>
+		</div>
+	</div>
+	<div class="row clearfix">
+		<div class="col-md-3 column">
+			<div class="list-group">
+				 <a href="#" class="list-group-item active">HelloWorld</a>
+				 <a href="#" class="list-group-item">sayHello</a>
+				 <a href="#" class="list-group-item">sayHi</a>
+				 <a href="#" class="list-group-item active">User</a>
+				 <a href="#" class="list-group-item active">Blog</a>
+			</div>
+		</div>
+		<div class="col-md-9 column">
+			<div class="alert alert-dismissable alert-success">
+				 <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
+				<h4>
+					Alert!
+				</h4> <strong>Warning!</strong> Best check yo self, you're not looking too good. <a href="#" class="alert-link">alert link</a>
+			</div>
+			<div class="alert alert-dismissable alert-danger">
+				 <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
+				<h4>
+					Alert!
+				</h4> <strong>Warning!</strong> Best check yo self, you're not looking too good. <a href="#" class="alert-link">alert link</a>
+			</div>
+			<h3 class="text-primary text-center">
+				{$module}::{$interface}请求量曲线
+			</h3>
+			<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
+			<h3 class="text-danger text-center">
+				{$module}::{$interface}耗时曲线
+			</h3>
+			<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
+			<h3 class="text-primary text-center">
+				{$module}::{$interface}请求量曲线
+			</h3>
+			<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
+			<h3 class="text-danger text-center">
+				{$module}::{$interface}耗时曲线
+			</h3> 
+			<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
+			<div class="text-center">
+			<button class="btn btn-primary" type="button">分别统计</button>
+			<button class="btn btn-primary" type="button">汇总统计</button>
+			</div>
+			<table class="table table-hover table-condensed table-bordered">
+				<thead>
+					<tr>
+						<th>时间</th><th>调用总数</th><th>平均耗时</th><th>成功调用总数</th><th>成功平均耗时</th><th>失败调用总数</th><th>失败平均耗时</th><th>成功率</th>
+					</tr>
+				</thead>
+				<tbody>
+					<tr>
+						<td>
+							2014-02-09 00:05:00
+						</td>
+						<td>
+							13500
+						</td>
+						<td>
+							0.0268
+						</td>
+						<td>
+							13500
+						</td>
+						<td>
+							0.0268
+						</td>
+						<td>
+							0
+						</td>
+						<td>
+							0
+						</td>
+						<td>
+							100%
+						</td>
+					</tr>
+					<tr class="danger">
+						<td>
+							2014-02-09 00:10:00
+						</td>
+						<td>
+							13500
+						</td>
+						<td>
+							0.0268
+						</td>
+						<td>
+							13400
+						</td>
+						<td>
+							0.0268
+						</td>
+						<td>
+							100
+						</td>
+						<td>
+							0.0263
+						</td>
+						<td>
+							98.1%
+						</td>
+					</tr>
+					<tr>
+						<td>
+							2014-02-09 00:15:00
+						</td>
+						<td>
+							13500
+						</td>
+						<td>
+							0.0268
+						</td>
+						<td>
+							13500
+						</td>
+						<td>
+							0.0268
+						</td>
+						<td>
+							0
+						</td>
+						<td>
+							0
+						</td>
+						<td>
+							100%
+						</td>
+					</tr>
+					<tr>
+						<td>
+							2014-02-09 00:20:00
+						</td>
+						<td>
+							13500
+						</td>
+						<td>
+							0.0268
+						</td>
+						<td>
+							13500
+						</td>
+						<td>
+							0.0268
+						</td>
+						<td>
+							0
+						</td>
+						<td>
+							0
+						</td>
+						<td>
+							100%
+						</td>
+					</tr>
+				</tbody>
+			</table>
+		</div>
+	</div>
+</div>
+
+<script type="text/javascript">
+
+</script>

+ 23 - 1
applications/Statistics/Web/index.php

@@ -1,3 +1,25 @@
 <?php
 require_once WORKERMAN_ROOT_DIR .'applications/Statistics/Web/_init.php';
-include ST_ROOT . '/Views/home.php';
+require_once WORKERMAN_ROOT_DIR .'applications/Statistics/Lib/functions.php';
+// fn = main/statistic/log/admin
+
+$func = isset($_GET['fn']) ? $_GET['fn'] : 'main';
+if(!function_exists($func))
+{
+    foreach(glob(ST_ROOT . "/Modules/*") as $php_file)
+    {
+        require_once $php_file;
+    }
+}
+
+if(!function_exists($func))
+{
+    $func = 'main';
+}
+
+$module = isset($_GET['module']) ? $_GET['module'] : '';
+$interface = isset($_GET['interface']) ? $_GET['interface'] : '';
+$date = isset($_GET['date']) ? $_GET['date'] : date('Y-m-d');
+$start_time = isset($_GET['start_time']) ? $_GET['start_time'] : date('Y-m-d');
+$offset =  isset($_GET['offset']) ? $_GET['offset'] : 0; 
+echo $func();

+ 15 - 28
workers/StatisticWorker.php

@@ -267,22 +267,9 @@ class StatisticWorker extends Man\Core\SocketWorker
             }
             return;
         }
-        foreach (glob($file."/*") as $file_name) {
-            if(is_dir($file_name))
-            {
-                $this->clearDisk($file_name, $exp_time);
-                continue;
-            }
-            $mtime = filemtime($file);
-            if(!$mtime)
-            {
-                $this->notice("filemtime $file fail");
-                return;
-            }
-            if($time_now - $mtime > $exp_time)
-            {
-                unlink($file_name);
-            }
+        foreach (glob($file."/*") as $file_name) 
+        {
+            $this->clearDisk($file_name, $exp_time);
         }
     }
     
@@ -301,7 +288,7 @@ class StatisticWorker extends Man\Core\SocketWorker
         $date = isset($req_data['date']) ? $req_data['date'] : '';
         $code = isset($req_data['code']) ? $req_data['code'] : '';
         $msg = isset($req_data['msg']) ? $req_data['msg'] : '';
-        $pointer = isset($req_data['pointer']) ? $req_data['pointer'] : '';
+        $offset = isset($req_data['offset']) ? $req_data['offset'] : '';
         $count = isset($req_data['count']) ? $req_data['count'] : 10;
         switch($cmd)
         {
@@ -309,7 +296,7 @@ class StatisticWorker extends Man\Core\SocketWorker
                 $buffer = json_encode(array('modules'=>$this->getModules($module), 'statistic' => $this->getStatistic($date, $module, $interface)))."\n";
                 return $this->sendToClient($buffer);
             case 'get_log':
-                $buffer = json_encode($this->getStasticLog($module, $interface , $start_time , $end_time, $code = '', $msg = '', $pointer='', $count=10))."\n";
+                $buffer = json_encode($this->getStasticLog($module, $interface , $start_time , $end_time, $code = '', $msg = '', $offset='', $count=10))."\n";
                 return $this->sendToClient($buffer);
         }
     }
@@ -439,22 +426,22 @@ class StatisticWorker extends Man\Core\SocketWorker
      * 获取指定日志
      *
      */
-    protected function getStasticLog($module, $interface , $start_time = '', $end_time = '', $code = '', $msg = '', $pointer='', $count=100)
+    protected function getStasticLog($module, $interface , $start_time = '', $end_time = '', $code = '', $msg = '', $offset='', $count=100)
     {
         // log文件
         $log_file = WORKERMAN_ROOT_DIR . $this->logDir. (empty($start_time) ? date('Y-m-d') : date('Y-m-d', $start_time));
         if(!is_readable($log_file))
         {
-            return array('pointer'=>0, 'data'=>$log_file . 'not exists or not readable');
+            return array('offset'=>0, 'data'=>$log_file . 'not exists or not readable');
         }
         // 读文件
         $h = fopen($log_file, 'r');
     
         // 如果有时间,则进行二分查找,加速查询
-        if($start_time && $pointer === '' && ($file_size = filesize($log_file) > 50000))
+        if($start_time && $offset === '' && ($file_size = filesize($log_file) > 50000))
         {
-            $pointer = $this->binarySearch(0, $file_size, $start_time-1, $h);
-            $pointer = $pointer < 1000 ? 0 : $pointer - 1000;
+            $offset = $this->binarySearch(0, $file_size, $start_time-1, $h);
+            $offset = $offset < 1000 ? 0 : $offset - 1000;
         }
     
         // 正则表达式
@@ -469,7 +456,7 @@ class StatisticWorker extends Man\Core\SocketWorker
             $pattern .= ".*::";
         }
     
-        if($interface && $module != 'PHPServer')
+        if($interface && $module != 'WorkerMan')
         {
             $pattern .= $interface."\t";
         }
@@ -495,9 +482,9 @@ class StatisticWorker extends Man\Core\SocketWorker
         $pattern .= '/';
     
         // 指定偏移位置
-        if($pointer >= 0)
+        if($offset >= 0)
         {
-            fseek($h, (int)$pointer);
+            fseek($h, (int)$offset);
         }
     
         // 查找符合条件的数据
@@ -539,8 +526,8 @@ class StatisticWorker extends Man\Core\SocketWorker
             }
         }
         // 记录偏移位置
-        $pointer = ftell($h);
-        return array('pointer'=>$pointer, 'data'=>$log_buffer);
+        $offset = ftell($h);
+        return array('offset'=>$offset, 'data'=>$log_buffer);
     }
     /**
      * 日志二分查找法