0
我編寫了一個程序來創建IP地址,URL,錯誤代碼和IP訪問頻率的直方圖,並且希望獲取爲每個直方圖收集的數據的大小(以字節爲單位) 。我環顧四周,看到了一些關於bytes
方法,但似乎無法使其發揮作用。顯示數據集中的字節數
任何想法如何做到這一點的代碼?我想在每個方法中顯示文件名後添加「字節放置」行。
class CommonLog
def initialize(logfile)
@logfile = logfile
end
def readfile
@readfile = File.readlines(@logfile).map { |line|
line.split()
}
@readfile = @readfile.to_s.split(" ")
end
def ip_histogram
@ip_count = 0
@readfile.each_index { |index|
if (@readfile[index] =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/)
puts @readfile[index]
puts @ip_count += 1
end
}
puts File.basename @logfile
end
def url_histogram
@url_count = 0
@readfile.each_index { |index|
if (@readfile[index] =~ /\/{1}(([a-z]{4,})|(\~{1}))\:{0}\S+/)
puts @readfile[index]
puts @url_count += 1
end
}
puts File.basename @logfile
end
def requests_per_hour
@time_count2 = 0
@time_count3 = 0
@time_count4 = 0
@time_count5 = 0
@time_count6 = 0
@readfile.each_index { |index|
if (@readfile[index] =~ /\:\d{2}\:/)
@new = @readfile[index].split(":")
if @new[1] == "02"
@time_count2 += 1
elsif @new[1] == "03"
@time_count3 += 1
elsif @new[1] == "04"
@time_count4 += 1
elsif @new[1] == "05"
@time_count5 += 1
elsif @new[1] == "06"
@time_count6 += 1
end
end
}
puts "#{@time_count2} instances during hour 2"
puts "#{@time_count3} instances during hour 3"
puts "#{@time_count4} instances during hour 4"
puts "#{@time_count5} instances during hour 5"
puts "#{@time_count6} instances during hour 6"
puts File.basename @logfile
end
def sorted_list
@codearray = Array.new
@http_code_count = 0
@count200 = 0
@count304 =0
@count301 = 0
@count403 = 0
@readfile.each_index { |index|
if @readfile[index] =~ /([2][0][0]")|([3][0][4])|([3][0][1])|([4][0][3])/
@codearray << @readfile[index]
@http_code_count += 1
if @readfile[index] == '200'
@count200 += 1
elsif @readfile[index] == "304"
@count304 += 1
elsif @readfile[index] == "301"
@count301 += 1
elseif @readfile[index] == "403"
@count403 += 1
end
end
}
@hash_count = 0
@frequencies = Hash.new(0)
@codearray.each { |word| @frequencies[word] += 1 }
@frequencies = @frequencies.sort_by { |a, b| a}
@frequencies.each { |word, frequency| @hash_count += frequency}
@frequencies.each { |key, value|
puts "Error #{key} : #{(value.to_f/@hash_count.to_f)*100}%"
}
puts File.basename @logfile
end
end
my_file = CommonLog.new("test_log")
my_file.readfile
my_file.ip_histogram
my_file.url_histogram
my_file.requests_per_hour
my_file.sorted_list
歡迎堆棧溢出。看看你的代碼,我認爲這是[XY問題](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)。我強烈建議添加一個輸入數據的最小示例以及您希望返回的內容,並使用正則表達式和匹配模式尋求幫助,使用條件邏輯,定義變量和步行數組。這可以寫得更簡單,流程更好。我們很樂意提供幫助,但我們需要更多信息才能正確執行。 –