12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
- namespace Log\Lib;
- use Dever;
- class Cron extends Core
- {
- public function get()
- {
- # 获取所有项目
- $config = $this->config();
- $day = array();
- $day[0] = date('Y-m-d-H');
- $day[1] = date('Y-m-d-H', strtotime("-1 hour"));
- if (isset($config['project'])) {
- $log = Dever::config('base', 'project')->log;
- foreach ($config['project'] as $k => $v) {
- foreach ($day as $i => $j) {
- $data = $this->getDataByProject($j, $v['id'], $v['key']);
- $this->write($data, $log);
- }
- }
- }
- return 'ok';
- }
- private function write($data, $log)
- {
- //$data[0]写入到data表中,data[1]写入到es中
- $source = $data[0];
- $desc = $data[1];
- $time = explode('-', $source['time']);
- $update['project_id'] = $source['project_id'];
- $update['time'] = Dever::maketime($time[0] . '-' . $time[1] . '-' . $time[2] . ' ' . $time[3] . ':00:00');
- $info = Dever::db('log/data')->one($update);
- $update['pv'] = $source['pv'] ? $source['pv'] : 0;
- $update['uv'] = $source['uv'] ? $source['uv'] : 0;
- $update['year'] = $time[0];
- $update['month'] = $time[1];
- $update['day'] = $time[2];
- $update['hour'] = $time[3];
- if (!$info) {
- Dever::db('log/data')->insert($update);
- } else {
- $update['where_id'] = $info['id'];
- Dever::db('log/data')->update($update);
- }
- # desc写入到elk中?
- # 如果有其他的系统,则进行处理
- if (isset($log['cron']) && $log['cron']) {
- $data[2] = $update;
- foreach ($log['cron'] as $k => $v) {
- Dever::load($v, $data);
- }
- }
- }
- private function getDataByProject($day, $id, $key)
- {
- $data = Dever::getLog($day, $key . '/');
- $source = $result = array();
- $source['project_id'] = $id;
- $source['time'] = $day;
- $source['pv'] = 0;
- $source['uv'] = 0;
- if ($data) {
- $result = array();
- $pv = $uv = 0;
- $user = array();
- foreach ($data as $k => $v) {
- if ($v) {
- $temp = explode('dever&', $v);
- $info = explode(' ', $temp[0]);
- $result[$k]['time'] = $info[0];
- $result[$k]['project'] = $info[1];
- $result[$k]['app'] = $info[2];
- parse_str($temp[1], $result[$k]['param']);
- $pv++;
- if (isset($result[$k]['param']['uid']) && $result[$k]['param']['uid'] > 0) {
- $user[$result[$k]['param']['uid']] = 1;
- } else {
- $user[$result[$k]['param']['ip']] = 1;
- }
- }
- }
- $source['pv'] = $pv;
- $source['uv'] = count($user);
- }
- return array($source, $result);
- }
- public function get_api(){}
- }
|