Class: Vanity::Adapters::MongodbAdapter
- Inherits:
-
AbstractAdapter
- Object
- AbstractAdapter
- Vanity::Adapters::MongodbAdapter
- Defined in:
- lib/vanity/adapters/mongodb_adapter.rb
Overview
MongoDB adapter.
Instance Method Summary collapse
- #ab_add_conversion(experiment, alternative, identity, count = 1, implicit = false) ⇒ Object
- #ab_add_participant(experiment, alternative, identity) ⇒ Object
- #ab_counts(experiment, alternative) ⇒ Object
- #ab_get_outcome(experiment) ⇒ Object
- #ab_not_showing(experiment, identity) ⇒ Object
- #ab_set_outcome(experiment, alternative = 0) ⇒ Object
- #ab_show(experiment, identity, alternative) ⇒ Object
- #ab_showing(experiment, identity) ⇒ Object
- #active? ⇒ Boolean
- #connect! ⇒ Object
- #destroy_experiment(experiment) ⇒ Object
- #destroy_metric(metric) ⇒ Object
- #disconnect! ⇒ Object
- #flushdb ⇒ Object
- #get_experiment_completed_at(experiment) ⇒ Object
- #get_experiment_created_at(experiment) ⇒ Object
-
#get_metric_last_update_at(metric) ⇒ Object
– Metrics –.
-
#initialize(options) ⇒ MongodbAdapter
constructor
A new instance of MongodbAdapter.
- #is_experiment_completed?(experiment) ⇒ Boolean
- #metric_track(metric, timestamp, identity, values) ⇒ Object
- #metric_values(metric, from, to) ⇒ Object
- #reconnect! ⇒ Object
- #set_experiment_completed_at(experiment, time) ⇒ Object
-
#set_experiment_created_at(experiment, time) ⇒ Object
– Experiments –.
- #to_s ⇒ Object
Constructor Details
#initialize(options) ⇒ MongodbAdapter
Returns a new instance of MongodbAdapter.
18 19 20 21 22 23 |
# File 'lib/vanity/adapters/mongodb_adapter.rb', line 18 def initialize() @mongo = Mongo::Connection.new([:host], [:port], :connect=>false) @options = .clone @options[:database] ||= (@options[:path] && @options[:path].split("/")[1]) || "vanity" connect! end |
Instance Method Details
#ab_add_conversion(experiment, alternative, identity, count = 1, implicit = false) ⇒ Object
137 138 139 140 141 142 143 144 145 |
# File 'lib/vanity/adapters/mongodb_adapter.rb', line 137 def ab_add_conversion(experiment, alternative, identity, count = 1, implicit = false) if implicit @participants.update({ :experiment=>experiment, :identity=>identity }, { "$set"=>{ :seen=>alternative } }, :upsert=>true) else participating = @participants.find_one(:experiment=>experiment, :identity=>identity, :seen=>alternative) end @participants.update({ :experiment=>experiment, :identity=>identity }, { "$set"=>{ :converted=>alternative } }, :upsert=>true) if implicit || participating @experiments.update({ :_id=>experiment }, { "$inc"=>{ "conversions.#{alternative}"=>count } }, :upsert=>true) end |
#ab_add_participant(experiment, alternative, identity) ⇒ Object
133 134 135 |
# File 'lib/vanity/adapters/mongodb_adapter.rb', line 133 def ab_add_participant(experiment, alternative, identity) @participants.update({ :experiment=>experiment, :identity=>identity }, { "$set"=>{ :seen=>alternative } }, :upsert=>true) end |
#ab_counts(experiment, alternative) ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/vanity/adapters/mongodb_adapter.rb', line 112 def ab_counts(experiment, alternative) record = @experiments.find_one({ :_id=>experiment }, { :fields=>[:conversions] }) conversions = record && record["conversions"] { :participants => @participants.find({ :experiment=>experiment, :seen=>alternative }, { :fields=>[] }).count, :converted => @participants.find({ :experiment=>experiment, :converted=>alternative }, { :fields=>[] }).count, :conversions => conversions && conversions[alternative.to_s] || 0 } end |
#ab_get_outcome(experiment) ⇒ Object
147 148 149 150 |
# File 'lib/vanity/adapters/mongodb_adapter.rb', line 147 def ab_get_outcome(experiment) experiment = @experiments.find_one({ :_id=>experiment }, { :fields=>[:outcome] }) experiment && experiment["outcome"] end |
#ab_not_showing(experiment, identity) ⇒ Object
129 130 131 |
# File 'lib/vanity/adapters/mongodb_adapter.rb', line 129 def ab_not_showing(experiment, identity) @participants.update({ :experiment=>experiment, :identity=>identity }, { "$unset"=>:show }) end |
#ab_set_outcome(experiment, alternative = 0) ⇒ Object
152 153 154 |
# File 'lib/vanity/adapters/mongodb_adapter.rb', line 152 def ab_set_outcome(experiment, alternative = 0) @experiments.update({ :_id=>experiment }, { "$set"=>{ :outcome=>alternative } }, :upsert=>true) end |
#ab_show(experiment, identity, alternative) ⇒ Object
120 121 122 |
# File 'lib/vanity/adapters/mongodb_adapter.rb', line 120 def ab_show(experiment, identity, alternative) @participants.update({ :experiment=>experiment, :identity=>identity }, { "$set"=>{ :show=>alternative } }, :upsert=>true) end |
#ab_showing(experiment, identity) ⇒ Object
124 125 126 127 |
# File 'lib/vanity/adapters/mongodb_adapter.rb', line 124 def ab_showing(experiment, identity) participant = @participants.find_one({ :experiment=>experiment, :identity=>identity }, { :fields=>[:show] }) participant && participant["show"] end |
#active? ⇒ Boolean
25 26 27 |
# File 'lib/vanity/adapters/mongodb_adapter.rb', line 25 def active? @mongo.connected? end |
#connect! ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/vanity/adapters/mongodb_adapter.rb', line 40 def connect! @mongo.connect database = @mongo.db(@options[:database]) database.authenticate @options[:username], @options[:password], true if @options[:username] @metrics = database.collection("vanity.metrics") @experiments = database.collection("vanity.experiments") @participants = database.collection("vanity.participants") @participants.create_index [[:experiment, 1], [:identity, 1]], :unique=>true end |
#destroy_experiment(experiment) ⇒ Object
156 157 158 159 |
# File 'lib/vanity/adapters/mongodb_adapter.rb', line 156 def destroy_experiment(experiment) @experiments.remove :_id=>experiment @participants.remove :experiment=>experiment end |
#destroy_metric(metric) ⇒ Object
83 84 85 |
# File 'lib/vanity/adapters/mongodb_adapter.rb', line 83 def destroy_metric(metric) @metrics.remove :_id=>metric end |
#disconnect! ⇒ Object
29 30 31 32 33 |
# File 'lib/vanity/adapters/mongodb_adapter.rb', line 29 def disconnect! @mongo.close rescue nil if @mongo @metrics, @experiments = nil @mongo = nil end |
#flushdb ⇒ Object
55 56 57 58 59 |
# File 'lib/vanity/adapters/mongodb_adapter.rb', line 55 def flushdb @metrics.drop @experiments.drop @participants.drop end |
#get_experiment_completed_at(experiment) ⇒ Object
103 104 105 106 |
# File 'lib/vanity/adapters/mongodb_adapter.rb', line 103 def get_experiment_completed_at(experiment) record = @experiments.find_one({ :_id=>experiment }, { :fields=>[:completed_at] }) record && record["completed_at"] end |
#get_experiment_created_at(experiment) ⇒ Object
94 95 96 97 |
# File 'lib/vanity/adapters/mongodb_adapter.rb', line 94 def get_experiment_created_at(experiment) record = @experiments.find_one({ :_id=>experiment }, { :fields=>[:created_at] }) record && record["created_at"] end |
#get_metric_last_update_at(metric) ⇒ Object
– Metrics –
64 65 66 67 |
# File 'lib/vanity/adapters/mongodb_adapter.rb', line 64 def get_metric_last_update_at(metric) record = @metrics.find_one(:_id=>metric) record && record["last_update_at"] end |
#is_experiment_completed?(experiment) ⇒ Boolean
108 109 110 |
# File 'lib/vanity/adapters/mongodb_adapter.rb', line 108 def is_experiment_completed?(experiment) !!@experiments.find_one(:_id=>experiment, :completed_at=>{ "$exists"=>true }) end |
#metric_track(metric, timestamp, identity, values) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/vanity/adapters/mongodb_adapter.rb', line 69 def metric_track(metric, , identity, values) inc = {} values.each_with_index do |v,i| inc["data.#{.to_date}.#{i}"] = v end @metrics.update({ :_id=>metric }, { "$inc"=>inc, "$set"=>{ :last_update_at=>Time.now } }, :upsert=>true) end |
#metric_values(metric, from, to) ⇒ Object
77 78 79 80 81 |
# File 'lib/vanity/adapters/mongodb_adapter.rb', line 77 def metric_values(metric, from, to) record = @metrics.find_one(:_id=>metric) data = record && record["data"] || {} (from.to_date..to.to_date).map { |date| (data[date.to_s] || {}).values } end |
#reconnect! ⇒ Object
35 36 37 38 |
# File 'lib/vanity/adapters/mongodb_adapter.rb', line 35 def reconnect! disconnect! connect! end |
#set_experiment_completed_at(experiment, time) ⇒ Object
99 100 101 |
# File 'lib/vanity/adapters/mongodb_adapter.rb', line 99 def set_experiment_completed_at(experiment, time) @experiments.update({ :_id=>experiment }, { "$set"=>{ :completed_at=>time } }, :upsert=>true) end |
#set_experiment_created_at(experiment, time) ⇒ Object
– Experiments –
90 91 92 |
# File 'lib/vanity/adapters/mongodb_adapter.rb', line 90 def set_experiment_created_at(experiment, time) @experiments.insert :_id=>experiment, :created_at=>time end |
#to_s ⇒ Object
50 51 52 53 |
# File 'lib/vanity/adapters/mongodb_adapter.rb', line 50 def to_s userinfo = @options.values_at(:username, :password).join(":") if @options[:username] URI::Generic.build(:scheme=>"mongodb", :userinfo=>userinfo, :host=>@options[:host], :port=>@options[:port], :path=>"/#{@options[:database]}").to_s end |