debian-mirror-gitlab/app/models/experiment.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

78 lines
2.8 KiB
Ruby
Raw Normal View History

2020-10-24 23:57:45 +05:30
# frozen_string_literal: true
class Experiment < ApplicationRecord
has_many :experiment_users
2021-02-22 17:27:13 +05:30
has_many :experiment_subjects, inverse_of: :experiment
2020-10-24 23:57:45 +05:30
validates :name, presence: true, uniqueness: true, length: { maximum: 255 }
2021-02-22 17:27:13 +05:30
def self.add_user(name, group_type, user, context = {})
2022-03-02 08:16:31 +05:30
by_name(name).record_user_and_group(user, group_type, context)
2021-02-22 17:27:13 +05:30
end
2020-10-24 23:57:45 +05:30
2021-03-11 19:13:27 +05:30
def self.add_group(name, variant:, group:)
2021-09-04 01:27:46 +05:30
add_subject(name, variant: variant, subject: group)
end
def self.add_subject(name, variant:, subject:)
2022-03-02 08:16:31 +05:30
by_name(name).record_subject_and_variant!(subject, variant)
2021-03-11 19:13:27 +05:30
end
2021-04-17 20:07:23 +05:30
def self.record_conversion_event(name, user, context = {})
2022-03-02 08:16:31 +05:30
by_name(name).record_conversion_event_for_user(user, context)
end
def self.by_name(name)
find_or_create_by!(name: name)
2020-10-24 23:57:45 +05:30
end
2021-01-29 00:20:46 +05:30
# Create or update the recorded experiment_user row for the user in this experiment.
2021-02-22 17:27:13 +05:30
def record_user_and_group(user, group_type, context = {})
2021-03-08 18:12:59 +05:30
experiment_user = experiment_users.find_or_initialize_by(user: user)
2021-04-29 21:17:54 +05:30
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
2021-02-22 17:27:13 +05:30
end
2021-04-17 20:07:23 +05:30
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-10-24 23:57:45 +05:30
end
2021-03-11 19:13:27 +05:30
2022-03-02 08:16:31 +05:30
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
2021-09-04 01:27:46 +05:30
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)
2021-04-29 21:17:54 +05:30
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-03-11 19:13:27 +05:30
end
2021-04-17 20:07:23 +05:30
private
2022-03-02 08:16:31 +05:30
def merged_context(experiment_subject, new_context)
experiment_subject.context.deep_merge(new_context.deep_stringify_keys)
2021-04-17 20:07:23 +05:30
end
2020-10-24 23:57:45 +05:30
end