Cron.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace Log\Lib;
  3. use Dever;
  4. class Cron extends Core
  5. {
  6. public function get()
  7. {
  8. # 获取所有项目
  9. $config = $this->config();
  10. $day = array();
  11. $day[0] = date('Y-m-d-H');
  12. $day[1] = date('Y-m-d-H', strtotime("-1 hour"));
  13. if (isset($config['project'])) {
  14. $log = Dever::config('base', 'project')->log;
  15. foreach ($config['project'] as $k => $v) {
  16. foreach ($day as $i => $j) {
  17. $data = $this->getDataByProject($j, $v['id'], $v['key']);
  18. $this->write($data, $log);
  19. }
  20. }
  21. }
  22. return 'ok';
  23. }
  24. private function write($data, $log)
  25. {
  26. //$data[0]写入到data表中,data[1]写入到es中
  27. $source = $data[0];
  28. $desc = $data[1];
  29. $time = explode('-', $source['time']);
  30. $update['project_id'] = $source['project_id'];
  31. $update['time'] = Dever::maketime($time[0] . '-' . $time[1] . '-' . $time[2] . ' ' . $time[3] . ':00:00');
  32. $info = Dever::db('log/data')->one($update);
  33. $update['pv'] = $source['pv'] ? $source['pv'] : 0;
  34. $update['uv'] = $source['uv'] ? $source['uv'] : 0;
  35. $update['year'] = $time[0];
  36. $update['month'] = $time[1];
  37. $update['day'] = $time[2];
  38. $update['hour'] = $time[3];
  39. if (!$info) {
  40. Dever::db('log/data')->insert($update);
  41. } else {
  42. $update['where_id'] = $info['id'];
  43. Dever::db('log/data')->update($update);
  44. }
  45. # desc写入到elk中?
  46. # 如果有其他的系统,则进行处理
  47. if (isset($log['cron']) && $log['cron']) {
  48. $data[2] = $update;
  49. foreach ($log['cron'] as $k => $v) {
  50. Dever::load($v, $data);
  51. }
  52. }
  53. }
  54. private function getDataByProject($day, $id, $key)
  55. {
  56. $data = Dever::getLog($day, $key . '/');
  57. $source = $result = array();
  58. $source['project_id'] = $id;
  59. $source['time'] = $day;
  60. $source['pv'] = 0;
  61. $source['uv'] = 0;
  62. if ($data) {
  63. $result = array();
  64. $pv = $uv = 0;
  65. $user = array();
  66. foreach ($data as $k => $v) {
  67. if ($v) {
  68. $temp = explode('dever&', $v);
  69. $info = explode(' ', $temp[0]);
  70. $result[$k]['time'] = $info[0];
  71. $result[$k]['project'] = $info[1];
  72. $result[$k]['app'] = $info[2];
  73. parse_str($temp[1], $result[$k]['param']);
  74. $pv++;
  75. if (isset($result[$k]['param']['uid']) && $result[$k]['param']['uid'] > 0) {
  76. $user[$result[$k]['param']['uid']] = 1;
  77. } else {
  78. $user[$result[$k]['param']['ip']] = 1;
  79. }
  80. }
  81. }
  82. $source['pv'] = $pv;
  83. $source['uv'] = count($user);
  84. }
  85. return array($source, $result);
  86. }
  87. public function get_api(){}
  88. }