Module: GraphHelper

Included in:
RhoconnectConsole::Server
Defined in:
lib/rhoconnect/graph_helper.rb

Instance Method Summary collapse

Instance Method Details

#count_graph(uri, title, name, metric) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/rhoconnect/graph_helper.rb', line 225

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

#get_http_routesObject



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/rhoconnect/graph_helper.rb', line 208

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

#get_sources(partition_type) ⇒ Object



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/rhoconnect/graph_helper.rb', line 289

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

#get_user_count(name = nil, metric_p = nil, start = nil, finish = nil) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/rhoconnect/graph_helper.rb', line 263

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

#http_timing(params) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/rhoconnect/graph_helper.rb', line 141

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

#http_timing_key(params) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/rhoconnect/graph_helper.rb', line 76

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

#source_timing(params) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rhoconnect/graph_helper.rb', line 4

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