diff --git a/.idea/ApacheLogStats.iml b/.idea/ApacheLogStats.iml index a39aa12..8201cdb 100644 --- a/.idea/ApacheLogStats.iml +++ b/.idea/ApacheLogStats.iml @@ -10,21 +10,21 @@ - + - - - - - - - - - - - - - - + + + + + + + + + + + + + + \ No newline at end of file diff --git a/main.rb b/main.rb index ff4d13e..9fff73a 100644 --- a/main.rb +++ b/main.rb @@ -44,8 +44,8 @@ def get_line_date(line) pattern = /\[(.*?)\]/ match = line.match(pattern)[0] if line.match(pattern) - # [12/Apr/2023:13:56:41 +0100] -> 12/Apr/2023:13:56:41 +0100 - Date.parse(match.gsub('[', '').gsub(']', '')) + # [12/Apr/2023:13:56:41 +0100] -> 12/Apr/2023:13:56:41 +0100 -> Apr/2023 + Date.parse(match.gsub('[', '').gsub(']', '')).strftime('%b/%Y') end # Gets the HTTP status code of the given log line @@ -87,6 +87,8 @@ def sort_unique_ip(ips) end visit_counter = {} +monthly_visits = {} + client_errors = {} user_agents = {} all_ips = [] @@ -108,6 +110,7 @@ lines.each do |line| ip = get_line_ip(line).to_s code = get_line_code(line) ua = get_line_ua(line) + date = get_line_date(line) if is_client_err?(code.to_i) if client_errors[ip] @@ -123,6 +126,16 @@ lines.each do |line| user_agents[ua] = 1 end + if monthly_visits[ip] + if monthly_visits[ip][date] + monthly_visits[ip][date] += 1 + else + monthly_visits[ip][date] = 1 + end + else + monthly_visits[ip] = { date => 1 } + end + end top_user_agents = user_agents.sort_by { |_ua, count| -count }.first(5) @@ -136,3 +149,19 @@ puts 'Top 5 IPs with most client errors (400-499):' top_client_errors.each do |ip, count| puts "#{ip}: #{count} errors" end + +top_monthly_visits = monthly_visits.sort_by { |_ip, dates| -dates.values.sum }.first(5) +top_monthly_visits.each do |ip, dates| + puts "IP #{ip} had the most visits in the following months:" + dates.each do |date, count| + puts " #{date}: #{count} visits" + end +end + + +monthly_visits.each do |ip, visits| + puts "IP #{ip} had the following visits:" + visits.each do |date, count| + puts " #{date}: #{count} visits" + end +end