From ec07527653433ccdbcf41c3e12c6e97b6a48f2f1 Mon Sep 17 00:00:00 2001 From: Miguel Nogueira Date: Fri, 17 May 2024 12:08:56 +0100 Subject: [PATCH] New logic --- README.md | 4 ++-- main.rb | 38 ++++++++++++++++++++++++++++++++++---- utils/http_codes.rb | 8 ++++++++ 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 9132f8f..1488abc 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,8 @@ ## Objetivos do programa - **Objetivo 1**: ~~Número de contactos que um determinado IP realizou~~ (Feito) - - **Objetivo 2**: Quais IPs resultaram no maior número de erros de cliente (400-499) - - **Objetivo 3**: Agrupar por tipo de cliente e saber que tipos de clientes contactam mais o site + - **Objetivo 2**: ~~Quais IPs resultaram no maior número de erros de cliente (400-499)~~ (Feito) + - **Objetivo 3**: ~~Agrupar por tipo de cliente e saber que tipos de clientes contactam mais o site~~ (Feito) ### Sub-objetivos diff --git a/main.rb b/main.rb index 0e96098..6a6a412 100644 --- a/main.rb +++ b/main.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'date' +require_relative 'utils/http_codes' logfile = File.open("teambuilder.pt.log") data = logfile.read @@ -63,6 +64,7 @@ end visit_counter = {} client_errors = {} +user_agents = {} all_ips = [] lines.each do | line | @@ -71,15 +73,43 @@ end unique_ips = sort_unique_ip(all_ips) -puts("Houve um total de #{unique_ips.length} visitas registas ao nosso site.") +puts("There were a total of #{unique_ips.length} unique IPs who connected to our site.") unique_ips.each do |ip| visit_counter[ip] = times_appeared_single(all_ips, ip) - puts("O IP #{ip} contactou o nosso site #{times_appeared_single(all_ips, ip)} vezes.") + puts("IP #{ip} contacted our site #{times_appeared_single(all_ips, ip)} times.") end lines.each do |line| - cur_ip = get_line_ip(line) + ip = get_line_ip(line).to_s + code = get_line_code(line) + ua = get_line_ua(line) + + #if code.to_i >= 400 && code.to_i < 500 + if is_client_err?(code.to_i) + if client_errors[ip] + client_errors[ip] += 1 + else + client_errors[ip] = 1 + end + end + + if user_agents[ua] + user_agents[ua] += 1 + else + user_agents[ua] = 1 + end - client_errors[cur_ip] end + +top_user_agents = user_agents.sort_by { |ua, count| -count }.first(5) +puts "Top 5 User-Agents contacting the site:" +top_user_agents.each do |ua, count| + puts "#{ua}: #{count} visits" +end + +top_client_errors = client_errors.sort_by { |ip, count| -count }.first(5) +puts "Top 5 IPs with most client errors (400-499):" +top_client_errors.each do |ip, count| + puts "#{ip}: #{count} errors" +end \ No newline at end of file diff --git a/utils/http_codes.rb b/utils/http_codes.rb index 35be317..e8c1157 100644 --- a/utils/http_codes.rb +++ b/utils/http_codes.rb @@ -8,5 +8,13 @@ def is_client_err?(code) end def is_server_err?(code) + HTTP_SERVER_ERROR_RANGE === code +end +def is_info_status?(code) + HTTP_INFORMATIONAL_RANGE === code +end + +def is_successful?(code) + HTTP_SUCCESS_RANGE === code end \ No newline at end of file