Class: RightSupport::Stats::Activity
- Defined in:
- lib/right_support/stats/activity.rb
Overview
Track statistics for a given kind of activity
Constant Summary collapse
- RECENT_SIZE =
Number of samples included when calculating average recent activity with the smoothing formula A = ((A * (RECENT_SIZE - 1)) + V) / RECENT_SIZE, where A is the current recent average and V is the new activity value As a rough guide, it takes approximately 2 * RECENT_SIZE activity values at value V for average A to reach 90% of the original difference between A and V For example, for A = 0, V = 1, RECENT_SIZE = 3 the progression for A is 0, 0.3, 0.5, 0.7, 0.8, 0.86, 0.91, 0.94, 0.96, 0.97, 0.98, 0.99, …
3- MAX_TYPE_SIZE =
Maximum string length for activity type
60
Instance Attribute Summary collapse
-
#count_per_type ⇒ Object
readonly
(Hash) Count of activity per type.
-
#total ⇒ Object
readonly
(Integer) Total activity count.
Class Method Summary collapse
-
.all(stats) ⇒ Hash, NilClass
Aggregate the stats from multiple ‘all’ calls.
-
.avg_rate(stats, total) ⇒ Fixnum, NilClass
Compute average rate from multiple average rates.
-
.last(stats) ⇒ Hash, NilClass
Determine last activity from multiple last activity stats.
-
.percentage(stats, total) ⇒ Hash
Aggregate multiple percentage stats.
Instance Method Summary collapse
-
#all ⇒ Hash, NilClass
Get stat summary including all aspects of activity that were measured except duration.
-
#avg_duration ⇒ Float, NilClass
Get average duration of activity.
-
#avg_rate ⇒ Float, NilClass
Convert average interval to average rate.
-
#finish(start_time = nil, id = nil) ⇒ Float
Mark the finish of an activity and update the average duration.
-
#initialize(measure_rate = true) ⇒ Activity
constructor
Initialize activity data.
-
#last ⇒ Hash, NilClass
Get stats about last activity.
-
#percentage ⇒ Hash, NilClass
Convert count per type into percentage by type.
-
#reset ⇒ TrueClass
Reset statistics.
-
#update(type = nil, id = nil) ⇒ Time
Mark the start of an activity and update counts and average rate with weighting toward past activity Ignore the update if its type contains “stats”.
Constructor Details
#initialize(measure_rate = true) ⇒ Activity
Initialize activity data
48 49 50 51 |
# File 'lib/right_support/stats/activity.rb', line 48 def initialize(measure_rate = true) @measure_rate = measure_rate reset end |
Instance Attribute Details
#count_per_type ⇒ Object (readonly)
(Hash) Count of activity per type
43 44 45 |
# File 'lib/right_support/stats/activity.rb', line 43 def count_per_type @count_per_type end |
#total ⇒ Object (readonly)
(Integer) Total activity count
40 41 42 |
# File 'lib/right_support/stats/activity.rb', line 40 def total @total end |
Class Method Details
.all(stats) ⇒ Hash, NilClass
Aggregate the stats from multiple ‘all’ calls
206 207 208 209 210 211 212 213 214 |
# File 'lib/right_support/stats/activity.rb', line 206 def self.all(stats) if (total = stats.inject(0) { |t, s| t += s["total"] if s && s["total"]; t }) > 0 all = percentage(stats, total) all["last"] = last(stats.map { |s| s["last"] if s }) rate = avg_rate(stats.map { |s| {"rate" => s["rate"], "total" => s["total"]} if s }, total) all["rate"] = rate if rate all end end |
.avg_rate(stats, total) ⇒ Fixnum, NilClass
Compute average rate from multiple average rates
249 250 251 252 |
# File 'lib/right_support/stats/activity.rb', line 249 def self.avg_rate(stats, total) sum = stats.inject(nil) { |sum, stat| sum = (sum || 0.0) + (stat["rate"] * stat["total"]) if stat && stat["rate"]; sum } sum / total if sum end |
.last(stats) ⇒ Hash, NilClass
Determine last activity from multiple last activity stats
259 260 261 |
# File 'lib/right_support/stats/activity.rb', line 259 def self.last(stats) stats.inject(nil) { |l, s| l = s if s && (l.nil? || (l["elapsed"] > s["elapsed"] && (s["type"] || s["active"]))); l } end |
.percentage(stats, total) ⇒ Hash
Aggregate multiple percentage stats
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/right_support/stats/activity.rb', line 224 def self.percentage(stats, total) count_per_type = {} stats.each do |s| if s t = s["total"] s["percent"].each do |k, v| count_per_type[k] = (count_per_type[k] || 0) + ((v / 100.0) * t).round end if s["percent"] end end if count_per_type.empty? {"total" => total} else percent = {} count_per_type.each { |k, v| percent[k] = (v / total.to_f) * 100.0 } {"percent" => percent, "total" => total} end end |
Instance Method Details
#all ⇒ Hash, NilClass
Get stat summary including all aspects of activity that were measured except duration
167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/right_support/stats/activity.rb', line 167 def all if @total > 0 result = if @count_per_type.empty? {"total" => @total} else percentage end result.merge!("last" => last) result.merge!("rate" => avg_rate) if @measure_rate result end end |
#avg_duration ⇒ Float, NilClass
Get average duration of activity
125 126 127 |
# File 'lib/right_support/stats/activity.rb', line 125 def avg_duration @avg_duration if @total > 0 end |
#avg_rate ⇒ Float, NilClass
Convert average interval to average rate
114 115 116 117 118 |
# File 'lib/right_support/stats/activity.rb', line 114 def avg_rate if @total > 0 if @interval == 0.0 then 0.0 else 1.0 / @interval end end end |
#finish(start_time = nil, id = nil) ⇒ Float
Mark the finish of an activity and update the average duration
102 103 104 105 106 107 108 109 |
# File 'lib/right_support/stats/activity.rb', line 102 def finish(start_time = nil, id = nil) now = Time.now start_time ||= @last_start_time duration = now - start_time @avg_duration = average(@avg_duration || 0.0, duration) @last_id = 0 if id && id == @last_id duration end |
#last ⇒ Hash, NilClass
Get stats about last activity
135 136 137 138 139 140 141 142 |
# File 'lib/right_support/stats/activity.rb', line 135 def last if @total > 0 result = {"elapsed" => (Time.now - @last_start_time).to_i} result["type"] = @last_type if @last_type result["active"] = @last_id != 0 if !@last_id.nil? result end end |
#percentage ⇒ Hash, NilClass
Convert count per type into percentage by type
149 150 151 152 153 154 155 |
# File 'lib/right_support/stats/activity.rb', line 149 def percentage if @total > 0 percent = {} @count_per_type.each { |k, v| percent[k] = (v / @total.to_f) * 100.0 } {"percent" => percent, "total" => @total} end end |
#reset ⇒ TrueClass
Reset statistics
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/right_support/stats/activity.rb', line 56 def reset @interval = 0.0 @last_start_time = Time.now @avg_duration = nil @total = 0 @count_per_type = {} @last_type = nil @last_id = nil true end |
#update(type = nil, id = nil) ⇒ Time
Mark the start of an activity and update counts and average rate with weighting toward past activity Ignore the update if its type contains “stats”
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/right_support/stats/activity.rb', line 77 def update(type = nil, id = nil) now = Time.now if type.nil? || !(type =~ /stats/) @interval = average(@interval, now - @last_start_time) if @measure_rate @last_start_time = now @total += 1 unless type.nil? unless [Symbol, TrueClass, FalseClass].include?(type.class) type = type.inspect unless type.is_a?(String) type = type[0, MAX_TYPE_SIZE - 3] + "..." if type.size > (MAX_TYPE_SIZE - 3) end @count_per_type[type] = (@count_per_type[type] || 0) + 1 end @last_type = type @last_id = id end now end |