首先介绍我实现的是xhprof插件的日志转为无限树状图,先看效果图:
废话不多说,直接看代码:(辛辛苦苦敲了好久才搞定,逻辑比较多,新手多揣摩)
控制器:
namespace AdminController;
class XhprofController extends AdminbaseController
{
function _initialize()
{
parent::_initialize();
}
public function index()
{
$file = scandir("./public/xhprof");
$str = ".xhprof";
foreach ($file as $value) {
if (preg_replace("/($str)/","",$value) != $value) {
$dir[] = $value;
}
}
$this->assign("dir",$dir);
$this->display();
}
//获取适当的结构
public function tree($file_name)
{
$file_path = "./public/xhprof/$file_name";
$file_name = str_replace('.','',$file_name);
if (F($file_name)) {
$mainTree = F($file_name);
} else {
if (file_exists($file_path)) {
$fp = fopen($file_path,"r");
$str = fread($fp,filesize($file_path));//指定读取大小,这里把整个文件内容读取出来
$array = json_decode($str,true);
$array_keys = array_keys($array);
//按照调用者整理数据
$calls = [];
foreach ($array_keys as $array_key) {
$caller = explode("==>",$array_key)[0];
$beCall = explode("==>",$array_key)[1];
if (!$calls[$caller]) {
$calls[$caller] = [
"caller" => $caller,
"beCalls" => []
];
}
$status = 0;//是否有子目录
foreach ($array_keys as $keyv) {
if (strpos($keyv,$beCall . '==>') !== false && strpos($keyv,"@") === false && $beCall !== null && $beCall !== "count") {
$status = 1;
}
}
if ($beCall !== null) {
$calls[$caller]['beCalls'][] = array('name' => $beCall,"status" => $status,'data' => $array[$array_key]);
}
}
foreach ($calls as &$v) {
$tmpAll = 0;
foreach ($v['beCalls'] as $vv) {
$tmpAll += $vv['data']['wt'];
}
foreach ($v['beCalls'] as &$vv1) {
$vv1['data']['wtp'] = round($vv1['data']['wt'] * 100 / $tmpAll,2);
$vv1['wtp'] = $vv1['data']['wt'];
}
}
$mainTree = $calls;
//缓存$mainTree
F($file_name,$mainTree);
} else {
$mainTree = "";
}
}
array_multisort(array_column($mainTree["main()"]["beCalls"],"wtp"),SORT_DESC,array_column($mainTree["main()"]["beCalls"],"name"),$mainTree["main()"]["beCalls"]);
$this->assign("main",$array["main()"]);
$this->assign("mainTree",$mainTree["main()"]);
$this->assign("level",1);
$this->assign("file_name",$file_name);
$this->assign("file_path",$file_path);
$this->display();
}
public function ajax()
{
$note = htmlspecialchars_decode(I("post.note"));
$file_name = htmlspecialchars_decode(I("post.file_name"));
$file_path = htmlspecialchars_decode(I("post.file_path"));
//处理$mainTree
if (F($file_name)) {
$mainTree = F($file_name);
} else {
if (file_exists($file_path)) {
$fp = fopen($file_path,$mainTree);
} else {
$mainTree = "";
}
}
array_multisort(array_column($mainTree[$note]["beCalls"],array_column($mainTree[$note]["beCalls"],$mainTree[$note]["beCalls"]);
$this->ajaxReturn($mainTree[$note]);
}
}
veiw界面:
<Meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
$(function () {
$('.tree li:has(ul)').addClass('parent_li').find(' > span').attr('title','Collapse this branch');
$('body').on('click',"span",function (e) {
e.stopPropagation();
if ($(this).attr('is_click') === 1) {
return false;
}
$(this).attr('is_click',1);
var children = $(this).parent('li.parent_li').find(' > ul > li');
if (children.is(":visible")) {
children.hide('fast');
$(this).attr('title','Expand this branch').find(' > i').addClass('icon-plus').removeClass('icon-minus');
$(this).attr('is_click',0);
} else {
children.show('fast');
$(this).attr('title','Collapse this branch').find(' > i').addClass('icon-minus').removeClass('icon-plus');
if ($(this).hasClass('main')) {
$(this).attr('is_click',0);
return;
}
if (!$(this).siblings('.child').length) {
do_ajax($(this));
} else {
$(this).attr('is_click',0);
}
}
});
function do_ajax(e) {
var note_js = e.attr('note');
var level_js = e.attr('level');
var file_name_js = e.attr('file_name');
var file_path_js = e.attr('file_path');
$.ajax({
type: "post",
cache: false,
dataType: "json",
url: "{:U('Admin/Xhprof/ajax')}",
data: {
note: note_js,
level: level_js,
file_name: file_name_js,
file_path: file_path_js
},
success: function (data) {
if (data.beCalls !== null) {
var str = "";
for (var i = 0; i < data.beCalls.length; i++) {
str += "
- ";
- ";
str += "
if (data.beCalls[i].status == 1) {
str += "";
} else {
str += " ";
}
str += "第 ' + eval(Number(level_js) + 1) + " 级   ct:" + data.beCalls[i].data.ct + " wt:" + data.beCalls[i].data.wt + "(" + data.beCalls[i].data.wtp + "%) cpu:" + data.beCalls[i].data.cpu + " mu:" + data.beCalls[i].data.mu + " pmu:" + data.beCalls[i].data.pmu + "
";str += '
';
str += "
str += '
}
e.siblings('.child').remove();
e.after(str);
e.attr('is_click',0);
}
},
error: function () {
}
})
}
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。