Created
February 18, 2010 20:40
-
-
Save codesnik/308035 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Вынес из content_object без изменений. переписвать страшновато | |
module Rateable | |
def self.included(base) | |
base.extend ClassMethods | |
base.after_create :increment_user_rating | |
end | |
# рейтинг объектов | |
def rate(user, value) | |
Rating.create(:ratable => self, :rated => value, :user => user) | |
counter_symbol = value < 0 ? :decremented_rating : :incremented_rating | |
self.class.update_counters(self.id, counter_symbol => value.abs) | |
self.reload | |
end | |
# пересчет рейтинга | |
def recalculate_rating | |
self.class.update_counters(self.id, :rating => read_attribute(:incremented_rating) - read_attribute(:decremented_rating) - read_attribute(:rating)) | |
self.reload | |
read_attribute(:rating) | |
end | |
def increment_user_rating | |
return unless user | |
User.update_counters(user.id, :rating => RatingCoefficient.coefficient('user_create_object')) | |
User.update_counters(user.id, :objects_count => 1) | |
end | |
module ClassMethods | |
LogPath = (Rails.env == 'development' ? Rails.root.to_s+'/' : "/mnt/storage/logs/") + Conf.project+"_2_access.log" | |
Counted = %w(places events news articles topics contests) | |
def update_ratings_from_log | |
file = File.open(LogPath) | |
line = file.readline | |
hits = {} | |
begin | |
while line | |
data = line.match(/\"GET \/([^\/]+)(\/[^\/]+)?\/(\d+)-.* HTTP/) | |
if data | |
type = data[2] || data[1] | |
id = data[3].to_i | |
if Counted.include?(type) | |
hits[id] = 0 if !hits[id] | |
hits[id]+=1 | |
end | |
end | |
line = file.readline | |
end | |
rescue EOFError | |
end | |
# посчитаем номер колонки, с которой мы сейчас будем работать | |
tn = Time.now | |
colnum = tn.wday*48+tn.hour*2+(tn.min > 30 ? 1 : 0) | |
day_ago_colnum = (colnum - 48) % 336 | |
Fact.connection.execute("update facts inner join facts_hits on facts_hits.fact_id = facts.id set facts.last_day_hits=facts.last_day_hits-facts_hits.hits_#{day_ago_colnum} where facts_hits.hits_#{day_ago_colnum} != 0") | |
Fact.connection.execute("update facts inner join facts_hits on facts_hits.fact_id = facts.id set facts.last_week_hits=facts.last_week_hits-facts_hits.hits_#{colnum} where facts_hits.hits_#{colnum} != 0") | |
FactsHit.update_all("hits_#{colnum} = 0") | |
hits.each{|k, v| | |
Fact.update_all("last_day_hits=last_day_hits+#{v}, last_week_hits=last_week_hits+#{v}, hits=hits+#{v}", "id = #{k}") | |
fh = FactsHit.find_by_fact_id k | |
fh = FactsHit.new(:fact_id=>k) unless fh | |
fh.send("hits_#{colnum}=", v) | |
fh.save | |
} | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment