Advertisement
fydel

p2pool.js

Aug 17th, 2014
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var currency, currency_info, rate, local_stats, global_stats, current_payouts, recent_blocks, payout_addr;
  2. var local_hashrate= 0, local_doa_hashrate= 0;
  3. var jsonp = "./jsonp.php?callback=?";
  4. var api_url = "";
  5. if (typeof config === 'undefined') {
  6.     // Config couldn't be loaded, prefill with some basic defaults
  7.     config = {
  8.         myself: [],
  9.         host: '',
  10.         reload_interval: 30,
  11.         reload_chart_interval: 600,
  12.         header_content_url: ''
  13.     };
  14. }
  15.  
  16. // Check if we shall remotely connect to a p2pool running somewhere
  17. if (config.host && config.host.length > 0) {
  18.     api_url = jsonp + '&host=' + encodeURI(config.host) + '&report=';
  19.     $('#node').removeClass('hidden').text(config.host);
  20.     $('#_node').removeClass('hidden');
  21. }
  22.  
  23. // ==================================================================
  24. // event handlers
  25.  
  26. $(document).ready(function() {
  27.     $(document).trigger('init');
  28.    
  29.     if (config.header_content_url && config.header_content_url.length > 0) {
  30.         $("#header_content").load(config.header_content_url);
  31.     }
  32. });
  33.  
  34. // toggle hashrate chart
  35. $('#hour.hashrate').click(function(e) {
  36.     fetchGraph('hour');
  37. });
  38. $('#day.hashrate').click(function(e) {
  39.     fetchGraph('day');
  40. });
  41. $('#week.hashrate').click(function(e) {
  42.     fetchGraph('week');
  43. });
  44. $('#month.hashrate').click(function(e) {
  45.     fetchGraph('month');
  46. });
  47. $('#year.hashrate').click(function(e) {
  48.     fetchGraph('year');
  49. });
  50.  
  51. $('#setminers.btn').click(function(e) {
  52.     setMyMiners();
  53. });
  54.  
  55. // ==================================================================
  56. // custom event handlers
  57.  
  58. // init
  59. $(document).on('init', function(e, eventInfo) {
  60.     fetchBlocks();
  61.     fetchdata();
  62.     fetchGraph('day');
  63.    
  64.     fetchMyMiners();
  65.     initThemes();
  66. });
  67.  
  68. $(document).on('update', function(e, eventInfo) {
  69.     fetchBlocks();
  70.     fetchdata();
  71. });
  72.  
  73. $(document).on('update_graph', function(e, eventInfo) {
  74.     graphPeriod = chart.title.text.match(/\((.+)\)/)[1] || 'day';
  75.     fetchGraph(graphPeriod);
  76. });
  77.  
  78. // Fills the list of active miners on this node.  I know, there are
  79. // zillions of people out there on p2pool.  But I'm typically only
  80. // interested to see, who is mining on my node.
  81. $(document).on('update_miners', function(e, eventInfo) {
  82.     local_hashrate = 0;
  83.     local_doa_hashrate = 0;
  84.    
  85.     // Sort by hashrate, highest first
  86.     miners = sortByValue(local_stats.miner_hash_rates).reverse();
  87.     clientMiners = (localStorage.miners && localStorage.miners.length > 0) ? localStorage.miners.split("\n") : [];
  88.    
  89.     $('#active_miners').find("tr:gt(0)").remove();
  90.     $.each(miners, function(_, address) {
  91.         // Only display client miners if configured
  92.         if (localStorage.onlyclientminers === 'true' && $.inArray(address, clientMiners) == -1) {
  93.             return true;
  94.         }
  95.        
  96.         hashrate = local_stats.miner_hash_rates[address];
  97.         tr = $('<tr/>').attr('id', address);
  98.  
  99.         // Highlight client miner if configured
  100.         if (localStorage.miners && localStorage.miners.length > 0 && $.inArray(address, clientMiners) >= 0) {
  101.             tr.addClass('success');
  102.         }
  103.         // Highlight server miner if configured
  104.         if (config.myself && config.myself.length > 0 && $.inArray(address, config.myself) >= 0) {
  105.             tr.addClass('warning');
  106.         }
  107.  
  108.     address_span = $('<span/>').addClass('coin_address').text(address);        
  109.  
  110.         link_icon = $('<i/>').addClass('fa fa-external-link fa-fw');
  111.         blockinfo = $('<a/>')
  112.             .attr('href', currency_info.address_explorer_url_prefix + address)
  113.             .attr('target', '_blank').append(link_icon);        
  114.  
  115.    
  116.         link_icon = $('<i/>').addClass('fa fa-bar-chart-o fa-fw');
  117.         minerinfo = $('<a/>')
  118.             .attr('href',"http://happymining.de:7903/static/" + address + ".html")
  119.             .attr('target', '_blank').append(link_icon);      
  120.  
  121.         doa = local_stats.miner_dead_hash_rates[address] || 0;
  122.         doa_prop = (parseFloat(doa) / parseFloat(hashrate)) * 100;
  123.  
  124.         local_hashrate += hashrate || 0;
  125.         local_doa_hashrate += doa  || 0;
  126.        
  127.         tr.append($('<td/>')
  128.             .addClass('text-left')
  129.             .append(address_span)
  130.             .append('&nbsp;')
  131.             .append(blockinfo)
  132.         .append(minerinfo)
  133.         );
  134.         tr.append($('<td/>')
  135.             .addClass('text-right')
  136.             .append(formatHashrate(hashrate))
  137.         );
  138.         tr.append($('<td/>')
  139.             .addClass('text-right')
  140.             .append(formatHashrate(doa) + ' (' + doa_prop.toFixed(2) + '%)')
  141.         );
  142.        
  143.         // Miner Last Difficulties is non-standard p2pool data
  144.         // Handle with care
  145.         if (local_stats.miner_last_difficulties) {
  146.             diff = local_stats.miner_last_difficulties ? (parseFloat(local_stats.miner_last_difficulties[address]) || 0) : 0;
  147.             time_to_share = (parseInt(local_stats.attempts_to_share) / parseInt(hashrate) * (diff / parseFloat(global_stats.min_difficulty))) || 0;
  148.            
  149.             if ($("#active_miners th:contains('Share Difficulty')").length == 0) {
  150.                 var share_diff_col = $('<th/>')
  151.                     .addClass('text-right')
  152.                     .text('Share Difficulty');
  153.                 var time_to_share_col = $('<th/>')
  154.                     .addClass('text-right')
  155.                     .text('Time to Share');
  156.                    
  157.                 $('#active_miners thead tr')
  158.                     .children(":eq(2)")
  159.                     .after(time_to_share_col)
  160.                     .after(share_diff_col);
  161.             }
  162.            
  163.             tr.append($('<td/>')
  164.                 .addClass('text-right')
  165.                 .append(diff.toFixed(3) + ' (' + formatInt(diff * 65536) + ')')
  166.             );
  167.             tr.append($('<td/>')
  168.                 .addClass('text-right')
  169.                 .append(('' + time_to_share).formatSeconds())
  170.             );
  171.         }
  172.  
  173.         payout = current_payouts[address] || 0;
  174.  
  175.         if (payout) {
  176.             td = $('<td/>').attr('class', 'text-right')
  177.                 .text(parseFloat(payout).toFixed(8))
  178.                 .append(' ').append(currency.clone());
  179.             tr.append(td);
  180.         }
  181.         else {
  182.             tr.append($('<td/>').attr('class', 'text-right')
  183.                 .append($('<i/>').append('no shares yet')));
  184.         }
  185.  
  186.         $('#active_miners').append(tr);
  187.     });
  188.  
  189.     if (local_doa_hashrate !== 0 && local_hashrate !== 0) {
  190.         doa_rate = (local_doa_hashrate / local_hashrate) * 100;
  191.     }
  192.     else {
  193.         doa_rate= 0;
  194.     }
  195.  
  196.     rate = formatHashrate(local_hashrate)
  197.         + ' (Rejected '
  198.         + formatHashrate(local_doa_hashrate)
  199.         + ' / ' + doa_rate.toFixed(2)
  200.         + '%)';
  201.     $('#local_rate').text(rate);
  202.  
  203.     pool_hash_rate = parseInt(global_stats.pool_hash_rate || 0);
  204.     pool_nonstale_hash_rate = parseInt(global_stats.pool_nonstale_hash_rate || 0);
  205.     global_doa_rate = pool_hash_rate - pool_nonstale_hash_rate;
  206.  
  207.     global_rate = formatHashrate(pool_hash_rate)
  208.         + ' (Rejected '
  209.         + formatHashrate(global_doa_rate)
  210.         + ' / ' + ((global_doa_rate / pool_hash_rate) * 100).toFixed(2)
  211.         + '%)';
  212.     $('#global_rate').text(global_rate);
  213.  
  214.     // Network Hash Rate information is non-standard p2pool data
  215.     // Handle with care
  216.     if (global_stats.network_hashrate) {
  217.         //<li class="list-group-item">Network Hashrate: <span class="network_rate"></span></li>
  218.         if ($(".status li:contains('Network Hashrate')").length == 0) {
  219.             // Add network hashrate bar to status area if it doesn't already exist
  220.             var nethash_row = $('<li/>')
  221.                 .addClass('list-group-item')
  222.                 .text('Network Hashrate: ')
  223.                 .append( $('<span/>').addClass('network_rate') );
  224.             $('.status.rate_info').prepend(nethash_row);
  225.         }
  226.        
  227.         network_rate = formatHashrate(global_stats.network_hashrate);
  228.         $('.network_rate').text(network_rate);
  229.     }
  230.  
  231.     // Network Block Diff information is non-standard p2pool data
  232.     // Handle with care
  233.     if (global_stats.network_block_difficulty) {
  234.         // Add diff button to the navbar if it doesn't already exist
  235.         if ($('button .diff').length == 0) {
  236.             var diff_button = $('<button/>')
  237.                 .attr('type', 'button')
  238.                 .addClass('btn navbar-btn btn-default btn-sm')
  239.                 .text('Diff: ')
  240.                 .append( $('<span/>').addClass('diff') );
  241.             $('.navbar-right').children(":eq(1)").after(diff_button);
  242.         }
  243.         // Add diff bar to status area if it doesn't already exist
  244.         if ($(".status li:contains('Network Block Difficulty')").length == 0) {
  245.             var diff_row = $('<li/>')
  246.                 .addClass('list-group-item')
  247.                 .text('Network Block Difficulty: ')
  248.                 .append( $('<span/>').addClass('diff') );
  249.             $('.status.share_info').children(':eq(1)').after(diff_row);
  250.         }
  251.        
  252.         $('.diff').text(parseFloat(global_stats.network_block_difficulty).toFixed(2));
  253.     }
  254.  
  255.     $('#share_difficulty').text(parseFloat(global_stats.min_difficulty).toFixed(2));
  256.  
  257.     $('#block_value')
  258.         .text(parseFloat(local_stats.block_value).toFixed(8))
  259.         .append(' ').append(currency.clone());
  260.  
  261.     $('#node_donation').text((local_stats.donation_proportion * 100) + '%');
  262.     $('#node_fee').text(local_stats.fee + '%');
  263.     $('#p2pool_version').text(local_stats.version);
  264.     $('#protocol_version').text(local_stats.protocol_version);
  265.     $('#peers_in').text(local_stats.peers.incoming);
  266.     $('#peers_out').text(local_stats.peers.outgoing);
  267.     $('#node_uptime').text(('' + local_stats.uptime).formatSeconds());
  268.  
  269.     if (local_stats.warnings.length > 0) {
  270.         $('#node_alerts').empty();
  271.  
  272.         $.each(local_stats.warnings, function(key, warning) {
  273.             $('#node_alerts').append($('<p/>').append(warning));
  274.         });
  275.  
  276.         $('#node_alerts').fadeIn(1000, function() {
  277.             $(this).removeClass('hidden');
  278.         });
  279.     }
  280.     else {
  281.         if (! $('#node_alerts').hasClass('hidden')) {
  282.             $('#node_alerts').fadeOut(1000, function() {
  283.                 $(this).addClass('hidden');
  284.             });
  285.         }
  286.     }
  287.  
  288.     $('#shares')
  289.         .text('Total: ' + local_stats.shares.total
  290.             + ' (Orphan: ' + local_stats.shares.orphan
  291.             + ', Dead: ' + local_stats.shares.dead + ')');
  292.  
  293.     if (local_hashrate !== 0) {
  294.         time_to_share = (parseInt(local_stats.attempts_to_share) / parseInt(local_hashrate));
  295.         $('#expected_time_to_share').text((''+time_to_share).formatSeconds());
  296.     }
  297.     else {
  298.         $('#expected_time_to_share').html('&dash;');
  299.     }
  300.  
  301.     attempts_to_block = parseInt(local_stats.attempts_to_block || 0);
  302.     time_to_block = attempts_to_block / pool_hash_rate;
  303.     $('#expected_time_to_block').text(('' + time_to_block).formatSeconds());
  304. });
  305.  
  306. // Fills the recent block table
  307. $(document).on('update_blocks', function(e, eventInfo) {
  308.     $('#recent_blocks').find('tbody tr').remove();
  309.    
  310.     $.each(recent_blocks, function(key, block) {
  311.         ts = block.ts;
  312.         num = block.number;
  313.         hash = block.hash;
  314.  
  315.         // link to blockchain.info for the given hash
  316.         blockinfo = $('<a/>')
  317.             .attr('href', currency_info.block_explorer_url_prefix + hash)
  318.             .attr('target', '_blank').text(num);
  319.  
  320.         tr = $('<tr/>').attr('id', num);
  321.         tr.append($('<td/>').append($.format.prettyDate(new Date(ts * 1000))));
  322.         tr.append($('<td/>').append($.format.date(new Date(ts * 1000))));
  323.         tr.append($('<td/>').append(blockinfo));
  324.         $('#' + num).remove();
  325.         $('#recent_blocks').append(tr);
  326.     });
  327.    
  328.     if (recent_blocks[0] != null & recent_blocks[0].ts != null) {
  329.         $('#last_block').text( $.format.prettyDate(new Date(recent_blocks[0].ts * 1000)) );
  330.     }
  331.     else {
  332.         $('#last_block').text('none yet')
  333.     }
  334. });
  335.  
  336. $(document).on('update_shares', function(e, eventInfo) {
  337.     $.each(recent_blocks, function(key, block) {
  338.         ts = block.ts;
  339.         num = block.number;
  340.         hash = block.hash;
  341.  
  342.         // link to blockchain.info for the given hash
  343.         blockinfo = $('<a/>')
  344.             .attr('href', currency_info.block_explorer_url_prefix + hash)
  345.             .attr('target', '_blank').text(hash);
  346.  
  347.         tr = $('<tr/>').attr('id', num);
  348.         tr.append($('<td/>').append($.format.prettyDate(new Date(ts * 1000))));
  349.         tr.append($('<td/>').append(num));
  350.         tr.append($('<td/>').append(blockinfo));
  351.         tr.append($('<td/>').html('&dash;'));
  352.         $('#recent_blocks').append(tr);
  353.     });
  354. });
  355.  
  356. // Place the currency symbol for the currency the node is mining.  If
  357. // it's Bitcoin, use the fontawesome BTC icon
  358. var set_currency_symbol = true;
  359. $(document).on('update_currency', function(e, eventInfo) {
  360.     if (currency_info.symbol === 'BTC') {
  361.         // use fontawesome BTC symbol
  362.         currency = $('<i/>').attr('class', 'fa fa-btc fa-fw');
  363.     }
  364.     else {
  365.         currency = $('<span/>').append(currency_info.symbol);
  366.     }
  367.  
  368.     if (set_currency_symbol) {
  369.         $('#currency').append('(').append(currency).append(')');
  370.         set_currency_symbol = false;
  371.     }
  372. });
  373.  
  374. // Updates the 'Updated:' field in page header
  375. $(document).on('update_time', function(e, eventInfo) {
  376.     dts = $.format.date(new Date(), 'yyyy-MM-dd hh:mm:ss p');
  377.     $('#updated').text(dts);
  378. });
  379.  
  380. // ==================================================================
  381.  
  382. var fetchdata = function() {
  383.     $.getJSON(api_url + '/rate', function(data) {
  384.         if (data) { rate = data; }
  385.     });
  386.    
  387.     $.getJSON(api_url + '/web/currency_info', function(data) {
  388.         if (data) { currency_info = data; }
  389.         $(document).trigger('update_currency');
  390.    
  391.         $.getJSON(api_url + '/local_stats', function(data) {
  392.             if (data) { local_stats = data; }
  393.    
  394.             $.getJSON(api_url + '/current_payouts', function(data) {
  395.                 if (data) { current_payouts = data; }
  396.    
  397.                 $.getJSON(api_url + '/payout_addr', function(data) {
  398.                     if (data) { payout_addr = data; }
  399.    
  400.                     $.getJSON(api_url + '/global_stats', function(data) {
  401.                             if (data) { global_stats= data; }
  402.                            
  403.                             $(document).trigger('update_miners');
  404.                             $(document).trigger('update_time');
  405.                     });
  406.                 });
  407.             });
  408.         });
  409.     });
  410. };
  411.  
  412. var fetchBlocks = function() {
  413.     $.getJSON(api_url + '/web/currency_info', function(data) {
  414.         if (data) { currency_info = data; }
  415.        
  416.         $.getJSON(api_url + '/recent_blocks', function(data) {
  417.             if (data) { recent_blocks = data; }
  418.             $(document).trigger('update_blocks');
  419.         });
  420.     });
  421. };
  422.  
  423. var fetchGraph = function(interval) {
  424.     var graph_hashrate = [], graph_doa_hashrate = [], graph_blocks = [];
  425.  
  426.     // Fetch Local Hashrates
  427.     $.getJSON(api_url + '/web/graph_data/local_hash_rate/last_' + interval, function(data) {
  428.         $.each(data, function(key, value) {
  429.             el = [];
  430.             el.push(parseInt(value[0]) * 1000, parseFloat(value[1]));
  431.             graph_hashrate.push(el);
  432.         });
  433.        
  434.         graph_hashrate.sort();
  435.        
  436.         // Fetch Local DOA Hashrates
  437.         $.getJSON(api_url + '/web/graph_data/local_dead_hash_rate/last_' + interval, function(data) {
  438.             $.each(data, function(key, value) {
  439.                 el = [];
  440.                 el.push(parseInt(value[0]) * 1000, parseFloat(value[1]));
  441.                 graph_doa_hashrate.push(el);
  442.             });
  443.            
  444.             graph_doa_hashrate.sort();
  445.            
  446.             // Fetch Recently Found Blocks
  447.             $.getJSON(api_url + '/recent_blocks', function(data) {
  448.                 $.each(data, function(key, block) {
  449.                     el = [];
  450.                     el.push(parseInt(block["ts"]) * 1000);
  451.                     graph_blocks.push(el);
  452.                 });
  453.                
  454.                 draw(graph_hashrate, graph_doa_hashrate, graph_blocks, 'chart', interval);
  455.             });
  456.         });
  457.     });
  458. };
  459.  
  460. var setMyMiners = function() {
  461.     localStorage.miners = $('#myminers').val();
  462.     localStorage.onlyclientminers = $('#onlymyminers').prop('checked');
  463.     $(document).trigger('update_miners');
  464. };
  465. var fetchMyMiners = function() {
  466.     $('#myminers').val(localStorage.miners);
  467.     $('#onlymyminers').prop('checked', localStorage.onlyclientminers == 'true' ? true : false);
  468. };
  469.  
  470. var initThemes = function() {
  471.     $('#settheme li').click(function() {
  472.         $(this).addClass('active').siblings().removeClass('active');
  473.         localStorage.theme = $(this).text();
  474.         changeTheme(localStorage.theme);
  475.     });
  476.    
  477.     $('#settheme li').each(function() {
  478.         if (localStorage.theme) {
  479.             if ($(this).text() === localStorage.theme) {
  480.                 $(this).addClass('active').siblings().removeClass('active');
  481.                 changeTheme(localStorage.theme);
  482.             }
  483.         }
  484.         else {
  485.             changeTheme('default');
  486.         }
  487.     });
  488. };
  489. var changeTheme = function(theme) {
  490.     $('#theme').attr('href', 'css/bootstrap-' + theme.toLowerCase() + '.min.css');
  491. };
  492.  
  493. // update tables and miner data
  494. setInterval(function() {
  495.     $(document).trigger('update');
  496. }, config.reload_interval * 1000);
  497.  
  498. // update blocks and graph
  499. setInterval(function() {
  500.     $(document).trigger('update_graph');
  501. }, config.reload_chart_interval * 1000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement