/** * @param array $router * @param string $path * @return array */ private function _dispatch_to_method($router, $path) { if (!isset($router[$path])) { return ['errno' => Da\Sys_Router::ERRNO_NOT_FOUND, 'data' => 'path [' . $path . '] not exists.']; } $rules = $router[$path]; $count = count($rules); $http_method = 'get'; if ($count > 2) { $http_method = $rules[2]; if (strtolower($http_method) == 'post' && empty($_POST)) { return ['errno' => Da\Sys_Router::ERRNO_NOT_FOUND, 'data' => 'ONLY SUPPORT POST FOR URL:' . $path . ', but got empty Post vars.']; } } $method = array_slice($rules, 0, 2); if (!is_callable($method)) { return ['errno' => Da\Sys_Router::ERRNO_NOT_FOUND, 'data' => 'method ' . json_encode($method) . ' not exists']; } $this->_carry_current_url_path(); $this->_current_url_info['url_path'] = self::current_url_path(); $this->_current_url_info['rule'] = $rules; foreach ($this->_pre_route_hooks as $_hook) { if (is_callable($_hook)) { call_user_func($_hook); } } try { $result = call_user_func($method); } catch (Exception $ex) { \Lib_Log::error('Runtime error errno: [%d], msg: [%s]', [$ex->getCode(), $ex->getMessage()]); return ['errno' => Da\Sys_Router::ERRNO_SERVER_ERR, 'data' => 'something error!']; } return ['errno' => 200, 'data' => 'REUQEST OK!']; }
/** * @return array The return value should include 'errno' and 'data' */ function dispatch() { $parse_url = $this->parse_url(); if ($parse_url['errno'] != self::ERRNO_OK) { return $parse_url; } $url_piece = $parse_url['data']; $router = call_user_func($this->_router, $url_piece); if (isset($router['errno']) && $router['errno'] === self::ERRNO_OK) { return ['errno' => self::ERRNO_OK, 'data' => $router]; } elseif (isset($router['errno'])) { switch ($router['errno']) { case self::ERRNO_FORBIDDEN: \Lib_Log::info('forbidden page! url:[%s] case:[%s]', [json_encode($parse_url), json_encode($router)]); return ['errno' => self::ERRNO_FORBIDDEN, 'data' => 'You cannot visit this page!']; case self::ERRNO_SERVER_ERR: \Lib_Log::error('page err! url:[%s] case:[%s]', [json_encode($parse_url), json_encode($router)]); return ['errno' => self::ERRNO_SERVER_ERR, 'data' => 'Something wrong!']; case self::ERRNO_NOT_FOUND: default: \Lib_Log::notice('page not found! url:[%s] case:[%s]', [json_encode($parse_url), json_encode($router)]); return ['errno' => self::ERRNO_NOT_FOUND, 'data' => 'PAGE NOT FOUND!']; } } else { \Lib_Log::notice('page not found! url:[%s] case:[%s]', [json_encode($parse_url), json_encode($router)]); return ['errno' => self::ERRNO_FORBIDDEN, 'data' => 'PAGE NOT FOUND!']; } }
/** * @param array * @return array */ static function process_result($result) { if ($result['errno'] !== 0) { Lib_Log::error("Redis: get result failed! result: %s", json_encode($result)); return Lib_Helper::get_err_struct(Const_Err_DataAccess::ERR_GET_CARD, 'get card failed! result:' . json_encode($result)); } return Lib_Helper::get_return_struct($result['data']); }
/** * @return array */ static function module_info() { $modules_info = new Model_ModulesInfo(); $module_info = $modules_info->get_module_by_indentify(self::indentify()); if ($module_info['errno'] !== Const_Err_Base::ERR_OK) { Lib_Log::error(__METHOD__ . ' faild! cause: ' . json_encode($module_info['data'])); self::$module_id = self::MODULE_UNREG_ID; return Lib_Helper::get_err_struct(Const_Err_Db::ERR_GET_DATA_FAIL, '获取模块失败', __FILE__, __LINE__); } $module_info = $module_info['data']; if (empty($module_info)) { self::$module_id = self::MODULE_UNREG_ID; return Lib_Helper::get_err_struct(Const_Err_DataAccess::ERR_MODULE_UNREG, '模块未注册', __FILE__, __LINE__); } self::$module_id = $module_info['_id']; return ['errno' => Const_Err_Base::ERR_OK, 'data' => $module_info]; }
/** * @param int $task_id * @param int $mid * @param array $data * @return array */ private function _run_module($task_id, $mid, $data) { $data = Module_ModuleManager_Main::get_instance()->run_module($task_id, $mid, $data, $this->options); // 数据检查 if (!isset($data['errno'])) { Lib_Log::error("run module [%d] faild!. err: return data not has errno.\tdata:%s", [$mid, json_encode($data)]); return Lib_Helper::get_err_struct(Const_Err_Base::ERR_DATA_FORMAT, '模块[' . $mid . ']返回数据错误'); } return $data; }
/** * @param array * @param string * @param string * @param string * @param string * @param string * @param string * @param int * @param string * @return array */ function save_fail_data($db, $task_id, $ap_id, $ts, $uid, $content, $module, $errno, $errmsg) { $data = []; $data['_id'] = $task_id . '_' . $uid; $data['task_id'] = $task_id; $data['uid'] = $uid; $data['ap_id'] = $ap_id; $data['ts'] = $ts; $data['content'] = $content; $data['module'] = $module; $data['errno'] = $errno; $data['errmsg'] = $errmsg; $this->get_db()->set_db_name('fail_data'); $this->get_db()->set_table_name('t_fail_data'); $ret = $this->get_db()->save($data); if (0 != $ret['errno']) { Lib_Log::error(__METHOD__ . ' Fail to save fail data.'); } return $ret; }
/** * @param int $id * @param string $value * @return array */ private static function _format_field_value($id, $value) { if (!is_array($value)) { Lib_Log::debug('filed values is not array, values: %s', json_encode($value)); return []; } foreach ($value as $_k => $_v) { if (is_array($_v) && isset($_v[Const_Interface::FIELD_ATTR_RELATION])) { $new_value = []; if (!is_array($_v[Const_Interface::FIELD_ATTR_RELATION])) { Lib_Log::error('Fields error, please check relaction fields :' . json_encode($_v) . ' in an array'); continue; } foreach ($_v[Const_Interface::FIELD_ATTR_RELATION] as $__value) { $new_value[] = [Const_Interface::FIELD_ATTR_KEY => $__value, Const_Interface::FIELD_ATTR_MODULE => $id]; } $value[$_k][Const_Interface::FIELD_ATTR_RELATION] = $new_value; } } return $value; }
/** * @param int $task_id * @param int $module_id * @param array $data * @param array $options * @return array */ function run_module($task_id, $module_id, $data, $options) { $class = $this->get_module_class($module_id); Lib_Log::debug("%s begin run module [%d]\ttask_id:[%d]\toptions:%s", [__METHOD__, $module_id, $task_id, json_encode($options)]); $class->set_options($options); $this->run_hooks(self::get_run_module_hook_type(self::HOOK_TYPE_BEFORE_RUN_MODULE_PREFIX, $module_id), [$task_id, $module_id, $class, $data]); $this->run_hooks(self::HOOK_TYPE_BEFORE_RUN, [$task_id, $module_id, $class, $data]); try { $result = $class->run($task_id, $data); } catch (Exception $ex) { $code = $ex->getCode(); $msg = $ex->getMessage(); Lib_Log::error("Got a Exception, code [%d], message [%s]", [$code, $msg]); $result = Lib_Helper::get_err_struct($code, $msg); } $this->run_hooks(self::get_run_module_hook_type(self::HOOK_TYPE_AFTER_RUN_MODULE_PREFIX, $module_id), [$task_id, $module_id, $class, $result]); $this->run_hooks(self::HOOK_TYPE_AFTER_RUN, [$task_id, $module_id, $class, $result]); return $result; }
/** * @param int $module_id * @return array */ static function functional_module_class_name($module_id) { $module = self::get_instance()->get_module_info_by_id($module_id); if (empty($module)) { return Lib_Helper::get_err_struct(Const_Err_DataAccess::ERR_MODULE_NOT_EXISTS, '模块[' . $module_id . ']不存在'); } if (!isset($module[Const_Module::META_TYPE])) { Lib_Log::error('errno:' . Const_Err_DataAccess::ERR_MODULE_INCORRECT_INFO . ' module not has meta type, module_id: ' . $module_id); return Lib_Helper::get_err_struct(Const_Err_DataAccess::ERR_MODULE_INCORRECT_INFO, ' module not has meta type, module_id: ' . $module_id); } if (strcmp($module[Const_Module::META_TYPE], Const_Module::TYPE_FUNCTIONAL) !== 0) { Lib_Log::error('errno:' . Const_Err_DataAccess::ERR_MODULE_WRONG_TYPE . ' module is not functional module, module_id: ' . $module_id); return Lib_Helper::get_err_struct(Const_Err_DataAccess::ERR_MODULE_WRONG_TYPE, ' module not has meta type, module_id: ' . $module_id); } $class_name = $module[Model_ModulesInfo::FIELD_MODULE_CLASS]; if (!class_exists($class_name)) { Lib_Log::error("errno: %d, err: module class %s not exists!", [Const_Err_DataAccess::ERR_MODULE_CLASS_NOT_EXISTS, $class_name]); return Lib_Helper::get_err_struct(Const_Err_DataAccess::ERR_MODULE_CLASS_NOT_EXISTS, 'Module class ' . $class_name . ' not exists!'); } return Lib_Helper::get_return_struct($class_name); }
/** * Launch a job from the job queue */ protected function launchJob($childData) { $pid = pcntl_fork(); if ($pid == -1) { //Problem launching the job Lib_Log::error('Could not launch new job, exiting'); return false; } else { if ($pid) { $t = time(); // Parent process // Sometimes you can receive a signal to the childSignalHandler function before this code executes if // the child script executes quickly enough! // $this->currentJobs[$pid] = time(); // In the event that a signal for this pid was caught before we get here, it will be in our signalQueue array // So let's go ahead and process it now as if we'd just received the signal if (isset($this->signalQueue[$pid])) { Lib_Log::info("found {$pid} in the signal queue, processing it now"); $this->childSignalHandler(SIGCHLD, $pid, $this->signalQueue[$pid]); unset($this->signalQueue[$pid]); } } else { try { //Forked child, do your deeds.... $exitStatus = 0; //Error code if you need to or whatever $this->childJob($childData); exit($exitStatus); } catch (Exception $e) { exit($e->getCode()); Lib_Log::error($e->getMessage()); } } } return $pid; }
static function task_list_action() { $task_id = Lib_Request::get_int('task_id'); $ap_name = Lib_Request::get_var('ap_name'); $ap_name = trim($ap_name); $src_id = Lib_Request::get_var('sub_src'); $src_id = trim($src_id); $status = Lib_Request::get_int('status'); $time_begin = Lib_Request::get_var('time_begin'); $time_begin = trim($time_begin); $time_end = Lib_Request::get_var('time_end'); $time_end = trim($time_end); $cond = []; $search_vars = []; $ap_cond = []; if (!empty($task_id)) { $cond = ['_id' => $task_id]; $search_vars = ['task_id' => $task_id]; } else { if (strpos($src_id, ',')) { $src_ids = explode(',', $src_id); $int_src_ids = []; foreach ($src_ids as $k => $v) { $int_src_ids[] = intval($v); } $cond['src_id'] = ['$in' => array_merge($src_ids, $int_src_ids)]; } elseif (!empty($src_id)) { $cond['src_id'] = ['$in' => [$src_id, intval($src_id)]]; } if (!empty($status)) { $cond['status'] = $status; $search_vars['status'] = $status; } if (!empty($time_begin) || !empty($time_end)) { $time_cond = []; if (!empty($time_begin)) { $time_begin = strtotime($time_begin); $time_cond['$gt'] = $time_begin; } if (!empty($time_end)) { $time_end = strtotime($time_end); $time_cond['$lte'] = $time_end; } if ($time_begin >= $time_end) { unset($time_cond['$lte']); } if (!empty($time_cond)) { $cond['create_time'] = $time_cond; } } } if (!Module_Account_User::has_perms([Module_Account_Perm::PERM_AP_ADMIN])) { $ap_cond['interface_people'] = new MongoRegex("/" . Module_Account_User::get_current_user() . "/"); } if (!empty($ap_name)) { $ap_cond = ['ap_name' => new MongoRegex("/" . $ap_name . "/")]; } if (!empty($ap_cond)) { $search_vars['ap_name'] = $ap_name; $ap_ids = []; $int_ap_ids = []; $model = new Model_AccessPoint(); $ap_infos = $model->get_all($ap_cond); if ($ap_infos['errno'] == Const_Err_Base::ERR_OK) { foreach ($ap_infos['data'] as $k => $ap) { $ap_ids[] = $ap['ap_id']; $int_ap_ids[] = intval($ap['ap_id']); } } $cond['ap_id'] = ['$in' => array_merge($ap_ids, $int_ap_ids)]; } $model_task = new Model_Task(); $total_num = $model_task->get_tasks_count($cond); $current_page = Lib_Request::get_int('page'); //当前页码,必须 $total_size = isset($total_num['data']) ? intval($total_num['data']) : 0; //总记录数,必须 $page_size = 15; //每页条数,必须 $skip = ($current_page - 1) * $page_size; //跳过记录,必须 $skip = $skip < 0 ? 0 : $skip; $mode = 2; //页码模式,决定页码个数,默认1 $tasks = $model_task->get_all($cond, $page_size, $skip); if ($tasks['errno'] !== Const_Err_Base::ERR_OK) { Lib_Log::error(Lib_Helper::format_err_struct($tasks)); $tasks = []; Lib_Request::flash('未获取到任务信息'); } $tasks = $tasks['data']; $ap_infos = []; $ap_model = new Model_AccessPoint(); foreach ($tasks as $_k => $_t) { $ap_info = $ap_model->get_ap_by_id($_t['ap_id']); $ap_info['ap_id'] = $_t['ap_id']; if ($ap_info['errno'] !== Const_Err_Base::ERR_OK) { Lib_Log::error(Lib_Helper::format_err_struct($ap_info)); Lib_Request::flash('获取接入点信息失败'); Module_HttpRequest_Router::redirect_to('/'); } $ap_info = $ap_info[Const_DataAccess::MREK_DATA]; $ap_infos[$_t['ap_id']] = $ap_info; } $source_model = new Model_Source(); $all_sources = $source_model->get_all(); $sources = $all_sources[Const_DataAccess::MREK_DATA]; $all_src_type = []; foreach ($all_sources['data'] as $k => $v) { if (!in_array($v['src_type'], $all_src_type)) { $all_src_type[] = $v['src_type']; } } asort($all_src_type); $task_status_list = Module_ControlCentre_Main::$task_status_list; $pages = Module_View_Template::get_pages_html($current_page, $total_size, $page_size, $mode); Module_Page_Main::render('control_centre/task_list', ['ap_info' => $ap_infos, 'tasks' => $tasks, 'sources' => $sources, 'pages' => $pages, 'all_src_type' => $all_src_type, 'status_list' => $task_status_list, 'search_vars' => $search_vars]); }
/** * @param array $cond * @return array */ public function get_tables($cond = []) { switch ($this->_driver) { case self::DB_DRIVER_MONGO: default: Lib_Log::debug(__METHOD__ . ': Use mongo driver, cond: %s', [json_encode($cond)]); try { $result = $this->_db->GetTables($cond); if (is_null($result)) { $result = Lib_Helper::get_err_struct(Const_Err_Db::ERR_MONGO_COLLECTIONS_FAIL, '获取数据集失败', __FILE__, __LINE__); } else { $result = Lib_Helper::get_return_struct($result); } } catch (Exception $ex) { Lib_Log::error(__METHOD__ . " get faild! cond: %s, code: %d, messsage: %s", [json_encode($cond), $ex->getCode(), $ex->getMessage()]); $result = Lib_Helper::get_err_struct(Const_Err_Db::ERR_MONGO_COLLECTIONS_FAIL, '获取数据集失败', __FILE__, __LINE__); } } return $result; }
<?php session_start(); require_once dirname(dirname(__FILE__)) . '/app/init.php'; Module_HttpRequest_Router::get_instance()->register_pre_router_hook(['Module_Account_Manager', 'check_perm']); Module_View_Main::view()->set_template_dir(Da\Sys_App::template_path()); $router = [Module_HttpRequest_Router::get_instance(), 'route']; if (Da\Sys_Router::get_instance()->register_router($router)) { MT::lang(MT::LANG_ZH_CN); $dispatch = Da\Sys_Router::get_instance()->dispatch(); if ($dispatch['errno'] != Da\Sys_Router::ERRNO_OK) { echo $dispatch['data']; exit; } } else { Lib_Log::error('Route rules ' . json_encode($rules) . ' is not callable!'); }
static function index_action() { $model_task = new Model_Task(); $tasks = $model_task->get_all(['status' => 2], 10); if ($tasks['errno'] !== Const_Err_Base::ERR_OK) { Lib_Log::error(Lib_Helper::format_err_struct($tasks)); $tasks = []; } $tasks = $tasks['data']; $ap_ids = []; $src_ids = []; foreach ($tasks as $_k => $_t) { $ap_ids[$_k] = isset($_t['ap_id']) ? $_t['ap_id'] : ''; $src_ids[$_k] = isset($_t['src_id']) ? $_t['src_id'] : ''; } $ap_model = new Model_AccessPoint(); $aps = $ap_model->get_ap_by_ids($ap_ids); if ($aps['errno'] !== Const_Err_Base::ERR_OK) { Lib_Log::error(Lib_Helper::format_err_struct($aps)); $aps = []; } $aps = $aps['data']; $source_model = new Model_Source(); $sources = $source_model->get_sources_by_ids($src_ids); if ($sources['errno'] !== Const_Err_Base::ERR_OK) { Lib_Log::error(Lib_Helper::format_err_struct($sources)); $sources = []; } $sources = $sources['data']; $gt_time = strtotime('-1 month'); $source_model->set_table_name('t_src_statistics'); $cond = ['update_time' => ['$gte' => $gt_time]]; $source_statistics = $source_model->get_table()->get_all($cond, [], 0, 0, null, true); if ($source_statistics['errno'] !== Const_Err_Base::ERR_OK) { Lib_Log::error(Lib_Helper::format_err_struct($source_statistics)); $source_statistics = []; } $source_statistics = $source_statistics['data']; $statistics = []; foreach ($source_statistics as $v) { $day = date('m-d', $v['update_time']); $statistics[$day] = isset($statistics[$day]) ? $statistics[$day] : []; $statistics[$day]['total'] = isset($statistics[$day]['total']) ? $statistics[$day]['total'] : 0; $statistics[$day]['status_1'] = isset($statistics[$day]['status_1']) ? $statistics[$day]['status_1'] : 0; $statistics[$day]['status_other'] = isset($statistics[$day]['status_other']) ? $statistics[$day]['status_other'] : 0; $statistics[$day]['total'] += intval($v['total']); $statistics[$day]['status_1'] += intval($v['status_1']); $statistics[$day]['status_other'] = $statistics[$day]['total'] - $statistics[$day]['status_1']; } foreach ($statistics as $day => $v) { $source_statistics_min = isset($source_statistics_min) && $source_statistics_min < $v['status_1'] ? $source_statistics_min : $v['status_1']; } $_access = Module_Statistics_Action::get_index_month_charts(); $access_date = $_access['access_date']; $receive_data = $_access['receive_data']; $send_data = $_access['send_data']; self::render('common/index', ['tasks' => $tasks, 'aps' => $aps, 'sources' => $sources, 'source_statistics' => $statistics, 'source_statistics_min' => $source_statistics_min, 'access_date' => $access_date, 'send_data' => $send_data, 'receive_data' => $receive_data]); }
/** * @param array $template * @return bool|string */ public function get_template_file($template) { $template_file = $this->_template_dir . '/' . $template . '.phtml'; if (is_file($template_file)) { return $template_file; } else { Lib_Log::error('teplate file [' . $template_file . '] not exists!'); return false; } }