module GraphHelper 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 = {} data = [] series = [] options = { :legend => { :show => true }, :title => name } s['name'] = name s['data'] = [] @keys = get_user_count("source:*:#{name}") @keyname = params['key'] || @keys.first xmin = 9999999999999999 xmax = -1 ymin = 9999999999999999 ymax = -1 if @keyname method = @keyname.gsub(/source:/,"").gsub(/:.*/,"") series << {:showLabel => true, :label => method } range = get_user_count(nil,@keyname,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 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 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 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'}} } s['data'] = data options[:series] = series options[:cursor] = {:zoom => true, :showTooltip => 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 } 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 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 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 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'}} } s['data'] = data options[:series] = series options[:cursor] = {:zoom => true, :showTooltip => 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' #name,data,options @displayname = params['display_name'] names = ["GET","POST"] @sources = [] names = [params['display_name']] names.each do |name| s = {} data = [] series = [] options = { :legend => { :show => true, :location => 'ne' }, :title => name } s['name'] = name name = "*" if name == "ALL" keys = get_user_count("http:*:#{name}") xmin = 9999999999999999 xmax = -1 ymin = 9999999999999999 ymax = -1 keys.each_with_index do |key,index| method = key.gsub(/http:.*?:/,"") method.gsub!(/:.*/,"") unless name == "*" 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 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 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 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'}} } s['data'] = data options[:series] = series options[:cursor] = {:zoom => true, :showTooltip => true} s['options'] = options @sources << s end @graph_t = 'http' @data = [[[1,2],[3,4],[5,6]]].to_json 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 end def count_graph(uri,title,name,metric) start = 0 finish = -1 now = Time.now.to_i format = "%m/%d/%y" thisdata = [] series = [] series << options = { :legend => { :show => false }, :title => title } @sources = [] s = {} usercount = [] usercount = get_user_count(nil,metric,start,finish) usercount.each do |count| user,timestamp = count.split(':') user = user.to_i timestamp = timestamp.to_i * 1000 thisdata << [timestamp,user] end options[:axes] = { :xaxis => { :autoscale => true, :renderer=>'$.jqplot.DateAxisRenderer', :tickOptions => {:formatString => format}}, :yaxis => {:label => name, :labelRenderer => '$.jqplot.CanvasAxisLabelRenderer'} } options[:cursor] = {:zoom => true, :showTooltip => true} s['name'] = name s['data'] = [thisdata] s['options'] = options @sources << s erb :jqplot, :layout => false end def get_user_count(name=nil,metric_p=nil,start=nil,finish=nil) begin if Rhoconnect.stats == true names = name if names Rhoconnect::Stats::Record.keys(names) else metric = metric_p.strip rtype = Rhoconnect::Stats::Record.rtype(metric) if rtype == 'zset' # returns [] if no results Rhoconnect::Stats::Record.range(metric,start,finish) elsif rtype == 'string' Rhoconnect::Stats::Record.get_value(metric) || '' else raise ApiException.new(404, "Unknown metric") end end else raise ApiException.new(500, "Stats not enabled") end rescue Exception => e usercount = ["0:#{Time.now.to_i}"] end end def get_sources(partition_type) # app.sources returns reference # so we need to make a copy # otherwise we will end up modifying the @@sources variable in App sources = App.load(APP_NAME).sources.clone if partition_type.nil? or partition_type == 'all' sources else res = [] sources.each do |name| s = Source.load(name,{:app_id => APP_NAME,:user_id => '*'}) if s.partition_type and s.partition_type == partition_type.to_sym res << name end end res end end end