From 9affb83a2cc118fa6a5b8cc767c7cae264bb37ad Mon Sep 17 00:00:00 2001 From: Kyle Drake Date: Sat, 9 May 2015 00:48:23 -0700 Subject: [PATCH] Fix stats for paths with spaces in them --- models/stat.rb | 15 ++++++++++++++- tests/stat_tests.rb | 12 ++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/models/stat.rb b/models/stat.rb index 45a9d79b..9a05fdf9 100644 --- a/models/stat.rb +++ b/models/stat.rb @@ -20,7 +20,20 @@ class Stat < Sequel::Model logfile = File.open log_path, 'r' while hit = logfile.gets - time, username, size, path, ip, referrer = hit.split ' ' + hit_array = hit.split ' ' + + # 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 + end next if !referrer.nil? && referrer.match(/bot/i) diff --git a/tests/stat_tests.rb b/tests/stat_tests.rb index 8b85021e..5199db5d 100644 --- a/tests/stat_tests.rb +++ b/tests/stat_tests.rb @@ -27,6 +27,18 @@ describe 'stats' do end end + it 'deals with spaces in paths' do + @site = Fabricate :site + 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" + end + + Stat.parse_logfiles STAT_LOGS_PATH + @site.stats.first.bandwidth.must_equal 612917 + @site.stat_referrers.first.url.must_equal 'http://derp.com' + @site.stat_locations.first.city_name.must_equal 'Menlo Park' + end + it 'prunes logs for free sites' do @free_site = Fabricate :site @supporter_site = Fabricate :site, plan_type: 'supporter'