var FnordMetric = (function(){ var canvasElem = false; var currentNamespace = false; var currentView = false; var currentWidgetUID=23; function decPrint(val){ return (val < 10 ? '0'+val : val); } function formatTimeOfDay(_time){ if(_time === null) { return ""; } else { var time = new Date(); time.setTime(_time*1000); return decPrint(time.getHours()) + ':' + decPrint(time.getMinutes()) + ':' + decPrint(time.getSeconds()); } } function formatTimeRange(range){ if (range < 60){ return parseInt(range) + ' sec'; } else if(range<3600){ return parseInt(range/60) + ' min'; } else if(range==3600){ return '1 hour'; } else if(range<(3600*24)){ return parseInt(range/3600) + ' hours'; } else if(range==(3600*24)){ return '1 day'; } else { return parseInt(range/(3600*24)) + ' days'; } } function formatTimeSince(time){ var now = new Date().getTime()/1000; var since = now - time; return formatTimeRange(since); } function formatOffset(offset, next_offset){ if((offset == 0) && (next_offset==(3600*24))){ return 'today'; } if((offset == 0) && (next_offset==3600)){ return 'this hour'; } else if(offset == 0){ return 'last ' + formatTimeRange(next_offset||0); } else if(offset==(3600*24)){ return 'yesterday'; } else if(offset==3600){ return 'last hour'; } else { return formatTimeRange(offset) + ' ago'; } } function formatValue(value){ if(value < 10){ return value.toFixed(2); } else if(value > 1000){ return (value/1000.0).toFixed(1) + "k"; } else { return value.toFixed(0); } } function formatTimeValue(value){ if (value < 60){ return parseFloat(value).toFixed(1) + 's'; } else if(value<3600){ return parseFloat(value/60).toFixed(1) + 'm'; } else if(value<(3600*24)){ return parseFloat(value/3600).toFixed(1) + 'h'; } else { return parseFloat(value/(3600*24)).toFixed(1) + 'd'; } } function formatPercentValue(value){ return value + '%'; } function formatGaugeValue(gauge_key, value){ if(gauge_key.slice(0,8) === '__time__'){ return formatTimeValue(value); } else if(gauge_key.slice(0,11) === '__percent__'){ return formatPercentValue(value); } else { return formatValue(value); } } function getNextWidgetUID(){ return (currentWidgetUID += 1); } var toplistWidget = function(){ function render(opts){ var current_gauge = false; var headbar = $('
').append( $('').html(opts.title) ); opts.elem.append(headbar).css({ 'marginBottom': 20, 'overflow': 'hidden' }).append( $('') ); var first = true; for(k in opts.gauges){ headbar.append( $('') .attr('class', 'button mr') .attr('rel', k) .append( $('').html(opts.gauges[k].title) ).click(function(){ loadGauge($(this).attr('rel')); } ) ); if(first){ first = false; loadGauge(k); } } if(opts.autoupdate){ var secs = parseInt(opts.autoupdate); if(secs > 0){ var autoupdate_interval = window.setInterval(function(){ loadGauge(false, true); }, secs*1000); $('body').bind('fm_dashboard_close', function(){ window.clearInterval(autoupdate_interval); }); } }; function loadGauge(gkey, silent){ if(!gkey){ gkey = current_gauge; } current_gauge = gkey; if(!silent){ $('.toplist_inner', opts.elem).addClass('loading'); } var _url = FnordMetric.p + '/' + currentNamespace + '/gauge/' + gkey; $.get(_url, function(_resp){ var resp = JSON.parse(_resp); renderGauge(gkey, resp); }) } function renderGauge(gkey, gdata){ var _elem = $('.toplist_inner', opts.elem).removeClass('loading').html(''); $(gdata.values).each(function(n, _gd){ var _perc = (parseInt(gdata.values[n][1]) / parseFloat(gdata.count))*100; var _item = $('