New stats needs to escape gets newline

This commit is contained in:
Kyle Drake 2015-05-09 16:52:22 -07:00
parent 9affb83a2c
commit c5d62b19a3
2 changed files with 23 additions and 22 deletions

View file

@ -20,20 +20,11 @@ class Stat < Sequel::Model
logfile = File.open log_path, 'r' logfile = File.open log_path, 'r'
while hit = logfile.gets while hit = logfile.gets
hit_array = hit.split ' ' hit_array = hit.strip.split "\t"
raise ArgumentError, hit.inspect if hit_array.length > 6
# If > 6, then the path has a space in it, combine.
if hit_array.length > 6
time = hit_array[0]
username = hit_array[1]
size = hit_array[2]
path_end_length = 3 + (hit_array.length - 6)
path = hit_array[3..path_end_length].join ' '
ip = hit_array[path_end_length+1]
referrer = hit_array[path_end_length+2]
else
time, username, size, path, ip, referrer = hit_array time, username, size, path, ip, referrer = hit_array
end
next if !referrer.nil? && referrer.match(/bot/i) next if !referrer.nil? && referrer.match(/bot/i)

View file

@ -13,13 +13,13 @@ describe 'stats' do
@time_iso8601 = @time.iso8601 @time_iso8601 = @time.iso8601
log = [ log = [
"#{@time_iso8601} #{@site_one.username} 5000 / 67.180.75.140 http://example.com", "#{@time_iso8601}\t#{@site_one.username}\t5000\t/\t67.180.75.140\thttp://example.com",
"#{@time_iso8601} #{@site_one.username} 5000 / 67.180.75.140 http://example.com", "#{@time_iso8601}\t#{@site_one.username}\t5000\t/\t67.180.75.140\thttp://example.com",
"#{@time_iso8601} #{@site_one.username} 5000 / 172.56.16.152 http://example.com", "#{@time_iso8601}\t#{@site_one.username}\t5000\t/\t172.56.16.152\thttp://example.com",
"#{@time_iso8601} #{@site_one.username} 5000 / 172.56.16.152 -", "#{@time_iso8601}\t#{@site_one.username}\t5000\t/\t172.56.16.152\t-",
"#{@time_iso8601} #{@site_two.username} 5000 / 67.180.75.140 http://example.com", "#{@time_iso8601}\t#{@site_two.username}\t5000\t/\t67.180.75.140\thttp://example.com",
"#{@time_iso8601} #{@site_two.username} 5000 / 127.0.0.1 -", "#{@time_iso8601}\t#{@site_two.username}\t5000\t/\t127.0.0.1\t-",
"#{@time_iso8601} #{@site_two.username} 5000 /derp.html 127.0.0.2 https://example.com" "#{@time_iso8601}\t#{@site_two.username}\t5000\t/derp.html\t127.0.0.2\thttps://example.com"
] ]
File.open("tests/stat_logs/#{SecureRandom.uuid}.log", 'w') do |file| File.open("tests/stat_logs/#{SecureRandom.uuid}.log", 'w') do |file|
@ -30,15 +30,25 @@ describe 'stats' do
it 'deals with spaces in paths' do it 'deals with spaces in paths' do
@site = Fabricate :site @site = Fabricate :site
File.open("tests/stat_logs/#{SecureRandom.uuid}.log", 'w') do |file| File.open("tests/stat_logs/#{SecureRandom.uuid}.log", 'w') do |file|
file.write "2015-05-02T21:16:35+00:00 #{@site.username} 612917 /images/derpie space.png 67.180.75.140 http://derp.com\n" file.write "2015-05-02T21:16:35+00:00\t#{@site.username}\t612917\t/images/derpie space.png\t67.180.75.140\thttp://derp.com\n"
file.write "2015-05-02T21:16:35+00:00\t#{@site.username}\t612917\t/images/derpie space.png\t67.180.75.140\thttp://derp.com\n"
end end
Stat.parse_logfiles STAT_LOGS_PATH Stat.parse_logfiles STAT_LOGS_PATH
@site.stats.first.bandwidth.must_equal 612917 @site.stats.first.bandwidth.must_equal 612917*2
@site.stat_referrers.first.url.must_equal 'http://derp.com' @site.stat_referrers.first.url.must_equal 'http://derp.com'
@site.stat_locations.first.city_name.must_equal 'Menlo Park' @site.stat_locations.first.city_name.must_equal 'Menlo Park'
end end
it 'deals with spaces in referrer' do
@site = Fabricate :site
File.open("tests/stat_logs/#{SecureRandom.uuid}.log", 'w') do |file|
file.write "2015-05-02T21:16:35+00:00\t#{@site.username}\t612917\t/images/derpie space.png\t67.180.75.140\thttp://derp.com?q=what the lump\n"
end
Stat.parse_logfiles STAT_LOGS_PATH
end
it 'prunes logs for free sites' do it 'prunes logs for free sites' do
@free_site = Fabricate :site @free_site = Fabricate :site
@supporter_site = Fabricate :site, plan_type: 'supporter' @supporter_site = Fabricate :site, plan_type: 'supporter'