Class ResourceUsage
In: app/models/resource_usage.rb
Parent: ActiveRecord::Base

This is the model class ResourceUsage related to the resource_usages table in the database.

Methods

Public Class methods

Due to technical reasons, e.g. sleep(60) , thread can remain in sleep state for more than 60 seconds, in this case it may not get sample for that minute. With this method filling any such gaps(consecutive one gap at max) by predicting usage from around values. Parameters : array(Array) of integer or float Find out the elements having 0 or 0.0 as value, normalize it value according to previous/next values.

[Source]

     # File app/models/resource_usage.rb, line 156
156:     def fill_gaps!(array)
157:       if array.length > 0  # proceed only if array is not empty
158:         if array[0] == 0 # if first element is zero, predict value from next two values.
159:           if array.length > 2 and array[1] != 0 and array[2] != 0 and !array[1].nil? and !array[2].nil?
160:             array[0] = (array[1] + array[2]) / 2
161:           end # if array.length > 2
162:         end # if array[0].to_i == 0
163:         
164:         if array[-1] == 0 #if last element is zero, predict value from previous two values.
165:           if array.length > 2 and array[-2] != 0 and array[-3] != 0 and !array[-2].nil? and !array[-3].nil?
166:             array[-1] = (array[-2] + array[-3]) / 2
167:           end # if array.length > 2
168:         end # if array[-1].to_i == 0
169:         
170:         for i in 1..array.length-1 # checking for interminant values
171:           if array[i] == 0 and array[i-1] != 0 and array[i+1] != 0 and !array[i-1].nil? and !array[i+1].nil?
172:             array[i] = (array[i-1] + array[i+1]) / 2
173:           end # if
174:         end # for
175:       end # if array.length > 0
176:     end

This method Returns the Resource Usage data for a particular application. This data is used to plot the graph for cpu usage and memory usage for that application. CPU usage is in percentage and the physical memory usage is MBs.

[Source]

     # File app/models/resource_usage.rb, line 101
101:     def get_application_data(app_id, start_time, end_time, type)
102:       max = 0
103:       interval = 0
104:       interval = ((end_time - start_time) / 60).to_i
105:       final_data = Array.new(interval)
106:       wall_time = Array.new(interval)
107:       resource_usages = find(:all, :select => 'sum(cpu_usage) as cpu_usage, sum(memory_usage) as memory_usage, count(*) as count, wall_time', :conditions => ['app_id = ? and wall_time >= ? and wall_time < ?', app_id, start_time, end_time], :group => 'wall_time')
108:         resource_usages.each do |resource_usage|
109:             current_time = Time.local(resource_usage.wall_time.year, resource_usage.wall_time.month, resource_usage.wall_time.day, resource_usage.wall_time.hour, resource_usage.wall_time.min, '0')
110:             index = (current_time - start_time) / 60
111:             count = resource_usage.count
112:             if type == "cpu"
113:               total_data = resource_usage.cpu_usage
114:             elsif type == "memory"
115:               total_data = resource_usage.memory_usage.to_f / 1024
116:             end
117:           if max < total_data
118:             max = total_data.to_i
119:           end
120:           final_data[index] = total_data
121:           wall_time[index] = current_time.strftime("%H:%M")
122:         end
123: 
124:        for i in 0..interval
125:           if final_data[i].nil?
126:               wall_time[i] = (start_time+i*60).strftime("%H:%M")
127:           end
128:       end
129: 
130:       max,slab = get_max_and_slab(max)
131:       fill_gaps!(final_data)
132:       step = (interval + 1) / 20
133:       return wall_time, final_data, max, slab, step
134:     end

This method is used to get the latest state of the application i.e. its cpu usage and physical memory usage.

 Returns hash with application name as key and 2 element array as value. In 2 element, first element is cpu usage, 2nd is memory usage in kb

[Source]

    # File app/models/resource_usage.rb, line 41
41:     def get_latest_for_apps
42:       t1 = Time.now
43:       interval = 60 # in seconds
44:       t2 = t1 - interval
45:       result_set = find(:all, :select => "app_id, sum(cpu_usage) as tot_cpu, sum(memory_usage) as tot_memory", :conditions => ["wall_time <= ? and wall_time >= ?",t1,t2], :group => 'app_id')
46:       
47:       if result_set.length == 0
48:         {}
49:       else
50:         apps = App.find(:all, :select => "id, name")
51:         app_hash = Hash.new
52:         apps.each do |app|
53:           app_hash[app.id] = app.name
54:         end
55:         result_hash = Hash.new
56:         result_set.each do |result|
57:           result_hash[app_hash[result.app_id]] = [result.tot_cpu.to_f, result.tot_memory.to_i]
58:         end # do |result|
59:         result_hash
60:       end # if
61:     end

This method is used to get the latest state of the server i.e. its cpu usage and physical memory usage.

 Returns array wtih 2 element, first element is cpu usage, 2nd is memory usage in kb

[Source]

    # File app/models/resource_usage.rb, line 26
26:     def get_latest_for_server
27:       t1 = Time.now
28:       interval = 60 # in seconds
29:       t2 = t1 - interval
30:       result = find(:first, :select => "sum(cpu_usage) as tot_cpu, sum(memory_usage) as tot_memory", :conditions => ["wall_time <= ? and wall_time >= ?",t1,t2])
31:       if !result
32:         res = [0.0, 0]
33:       else
34:         res = [result.tot_cpu.to_f, result.tot_memory.to_i]
35:       end
36:       res
37:     end

This method gives the maximum value for y axis and the value by which the y axis is to be partitioned.

[Source]

     # File app/models/resource_usage.rb, line 137
137:     def get_max_and_slab(max)
138:       if max == 0
139:         max = 1
140:         slab = 1
141:       else
142:         if max > 8
143:           slab = max/8.to_i
144:         else
145:           slab = 1
146:         end
147:       end
148:       max = max.to_i + slab
149:       return max, slab
150:     end

This method Returns the Resource Usage data for the server. This data is used to plot the graph for cpu usage and memory usage of the server. CPU usage is in percentage and the physical memory usage is MBs.

[Source]

    # File app/models/resource_usage.rb, line 65
65:     def get_server_resource_usage(start_time, end_time, type)
66:       max = 0
67:       interval = 0
68:       interval = ((end_time - start_time) / 60).to_i
69:       final_data = Array.new(interval)
70:       wall_time = Array.new(interval)
71:       resource_usages = find(:all, :select=>'wall_time, sum(cpu_usage) as cpu_usage, sum(memory_usage) as memory_usage', :conditions => ["wall_time >= ? and wall_time < ?", start_time, end_time], :group => 'wall_time')
72:      resource_usages.each do |resource_usage| 
73:           current_time = Time.local(resource_usage.wall_time.year,  resource_usage.wall_time.month, resource_usage.wall_time.day, resource_usage.wall_time.hour, resource_usage.wall_time.min, '0')
74:           index = (current_time - start_time) / 60
75:           if type == "CPU"
76:             total_data = resource_usage.cpu_usage
77:           elsif type == "Memory"
78:             total_data = resource_usage.memory_usage.to_f / 1024
79:           end
80:           if max < total_data
81:             max = total_data.to_i
82:           end
83:           final_data[index] = total_data
84:           wall_time[index] = current_time.strftime("%H:%M")
85:       end  
86:       for i in 0..interval
87:           if final_data[i].nil?
88:               wall_time[i] = (start_time+i*60).strftime("%H:%M")
89:           end
90:       end
91:       max, slab = get_max_and_slab(max)
92:       fill_gaps!(final_data)
93:       step = (interval + 1) / 20
94:       return wall_time, final_data, max, slab, step
95:     end

[Validate]