feat(admin): XHProf 函数监控规则与命中记录

This commit is contained in:
augushong
2026-08-15 07:02:37 +08:00
parent e57b186126
commit c980ebdff0
23 changed files with 901 additions and 1 deletions

View File

@@ -0,0 +1,84 @@
<?php
namespace app\admin\controller\xhprof;
use app\common\controller\AdminController;
use app\admin\service\annotation\ControllerAnnotation;
use app\admin\service\annotation\NodeAnotation;
use think\App;
/**
* XHProf 监控规则命中记录(只读)
* 数据由导入定时任务物化写入,后台仅查看,不支持增删改。
* 页面零计算:耗时换算与阈值比对均在前端展示层完成。
*
* @ControllerAnnotation(title="XHProf 命中记录")
*/
class RunWatch extends AdminController
{
use \app\admin\traits\Curd;
protected $relationSearch = true;
public function __construct(App $app)
{
parent::__construct($app);
$this->model = new \app\admin\model\XhprofRunWatch();
// 命中记录的规则下拉(含禁用规则,便于回溯历史命中)
$this->assign('select_list_watch', \app\admin\model\XhprofWatch::column('name', 'id'), true);
}
/**
* @NodeAnotation(title="列表")
*/
public function index()
{
if ($this->request->isAjax()) {
if (input('selectFields')) {
return $this->selectList();
}
list($page, $limit, $where, $excludes, $request_options, $group) = $this->buildTableParames();
$count = $this->model
->withJoin(['run', 'watch'], 'LEFT')
->where($where)
->group($group)
->count();
$list = $this->model
->withJoin(['run', 'watch'], 'LEFT')
->where($where)
->page($page, $limit)
->order($this->sort)
->group($group)
->select();
$data = [
'code' => 0,
'msg' => '',
'count' => $count,
'data' => $list,
];
return json($data);
}
return $this->fetch();
}
/**
* @NodeAnotation(title="详情")
*/
public function read($id)
{
$row = $this->model->withJoin(['run', 'watch'], 'LEFT')->find($id);
empty($row) && $this->error('数据不存在');
// 获取模型的标题(表注释)
$title = $row->title;
$this->assign('row', $row);
$this->assign('title', $title);
return $this->fetch();
}
}

View File

@@ -0,0 +1,72 @@
<?php
namespace app\admin\controller\xhprof;
use app\common\controller\AdminController;
use app\admin\service\annotation\ControllerAnnotation;
use app\admin\service\annotation\NodeAnotation;
use think\App;
/**
* XHProf 函数监控规则管理
* regex 为 PCRE对被调用函数名匹配保存前校验编译合法性
* 防止非法正则打爆导入任务的规则编译。
*
* @ControllerAnnotation(title="XHProf 函数监控规则")
*/
class Watch extends AdminController
{
use \app\admin\traits\Curd {
\app\admin\traits\Curd::add as curdAdd;
\app\admin\traits\Curd::edit as curdEdit;
}
public function __construct(App $app)
{
parent::__construct($app);
$this->model = new \app\admin\model\XhprofWatch();
$this->assign('select_list_status', $this->model::SELECT_LIST_STATUS, true);
}
/**
* @NodeAnotation(title="添加")
*/
public function add()
{
if ($this->request->isPost()) {
$this->validateWatchRegex((string) $this->request->post('regex', ''));
}
return $this->curdAdd();
}
/**
* @NodeAnotation(title="编辑")
*/
public function edit($id)
{
if ($this->request->isPost()) {
$this->validateWatchRegex((string) $this->request->post('regex', ''));
}
return $this->curdEdit($id);
}
/**
* 校验 PCRE 正则合法性(与导入任务 compileWatchRegexes 同一判据).
*/
protected function validateWatchRegex(string $regex): void
{
if ($regex === '') {
$this->error('正则不能为空');
}
error_clear_last();
if (@preg_match($regex, '') === false) {
$error = error_get_last();
$message = $error !== null ? $error['message'] : preg_last_error_msg();
$this->error('正则非法(必须带定界符,如 ~think\\\\db\\\\.*~' . $message);
}
}
}