2013-02-05 13:16:51 -06:00
|
|
|
class UserVisit < ActiveRecord::Base
|
2013-02-27 21:39:42 -06:00
|
|
|
|
2015-07-07 11:31:07 -05:00
|
|
|
def self.counts_by_day_query(start_date, end_date)
|
|
|
|
where('visited_at >= ? and visited_at <= ?', start_date.to_date, end_date.to_date).group(:visited_at).order(:visited_at)
|
|
|
|
end
|
|
|
|
|
|
|
|
# A count of visits in a date range by day
|
2014-11-04 16:08:39 -06:00
|
|
|
def self.by_day(start_date, end_date)
|
2015-07-07 11:31:07 -05:00
|
|
|
counts_by_day_query(start_date, end_date).count
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.mobile_by_day(start_date, end_date)
|
|
|
|
counts_by_day_query(start_date, end_date).where(mobile: true).count
|
2013-02-27 21:39:42 -06:00
|
|
|
end
|
2013-04-05 01:43:48 -05:00
|
|
|
|
|
|
|
def self.ensure_consistency!
|
|
|
|
exec_sql <<SQL
|
2013-10-03 22:28:49 -05:00
|
|
|
UPDATE user_stats u set days_visited =
|
2013-04-05 01:43:48 -05:00
|
|
|
(
|
2013-10-03 22:28:49 -05:00
|
|
|
SELECT COUNT(*) FROM user_visits v WHERE v.user_id = u.user_id
|
2013-04-05 01:43:48 -05:00
|
|
|
)
|
|
|
|
WHERE days_visited <>
|
|
|
|
(
|
2013-10-03 22:28:49 -05:00
|
|
|
SELECT COUNT(*) FROM user_visits v WHERE v.user_id = u.user_id
|
2013-04-05 01:43:48 -05:00
|
|
|
)
|
|
|
|
SQL
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
2013-05-23 21:48:32 -05:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: user_visits
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# user_id :integer not null
|
|
|
|
# visited_at :date not null
|
2014-02-06 18:07:36 -06:00
|
|
|
# posts_read :integer default(0)
|
2015-07-07 11:31:07 -05:00
|
|
|
# mobile :boolean default(FALSE)
|
2013-05-23 21:48:32 -05:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_user_visits_on_user_id_and_visited_at (user_id,visited_at) UNIQUE
|
|
|
|
#
|