示例#1
0
文件: Log.php 项目: ice-gyx/transfer
 /**
  * Analysis Log File In laravel (MonoLog)
  */
 function getReturnInLogFile($dir, $fileName, $last = 0, $url = '')
 {
     $filePath = storage_path() . "/{$dir}/" . $fileName;
     $i = $last;
     while (!file_exists($fileRealPath = $filePath . date('Y-m-d', strtotime("-{$i} days"))) && ++$i && $i < 5) {
     }
     // echo $fileRealPath;
     if (file_exists($fileRealPath)) {
         return readMonoLogFile($fileRealPath, $url);
     }
 }
 public function index()
 {
     //获取接口调用频度
     $dir = 'logs';
     $fileName = 'ReqLogs';
     $filePath = storage_path() . "/{$dir}/" . $fileName;
     $fileRealPath = $filePath . date('Y-m-d');
     file_exists($fileRealPath) && readMonoLogFile($fileRealPath);
     $todayReq = readMonoLogFile('');
     $todayReq = array_map(function ($v) {
         return $v['Times'];
     }, $todayReq);
     asort($todayReq);
     $routes = Route::getRoutes();
     $baseUrls = array('Localhost' => 'http://' . $_SERVER['HTTP_HOST'], 'Test Api' => 'http://api.xb.guozhongbao.com');
     $todayReqF = [];
     $routes_select = array();
     $all_params = array();
     foreach ($routes as $v) {
         $data = array();
         $method = array();
         $methods = $v->getMethods();
         $uri = $v->getPath();
         $action = $v->getActionName();
         $actionData = $v->getAction();
         $filter = [];
         if (isset($actionData['middleware']) && $actionData['middleware']) {
             $filter = (array) $actionData['middleware'];
         }
         //获取filters
         //     		$filter  = $v->beforeFilters();
         //分割action
         $action = $this->compileAction($action);
         if (!method_exists($action[0], $action[1])) {
             continue;
         }
         in_array('GET', $methods) and $method[] = 'GET';
         in_array('POST', $methods) and $method[] = 'POST';
         //生成method和uri
         !empty($method) and $data = array('method' => '[' . implode('/', $method) . ']', 'doMethod' => $method[0], 'uri' => '/' . ltrim($uri, '/'));
         //获取action指向的方法内的参数
         $data and $action and $data['params'] = $this->getInputParams($action);
         //获取filter内部所需参数
         if ($data && $filter) {
             $params = array();
             foreach ($filter as $key => $value) {
                 $p = $this->getMiddlewareParams($value);
                 $p && ($params += $p);
             }
             $params && $data['params'] && (is_array($params) && ($data['params'] += $params));
         }
         isset($data['params']) && is_array($data['params']) && ($all_params += $data['params']);
         $data && ($routes_select[] = $data) && isset($todayReq[$data['uri']]) && ($todayReqF[$data['uri']] = count($routes_select) - 1);
     }
     //高频度置前
     $res = [];
     foreach ($todayReqF as $k => $v) {
         $add = $routes_select[$v];
         array_unshift($res, $add);
         unset($routes_select[$v]);
     }
     foreach ($routes_select as $k => $v) {
         $res[] = $v;
     }
     unset($routes_select);
     return View::make('localtest.index')->with('route', $res)->with('baseUrls', $baseUrls)->with('all_params', $all_params);
 }