finish up pruning for stats

This commit is contained in:
Kyle Drake 2015-05-02 14:34:21 -07:00
parent 918231ea0f
commit 704011a1c0
6 changed files with 61 additions and 35 deletions

View file

@ -2,10 +2,14 @@ require 'geoip'
class StatLocation < Sequel::Model
GEOCITY_PATH = './files/GeoLiteCity.dat'
RETAINMENT_PERIOD = 7.days
RETAINMENT_DAYS = 7
many_to_one :site
def self.prune!
where{created_at < (RETAINMENT_DAYS-2).days.ago.to_date}.delete
end
def self.create_or_get(site_id, ip)
geoip = GeoIP.new GEOCITY_PATH
city = geoip.city ip
@ -13,7 +17,7 @@ class StatLocation < Sequel::Model
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
stat_location = where(opts).where{created_at > RETAINMENT_DAYS.days.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?