<commit_before>
module SwellMedia

	class EventService

		def self.log( args={} )

			name = args[:event] || args[:name]
			return false unless name.present?

			if args[:guest_session].present?
				args[:guest_session_attributes] = args[:guest_session].attributes
			elsif not( args[:guest_session_attributes].present? )
				return false
			end

			event = UserEvent.new( name: name.to_s, guest_session_id: args[:guest_session_attributes][:id] )

			event.user_id = args[:user].try( :id )

			parent_obj = args[:on] || args[:parent_obj]

			activity_obj = args[:obj] || args[:activity_obj]

			event.context = args[:context]

			args[:update_caches] = true if args[:update_caches].nil?

			event.parent_controller = args[:parent_controller]
			event.parent_action = args[:parent_action]

			event.traffic_source = args[:guest_session_attributes][:traffic_source]
			event.traffic_campaign = args[:guest_session_attributes][:traffic_campaign]
			event.traffic_medium = args[:guest_session_attributes][:traffic_medium]

			event.traffic_src_user = args[:guest_session_attributes][:traffic_src_user]
			event.content_src_user = args[:guest_session_attributes][:content_src_user]

			event.ui_variant = args[:params][:ui_variant] || args[:guest_session_attributes][:ui_variant]
			event.ui = args[:params][:ui]

			event.value = args[:value]
			event.value_type = args[:value_type]

			event.session_cluster_created_at = Time.at( args.delete(:session_cluster_created_at) ) if args[:session_cluster_created_at].is_a? Integer
			event.session_cluster_created_at ||= args[:session_cluster_created_at]

			event.req_path = event.req_full_path = args[:req_path]
			event.req_path = event.req_full_path.split('?')[0] unless event.req_full_path.blank? || not( event.req_full_path.include?('?') ) #rescue true

			event.http_referrer = args[:guest_session_attributes][:last_http_referrer]

			event.category_id = parent_obj.try( :category_id )

			event.content = args[:content]

			rate = args[:rate] || UserEvent.rates[ event.name.to_sym ] || UserEvent.rates[ :default ]

			event.publish_at = parent_obj.try( :publish_at ) || args[:publish_at] || Time.zone.now unless args[:unpublished]

			# setting owner_type so logging with nill owner doesn't populate owner_type with NilClass
			event.parent_obj_type = parent_obj.nil? ? nil : parent_obj.class.name
			event.parent_obj_id = parent_obj.try( :id )
			event.activity_obj_type = activity_obj.nil? ? nil : activity_obj.class.name
			event.activity_obj_id = activity_obj.try( :id )

			dup_events = UserEvent.where( name: event.name, guest_session_id: event.guest_session_id ).within_last( rate ).order( created_at: :asc )

			if event.activity_obj_id.present?
				dup_events = dup_events.where( activity_obj_id: activity_obj.id, parent_obj_id: activity_obj.class.name )
			elsif event.parent_obj_id.present?
				dup_events = dup_events.by_object( parent_obj )
			elsif event.name == 'page_view'
				dup_events = dup_events.where( parent_controller: event.parent_controller, parent_action: event.parent_action )
			end

			if event.context.present?
				dup_events = dup_events.where( context: event.context )
			end
			

			# DO NOT record if existing events within rate
			if dup_events.count > 0
				if args[:params][:page].present?
					dup_events.last.update( value: args[:params][:page], value_type: 'page_num' )
				end
				return false
			else
				event.save
				count_cache_field = "cached_#{name}_count"

				if parent_obj.present? && parent_obj.respond_to?( count_cache_field ) && args[:update_caches]
					if event.parent_action == 'destroy'
						parent_obj.class.name.constantize.decrement_counter( count_cache_field, parent_obj.id )
					else
						parent_obj.class.name.constantize.increment_counter( count_cache_field, parent_obj.id )
					end<commit_msg>adjust logic slightly to account for the absence of the activity obj<commit_after>
module SwellMedia

	class EventService

		def self.log( args={} )

			name = args[:event] || args[:name]
			return false unless name.present?

			if args[:guest_session].present?
				args[:guest_session_attributes] = args[:guest_session].attributes
			elsif not( args[:guest_session_attributes].present? )
				return false
			end

			event = UserEvent.new( name: name.to_s, guest_session_id: args[:guest_session_attributes][:id] )

			event.user_id = args[:user].try( :id )

			parent_obj = args[:on] || args[:parent_obj]

			activity_obj = args[:obj] || args[:activity_obj]
			activity_obj_id 		= activity_obj.try(:id) 							|| args[:activity_obj_id] 		|| args[:obj_id]
			activity_obj_class	= activity_obj.try(:class).try(:name) || args[:activity_obj_class]	|| args[:obj_class]

			event.context = args[:context]

			args[:update_caches] = true if args[:update_caches].nil?

			event.parent_controller = args[:parent_controller]
			event.parent_action = args[:parent_action]

			event.traffic_source = args[:guest_session_attributes][:traffic_source]
			event.traffic_campaign = args[:guest_session_attributes][:traffic_campaign]
			event.traffic_medium = args[:guest_session_attributes][:traffic_medium]

			event.traffic_src_user = args[:guest_session_attributes][:traffic_src_user]
			event.content_src_user = args[:guest_session_attributes][:content_src_user]

			event.ui_variant = args[:params][:ui_variant] || args[:guest_session_attributes][:ui_variant]
			event.ui = args[:params][:ui]

			event.value = args[:value]
			event.value_type = args[:value_type]

			event.session_cluster_created_at = Time.at( args.delete(:session_cluster_created_at) ) if args[:session_cluster_created_at].is_a? Integer
			event.session_cluster_created_at ||= args[:session_cluster_created_at]

			event.req_path = event.req_full_path = args[:req_path]
			event.req_path = event.req_full_path.split('?')[0] unless event.req_full_path.blank? || not( event.req_full_path.include?('?') ) #rescue true

			event.http_referrer = args[:guest_session_attributes][:last_http_referrer]

			event.category_id = parent_obj.try( :category_id )

			event.content = args[:content]

			rate = args[:rate] || UserEvent.rates[ event.name.to_sym ] || UserEvent.rates[ :default ]

			event.publish_at = parent_obj.try( :publish_at ) || args[:publish_at] || Time.zone.now unless args[:unpublished]

			# setting owner_type so logging with nill owner doesn't populate owner_type with NilClass
			event.parent_obj_type = parent_obj.nil? ? nil : parent_obj.class.name
			event.parent_obj_id = parent_obj.try( :id )
			event.activity_obj_type = activity_obj_class
			event.activity_obj_id = activity_obj_id

			dup_events = UserEvent.where( name: event.name, guest_session_id: event.guest_session_id ).within_last( rate ).order( created_at: :asc )

			if event.activity_obj_id.present?
				dup_events = dup_events.where( activity_obj_id: activity_obj_id, parent_obj_id: activity_obj_class )
			elsif event.parent_obj_id.present?
				dup_events = dup_events.by_object( parent_obj )
			elsif event.name == 'page_view'
				dup_events = dup_events.where( parent_controller: event.parent_controller, parent_action: event.parent_action )
			end

			if event.context.present?
				dup_events = dup_events.where( context: event.context )
			end
			

			# DO NOT record if existing events within rate
			if dup_events.count > 0
				if args[:params][:page].present?
					dup_events.last.update( value: args[:params][:page], value_type: 'page_num' )
				end
				return false
			else
				event.save
				count_cache_field = "cached_#{name}_count"

				if parent_obj.present? && parent_obj.respond_to?( count_cache_field ) && args[:update_caches]
					if event.parent_action == 'destroy'
						parent_obj.class.name.constantize.decrement_counter( count_cache_field, parent_obj.id )
					else
						parent_obj.class.name.constantize.increment_counter( count_cache_field, parent_obj.id )
					end
{'fraction_non_alphanumeric': 0.11618416018365005, 'fraction_numerical': 0.0005101390128810101, 'mean_word_length': 4.900677200902934, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3300419', 'n_tokens_mistral': 2957, 'n_tokens_neox': 2563, 'n_words': 447}