cohortly 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. data/Gemfile +11 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +61 -0
  4. data/Rakefile +29 -0
  5. data/app/controllers/cohortly/cohortly_controller.rb +8 -0
  6. data/app/controllers/cohortly/metrics_controller.rb +14 -0
  7. data/app/controllers/cohortly/reports_controller.rb +8 -0
  8. data/app/models/cohortly/metric.rb +79 -0
  9. data/app/models/cohortly/report.rb +78 -0
  10. data/app/views/cohortly/metrics/_metric.html.erb +8 -0
  11. data/app/views/cohortly/metrics/_tags.html.erb +8 -0
  12. data/app/views/cohortly/metrics/index.html.erb +22 -0
  13. data/app/views/cohortly/reports/index.html.erb +32 -0
  14. data/app/views/cohortly/reports/show.html.erb +22 -0
  15. data/app/views/layouts/cohortly/application.html.erb +144 -0
  16. data/config/routes.rb +7 -0
  17. data/lib/cohortly.rb +15 -0
  18. data/lib/cohortly/config.rb +6 -0
  19. data/lib/cohortly/engine.rb +16 -0
  20. data/lib/cohortly/tag_config.rb +85 -0
  21. data/lib/cohortly/version.rb +3 -0
  22. data/lib/tasks/run_reports.rake +11 -0
  23. data/test/cohortly_test.rb +119 -0
  24. data/test/dummy/Rakefile +7 -0
  25. data/test/dummy/app/controllers/application_controller.rb +17 -0
  26. data/test/dummy/app/controllers/stuff_controller.rb +12 -0
  27. data/test/dummy/app/helpers/application_helper.rb +2 -0
  28. data/test/dummy/app/helpers/stuff_helper.rb +2 -0
  29. data/test/dummy/app/views/layouts/application.html.erb +19 -0
  30. data/test/dummy/app/views/stuff/index.html.erb +1 -0
  31. data/test/dummy/app/views/stuff/my_stuff.html.erb +2 -0
  32. data/test/dummy/app/views/stuff/your_stuff.html.erb +2 -0
  33. data/test/dummy/config.ru +4 -0
  34. data/test/dummy/config/application.rb +48 -0
  35. data/test/dummy/config/boot.rb +9 -0
  36. data/test/dummy/config/cohortly.yml +15 -0
  37. data/test/dummy/config/database.yml +22 -0
  38. data/test/dummy/config/environment.rb +5 -0
  39. data/test/dummy/config/environments/development.rb +26 -0
  40. data/test/dummy/config/environments/production.rb +49 -0
  41. data/test/dummy/config/environments/test.rb +35 -0
  42. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  43. data/test/dummy/config/initializers/cohortly_config.rb +15 -0
  44. data/test/dummy/config/initializers/inflections.rb +10 -0
  45. data/test/dummy/config/initializers/mime_types.rb +5 -0
  46. data/test/dummy/config/initializers/secret_token.rb +7 -0
  47. data/test/dummy/config/initializers/session_store.rb +8 -0
  48. data/test/dummy/config/locales/en.yml +5 -0
  49. data/test/dummy/config/routes.rb +62 -0
  50. data/test/dummy/public/404.html +26 -0
  51. data/test/dummy/public/422.html +26 -0
  52. data/test/dummy/public/500.html +26 -0
  53. data/test/dummy/public/favicon.ico +0 -0
  54. data/test/dummy/public/javascripts/application.js +2 -0
  55. data/test/dummy/public/javascripts/controls.js +965 -0
  56. data/test/dummy/public/javascripts/dragdrop.js +974 -0
  57. data/test/dummy/public/javascripts/effects.js +1123 -0
  58. data/test/dummy/public/javascripts/prototype.js +6001 -0
  59. data/test/dummy/public/javascripts/rails.js +191 -0
  60. data/test/dummy/script/rails +6 -0
  61. data/test/integration/navigation_test.rb +14 -0
  62. data/test/support/integration_case.rb +5 -0
  63. data/test/support/test_tag_config.rb +40 -0
  64. data/test/test_helper.rb +27 -0
  65. metadata +154 -0
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "rails", "3.0.4"
4
+ gem "capybara"
5
+
6
+ gem 'mongo_mapper'
7
+ gem 'bson_ext'
8
+
9
+ # To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
10
+ # gem 'ruby-debug'
11
+ # gem 'ruby-debug19'
@@ -0,0 +1,20 @@
1
+ Copyright 2011 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,61 @@
1
+ = Cohortly
2
+
3
+ The beginings of an engine to do basic cohort analysis on a feature basis.
4
+
5
+ == Configuration
6
+
7
+ === Mongo database config
8
+
9
+ Setup a cohortly.yml file in your config directory
10
+
11
+ production:
12
+ host: localhost
13
+ port: 27017
14
+ username: nyusername
15
+ password: mypassword
16
+ database: cohortly-production
17
+
18
+ === Application controller config
19
+
20
+ Setup cohortly notifications by letting it know who your user is and his start date.
21
+ You also need to give cohortly the controller and action.
22
+
23
+ class ApplicationController < ActionController::Base
24
+ before_filter do
25
+ if current_user.logged_in?
26
+ ActiveSupport::Notifications.instrument("cohortly.event", {
27
+ :user_start_date => current_user.created_at,
28
+ :user_id => current_user.id,
29
+ :controller => params[:controller],
30
+ :action => params[:action]
31
+ })
32
+ end
33
+ end
34
+ end
35
+
36
+ === Tag Config
37
+
38
+ The tag config maps controllers and actions to tags that get added to the metrics that are recorded.
39
+
40
+ In confg/initializers/cohortly.rb
41
+
42
+ Cohortly::TagConfig.draw_tags do
43
+ tag :login do
44
+ controller :session do
45
+ actions :create
46
+ end
47
+ end
48
+ tag :logout do
49
+ controller :sessions do
50
+ actions :destroy
51
+ end
52
+ end
53
+ tag :view_account do
54
+ controller :account do
55
+ actions :show
56
+ end
57
+ end
58
+ end
59
+
60
+
61
+ This project rocks and uses MIT-LICENSE.
@@ -0,0 +1,29 @@
1
+ # encoding: UTF-8
2
+ require 'rubygems'
3
+ begin
4
+ require 'bundler/setup'
5
+ rescue LoadError
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
8
+
9
+ require 'rake'
10
+ require 'rake/rdoctask'
11
+
12
+ require 'rake/testtask'
13
+
14
+ Rake::TestTask.new(:test) do |t|
15
+ t.libs << 'lib'
16
+ t.libs << 'test'
17
+ t.pattern = 'test/**/*_test.rb'
18
+ t.verbose = false
19
+ end
20
+
21
+ task :default => :test
22
+
23
+ Rake::RDocTask.new(:rdoc) do |rdoc|
24
+ rdoc.rdoc_dir = 'rdoc'
25
+ rdoc.title = 'Cohortly'
26
+ rdoc.options << '--line-numbers' << '--inline-source'
27
+ rdoc.rdoc_files.include('README.rdoc')
28
+ rdoc.rdoc_files.include('lib/**/*.rb')
29
+ end
@@ -0,0 +1,8 @@
1
+ class Cohortly::CohortlyController < ApplicationController
2
+ layout 'cohortly/application'
3
+ before_filter do
4
+ if self.respond_to? :cohortly_authenticate
5
+ self.cohortly_authenticate
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,14 @@
1
+ class Cohortly::MetricsController < Cohortly::CohortlyController
2
+ def index
3
+ @metric_search = Cohortly::Metric.new(params[:cohortly_metric])
4
+
5
+ scope = Cohortly::Metric.limit(250).sort(:created_at.desc)
6
+ if params[:tags]
7
+ scope = scope.where(:tags => { :$all => params[:tags] })
8
+ end
9
+ if @metric_search.user_id
10
+ scope = scope.where(:user_id => @metric_search.user_id)
11
+ end
12
+ @metrics = scope.all
13
+ end
14
+ end
@@ -0,0 +1,8 @@
1
+ class Cohortly::ReportsController < Cohortly::CohortlyController
2
+ def index
3
+ @metric_search = Cohortly::Metric.new(params[:cohortly_metric])
4
+ Cohortly::Metric.cohort_chart_for_tag(params[:tags])
5
+ @report = Cohortly::Report.new( Cohortly::Metric.report_table_name(params[:tags]) )
6
+ end
7
+
8
+ end
@@ -0,0 +1,79 @@
1
+ module Cohortly
2
+ class Metric
3
+ include MongoMapper::Document
4
+
5
+ key :user_start_date, Time
6
+ key :user_id, Integer
7
+ key :user_email, String
8
+ key :tags, Array
9
+ key :controller, String
10
+ key :action, String
11
+ timestamps!
12
+
13
+ ensure_index :tags
14
+
15
+ def self.store!(args)
16
+ data = args[4]
17
+ if data[:controller] && !data[:controller]['cohortly']
18
+ data[:tags] = Cohortly::TagConfig.tags_for(data[:controller], data[:action])
19
+ data[:user_email] = data[:email] if data[:email]
20
+ data[:tags] += data[:add_tags] if data[:add_tags]
21
+ create(data)
22
+ end
23
+ end
24
+
25
+ def self.cohort_chart_for_tag(tags = nil)
26
+ query = {}
27
+ query = { :tags => { :$all => tags } } if tags
28
+ self.collection.map_reduce(self.month_map,
29
+ self.reduce,
30
+ { :out => self.report_table_name(tags),
31
+ :raw => true,
32
+ :query => query})
33
+ end
34
+
35
+ def self.report_table_name(tags = nil)
36
+ "cohort_report#{ tags ? "_#{ tags.sort.join('_') }" : '' }"
37
+ end
38
+
39
+ def self.month_map
40
+ <<-JS
41
+ function() {
42
+ function get_month_date(date) {
43
+ var year = date.getYear() + 1900;
44
+ var month = date.getMonth() + 1;
45
+ if(month < 10) { month = '0' + month }
46
+ return year + '-' + month;
47
+ }
48
+ var start_date = get_month_date(this.user_start_date);
49
+ var happened_on = get_month_date(this.created_at);
50
+ var payload = {};
51
+ payload[happened_on] = {};
52
+ payload[happened_on][this.user_id] = 1;
53
+ emit( start_date, payload );
54
+ }
55
+ JS
56
+ end
57
+
58
+ def self.reduce
59
+ <<-JS
60
+ function(key, values) {
61
+ var result = {};
62
+ values.forEach(function(value) {
63
+ for(happened_date in value) {
64
+ if(!result[happened_date]) {
65
+ result[happened_date] = value[happened_date];
66
+ } else {
67
+ for(user_id in value[happened_date]) {
68
+ result[happened_date][user_id] = 1;
69
+ }
70
+ }
71
+ }
72
+ });
73
+ return result;
74
+ }
75
+ JS
76
+ end
77
+
78
+ end
79
+ end
@@ -0,0 +1,78 @@
1
+ module Cohortly
2
+ class Report
3
+ # this is the reduced collection
4
+ attr_accessor :collection
5
+ def initialize(collection)
6
+ self.collection = collection
7
+ end
8
+
9
+ def data
10
+ @data ||= (MongoMapper.database[self.collection].find().collect {|x| x}).sort_by {|x| x['_id'] }
11
+ end
12
+
13
+ def start_month
14
+ data.first['_id']
15
+ end
16
+
17
+ def end_month
18
+ time_to_month(Time.now)
19
+ end
20
+
21
+ def time_to_month(time)
22
+ time.strftime('%Y-%m')
23
+ end
24
+
25
+ def month_to_time(str_month)
26
+ year, month = str_month.split('-')
27
+ Time.utc(year.to_i, month.to_i)
28
+ end
29
+
30
+ def user_count_in_cohort(str_month)
31
+ Cohortly::Metric.collection.distinct(:user_id,
32
+ { :user_start_date => { :$gt => month_to_time(str_month),
33
+ :$lt => (month_to_time(str_month) + 1.month)}}).length
34
+ end
35
+
36
+ def month_cohorts
37
+ return [] unless data.first
38
+ start_time = month_to_time(start_month)
39
+ end_time = month_to_time(end_month)
40
+ cur_time = start_time
41
+ res = []
42
+ while(cur_time <= end_time) do
43
+ res << time_to_month(cur_time)
44
+ cur_time += 1.month
45
+ end
46
+ res
47
+ end
48
+
49
+ def month_cohorts_from(cohort_key)
50
+ index = month_cohorts.index(cohort_key)
51
+ month_cohorts[(index + 1)..-1]
52
+ end
53
+
54
+ def report_line(cohort_key)
55
+ line = data.find {|x| x['_id'] == cohort_key}
56
+ return [] unless line
57
+ month_cohorts_from(cohort_key).collect do |key|
58
+ if line["value"][key]
59
+ line["value"][key].keys.length
60
+ else
61
+ 0
62
+ end
63
+ end
64
+ end
65
+
66
+ def percent_line(cohort_key)
67
+ line = report_line(cohort_key)
68
+ base = user_count_in_cohort(cohort_key)
69
+ line.collect { |x| (x/base.to_f * 100).round }.unshift base
70
+ end
71
+
72
+ def report_totals
73
+ month_cohorts.collect do |cohort_key|
74
+ report_line(cohort_key)
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,8 @@
1
+ <tr class="<%= cycle('odd', 'even') %>">
2
+ <td><%= metric.user_id %></td>
3
+ <td><%= metric.user_email %></td>
4
+ <td><%= metric.created_at.strftime("%m/%d/%Y %H:%M") %></td>
5
+ <td><%= metric.controller %></td>
6
+ <td><%= metric.action %></td>
7
+ <td><%= metric.tags.join(', ') %></td>
8
+ </tr>
@@ -0,0 +1,8 @@
1
+ <div class="tags">
2
+ <% Cohortly::Metric.collection.distinct(:tags).each do |tag| %>
3
+ <span class"tag">
4
+ <%= check_box_tag 'tags[]', tag, params[:tags] &&
5
+ params[:tags].include?(tag) %><label><%= tag %></label>
6
+ </span>
7
+ <% end %>
8
+ </div>
@@ -0,0 +1,22 @@
1
+ <h2>Metrics</h2>
2
+
3
+ <%= form_for(@metric_search, :url => cohortly_metrics_path, :html => {
4
+ :method =>:get }) do |f| %>
5
+ <div class="field">
6
+ <%= f.label :user_id %>
7
+ <%= f.text_field :user_id %>
8
+ </div>
9
+ <%= render :partial => 'cohortly/metrics/tags' %>
10
+ <%= f.submit "filter" %>
11
+ <% end %>
12
+ <table class="hor-zebra">
13
+ <tr>
14
+ <th>User</th>
15
+ <th>User Email</th>
16
+ <th>Time</th>
17
+ <th>Controller</th>
18
+ <th>Action</th>
19
+ <th>Tags</th>
20
+ </tr>
21
+ <%= render :collection => @metrics, :partial => 'metric' %>
22
+ </table>
@@ -0,0 +1,32 @@
1
+ <h2>Cohort Reports</h2>
2
+
3
+ <%= form_for @metric_search, :url => cohortly_reports_path, :html =>
4
+ {:method => :get} do |f| %>
5
+ <%= render :partial => 'cohortly/metrics/tags' %>
6
+ <%= f.submit 'Get Report'%>
7
+ <% end %>
8
+
9
+
10
+ <table class="one-column-emphasis">
11
+ <colgroup>
12
+ <col class="oce-first">
13
+ </colgroup>
14
+ <thead>
15
+ <tr>
16
+ <th>Month</th>
17
+ <th>N</th>
18
+ <% 12.times do |month| %>
19
+ <th>M<%= month + 1 %></th>
20
+ <% end %>
21
+ </tr>
22
+ </thead>
23
+ <% @report.month_cohorts.each do |cohort_key| %>
24
+ <tr class="<%= cycle('odd', 'even') %>">
25
+ <td><%= cohort_key %></td>
26
+ <% @report.percent_line(cohort_key)[0,13].each do |val| %>
27
+ <td style="text-align: right;"><%= val %></td>
28
+ <% end %>
29
+ </tr>
30
+ <% end %>
31
+ </table>
32
+
@@ -0,0 +1,22 @@
1
+ <h1><%= params[:id] %></h1>
2
+
3
+
4
+
5
+ <table>
6
+ <tr>
7
+ <th>Month</th>
8
+ <th>N</th>
9
+ <% 12.times do |month| %>
10
+ <th><%= (month + 1).ordinalize %> month</th>
11
+ <% end %>
12
+ </tr>
13
+ <% @report.month_cohorts.each do |cohort_key| %>
14
+ <tr class="<%= cycle('odd', 'even') %>">
15
+ <td><%= cohort_key %></td>
16
+ <% @report.percent_line(cohort_key)[0,13].each do |val| %>
17
+ <td style="text-align: right;"><%= val %></td>
18
+ <% end %>
19
+ </tr>
20
+ <% end %>
21
+ </table>
22
+
@@ -0,0 +1,144 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Cohortly</title>
5
+ <%= csrf_meta_tag %>
6
+ <style>
7
+
8
+ body {
9
+ font-family: "Lucida Sans Unicode","Lucida Grande",Sans-Serif;
10
+ font-size: 12px;
11
+ margin: 0;
12
+ color: #666699;
13
+ }
14
+ form {
15
+
16
+ }
17
+ .field, .tags {
18
+ margin-bottom: 1em;
19
+ }
20
+ .field label {
21
+ display:block;
22
+ }
23
+ .header {
24
+ background-color: #E8EDFF;
25
+ height: 50px;
26
+ }
27
+ .header h1 {
28
+ padding-top: 12px;
29
+ margin: 0;
30
+ color: #669;
31
+ }
32
+ .container {
33
+ position: relative;
34
+ margin:0 auto;
35
+ width: 960px;
36
+ }
37
+ .hor-zebra {
38
+ border-collapse: collapse;
39
+ font-family: "Lucida Sans Unicode","Lucida Grande",Sans-Serif;
40
+ font-size: 12px;
41
+ text-align: left;
42
+ width: 960px;
43
+ }
44
+ .hor-zebra th {
45
+ color: #003399;
46
+ font-size: 14px;
47
+ font-weight: normal;
48
+ padding: 10px 8px;
49
+ }
50
+ .hor-zebra td {
51
+ color: #666699;
52
+ padding: 8px;
53
+ }
54
+ .hor-zebra .odd {
55
+ background: none repeat scroll 0 0 #E8EDFF;
56
+ }
57
+
58
+ .one-column-emphasis {
59
+ border-collapse: collapse;
60
+ font-family: "Lucida Sans Unicode","Lucida Grande",Sans-Serif;
61
+ font-size: 12px;
62
+ text-align: left;
63
+ position: relative;
64
+ left: -4px;
65
+ margin-top: 20px;
66
+ }
67
+ .one-column-emphasis th {
68
+ color: #003399;
69
+ font-size: 14px;
70
+ font-weight: normal;
71
+ padding: 12px 15px;
72
+ }
73
+ .one-column-emphasis td {
74
+ border-top: 1px solid #E8EDFF;
75
+ color: #666699;
76
+ padding: 10px 15px;
77
+ }
78
+ .oce-first {
79
+ background: none repeat scroll 0 0 #D0DAFD;
80
+ border-left: 10px solid transparent;
81
+ border-right: 10px solid transparent;
82
+ }
83
+ .one-column-emphasis tr:hover td {
84
+ background: none repeat scroll 0 0 #EFF2FF;
85
+ color: #333399;
86
+ }
87
+ .navigation {
88
+ position: absolute;
89
+ bottom: -3px;
90
+ right: 0;
91
+ }
92
+
93
+
94
+ .navigation ul {
95
+ list-style: none;
96
+ margin: 0;
97
+ padding: 0;
98
+ }
99
+
100
+
101
+ .navigation ul li {
102
+ display: inline;
103
+ margin-right: .75em;
104
+ background-color: #FFF;
105
+ color: #669;
106
+ border-radius: 4px;
107
+ }
108
+
109
+ .navigation ul li.last {
110
+ margin-right: 0;
111
+ }
112
+ .navigation li {
113
+ padding: 5px 1em;
114
+ }
115
+ .navigation li a {
116
+ color: #669;
117
+ text-decoration: none;
118
+ }
119
+ </style>
120
+
121
+ </head>
122
+ <body>
123
+ <div class="header">
124
+ <div class="container">
125
+ <h1>Cohortly</h1>
126
+ <div class="navigation">
127
+ <ul>
128
+ <li><span><%= link_to "Metrics", cohortly_metrics_path %></span></li>
129
+ <li><%= link_to "Cohort Reports", cohortly_reports_path %></span></li>
130
+ </ul>
131
+ </div>
132
+ </div>
133
+ </div>
134
+ <div class="content">
135
+ <div class="container">
136
+ <%= yield %>
137
+ </div>
138
+ </div>
139
+ <div class="footer">
140
+ <div class="container">
141
+ </div>
142
+ </div>
143
+ </body>
144
+ </html>