2015-09-13 23:41:22 -04:00
|
|
|
class UserProfileView < ActiveRecord::Base
|
2015-09-14 03:51:17 -04:00
|
|
|
validates_presence_of :user_profile_id, :ip_address, :viewed_at
|
|
|
|
|
|
|
|
belongs_to :user_profile
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def self.add(user_profile_id, ip, user_id = nil, at = nil, skip_redis = false)
|
2015-09-14 03:51:17 -04:00
|
|
|
at ||= Time.zone.now
|
|
|
|
redis_key = "user-profile-view:#{user_profile_id}:#{at.to_date}"
|
|
|
|
if user_id
|
2016-08-23 04:25:31 -04:00
|
|
|
return if user_id < 1
|
2015-09-14 03:51:17 -04:00
|
|
|
redis_key << ":user-#{user_id}"
|
|
|
|
else
|
|
|
|
redis_key << ":ip-#{ip}"
|
|
|
|
end
|
|
|
|
|
|
|
|
if skip_redis || $redis.setnx(redis_key, '1')
|
|
|
|
skip_redis || $redis.expire(redis_key, SiteSetting.user_profile_view_duration_hours.hours)
|
|
|
|
|
|
|
|
self.transaction do
|
|
|
|
sql = "INSERT INTO user_profile_views (user_profile_id, ip_address, viewed_at, user_id)
|
|
|
|
SELECT :user_profile_id, :ip_address, :viewed_at, :user_id
|
|
|
|
WHERE NOT EXISTS (
|
|
|
|
SELECT 1 FROM user_profile_views
|
|
|
|
/*where*/
|
|
|
|
)"
|
|
|
|
|
|
|
|
builder = SqlBuilder.new(sql)
|
|
|
|
|
|
|
|
if !user_id
|
|
|
|
builder.where("viewed_at = :viewed_at AND ip_address = :ip_address AND user_profile_id = :user_profile_id AND user_id IS NULL")
|
|
|
|
else
|
|
|
|
builder.where("viewed_at = :viewed_at AND user_id = :user_id AND user_profile_id = :user_profile_id")
|
|
|
|
end
|
|
|
|
|
|
|
|
result = builder.exec(user_profile_id: user_profile_id, ip_address: ip, viewed_at: at, user_id: user_id)
|
|
|
|
|
|
|
|
if result.cmd_tuples > 0
|
|
|
|
UserProfile.find(user_profile_id).increment!(:views)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-09-14 13:30:06 -04:00
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def self.profile_views_by_day(start_date, end_date, group_id = nil)
|
2015-09-14 13:30:06 -04:00
|
|
|
profile_views = self.where("viewed_at >= ? AND viewed_at < ?", start_date, end_date + 1.day)
|
2016-02-02 21:29:51 -05:00
|
|
|
if group_id
|
|
|
|
profile_views = profile_views.joins("INNER JOIN users ON users.id = user_profile_views.user_id")
|
|
|
|
profile_views = profile_views.joins("INNER JOIN group_users ON group_users.user_id = users.id")
|
|
|
|
profile_views = profile_views.where("group_users.group_id = ?", group_id)
|
|
|
|
end
|
2015-09-14 13:30:06 -04:00
|
|
|
profile_views.group("date(viewed_at)").order("date(viewed_at)").count
|
|
|
|
end
|
2015-09-13 23:41:22 -04:00
|
|
|
end
|
2015-11-09 08:52:04 -05:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: user_profile_views
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# user_profile_id :integer not null
|
|
|
|
# viewed_at :datetime not null
|
|
|
|
# ip_address :inet not null
|
|
|
|
# user_id :integer
|
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_user_profile_views_on_user_id (user_id)
|
|
|
|
# index_user_profile_views_on_user_profile_id (user_profile_id)
|
|
|
|
# unique_profile_view_ip (viewed_at,ip_address,user_profile_id) UNIQUE
|
|
|
|
# unique_profile_view_user (viewed_at,user_id,user_profile_id) UNIQUE
|
|
|
|
#
|