profile_error.hbs 20.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
<div class="pageheader">
    <div class="media">
        <div class="pageicon pull-left">
            <i class="fa fa-th-list"></i>
        </div>
        <div class="media-body">
            <ul class="breadcrumb">
                <li><a href=""><i class="glyphicon glyphicon-home"></i></a></li>
                <li><a href="">性能监控</a></li>
                <li>错误分析</li>
            </ul>
            <h4>错误分析</h4>
        </div>
    </div>
    <!-- media -->
</div>
<!-- pageheader -->

<style>
    .form-item {
        margin-right: 10px;
        margin-bottom: 10px;
    }
    .time, .app, .type, .route, .api, .uid, .code, .preqid, .reqid, .script, .message, .stack .useragent, .line, .column, .hostname{
        word-wrap: break-word;
        word-break:break-all;
    }

pre {
    padding: 10px;
    overflow: hidden;

    font-family: consolas,monospace;

    word-wrap: break-word;
    word-break:break-all;
}

.errdetail {
    margin-top: 10px;

    background-color: #fff;
}
.errheader {
    padding: 10px;

    border-bottom: 1px solid #d6d2d2;
}
.code-line {
    padding: 4px;
}
.code-line.heightlight {
    color: #fff;
    background-color: #6c5fc7;
}

</style>


<div class="contentpanel page-servers">
    <div class="panel panel-default">
        <div class="panel-heading" style="overflow: hidden">
                <div class="form-inline inline-block form-item">
                    <label for="appName" class="control-label">主机</label>
                        <select id="selectedHost" class="form-control" >
                            <option value="">全部</option>
                            {{#each hostName}}
                                <option value="{{this}}">{{this}}</option>
                            {{/each}}
                        </select>
                </div>

                <div class="form-inline inline-block form-item">
                    <label for="appName" class="control-label">应用</label>
                        <select id="selectedApp" class="form-control" >
                            <option value="">全部</option>
                            {{#each app}}
                                <option value="{{this}}">{{this}}</option>
                            {{/each}}
                        </select>
                </div>

                <div class="form-inline inline-block form-item">
                    <label for="appName" class="control-label">类型</label>
                        <select id="selectedType" class="form-control" >
                            <option value="">全部</option>
                            {{#each type}}
                                <option value="{{this}}">{{this}}</option>
                            {{/each}}
                        </select>
                </div>

                <div class="form-inline inline-block form-item">
                    <label for="appName" class="control-label">时间</label>
                    <div id="reportrange" style="display: inline-block; background: #fff; cursor: pointer; padding: 9px 10px; border: 1px solid #ccc; width: 210px; color: black;">
                        <i class="glyphicon glyphicon-calendar fa fa-calendar"></i>&nbsp;
                        <span></span> <b class="caret"></b>
                    </div>
                </div>

                <div class="form-inline inline-block form-item">
                    <label for="appName" class="control-label">错误编码</label>
                        <select id="selectedCode" class="form-control" >
                            <option value="">全部</option>
                            {{#each code}}
                                <option value="{{this}}">{{this}}</option>
                            {{/each}}
                        </select>
                </div>

                <div class="form-inline inline-block form-item">
                    <label for="appName" class="control-label">路由</label>
                        <select id="selectedRoute" class="form-control" >
                            <option value="">全部</option>
                            {{#each route}}
                                <option value="{{this}}">{{this}}</option>
                            {{/each}}
                        </select>
                </div>

                <div class="form-inline inline-block form-item">
                    <label for="appName" class="control-label">api</label>
                        <select id="selectedApi" class="form-control" >
                            <option value="">全部</option>
                            {{#each api}}
                                <option value="{{this}}">{{this}}</option>
                            {{/each}}
                        </select>
                </div>

                <div class="form-inline inline-block">
                    <button id="search" class="btn btn-info" style="margin-left: 20px;">查询</button>
                </div>

                <div class="form-inline inline-block">
                    <button id="clear" class="btn btn-info" style="margin-left: 20px;">清除</button>
                </div>

        </div>
        <!-- panel-heading -->

        <table id="table-servers" class="table table-striped table-bordered">
            <thead class="">
            <tr>
                <td>时间</td>
                <td>应用</td>
                <td>类型</td>
                <td>路由</td>
                <td>接口名</td>
                <td>错误状态码</td>
                <td>操作</td>
            </tr>
            </thead>

            <tbody>
            </tbody>
        </table>
    </div>
    <!-- panel -->

    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog modal-lg" role="document" style="display:table;">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">详细信息</h4>
                </div>
                <div class="modal-body">
                    <table id="table-servers" class="table table-striped table-bordered">
                        <thead class="">
                        <tr>
                        </tr>
                        </thead>

                        <tbody>
                            <tr>
                                <td width="12%">时间</td>
                                <td class="time"></td>
                            </tr>
                            <tr>
                                <td>应用</td>
                                <td class="app"></td>
                            </tr>
                            <tr>
                                <td>类型</td>
                                <td class="type"></td>
                            </tr>
                            <tr>
                                <td>主机</td>
                                <td class="hostname"></td>
                            </tr>
                            <tr>
                                <td>路由</td>
                                <td class="route"></td>
                            </tr>
                            <tr>
                                <td>接口名</td>
                                <td class="api"></td>
                            </tr>
                            <tr>
                                <td>用户标识</td>
                                <td class="uid"></td>
                            </tr>
                            <tr>
                                <td>useragent</td>
                                <td class="useragent"></td>
                            </tr>
                            <tr>
                                <td>错识状态码</td>
                                <td class="code"></td>
                            </tr>
                            <tr>
                                <td>父请求标识</td>
                                <td class="preqid"></td>
                            </tr>
                            <tr>
                                <td>请求标识</td>
                                <td class="reqid"></td>
                            </tr>
                            <tr>
                                <td>行号</td>
                                <td class="line"></td>
                            </tr>
                            <tr>
                                <td>列号</td>
                                <td class="column"></td>
                            </tr>
                            <tr>
                                <td>脚本</td>
                                <td class="script"></td>
                            </tr>
                            <tr>
                                <td>接口返回消息</td>
                                <td class="message"></td>
                            </tr>
                            <tr>
                                <td>错误堆栈</td>
                                <td class="stack"></td>
                            </tr>
                            <tr class='js-source'>
                                <td>源文件</td>
                                <td><pre class="source"></pre></td>
                            </tr>
                        </tbody>
                    </table>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                </div>
            </div>
        </div>
    </div>
</div>


<script>

    function encodeHTML(str) {
        if(!str || str.length == 0) return "";
        return str.replace(/&/g, "&#38;").replace(/</g, "<").replace(/>/g, ">").replace(/\'/g, "'");
    }

    var data_end_point = '/profile/error.json';
    var dataTable = null;

    var query = {
        hostname: '',
        app: '',
        type: '',
        time: '',
        code: '',
        route: '',
        api: ''
    };

    function qs(obj) {
        return Object.keys(obj).map(key  => {
            return `${key}=${encodeURIComponent(obj[key])}`;
        }).join('&')
    }

    function ajaxUrl() {
        let result =  `${data_end_point}?${qs(query)}`;
        return result;
    }

    $(function() {
        initTable();
        initSelect();
        initDatePicker();
    });

    function formateDate(d) {
        return (d.getMonth() + 1) + "/" + d.getDate() + "/" + d.getFullYear() +
        '  ' + ("0" + d.getHours()).slice(-2) + ":" +
        ( "0" + d.getMinutes()).slice(-2) + ":" +
        ( "0" + d.getSeconds()).slice(-2)
    }

    function initTable() {
        dataTable = $("#table-servers").DataTable({
            pageLength: 20,
            serverSide: true,
            retrieve: true,
            searching: false,
            ajax: ajaxUrl(),
            dataSrc: 'data',
            pageLength: 25,
            ordering: false,
            columns: [
                {data: 'time'},
                {data: 'app'},
                {data: 'type'},
                {data: 'route'},
                {data: 'api'},
                {data: 'code'},
                {data: 'detail'},
            ],
            data: [{
                time: "",
                app: "",
                type: "",
                route: "",
                api: "",
                code: "",
                detail: ""
            }],
            columnDefs: [
                {
                    render: function(data, type, row) {
                        if (!data) {
                            return '';
                        }

                        return formateDate(new Date(data));
                    },
                    targets: 0,
                    width: '12%'
                },{
                    width: '10%',
                    targets: 1
                },{
                    width: '5%',
                    targets: 2
                },{
                    width: '20%',
                    targets: 3
                },{
                    width: '20%',
                    targets: 4
                },{
                    width: '8%',
                    targets: 5
                },{
                    targets: -1,
                    data: null,
                    render: function ( data, type, row ) {
                        return '<button>查看</button>';
                    }
                }

            ]

        });

        $('#myModal').on('hidden.bs.modal', function() {
            clearModalData();
        });

        $('#table-servers tbody').on( 'click', 'button', function (e) {
            var data = dataTable.row($(this).parents('tr')).data();

            setModalData(data);
            $('#myModal').modal('show');

            e.preventDefault();
        });
    }

    function clearModalData() {
        $('.time').text('');
        $('.app').text('');
        $('.type').text('');
        $('.route').text('');
        $('.api').text('');
        $('.uid').text('');
        $('.code').text('');
        $('.preqid').text('');
        $('.reqid').text('');
        $('.script').text('');
        $('.message').text('');
        $('.stack').text('');
        $('.useragent').text('');
        $('.line').text('');
        $('.column').text('');
        $('.hostname').text('');
        $('.source').html('');
    }

    function setModalData(data) {
        $('.time').text(formateDate(new Date(data.time)));
        $('.app').text(data.app);
        $('.type').text(data.type);
        $('.route').text(data.route);
        $('.api').text(data.api);
        $('.uid').text(data.uid);
        $('.code').text(data.code);
        $('.preqid').text(data.preqid);
        $('.reqid').text(data.reqid);
        $('.script').text(data.script);
        $('.message').text(data.message);
        $('.stack').text(data.stack);
        $('.useragent').text(data.useragent);
        $('.line').text(data.line);
        $('.column').text(data.column);
        $('.hostname').text(data.hostname);

        $('.source').data('url', data.script);
        $('.source').data('line', data.line);
        $('.source').data('column', data.column);

        if (data.type !== 'client') {
            $('.js-source').addClass('hide');
            return;
        }

        if (!/cdn.yoho.cn/.test(data.script)) {
            $('.js-source').addClass('hide');
            return;
        }

        $('.js-source').removeClass('hide');
        errorDetail(data.script, data.line, data.column);
    }

    function clickErrorDetail() {
        $('.source').on('click', function() {
            $this = $('.source');
            errorDetail($this.data('url'), $this.data('line'), $this.data('column'));
        })
    }

    const TIME = {
        '最近1分钟': [1, 'm'],
        '最近30分钟':  [30, 'm'],
        '最近1小时':  [1, 'h'],
        '最近3小时': [3, 'h'],
        '最近6小时': [6, 'h'],
        '最近12小时': [12, 'h']
    };

    function initDatePicker() {
        var first = '无';

        $('#reportrange span').html(first);

        lastTime = TIME[first];

        function cb(start, end, label) {
            if (label && label !== '自定义日期') {
                $('#reportrange span').html(label);

                var lastTime = TIME[label];
                var selectedStartTime = moment().subtract(...lastTime).valueOf();
                var selectedEndTime = moment().valueOf();

                query.time = `${selectedStartTime}:${selectedEndTime}`;
            } else if (label && label === '自定义日期'){
                $('#reportrange span').html(start.format('YYYY-MM-DD') + ' 至 ' + end.format('YYYY-MM-DD'));

                var selectedStartTime = moment(start.format('YYYY-MM-DD')).valueOf();
                var selectedEndTime = moment(end.add(1, 'd').format('YYYY-MM-DD')).valueOf();

                query.time = `${selectedStartTime}:${selectedEndTime}`;
            }

        }

        $('#reportrange').daterangepicker({
            startDate: undefined,
            endDate: undefined,
            timePicker: true,
            timePicker24Hour: true,
            dateLimit: {
                days: 7
            },
            ranges: {
                '最近1分钟': [moment().subtract(1, 'minutes'), moment()],
                '最近30分钟': [moment().subtract(30, 'minutes'), moment()],
                '最近1小时': [moment().subtract(1, 'hours'), moment()],
                '最近3小时': [moment().subtract(3, 'hours'), moment()],
                '最近6小时': [moment().subtract(6, 'hours'), moment()],
                '最近12小时': [moment().subtract(12, 'hours'), moment()]
            },
            locale: {
                format: "YYYY-MM-DD",
                applyLabel: '确定',
                cancelLabel: '取消',
                weekLabel: 'W',
                customRangeLabel: '自定义日期',
                daysOfWeek: '日_一_二_三_四_五_六'.split('_'),
                monthNames: '1月_2月_3月_4月_5月_6月_7月_8月_9月_10月_11月_12月'.split('_'),
                firstDay: 1
            },
        }, cb);

        $('#reportrange').on('cancel.daterangepicker', function() {
            $('#reportrange').data('daterangepicker').setStartDate(undefined);
            $('#reportrange').data('daterangepicker').setEndDate(undefined);
            $('#reportrange span').html(first);
        });
    }

    function initSelect() {
        $('#selectedHost').select2({
            width: 'resolve'
        });

        $('#selectedHost').on('change', function() {
            query.hostname = $('#selectedHost').val();
        });

        $('#selectedApp').change(function() {
            query.app = $('#selectedApp').val();
        });

        $('#selectedType').change(function() {
            query.type = $('#selectedType').val();
        });

        $('#selectedCode').change(function() {
            query.code = $('#selectedCode').val();
        });

        $('#selectedRoute').select2( {
            width: 'resolve'
        });

        $('#selectedRoute').on('change', function() {
            query.route = $('#selectedRoute').val();
        });

        $('#selectedApi').select2({
            width: 'resolve'
        });

        $('#selectedApi').on("change", function() {
            query.api = $('#selectedApi').val()
        });

        $('#search').on('click', () => {
            _handleChanged();
        });

        $('#clear').on('click', () => {
            clearSelect();
        })
    }

    function _handleChanged() {
        dataTable && dataTable.ajax.url(ajaxUrl()).load();
    }

    function clearSelect() {
        $('#selectedHost').val('').change();
        $('#selectedApp').val('');
        $('#selectedType').val('');
        $('#selectedCode').val('');

        $('#selectedRoute').val('').change();
        $('#selectedApi').val('').change();
        $('#reportrange').trigger('cancel.daterangepicker');

        query = {
            hostname: '',
            app: '',
            type: '',
            time: '',
            code: '',
            route: '',
            api: ''
        };
    }

    function errorDetail(script, line, column) {
        if (!/cdn.yoho.cn/.test(script)) {
            return ;
        }

        $.get('/profile/errordetail', {
            url: script,
            line: line,
            column: column
        }).then(res => {
            if (!res.file) {
                return;
            }

            var lines = res.file.split('\n');

            var row = res.row,
                len = lines.length - 1;

            var start = row - 10 >= 0? row - 10: 0,
                end = start + 19 >= len? len: start + 19; // 最多展示 20 行

            var newLines = [];
            for(var i = start; i <= end; i++) {
                newLines.push('<div class="code-line '+ (i + 1 == row? 'heightlight': '')+ '">' + (i+1) + '.    ' + encodeHTML(lines[i]) + '</div>');
            }

            var html = '<div class="errdetail"><div class="errheader">' + res.source + ' at line ' + res.row + ':' + res.column + '</div>' + 
                '<pre class="errCode">' + newLines.join("") + '</pre></div>';

            $('.source').html(html);
        })
    }

</script>