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.
Instance Method Summary collapse
-
#all ⇒ Object
Get stat summary including all aspects of activity that were measured except duration.
-
#avg_duration ⇒ Object
Get average duration of activity.
-
#avg_rate ⇒ Object
Convert average interval to average rate.
-
#finish(start_time = nil, id = nil) ⇒ Object
Mark the finish of an activity and update the average duration.
-
#initialize(measure_rate = true) ⇒ Activity
constructor
Initialize activity data.
-
#last ⇒ Object
Get stats about last activity.
-
#percentage ⇒ Object
Convert count per type into percentage by type.
-
#reset ⇒ Object
Reset statistics.
-
#update(type = nil, id = nil) ⇒ Object
Mark the start of an activity and update counts and average rate with weighting toward recent activity Ignore the update if its type contains “stats”.
Constructor Details
#initialize(measure_rate = true) ⇒ Activity
Initialize activity data
Parameters
- measure_rate(Boolean)
-
Whether to measure activity rate
49 50 51 52 |
# File 'lib/right_support/stats/activity.rb', line 49 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 |
Instance Method Details
#all ⇒ Object
Get stat summary including all aspects of activity that were measured except duration
Return
- (Hash|nil)
-
Information about activity, or nil if the total is 0
- “total”(Integer)
-
Total activity count
- “percent”(Hash)
-
Percentage for each type of activity if tracking type, otherwise omitted
- “last”(Hash)
-
Information about last activity
- “elapsed”(Integer)
-
Seconds since last activity started
- “type”(String)
-
Type of activity if tracking type, otherwise omitted
- “active”(Boolean)
-
Whether activity still active if tracking whether active, otherwise omitted
- “rate”(Float)
-
Recent average rate if measuring rate, otherwise omitted
177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/right_support/stats/activity.rb', line 177 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 ⇒ Object
Get average duration of activity
Return
(Float|nil) Average duration in seconds of activity weighted toward recent activity, or nil if total is 0
132 133 134 |
# File 'lib/right_support/stats/activity.rb', line 132 def avg_duration @avg_duration if @total > 0 end |
#avg_rate ⇒ Object
Convert average interval to average rate
Return
- (Float|nil)
-
Recent average rate, or nil if total is 0
121 122 123 124 125 |
# File 'lib/right_support/stats/activity.rb', line 121 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) ⇒ Object
Mark the finish of an activity and update the average duration
Parameters
- start_time(Time)
-
Time when activity started, defaults to last time update was called
- id(String)
-
Unique identifier associated with this activity
Return
- duration(Float)
-
Activity duration in seconds
108 109 110 111 112 113 114 115 |
# File 'lib/right_support/stats/activity.rb', line 108 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 ⇒ Object
Get stats about last activity
Return
- (Hash|nil)
-
Information about last activity, or nil if the total is 0
- “elapsed”(Integer)
-
Seconds since last activity started
- “type”(String)
-
Type of activity if specified, otherwise omitted
- “active”(Boolean)
-
Whether activity still active
143 144 145 146 147 148 149 150 |
# File 'lib/right_support/stats/activity.rb', line 143 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 ⇒ Object
Convert count per type into percentage by type
Return
- (Hash|nil)
-
Converted counts, or nil if total is 0
- “total”(Integer)
-
Total activity count
- “percent”(Hash)
-
Percentage for each type of activity if tracking type, otherwise omitted
158 159 160 161 162 163 164 |
# File 'lib/right_support/stats/activity.rb', line 158 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 ⇒ Object
Reset statistics
Return
- true
-
Always return true
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/right_support/stats/activity.rb', line 58 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) ⇒ Object
Mark the start of an activity and update counts and average rate with weighting toward recent activity Ignore the update if its type contains “stats”
Parameters
- type(String|Symbol)
-
Type of activity, with anything that is not a symbol, true, or false
automatically converted to a String and truncated to MAX_TYPE_SIZE characters,
defaults to nil
- id(String)
-
Unique identifier associated with this activity
Return
- now(Time)
-
Update time
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/right_support/stats/activity.rb', line 81 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 |