Class: DecisionAgent::ABTesting::Storage::ActiveRecordAdapter
- Defined in:
- lib/decision_agent/ab_testing/storage/activerecord_adapter.rb
Overview
ActiveRecord storage adapter for A/B tests Requires Rails models: ABTestModel, ABTestAssignmentModel
Class Method Summary collapse
-
.available? ⇒ Boolean
Check if ActiveRecord models are available.
Instance Method Summary collapse
- #delete_test(test_id) ⇒ Object
- #get_assignments(test_id) ⇒ Object
- #get_test(test_id) ⇒ Object
-
#initialize ⇒ ActiveRecordAdapter
constructor
A new instance of ActiveRecordAdapter.
- #list_tests(status: nil, limit: nil) ⇒ Object
- #save_assignment(assignment) ⇒ Object
- #save_test(test) ⇒ Object
- #update_assignment(assignment_id, attributes) ⇒ Object
- #update_test(test_id, attributes) ⇒ Object
Constructor Details
#initialize ⇒ ActiveRecordAdapter
Returns a new instance of ActiveRecordAdapter.
16 17 18 19 |
# File 'lib/decision_agent/ab_testing/storage/activerecord_adapter.rb', line 16 def initialize super raise "ActiveRecord models not available. Run the generator to create them." unless self.class.available? end |
Class Method Details
.available? ⇒ Boolean
Check if ActiveRecord models are available
12 13 14 |
# File 'lib/decision_agent/ab_testing/storage/activerecord_adapter.rb', line 12 def self.available? defined?(::ABTestModel) && defined?(::ABTestAssignmentModel) end |
Instance Method Details
#delete_test(test_id) ⇒ Object
82 83 84 85 86 |
# File 'lib/decision_agent/ab_testing/storage/activerecord_adapter.rb', line 82 def delete_test(test_id) record = ::ABTestModel.find(test_id) ::ABTestAssignmentModel.where(ab_test_id: test_id).delete_all record.destroy end |
#get_assignments(test_id) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/decision_agent/ab_testing/storage/activerecord_adapter.rb', line 75 def get_assignments(test_id) ::ABTestAssignmentModel .where(ab_test_id: test_id) .order(timestamp: :desc) .map { |record| to_assignment(record) } end |
#get_test(test_id) ⇒ Object
35 36 37 38 |
# File 'lib/decision_agent/ab_testing/storage/activerecord_adapter.rb', line 35 def get_test(test_id) record = ::ABTestModel.find_by(id: test_id) record ? to_ab_test(record) : nil end |
#list_tests(status: nil, limit: nil) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/decision_agent/ab_testing/storage/activerecord_adapter.rb', line 46 def list_tests(status: nil, limit: nil) query = ::ABTestModel.order(created_at: :desc) query = query.where(status: status) if status query = query.limit(limit) if limit query.map { |record| to_ab_test(record) } end |
#save_assignment(assignment) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/decision_agent/ab_testing/storage/activerecord_adapter.rb', line 54 def save_assignment(assignment) record = ::ABTestAssignmentModel.create!( ab_test_id: assignment.ab_test_id, user_id: assignment.user_id, variant: assignment.variant.to_s, version_id: assignment.version_id, decision_result: assignment.decision_result, confidence: assignment.confidence, context: assignment.context, timestamp: assignment. ) to_assignment(record) end |
#save_test(test) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/decision_agent/ab_testing/storage/activerecord_adapter.rb', line 21 def save_test(test) record = ::ABTestModel.create!( name: test.name, champion_version_id: test.champion_version_id, challenger_version_id: test.challenger_version_id, traffic_split: test.traffic_split, start_date: test.start_date, end_date: test.end_date, status: test.status ) to_ab_test(record) end |
#update_assignment(assignment_id, attributes) ⇒ Object
69 70 71 72 73 |
# File 'lib/decision_agent/ab_testing/storage/activerecord_adapter.rb', line 69 def update_assignment(assignment_id, attributes) record = ::ABTestAssignmentModel.find(assignment_id) record.update!(attributes) to_assignment(record) end |
#update_test(test_id, attributes) ⇒ Object
40 41 42 43 44 |
# File 'lib/decision_agent/ab_testing/storage/activerecord_adapter.rb', line 40 def update_test(test_id, attributes) record = ::ABTestModel.find(test_id) record.update!(attributes) to_ab_test(record) end |