(function(){var GRAPHITE_ENDPOINT,drawMorris,previousTerm,removeMorris,updateGraphiteData;GRAPHITE_ENDPOINT='/graphite/';$(document).ready(function(){var complete;updateGraphiteData();$('#id_metric').on('keyup',complete);$('#id_frequency').on('keyup',complete);complete=function(){var timer;clearTimeout(timer);return timer=setTimeout(updateGraphiteData,1000);};return $('#id_metric').autocomplete({source:function(request,response){return $.ajax({url:GRAPHITE_ENDPOINT,data:{metric:request.term}}).done(function(data){var m,metricList,metrics,ref,ref1;metricList=(ref=data.matchingMetrics)!=null?(ref1=ref.metrics)!=null?ref1.metrics:void 0:void 0;if(metricList){metrics=(function(){var i,len,results;results=[];for(i=0,len=metricList.length;i<len;i++){m=metricList[i];results.push(m.path);}
return results;})();return response(metrics);}});},minLength:0,close:function(evt,ui){if($('#id_metric').is(':focus')){return $('#id_metric').autocomplete("search",$('#id_metric').val());}}});});previousTerm=void 0;updateGraphiteData=function(){var f,m;m=$('#id_metric').val();f=$('#id_frequency').val();if(!m||previousTerm===m){return;}
previousTerm=m;$('#graphite_data_container').text('Getting data');return $.ajax({url:GRAPHITE_ENDPOINT,data:{metric:m,frequency:f}}).done(function(data){if((data.data!=null)&&(data.data.length!=null)!==0){$('#graphite_data_container').text(JSON.stringify(data.data,void 0,2));return drawMorris(data.data);}else{$('#graphite_data_container').text('Select a valid metric');return removeMorris();}});};drawMorris=function(data){var all,i,j,k,k2,kvs,label,labels,len,len1,point,ref,series,t,tmp,v,v2;kvs=[];labels=[];$('#graph').show();all={};for(i=0,len=data.length;i<len;i++){series=data[i];label=series.target;labels.push(label);ref=series.datapoints;for(j=0,len1=ref.length;j<len1;j++){point=ref[j];t=point[1]*1000;if(all[t]==null){all[t]={};}
all[t][label]=point[0];}}
for(k in all){v=all[k];tmp={time:parseInt(k)};for(k2 in v){v2=v[k2];tmp[k2]=v2;}
kvs.push(tmp);}
$('#graph').empty();if(!kvs){return;}
return window.morrisLine=Morris.Line({element:'graph',data:kvs,xkey:'time',ykeys:labels,labels:(function(){var l,len2,results;results=[];for(l=0,len2=labels.length;l<len2;l++){label=labels[l];results.push(label.slice(0,11)+'...'+label.slice(-10));}
return results;})(),hideHover:'auto'});};removeMorris=function(){return $('#graph').hide();};}).call(this);