Class: Experiment

Inherits:
ApplicationRecord show all
Defined in:
app/models/experiment.rb

Constant Summary

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from SensitiveSerializableHash

#serializable_hash

Class Method Details

.add_group(name, variant:, group:) ⇒ Object


13
14
15
# File 'app/models/experiment.rb', line 13

def self.add_group(name, variant:, group:)
  add_subject(name, variant: variant, subject: group)
end

.add_subject(name, variant:, subject:) ⇒ Object


17
18
19
# File 'app/models/experiment.rb', line 17

def self.add_subject(name, variant:, subject:)
  by_name(name).record_subject_and_variant!(subject, variant)
end

.add_user(name, group_type, user, context = {}) ⇒ Object


9
10
11
# File 'app/models/experiment.rb', line 9

def self.add_user(name, group_type, user, context = {})
  by_name(name).record_user_and_group(user, group_type, context)
end

.by_name(name) ⇒ Object


25
26
27
# File 'app/models/experiment.rb', line 25

def self.by_name(name)
  find_or_create_by!(name: name)
end

.record_conversion_event(name, user, context = {}) ⇒ Object


21
22
23
# File 'app/models/experiment.rb', line 21

def self.record_conversion_event(name, user, context = {})
  by_name(name).record_conversion_event_for_user(user, context)
end

Instance Method Details

#record_conversion_event_for_subject(subject, context = {}) ⇒ Object


48
49
50
51
52
53
54
55
56
# File 'app/models/experiment.rb', line 48

def record_conversion_event_for_subject(subject, context = {})
  raise 'Incompatible subject provided!' unless ExperimentSubject.valid_subject?(subject)

  attr_name = subject.class.table_name.singularize.to_sym
  experiment_subject = experiment_subjects.find_by(attr_name => subject)
  return unless experiment_subject

  experiment_subject.update!(converted_at: Time.current, context: merged_context(experiment_subject, context))
end

#record_conversion_event_for_user(user, context = {}) ⇒ Object


41
42
43
44
45
46
# File 'app/models/experiment.rb', line 41

def record_conversion_event_for_user(user, context = {})
  experiment_user = experiment_users.find_by(user: user)
  return unless experiment_user

  experiment_user.update!(converted_at: Time.current, context: merged_context(experiment_user, context))
end

#record_subject_and_variant!(subject, variant) ⇒ Object


58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/models/experiment.rb', line 58

def record_subject_and_variant!(subject, variant)
  raise 'Incompatible subject provided!' unless ExperimentSubject.valid_subject?(subject)

  attr_name = subject.class.table_name.singularize.to_sym
  experiment_subject = experiment_subjects.find_or_initialize_by(attr_name => subject)
  experiment_subject.assign_attributes(variant: variant)
  # We only call save when necessary because this causes the request to stick to the primary DB
  # even when the save is a no-op
  # https://gitlab.com/gitlab-org/gitlab/-/issues/324649
  experiment_subject.save! if experiment_subject.changed?

  experiment_subject
end

#record_user_and_group(user, group_type, context = {}) ⇒ Object

Create or update the recorded experiment_user row for the user in this experiment.


30
31
32
33
34
35
36
37
38
39
# File 'app/models/experiment.rb', line 30

def record_user_and_group(user, group_type, context = {})
  experiment_user = experiment_users.find_or_initialize_by(user: user)
  experiment_user.assign_attributes(group_type: group_type, context: merged_context(experiment_user, context))
  # We only call save when necessary because this causes the request to stick to the primary DB
  # even when the save is a no-op
  # https://gitlab.com/gitlab-org/gitlab/-/issues/324649
  experiment_user.save! if experiment_user.changed?

  experiment_user
end