main.html 12.8 KB
Newer Older
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Zebu Trace Viewer</title>
    <style>
        /* layout.css Style */
        .upload-drop-zone {
          height: 200px;
          border-width: 2px;
          margin-bottom: 20px;
        }

        /* skin.css Style*/
        .upload-drop-zone {
          color: #ccc;
          border-style: dashed;
          border-color: #ccc;
          line-height: 200px;
          text-align: center
        }
        .upload-drop-zone.drop {
          color: #222;
          border-color: #222;
        }

        .code {
          font-family: monospace;
          font-size: 9pt;
        }

        .code.highlightcode {
            color: red;
        }

        .scrollable {
            height: 720px;
            overflow: auto;
            -webkit-overflow-scrolling: touch;
        }
    </style>
</head>

<body>
<div class="container" id="file-loader">
    <div class="panel panel-default">
        <div class="panel-body">

            <!-- Standar Form -->
            <h4>Open a trace file:</h4>
            <form action="" method="post" enctype="multipart/form-data" id="js-upload-form">
                <div class="form-inline">
                    <div class="form-group">
                        <input type="file" name="files[]" id="js-upload-files" multiple>
                    </div>
                    <button type="submit" class="btn btn-sm btn-primary" id="js-upload-submit">Open</button>
                </div>
            </form>

            <!-- Drop Zone -->
            <h4>Or drag and drop files below:</h4>
            <div class="upload-drop-zone" id="drop-zone">
                Just drag and drop files here
            </div>

            <!-- Progress Bar -->
            <div class="progress" id="progress-div">
                <div class="progress-bar" id="progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%;">
                    <span class="sr-only"></span>
                </div>
            </div>

            <div class="dropdown" id="function-select-div">
                <button class="btn btn-primary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    Select a Function
                </button>
                <div class="dropdown-menu" aria-labelledby="dropdownMenuButton" id="function-select-list">
                </div>
            </div>
        </div>
    </div>
</div> <!-- /container -->

<div class="container" id="file-viewer">
    <div>
        <form class="form-inline" onsubmit="$(this).displaySearch();return false;">
            <input class="form-control" type="text" placeholder="Search" aria-label="Search" id="searchID">
            <button class="btn btn-primary" type="button" onclick="$(this).displaySearch()">Find</button>
        </form>
    </div>
    <ul class="nav nav-tabs" role="tablist" id="myTab">
    </ul>
    <div class="tab-content" id="myTabContent">
        <!--<div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">...</div>-->
        <!--<div class="tab-pane fade" id="profile" role="tabpanel" aria-labelledby="profile-tab">...</div>-->
    </div>
</div>

<!- bootstrap source ->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>
<style>
    /* Overrides list-group-item from Bootstrap */
    .list-group-item {
        padding: 0px 0px
    }
    .nav-tabs > li {
        position:relative;
    }
    .nav-tabs > li > a {
        display:inline-block;
    }
    .nav-tabs > li > span {
        display:none;
        cursor:pointer;
        position:absolute;
        right: 6px;
        top: 8px;
        color: red;
    }
    .nav-tabs > li:hover > span {
        display: inline-block;
    }
</style>
<script>
    + function($) {
    'use strict';

    // UPLOAD CLASS DEFINITION
    // ======================

    var dropZone = document.getElementById('drop-zone');
    var uploadForm = document.getElementById('js-upload-form');

    var startUpload = function(files) {
        if (files.length === 0) {
            console.log("no file selected")
        } else {
            var file = files[0];
            parseFile(file)
        }
    };

    var all_traces = {};

    var parseFile = function(file) {
        console.log("start parsing file...");
        $("#progress-div").show();

        var reader = new FileReader();
        reader.onload = function(progressEvent) {
            // By lines
            var lines = this.result.split('\n');
            var total_lines = lines.length;

            var cur_func = 0;
            startParsingFunction(cur_func);

            var func_start_regex = /.*compilation_start ([0-9]+)/g;

            for(var i = 0; i < total_lines; i++){
                // update progress bar every 100 lines
                if (i % 100 === 0) {
                    updateProgressBar(i, total_lines)
                }

                var line = lines[i];

                // find the start of a function compilation
                var match = func_start_regex.exec(line);
                if (match !== null) {
                    cur_func = match[1];
                    startParsingFunction(cur_func);
                }

                // insert the line to all_traces
                all_traces[cur_func][i] = line;
            }
            updateProgressBar(total_lines, total_lines);
            functionSelect();
        };
        reader.readAsText(file)
        console.log("done");
    };

    var startParsingFunction = function(cur_func) {
        all_traces[cur_func] = {};
        console.log("current function: " + cur_func);
        $("#function-select-list")
            .append('<a class="dropdown-item" href="#" id="' + cur_func + '">' + cur_func + '</a>');
        $("#" + cur_func).on('click', displayTrace);
    };

    var updateProgressBar = function(cur, total) {
        var percentage = (cur / total) * 100;
        $("#progress-bar").css("width", percentage + '%');
        $("#progress-bar").attr("aria-valuenow", percentage);
    };

    var functionSelect = function() {
        $("#function-select-div").show();
    };

    var displayingFunc = 0;

    var displayTrace = function() {
        console.log(this.id + " is chosen");
        displayingFunc = this.id;

        for (var property in all_traces) {
            if (property !== displayingFunc) {
                all_traces[property] = null;
            }
        }

        // hide file loader
        $("#file-loader").hide();

        // prepare for trace display
        $("#file-viewer").show();
    };

    var tabID = 1;
    var getTabID = function() {
        var ret = tabID;
        tabID += 1;
        return ret;
    };

    $.fn.displaySearch = function() {
        var keyword = $("#searchID").val();
        console.log('search for ' + keyword);
        keyword = $.trim(keyword);

        if (keyword !== "") {
            console.log(keyword);
            // a nav item
            var tabID = getTabID();
            newTab(tabID, 'Search:' + keyword, true);
            newTabContent(tabID, keyword);
        } else {
            console.log('empty keyword, skip');
        }
    };

    var highlight_entries = [];

    var activeTab = 0;

    var newTab = function(tabID, name, deletable) {
        var code = '<li class="nav-item"><a class="nav-link" href="#tab-' + tabID + '" data-toggle="tab">' +
            name +
            '</a>';
        if (deletable) {
            code += '<span class="remove-tab" onclick="$(this).deleteTab();">x</span>'
        }
        code += '</li>';
        $("#myTab").append(code);
        $(document).on('shown.bs.tab', 'a[data-toggle="tab"]', function (e) {
            // remove highlight of previous tab
            var prev_href = String($(e.relatedTarget).attr('href'));
            var prev_tab_id = prev_href.substring(1);

            for (var i = 0; i < highlight_entries.length; i++) {
                var id = highlight_entries[i];
                var node = $('#' + prev_tab_id + ' > .' + id);
                node.removeClass('active');
            }

            // highlight current tab
            var href = String($(e.target).attr('href'));
            var tab_id = href.substring(1);
            for (var i = 0; i < highlight_entries.length; i++) {
                var id = highlight_entries[i];
                console.log('on tab load, highlight ' + id);
                var node = $('#' + tab_id + " > ." + id);
                node.addClass('active');
            }
        });
    };

    var newTabContent = function(tabID, keyword) {
        $("#myTabContent").append(
            '<div class="tab-pane scrollable list-group" id="tab-' + tabID + '">' +
            getTraceEntriesDiv(keyword) +
            '</div>'
        )
    };

    $.fn.deleteTab = function() {
        var anchor = $(this).siblings('a');
        $(anchor.attr('href')).remove();
        $(this).parent().remove();
        $(".nav-tabs li").children('a').first().click();
    };

    var tagsToReplace = {
        '&': '&amp;',
        '<': '&lt;',
        '>': '&gt;'
    };

    var replaceTag = function(tag) {
        return tagsToReplace[tag] || tag;
    };

    var safe_tags_replace = function(str) {
        return str.replace(/[&<>]/g, replaceTag);
    };

    var getTraceEntriesDiv = function(keyword) {
        var code = "";

        var func = all_traces[displayingFunc];
        for (var propertyName in func) {
            var line = String(func[propertyName]);

            var shouldDisplay = false;
            if (keyword === null) {
                shouldDisplay = true;
                line = safe_tags_replace(line);
            } else {
                // if keyword starts or ends with a digit, we want some heuristics to help with search
                var regex = null;
                if (/^[0-9]+.*$/.test(keyword)) {
                    regex = new RegExp("[^0-9]" + keyword);
                } else if (/^.*[0-9]+$/.test(keyword)) {
                    regex = new RegExp(keyword + "[^0-9]");
                } else if (/^[0-9]+.*[0-9]+$/.test(keyword)) {
                    regex = new RegExp("[^0-9]" + keyword + "[^0-9]");
                } else {
                    regex = new RegExp(keyword);
                }

                if (regex.test(line)) {
                    shouldDisplay = true;
                    line = safe_tags_replace(line);
                    line = line.replace(new RegExp(keyword, 'g'), '<span class="code highlightcode">' + keyword + '</span>');
                }
            }

            if (shouldDisplay) {
                code += "<li class='list-group-item list-group-item-action code " + propertyName + "' onclick='$(this).entryClick();'>";
                code += "[" + propertyName + "] " + line;
                code += "</li>";
            }
        }
        return code;
    };

    $.fn.entryClick = function() {
        var text = this.text();
        var id_end = text.indexOf(']');
        var id = parseInt(text.substring(1, id_end));
        console.log(id);

        var node = $('.' + id);
        if (node.hasClass('active')) {
            node.removeClass('active');
            var index = highlight_entries.indexOf(id);
            if (index > -1) {
                highlight_entries.splice(index, 1);
            }
            console.log('no longer highlight '+ id);
        } else {
            node.addClass('active');
            highlight_entries.push(id);
            console.log('highlight ' + id);
        }
    };

    uploadForm.addEventListener('submit', function(e) {
        var uploadFiles = document.getElementById('js-upload-files').files;
        e.preventDefault();

        startUpload(uploadFiles);
    });

    dropZone.ondrop = function(e) {
        e.preventDefault();
        this.className = 'upload-drop-zone';

        startUpload(e.dataTransfer.files)
    };

    dropZone.ondragover = function() {
        this.className = 'upload-drop-zone drop';
        return false;
    };

    dropZone.ondragleave = function() {
        this.className = 'upload-drop-zone';
        return false;
    };

    $("#progress-div").hide();
    $("#file-viewer").hide();
    $("#function-select-div").hide();
}(jQuery);
</script>

</body>
</html>