lib/rhoconnect/graph_helper.rb in rhoconnect-3.3.6 vs lib/rhoconnect/graph_helper.rb in rhoconnect-3.4.2

- old
+ new

@@ -3,15 +3,11 @@ def source_timing(params) #name,data,options @displayname = params['display_name'] names = [] - # begin - # names = get_sources('all') - # rescue Exception => e - # puts "errror #{e.message}" - # end + names = [params['display_name']] @sources = [] names.each do |name| s = {} @@ -59,84 +55,81 @@ } s['data'] = data options[:series] = series options[:cursor] = {:zoom => true, :showTooltip => true} - + options[:seriesDefaults] = {:pointLabels => { :show=>true }} s['options'] = options end @sources << s end - @graph_t = 'source' - @data = [[[1,2],[3,4],[5,6]]].to_json + erb :jqplot, :layout => false end def http_timing_key(params) @uri = 'timing/httptiming' #name,data,options @displayname = params['display_name'] - #names = ["GET","POST"] - # begin - # names = get_sources('all') - # rescue Exception => e - # puts "errror #{e.message}" - # end + @sources = [] name = key = params['display_name'] s = {} data = [] series = [] - options = { :legend => { :show => true, :location => 'ne' }, :title => name } + options = { :legend => { :show => false }, :title => name } s['name'] = name xmin = 9999999999999999 xmax = -1 ymin = 9999999999999999 ymax = -1 - method = key.gsub(/http:.*?:/,"") - #method.gsub!(/:.*/,"") - series << {:showLabel => true, :label => method} - range = get_user_count(nil,key,0,-1) - thisdata = [] - range.each do |value| - count = value.split(',')[0] - value.gsub!(/.*,/,"") - thisdata << value.split(":").reverse - thisdata[-1][0] = thisdata[-1][0].to_i * 1000 - thisdata[-1][1] = thisdata[-1][1].to_f - thisdata[-1][1] /= count.to_f + keys = Rhoconnect::Stats::Record.keys(key) + + keys.each_with_index do |k,index| + method = key.gsub(/http:.*?:/,"") + #method.gsub!(/:.*/,"") unless name == "*" + series << {:showLabel => true, :label => method, :showLine => false } - ymin = thisdata[-1][1].to_f if thisdata[-1][1] && thisdata[-1][1].to_f < ymin - ymax = thisdata[-1][1].to_f if thisdata[-1][1] && thisdata[-1][1].to_f > ymax - + range = get_user_count(nil,k,0,-1) + thisdata = [] + range.each do |value| + count = value.split(',')[0] + value.gsub!(/.*,/,"") + thisdata << value.split(":").reverse + thisdata[-1][0] = thisdata[-1][0].to_i * 1000 + thisdata[-1][1] = thisdata[-1][1].to_f + thisdata[-1][1] /= count.to_f + end + data << thisdata + xmin = thisdata[0][0].to_i if thisdata[0] && thisdata[0][0].to_i < xmin + xmax = thisdata[-1][0].to_i if thisdata[-1] && thisdata[-1][0].to_i > xmax end - data << thisdata - xmin = thisdata[0][0].to_i if thisdata[0] && thisdata[0][0].to_i < xmin - xmax = thisdata[-1][0].to_i if thisdata[-1] && thisdata[-1][0].to_i > xmax - - + + data_normalized,ymin,ymax,days = average_data(data,ymin,ymax) + #puts "dn is ****************** #{data_normalized}" + format_str = days ? "%m/%d%y" : "%H" + label_time = days ? "Days" : "#{data_normalized.first.first[0].year}-#{data_normalized.first.first[0].month}-#{data_normalized.first.first[0].day} (Hours)" + options[:axes] = { :yaxis => { :tickOptions => { :formatString =>'%.3f'}, :autoscale => true, :min => 0, :max => ymax + (ymax * 0.05), :label => 'Seconds', :labelRenderer => '$.jqplot.CanvasAxisLabelRenderer' }, - :xaxis => { :autoscale => true, :renderer=>'$.jqplot.DateAxisRenderer', - :tickOptions => {:formatString => '%m/%d/%y'}} + :xaxis => { :autoscale => true, :label => label_time, :renderer=>'$.jqplot.DateAxisRenderer', + :tickOptions => {:formatString => format_str}} } - - s['data'] = data + s['data'] = data_normalized options[:series] = series - options[:cursor] = {:zoom => true, :showTooltip => true} + options[:cursor] = {:zoom => true, :showTooltip => true, :show => true} + options[:seriesDefaults] = {:pointLabels => { :show=>true },:rendererOptions => {:smooth => true}} s['options'] = options @sources << s - @graph_t = 'http' - @data = [[[1,2],[3,4],[5,6]]].to_json erb :jqplot, :layout => false end def http_timing(params) @uri = 'timing/httptiming' @@ -192,11 +185,12 @@ :tickOptions => {:formatString => '%m/%d/%y'}} } s['data'] = data options[:series] = series - options[:cursor] = {:zoom => true, :showTooltip => true} + options[:cursor] = {:zoom => true, :showTooltip => true} + options[:seriesDefaults] = {:rendererOptions => {:smooth => true}} s['options'] = options @sources << s end @@ -205,23 +199,15 @@ erb :jqplot, :layout => false end def get_http_routes() keys = get_user_count("http:*:*") - method = key.gsub(/http:.*?:/,"") - #sources = get_sources('all') - - #loop through arrays and remove any regex matches - # keysf = keys.inject([]) do |keys_final, element| - # found = true - # sources.each do |s| - # found = false if element.match(s) - # end - # keys_final << element.strip if found - # keys_final - # end - + keys.each do |k| + client_id = k.split("/")[4] + k.gsub!(client_id,"*") if client_id + end + keys.uniq end def count_graph(uri,title,name,metric) start = 0 finish = -1 @@ -251,10 +237,11 @@ :tickOptions => {:formatString => format}}, :yaxis => {:label => name, :labelRenderer => '$.jqplot.CanvasAxisLabelRenderer'} } options[:cursor] = {:zoom => true, :showTooltip => true} + options[:seriesDefaults] = {:pointLabels => { :show=>true },:rendererOptions => {:smooth => true}} s['name'] = name s['data'] = [thisdata] s['options'] = options @sources << s erb :jqplot, :layout => false @@ -301,9 +288,40 @@ res << name end end res end + end + + def average_data(data,ymin,ymax) + data_buckets = {} + data_count = {} + data_result = [] + days = false + data.each do |d_arr| + bucket = Time.at(d_arr.first[0]/1000) + bucket_key = "#{bucket.year}-#{bucket.month}-#{bucket.day} #{bucket.hour}:0:0" + if data_buckets.include? bucket_key + data_buckets[bucket_key] += d_arr.first[1] + data_count[bucket_key] += 1 + else + data_buckets[bucket_key] = d_arr.first[1] + data_count[bucket_key] = 1 + end + ymin = d_arr.first[1].to_f if d_arr.first[1].to_f < ymin.to_f + ymax = d_arr.first[1].to_f if d_arr.first[1].to_f > ymax.to_f + end + data_buckets.each do |index,value| + average_val = value/data_count[index] + data_result << [[Time.at(Time.parse(index).to_i),average_val.round(3)]] + end + + unless data_buckets.size < 2 + start_day = Time.parse(data_buckets.first.first) + end_day = Time.parse(data_buckets.last.first) + days = true if start_day.month != end_day.month and start_day.day != end_day.day + end + return data_result,ymin,ymax,days end end \ No newline at end of file