stat referrers, locations, and paths

This commit is contained in:
Kyle Drake 2015-05-02 02:34:24 -07:00
parent d9babf5cd6
commit 227b123fc9
10 changed files with 195 additions and 47 deletions

View file

@ -161,6 +161,11 @@ class Site < Sequel::Model
one_to_many :site_files
one_to_many :stats
one_to_many :stat_referrers
one_to_many :stat_locations
one_to_many :stat_paths
def account_sites_dataset
Site.where(Sequel.|({id: owner.id}, {parent_site_id: owner.id})).order(:parent_site_id.desc, :username)
end

View file

@ -1,6 +1,6 @@
class Stat < Sequel::Model
GEOCITY_PATH = './files/GeoLiteCity.dat'
FREE_RETAINMENT_DAYS = 7
REFERRAL_RETAINMENT_DAYS = 7
many_to_one :site
one_to_many :stat_referrers
@ -9,13 +9,23 @@ class Stat < Sequel::Model
class << self
def prune!
DB[
"DELETE FROM stats WHERE created_at < ? AND site_id NOT IN (SELECT id FROM sites WHERE plan_type IS NOT NULL OR plan_type != 'free')",
(FREE_RETAINMENT_DAYS-1).days.ago.to_date.to_s
].first
supporter_site_ids = DB["select id from sites where plan_type is not null or plan_type != 'free'"].all.collect {|s| s[:id]}
binding.pry
delete_stats_dataset = where{created_at < (FREE_RETAINMENT_DAYS-1).days.ago.to_date.to_s}.exclude(site_id: supporter_site_ids)
deleted_stat_ids = delete_stats_dataset.select(:id).all.collect {|s| s.id}
delete_stats_dataset.delete
puts "TODO: stat_referrers/paths/locations needs created_at timestamp for pruning."
StatReferrer.where(stat_id: deleted_stat_ids).delete
#DB[
# "DELETE FROM stats WHERE created_at < ? AND site_id NOT IN (SELECT id FROM sites WHERE plan_type IS NOT NULL OR plan_type != 'free')",
# (FREE_RETAINMENT_DAYS-1).days.ago.to_date.to_s
#].first
#binding.pry
end
def parse_logfiles(path)
@ -31,6 +41,7 @@ class Stat < Sequel::Model
site_logs[username] = {
hits: 0,
views: 0,
bandwidth: 0,
view_ips: [],
ips: [],
referrers: {},
@ -38,6 +49,7 @@ class Stat < Sequel::Model
} unless site_logs[username]
site_logs[username][:hits] += 1
site_logs[username][:bandwidth] += size.to_i
unless site_logs[username][:view_ips].include?(ip)
site_logs[username][:views] += 1
@ -76,19 +88,28 @@ class Stat < Sequel::Model
DB[:stats].lock('EXCLUSIVE') { stat = Stat.create opts } if stat.nil?
DB[
'update stats set hits=hits+?, views=views+? where site_id=?',
'update stats set hits=hits+?, views=views+?, bandwidth=bandwidth+? where site_id=?',
site_log[:hits],
site_log[:views],
site_log[:bandwidth],
site_log[:id]
].first
site_log[:referrers].each do |referrer,views|
opts = {stat_id: stat.id, url: referrer}
stat_referrer = StatReferrer.select(:id).where(opts).first
DB[:stat_referrers].lock('EXCLUSIVE') {
stat_referrer = StatReferrer.create opts
} if stat_referrer.nil?
DB['update stat_referrers set views=views+? where stat_id=?', views, stat.id].first
site_log[:referrers].each do |referrer, views|
stat_referrer = StatReferrer.create_or_get site_log[:id], referrer
DB['update stat_referrers set views=views+? where site_id=?', views, site_log[:id]].first
end
site_log[:view_ips].each do |ip|
site_location = StatLocation.create_or_get site_log[:id], ip
next if site_location.nil?
DB['update stat_locations set views=views+1 where id=?', site_location.id].first
end
site_log[:paths].each do |path, views|
site_path = StatPath.create_or_get site_log[:id], path
next if site_path.nil?
DB['update stat_paths set views=views+? where id=?', views, site_path.id].first
end
end
end

View file

@ -1,3 +1,23 @@
require 'geoip'
class StatLocation < Sequel::Model
many_to_one :stat
GEOCITY_PATH = './files/GeoLiteCity.dat'
RETAINMENT_PERIOD = 7.days
many_to_one :site
def self.create_or_get(site_id, ip)
geoip = GeoIP.new GEOCITY_PATH
city = geoip.city ip
return nil if city.nil?
opts = {site_id: site_id, country_code2: city.country_code2, region_name: city.region_name, city_name: city.city_name}
stat_location = where(opts).where{created_at > RETAINMENT_PERIOD.ago}.first
DB[table_name].lock('EXCLUSIVE') {
stat_location = create opts.merge(latitude: city.latitude, longitude: city.longitude, created_at: Date.today)
} if stat_location.nil?
stat_location
end
end

View file

@ -1,3 +1,15 @@
class StatPath < Sequel::Model
many_to_one :stat
RETAINMENT_PERIOD = 7.days
many_to_one :site
def self.create_or_get(site_id, name)
opts = {site_id: site_id, name: name}
stat_path = where(opts).where{created_at > RETAINMENT_PERIOD.ago}.first
DB[table_name].lock('EXCLUSIVE') {
stat_path = create opts.merge created_at: Date.today
} if stat_path.nil?
stat_path
end
end

View file

@ -1,3 +1,15 @@
class StatReferrer < Sequel::Model
many_to_one :stat
many_to_one :site
RETAINMENT_PERIOD = 7.days
def self.create_or_get(site_id, url)
opts = {site_id: site_id, url: url}
stat_referrer = where(opts).where{created_at > RETAINMENT_PERIOD.ago}.first
DB[table_name].lock('EXCLUSIVE') {
stat_referrer = create opts.merge(created_at: Date.today)
} if stat_referrer.nil?
stat_referrer
end
end