omkelderman

whatpulse client api thing

Jan 9th, 2017
33
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2. // config
  3.  
  4. // ip-address or hostname of the machine running whatpulse
  5. var IP = 'localhost';
  6.  
  7. // the api-port set in whatpulse settings
  8. var PORT = 3490;
  9.  
  10. // to how many decimals should the avg be rounded?
  11. var avgDecimalCount = 2;
  12.  
  13. // how often does this thing refresh?
  14. var updateMs = 500;
  15.  
  16. // am I developping stuff?
  17. var DEBUG = true;
  18.  
  19. // DO NOT TOCH ANYTHING BELOW THIS LINE UNLESS INSTRUCTED!!!!!!!!!!!!!!!!!!!!!!!
  20. </script>
  21. <style>
  22. * {
  23.     margin: 0;
  24.     padding: 0;
  25. }
  26. .clear {
  27.     clear: both;
  28. }
  29. body {
  30.     color: white;
  31.     font-size: 16px;
  32. }
  33. #stats {
  34.     position: relative;
  35. }
  36. #stats .stat {
  37.     position: absolute;
  38.     background: rgba(0, 0, 0, 0.6);
  39. }
  40. #stats .stat:last-child {
  41.     right: 0;
  42. }
  43. #log {
  44.     color: grey;
  45. }
  46. .label {
  47. }
  48. .value {
  49.     color: #04cebd;
  50.     font-size: larger;
  51.     font-weight: bold;
  52.     font-family: 'Courier New', Courier, 'Lucida Sans Typewriter', 'Lucida Typewriter', monospace;
  53. }
  54. </style>
  55.  
  56. <div id="stats">
  57.     <div class="stat"><span class="label">Uptime:</span> <span id="uptime" class="value">-</span></div>
  58.     <div class="stat"><span class="label">Keytaps:</span> <span id="keyCount" class="value">-</span></div>
  59.     <div class="stat"><span class="label">Average Session KPS:</span> <span id="avgSessionKps" class="value">-</span></div>
  60. </div>
  61.  
  62. <ul id="log"></ul>
  63.  
  64. <script src="http://code.jquery.com/jquery-2.2.1.min.js" integrity="sha256-gvQgAFzTH6trSrAWoH1iPo9Xc96QxSZ3feW6kem+O00=" crossorigin="anonymous"></script>
  65. <script>
  66. var intervalId = undefined;
  67. var $stats = $('#stats');
  68. var $log = $('#log');
  69. var $keyCount = $('#keyCount');
  70. var $uptime = $('#uptime');
  71. var $avgSessionKps = $('#avgSessionKps');
  72. var decimalNumber = Math.pow(10, avgDecimalCount);
  73.  
  74. function distributeElements() {
  75.     $kids = [];
  76.     var kidsCombinedWidth = 0;
  77.     $stats.children().each(function(i) {
  78.         var $kid = $(this);
  79.         $kids[i] = $kid;
  80.         kidsCombinedWidth += $kid.width();
  81.     });
  82.     console.log(kidsCombinedWidth, $kids.length);
  83.     if($kids.length <= 1) {
  84.        return;
  85.    }
  86.    var inbetweenWidth = (window.innerWidth-kidsCombinedWidth)/($kids.length-1);
  87.    var leftSoFar = 0;
  88.    for(var i=0, len=$kids.length-1; i<len; ++i) {
  89.        $kids[i].css('left', leftSoFar + 'px');
  90.        leftSoFar += $kids[i].width() + inbetweenWidth;
  91.    }
  92. }
  93.  
  94. function start() {
  95.    intervalId = setInterval(updateKeys, updateMs);
  96. }
  97.  
  98. function stop() {
  99.    clearInterval(intervalId);
  100. }
  101.  
  102. function updateKeys() {
  103.    $.ajax({
  104.        dataType: "json",
  105.        url: 'http://'+IP+':'+PORT+'/v1/unpulsed',
  106.        success: function(result) {
  107.            $keyCount.text(result.keys);
  108.            $uptime.text(toTimeString(result.uptime));
  109.            var avg = result.keys/result.uptime;
  110.            $avgSessionKps.text(Math.round(avg*decimalNumber)/decimalNumber);
  111.            distributeElements();
  112.        },
  113.        error: function(jqXHR, textStatus, errorThrown) {
  114.            log(textStatus);
  115.        }
  116.    });
  117. }
  118.  
  119. function toTimeString(sec_num) {
  120.    var hours   = Math.floor(sec_num / 3600);
  121.    var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
  122.    var seconds = sec_num - (hours * 3600) - (minutes * 60);
  123.  
  124.    if (hours   < 10) {hours   = "0"+hours;}
  125.    if (minutes < 10) {minutes = "0"+minutes;}
  126.    if (seconds < 10) {seconds = "0"+seconds;}
  127.    var time    = hours+':'+minutes+':'+seconds;
  128.    return time;
  129. }
  130.  
  131. function log(message) {
  132.    console.log(message);
  133.    if(DEBUG) {
  134.        $log.append($('<li>').text(message));
  135.     }
  136. }
  137. distributeElements();
  138. start();
  139. </script>
RAW Paste Data