track visits from parsing custom nginx log file

This commit is contained in:
Kyle Drake 2013-06-21 01:21:53 -07:00
parent 0bf9ccc746
commit d128afa01c
2 changed files with 22 additions and 1 deletions

View file

@ -13,3 +13,24 @@ Rake::TestTask.new do |t|
end end
task :default => :test task :default => :test
desc "parse logs"
task :parse_logs => [:environment] do
hits = {}
logfile = File.open '/var/log/nginx/neocities-sites.log.1', 'r'
while hit = logfile.gets
hit = hit.split ' '
# It says hits, but really we're tracking visits to index"
if hit[3] == '/'
hits[hit[1]] ||= 0
hits[hit[1]] += 1
end
end
logfile.close
hits.each do |username,hitcount|
DB['update sites set hits=hits+? where username=?', hitcount, username].first
end
end

View file

@ -13,7 +13,7 @@
.span3 .span3
h3 style="margin-left: 20px": a href="http://#{site.username}.neocities.org" target="_blank" #{site.username} h3 style="margin-left: 20px": a href="http://#{site.username}.neocities.org" target="_blank" #{site.username}
.span3.text-right .span3.text-right
p style="margin-right: 20px; margin-top: 10px" #{site.hits} hits<br>last updated #{site.updated_at.ago} p style="margin-right: 20px; margin-top: 10px" #{site.hits} visits<br>last updated #{site.updated_at.ago}
a href="http://#{site.username}.neocities.org" target="_blank": img src="/site_screenshots/#{ENV['RACK_ENV'] == 'development' ? 'victoria' : site.username}.jpg" a href="http://#{site.username}.neocities.org" target="_blank": img src="/site_screenshots/#{ENV['RACK_ENV'] == 'development' ? 'victoria' : site.username}.jpg"
- if @page_count > 1 - if @page_count > 1