refactor: lightly refactored code for easier reading
This commit refactored some of the variables so they're easier to read Signed-off-by: Miguel Nogueira <me@nogueira.codes>
This commit is contained in:
parent
61d5d26329
commit
f7520372a2
20
main.rb
20
main.rb
|
@ -56,7 +56,7 @@ def get_line_code(line)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Gets the user agent of the given log line
|
# Gets the user agent of the given log line
|
||||||
def get_line_ua(line)
|
def get_line_browser(line)
|
||||||
pattern = /"([^"]*)"$/
|
pattern = /"([^"]*)"$/
|
||||||
user_agent = line.match(pattern)[0] if line.match(pattern)
|
user_agent = line.match(pattern)[0] if line.match(pattern)
|
||||||
|
|
||||||
|
@ -89,7 +89,7 @@ end
|
||||||
|
|
||||||
visit_counter = {}
|
visit_counter = {}
|
||||||
client_errors = {}
|
client_errors = {}
|
||||||
user_agents = {}
|
browsers = {}
|
||||||
all_ips = []
|
all_ips = []
|
||||||
|
|
||||||
lines.each do |line|
|
lines.each do |line|
|
||||||
|
@ -108,7 +108,8 @@ end
|
||||||
lines.each do |line|
|
lines.each do |line|
|
||||||
ip = get_line_ip(line).to_s
|
ip = get_line_ip(line).to_s
|
||||||
code = get_line_code(line)
|
code = get_line_code(line)
|
||||||
ua = get_line_ua(line)
|
browser = get_line_browser(line)
|
||||||
|
# refactored from "ua" to "browser"
|
||||||
|
|
||||||
if is_client_err?(code.to_i)
|
if is_client_err?(code.to_i)
|
||||||
if client_errors[ip]
|
if client_errors[ip]
|
||||||
|
@ -118,17 +119,18 @@ lines.each do |line|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if user_agents[ua]
|
# refactored from: "user_agents[ua]" to "browsers[browser]"
|
||||||
user_agents[ua] += 1
|
if browsers[browser]
|
||||||
|
browsers[browser] += 1
|
||||||
else
|
else
|
||||||
user_agents[ua] = 1
|
browsers[browser] = 1
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
top_user_agents = user_agents.sort_by { |_ua, count| -count }.first(5)
|
top_browsers = browsers.sort_by { |_ua, count| -count }.first(5)
|
||||||
puts 'Top 5 User-Agents contacting the site:'
|
puts 'Top 5 browsers contacting the site:'
|
||||||
top_user_agents.each do |ua, count|
|
top_browsers.each do |ua, count|
|
||||||
puts "#{ua}: #{count} visits"
|
puts "#{ua}: #{count} visits"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue