Class: Statistics
- Inherits:
-
Object
- Object
- Statistics
- Defined in:
- lib/statistics.rb
Instance Attribute Summary (collapse)
-
- (Object) range
readonly
Returns the value of attribute range.
-
- (Object) start_time
readonly
Returns the value of attribute start_time.
Instance Method Summary (collapse)
- - (Object) comments_count_sql
- - (Object) contacts_sharing_with_count_sql
- - (Object) correlate(first_metric, second_metric)
- - (Object) correlation(x_array, y_array)
- - (Object) fb_connected_distribution
- - (Object) fb_connected_distribution_sql
- - (Object) generate_correlations
-
- (Statistics) initialize
constructor
A new instance of Statistics.
- - (Object) invites_sent_count_sql
- - (Object) mean(array)
- - (Object) mentions_count_sql
- - (Object) posts_count_sql
-
- (Object) retention(n)
% of cohort came back last week.
- - (Object) sign_in_count_sql
- - (Object) standard_deviation(array)
- - (Object) tags_followed_count_sql
- - (Object) top_active_users(n)
- - (Object) users_by_week(n)
Constructor Details
- (Statistics) initialize
A new instance of Statistics
6 7 8 9 |
# File 'lib/statistics.rb', line 6 def initialize #@start_time = start_time #@range = range end |
Instance Attribute Details
- (Object) range (readonly)
Returns the value of attribute range
3 4 5 |
# File 'lib/statistics.rb', line 3 def range @range end |
- (Object) start_time (readonly)
Returns the value of attribute start_time
3 4 5 |
# File 'lib/statistics.rb', line 3 def start_time @start_time end |
Instance Method Details
- (Object) comments_count_sql
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/statistics.rb', line 22 def comments_count_sql <<SQL SELECT users.id AS id, count(comments.id) AS count FROM users JOIN people ON people.owner_id = users.id LEFT OUTER JOIN comments ON people.id = comments.author_id #{self.where_clause_sql} GROUP BY users.id SQL end |
- (Object) contacts_sharing_with_count_sql
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/statistics.rb', line 64 def contacts_sharing_with_count_sql <<SQL SELECT users.id AS id, count(contacts.id) AS count FROM users JOIN contacts on contacts.user_id = users.id JOIN aspect_memberships on aspect_memberships.contact_id = contacts.id #{self.where_clause_sql} GROUP BY users.id SQL end |
- (Object) correlate(first_metric, second_metric)
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/statistics.rb', line 104 def correlate(first_metric, second_metric) # [{"id" => 1 , "count" => 123}] x_array = [] y_array = [] self.result_hash(first_metric).keys.each do |k| if val = self.result_hash(second_metric)[k] x_array << self.result_hash(first_metric)[k] y_array << val end end correlation(x_array, y_array) end |
- (Object) correlation(x_array, y_array)
130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/statistics.rb', line 130 def correlation(x_array, y_array) sum = 0.0 x_array.each_index do |i| sum = sum + x_array[i].to_f * y_array[i].to_f end x_y_mean = sum/x_array.length.to_f x_mean = mean(x_array) y_mean = mean(y_array) st_dev_x = standard_deviation(x_array) st_dev_y = standard_deviation(y_array) (x_y_mean - (x_mean*y_mean))/(st_dev_x * st_dev_y) end |
- (Object) fb_connected_distribution
86 87 88 89 90 91 92 93 94 |
# File 'lib/statistics.rb', line 86 def fb_connected_distribution User.connection.select_all(fb_connected_distribution_sql).map { |row| Hash[ row.map { |k,v| [k, v.to_i] } ] } end |
- (Object) fb_connected_distribution_sql
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/statistics.rb', line 75 def fb_connected_distribution_sql <<SQL SELECT users.id AS id, users.sign_in_count AS count, count(services.id) AS connected FROM users LEFT OUTER JOIN services on services.user_id = users.id AND services.type = 'Services::Facebook' #{self.where_clause_sql} GROUP BY users.id, users.sign_in_count SQL end |
- (Object) generate_correlations
121 122 123 124 125 126 127 128 |
# File 'lib/statistics.rb', line 121 def generate_correlations result = {} [:posts_count, :comments_count, :invites_sent_count, #:tags_followed_count, :mentions_count, :contacts_sharing_with_count].each do |metric| result[metric] = self.correlate(metric,:sign_in_count) end result end |
- (Object) invites_sent_count_sql
33 34 35 36 37 38 39 40 41 |
# File 'lib/statistics.rb', line 33 def invites_sent_count_sql <<SQL SELECT users.id AS id, count(invitations.id) AS count FROM users LEFT OUTER JOIN invitations ON users.id = invitations.sender_id #{self.where_clause_sql} GROUP BY users.id SQL end |
- (Object) mean(array)
145 146 147 148 149 150 |
# File 'lib/statistics.rb', line 145 def mean(array) sum = array.inject(0.0) do |sum, val| sum += val.to_f end sum / array.length end |
- (Object) mentions_count_sql
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/statistics.rb', line 53 def mentions_count_sql <<SQL SELECT users.id AS id, count(mentions.id) AS count FROM users JOIN people on users.id = people.owner_id LEFT OUTER JOIN mentions on people.id = mentions.person_id #{self.where_clause_sql} GROUP BY users.id SQL end |
- (Object) posts_count_sql
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/statistics.rb', line 11 def posts_count_sql <<SQL SELECT users.id AS id, count(posts.id) AS count FROM users JOIN people ON people.owner_id = users.id LEFT OUTER JOIN posts ON people.id = posts.author_id #{self.where_clause_sql} GROUP BY users.id SQL end |
- (Object) retention(n)
% of cohort came back last week
164 165 166 |
# File 'lib/statistics.rb', line 164 def retention(n) users_by_week(n).count.to_f/week_created(n).count end |
- (Object) sign_in_count_sql
96 97 98 99 100 101 102 |
# File 'lib/statistics.rb', line 96 def sign_in_count_sql <<SQL SELECT users.id AS id, users.sign_in_count AS count FROM users #{self.where_clause_sql} SQL end |
- (Object) standard_deviation(array)
152 153 154 155 156 157 158 159 160 161 |
# File 'lib/statistics.rb', line 152 def standard_deviation(array) variance = lambda do m = mean(array) sum = 0.0 array.each{ |v| sum += (v.to_f-m)**2 } sum/array.length.to_f end.call Math.sqrt(variance) end |
- (Object) tags_followed_count_sql
43 44 45 46 47 48 49 50 51 |
# File 'lib/statistics.rb', line 43 def <<SQL SELECT users.id AS id, count(tag_followings.id) AS count FROM users LEFT OUTER JOIN tag_followings on users.id = tag_followings.user_id #{self.where_clause_sql} GROUP BY users.id SQL end |
- (Object) top_active_users(n)
168 169 170 171 |
# File 'lib/statistics.rb', line 168 def top_active_users(n) ten_percent_lim = (users_by_week(n).count.to_f * 0.3).ceil users_by_week(n).joins(:person => :profile).where('users.sign_in_count > 4').order("users.sign_in_count DESC").limit(ten_percent_lim).select('users.email, users.username, profiles.first_name, users.sign_in_count') end |
- (Object) users_by_week(n)
173 174 175 |
# File 'lib/statistics.rb', line 173 def users_by_week(n) week_created(n).where("current_sign_in_at > ?", Time.now - 1.week) end |