2020-08-10 17:09:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Experiment < ApplicationRecord
|
|
|
|
has_many :experiment_users
|
2020-12-09 07:09:42 -05:00
|
|
|
has_many :experiment_subjects, inverse_of: :experiment
|
2020-08-10 17:09:44 -04:00
|
|
|
|
|
|
|
validates :name, presence: true, uniqueness: true, length: { maximum: 255 }
|
|
|
|
|
2020-12-03 16:09:35 -05:00
|
|
|
def self.add_user(name, group_type, user, context = {})
|
|
|
|
find_or_create_by!(name: name).record_user_and_group(user, group_type, context)
|
2020-11-25 19:09:35 -05:00
|
|
|
end
|
2020-08-10 17:09:44 -04:00
|
|
|
|
2021-01-29 10:09:40 -05:00
|
|
|
def self.add_group(name, variant:, group:)
|
2021-05-26 11:10:57 -04:00
|
|
|
add_subject(name, variant: variant, subject: group)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.add_subject(name, variant:, subject:)
|
|
|
|
find_or_create_by!(name: name).record_subject_and_variant!(subject, variant)
|
2021-01-29 10:09:40 -05:00
|
|
|
end
|
|
|
|
|
2021-03-09 16:09:15 -05:00
|
|
|
def self.record_conversion_event(name, user, context = {})
|
|
|
|
find_or_create_by!(name: name).record_conversion_event_for_user(user, context)
|
2020-08-10 17:09:44 -04:00
|
|
|
end
|
|
|
|
|
2020-11-17 07:09:15 -05:00
|
|
|
# Create or update the recorded experiment_user row for the user in this experiment.
|
2020-12-03 16:09:35 -05:00
|
|
|
def record_user_and_group(user, group_type, context = {})
|
2020-12-31 13:10:25 -05:00
|
|
|
experiment_user = experiment_users.find_or_initialize_by(user: user)
|
2021-03-17 14:09:01 -04:00
|
|
|
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
|
2020-08-10 17:09:44 -04:00
|
|
|
end
|
2020-11-25 19:09:35 -05:00
|
|
|
|
2021-03-09 16:09:15 -05:00
|
|
|
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))
|
2020-11-25 19:09:35 -05:00
|
|
|
end
|
2021-01-29 10:09:40 -05:00
|
|
|
|
2021-05-26 11:10:57 -04:00
|
|
|
def record_subject_and_variant!(subject, variant)
|
2021-06-03 02:10:07 -04:00
|
|
|
raise 'Incompatible subject provided!' unless ExperimentSubject.valid_subject?(subject)
|
2021-05-26 11:10:57 -04:00
|
|
|
|
2021-06-03 02:10:07 -04:00
|
|
|
attr_name = subject.class.table_name.singularize.to_sym
|
|
|
|
experiment_subject = experiment_subjects.find_or_initialize_by(attr_name => subject)
|
2021-03-17 14:09:01 -04:00
|
|
|
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
|
2021-01-29 10:09:40 -05:00
|
|
|
end
|
2021-03-09 16:09:15 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def merged_context(experiment_user, new_context)
|
|
|
|
experiment_user.context.deep_merge(new_context.deep_stringify_keys)
|
|
|
|
end
|
2020-08-10 17:09:44 -04:00
|
|
|
end
|