Apilog.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. namespace addons\apilog\model;
  3. use think\Model;
  4. class Apilog extends Model
  5. {
  6. protected $name = 'apilog';
  7. protected $autoWriteTimestamp = 'int';
  8. protected $createTime = 'createtime';
  9. protected $updateTime = false;
  10. protected $deleteTime = false;
  11. protected $append = [
  12. 'method_text',
  13. 'time_text'
  14. ];
  15. public function getMethodList()
  16. {
  17. return ['GET' => 'GET', 'POST' => 'POST', 'PUT' => 'PUT', 'DELETE' => 'DELETE'];
  18. }
  19. public function getMethodTextAttr($value, $data)
  20. {
  21. $value = $value ? $value : (isset($data['method']) ? $data['method'] : '');
  22. $list = $this->getMethodList();
  23. return isset($list[$value]) ? $list[$value] : '';
  24. }
  25. public function getTimeTextAttr($value, $data)
  26. {
  27. $value = $value ? $value : (isset($data['time']) ? $data['time'] : '');
  28. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  29. }
  30. protected function setTimeAttr($value)
  31. {
  32. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  33. }
  34. /**
  35. * 基本数据
  36. *
  37. * @param [type] $start
  38. * @param [type] $end
  39. * @return void
  40. */
  41. public static function getBaseInfo($start, $end)
  42. {
  43. //请求次数
  44. $count_request = Apilog::whereTime('createtime', 'between', [$start, $end])->count();
  45. //平均处理时间
  46. $avg_time = Apilog::whereTime('createtime', 'between', [$start, $end])->avg('time');
  47. //404
  48. $count_404 = Apilog::whereTime('createtime', 'between', [$start, $end])->where('code', 404)->count();
  49. //500
  50. $count_500 = Apilog::whereTime('createtime', 'between', [$start, $end])->where('code', 500)->count();
  51. //错误率占比
  52. $error_rank = $count_request > 0 ? $count_500 / $count_request : 0;
  53. //接口总数(已请求)
  54. $count_api = Apilog::whereTime('createtime', 'between', [$start, $end])->group('controller,action')->count();
  55. //echo Apilog::getLastSql();
  56. return [
  57. 'count_request' => $count_request,
  58. 'avg_time' => $avg_time,
  59. 'count_404' => $count_404,
  60. 'count_500' => $count_500,
  61. 'error_rank' => $error_rank,
  62. 'count_api' => $count_api
  63. ];
  64. }
  65. /**
  66. * 请求状态码 饼图
  67. *
  68. * @return void
  69. */
  70. public static function getHttpCodePie($start, $end)
  71. {
  72. $list = Apilog::whereTime('createtime', 'between', [$start, $end])->group('code')->field('count(1) num,code')->select();
  73. $data['x'] = [];
  74. $data['y'] = [];
  75. foreach ($list as $k => $v) {
  76. $data['x'][] = $v['code'];
  77. $data['y'][] = $v['num'];
  78. $data['kv'][] = ['name' => $v['code'], 'value' => $v['num']];
  79. }
  80. return $data;
  81. }
  82. /**
  83. * 请求处理时间(ms)饼图
  84. * 按0-100 100-500,500-1000,1000-3000,3000-5000,5000以上划分
  85. *
  86. * @return void
  87. */
  88. public static function getResponseTimePie($start, $end)
  89. {
  90. $row = Apilog::whereTime('createtime', 'between', [$start, $end])
  91. ->field("sum(CASE WHEN TIME<100 THEN 1 ELSE 0 END) AS '0-100' ,
  92. sum(CASE WHEN TIME>=100 and TIME<500 THEN 1 ELSE 0 END) AS '100-500' ,
  93. sum(CASE WHEN TIME>=500 and TIME<1000 THEN 1 ELSE 0 END) AS '500-1000' ,
  94. sum(CASE WHEN TIME>=1000 and TIME<3000 THEN 1 ELSE 0 END) AS '1000-3000' ,
  95. sum(CASE WHEN TIME>=3000 and TIME<5000 THEN 1 ELSE 0 END) AS '3000-5000' ,
  96. sum(CASE WHEN TIME>=5000 THEN 1 ELSE 0 END) AS '5000以上'
  97. ")
  98. ->find();
  99. // echo Apilog::getLastSql();
  100. $data['x'] = ['0-100', '100-500', '500-1000', '1000-3000', '3000-5000', '5000以上'];
  101. $data['y'] = [$row['0-100'], $row['100-500'], $row['500-1000'], $row['1000-3000'], $row['3000-5000'], $row['5000以上']];
  102. foreach ($data['x'] as $k => $v) {
  103. $data['kv'][] = ['name' => $v, 'value' => $data['y'][$k]];
  104. }
  105. return $data;
  106. }
  107. /**
  108. * 最多请求 Top n,展现接口名称
  109. *
  110. * @return void
  111. */
  112. public static function getMaxRequestTop($start, $end)
  113. {
  114. $list = Apilog::whereTime('createtime', 'between', [$start, $end])
  115. ->group('url')->field('count(1) num, url')->order('num desc')->limit(0, 15)->select();
  116. // echo Apilog::getLastSql();
  117. $data['x'] = [];
  118. $data['y'] = [];
  119. foreach ($list as $k => $v) {
  120. $data['x'][] = $v['url'];
  121. $data['y'][] = $v['num'];
  122. }
  123. return $data;
  124. }
  125. /**
  126. * 请求错误 Top n
  127. *
  128. * @return void
  129. */
  130. public static function getMaxErrorTop($start, $end)
  131. {
  132. $list = Apilog::whereTime('createtime', 'between', [$start, $end])
  133. ->where('code', 500)
  134. ->group('url')->field('count(1) num, url')->order('num desc')->limit(0, 15)->select();
  135. // echo Apilog::getLastSql();
  136. $data['x'] = [];
  137. $data['y'] = [];
  138. foreach ($list as $k => $v) {
  139. $data['x'][] = $v['url'];
  140. $data['y'][] = $v['num'];
  141. }
  142. return $data;
  143. }
  144. /**
  145. * 平均处理时间最快 Top n
  146. *
  147. * @return void
  148. */
  149. public static function getDoFastTop($start, $end)
  150. {
  151. $list = Apilog::whereTime('createtime', 'between', [$start, $end])
  152. ->group('url')->field('avg(time) num, url')->order('num')->limit(0, 15)->select();
  153. // echo Apilog::getLastSql();
  154. $data['x'] = [];
  155. $data['y'] = [];
  156. foreach ($list as $k => $v) {
  157. $data['x'][] = $v['url'];
  158. $data['y'][] = $v['num'];
  159. }
  160. return $data;
  161. }
  162. /**
  163. * 平均处理时间最慢 Top n
  164. *
  165. * @return void
  166. */
  167. public static function getDoSlowTop($start, $end)
  168. {
  169. $list = Apilog::whereTime('createtime', 'between', [$start, $end])
  170. ->group('url')->field('avg(time) num, url')->order('num desc')->limit(0, 15)->select();
  171. // echo Apilog::getLastSql();
  172. $data['x'] = [];
  173. $data['y'] = [];
  174. foreach ($list as $k => $v) {
  175. $data['x'][] = $v['url'];
  176. $data['y'][] = $v['num'];
  177. }
  178. return $data;
  179. }
  180. /**
  181. * 请求次数 近一个小时,按分钟
  182. *
  183. * @param int $type 0:每分钟 1:每小时 2:每天
  184. * @return void
  185. */
  186. public static function getRequestCountLine($type)
  187. {
  188. $now = time();
  189. $where = $type == 0 ? [$now - 3600, $now] : ($type == 1 ? [$now - 3600 * 24, $now] : 'month');
  190. $format = $type == 0 ? 'i' : ($type == 1 ? 'H' : 'd');
  191. $group = "FROM_UNIXTIME(createtime,'%" . $format . "')";
  192. $list = Apilog::whereTime('createtime', $where)->group($group)->field('count(1) num,' . $group . ' as time')->select();
  193. $data['x'] = [];
  194. $data['y'] = [];
  195. foreach ($list as $k => $v) {
  196. $data['x'][] = $v['time'];
  197. $data['y'][] = $v['num'];
  198. }
  199. if ($type == 2) {
  200. return $data;
  201. }
  202. $max = $type == 0 ? 60 : ($type == 1 ? 24 : 0);
  203. $s = $type == 0 ? getdate()['minutes'] + 1 : ($type == 1 ? getdate()['hours'] + 1 : 0);
  204. $tmp = null;
  205. for ($i = 0; $i < $max; $i++) {
  206. $k = $s + $i >= $max ? $s + $i - $max : $s + $i;
  207. $tmp['x'][] = $k;
  208. if (($idx = array_search($k, $data['x'])) !== false) {
  209. $tmp['y'][] = $data['y'][$idx];
  210. } else {
  211. $tmp['y'][] = 0;
  212. }
  213. }
  214. return $tmp;
  215. }
  216. /**
  217. * 平均处理时间 近一个小时,按分钟
  218. *
  219. * @param int $type 0:每分钟 1:每小时 2:每天
  220. * @return void
  221. */
  222. public static function getDoTimeLine($type)
  223. {
  224. $now = time();
  225. $where = $type == 0 ? [$now - 3600, $now] : ($type == 1 ? [$now - 3600 * 24, $now] : 'month');
  226. $format = $type == 0 ? 'i' : ($type == 1 ? 'H' : 'd');
  227. $group = "FROM_UNIXTIME(createtime,'%" . $format . "')";
  228. $list = Apilog::whereTime('createtime', $where)->group($group)->field('avg(time) num,' . $group . ' as time')->select();
  229. $data['x'] = [];
  230. $data['y'] = [];
  231. foreach ($list as $k => $v) {
  232. $data['x'][] = $v['time'];
  233. $data['y'][] = $v['num'];
  234. }
  235. if ($type == 2) {
  236. return $data;
  237. }
  238. $max = $type == 0 ? 60 : ($type == 1 ? 24 : 0);
  239. $s = $type == 0 ? getdate()['minutes'] + 1 : ($type == 1 ? getdate()['hours'] + 1 : 0);
  240. $tmp = null;
  241. for ($i = 0; $i < $max; $i++) {
  242. $k = $s + $i >= $max ? $s + $i - $max : $s + $i;
  243. $tmp['x'][] = $k;
  244. if (($idx = array_search($k, $data['x'])) !== false) {
  245. $tmp['y'][] = $data['y'][$idx];
  246. } else {
  247. $tmp['y'][] = 0;
  248. }
  249. }
  250. return $tmp;
  251. }
  252. }