From 7df3e9418567b768794a1ef98f685499639d257f Mon Sep 17 00:00:00 2001 From: Miguel Nogueira Date: Thu, 16 May 2024 16:37:02 +0100 Subject: [PATCH] New logic --- main.rb | 23 +++++++++++------------ utils/http_codes.rb | 12 ++++++++++++ 2 files changed, 23 insertions(+), 12 deletions(-) create mode 100644 utils/http_codes.rb diff --git a/main.rb b/main.rb index c920f57..0e96098 100644 --- a/main.rb +++ b/main.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true require 'date' -require 'set' logfile = File.open("teambuilder.pt.log") data = logfile.read @@ -22,11 +21,13 @@ def get_line_date(line) Date.parse(match.gsub("[", "").gsub("]", "")) end +# Gets the HTTP status code of the given log line def get_line_code(line) pattern = /\s(\d{3})\s/ line.match(pattern)[1] if line.match(pattern) end +# Gets the user agent of the given log line def get_line_ua(line) pattern = /"([^"]*)"$/ user_agent = line.match(pattern)[0] if line.match(pattern) @@ -45,32 +46,25 @@ def times_appeared_single(ips, ip_to_check) counter end +# Returns all unique IPs in a given list of IP addresses with duplicates def sort_unique_ip(ips) - # Hash para rastrear IPs únicos seen_ips = {} - - # Array para armazenar IPs únicos unique_ips = [] - # Itera sobre cada IP no array ips.each do |ip| - # Verifica se o IP não está no hash unless seen_ips[ip] - # Adiciona o IP ao array de IPs únicos unique_ips << ip - # Marca o IP como visto no hash seen_ips[ip] = true end end unique_ips end - - visit_counter = {} - +client_errors = {} all_ips = [] + lines.each do | line | all_ips << get_line_ip(line) end @@ -81,6 +75,11 @@ puts("Houve um total de #{unique_ips.length} visitas registas ao nosso 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.") end + +lines.each do |line| + cur_ip = get_line_ip(line) + + client_errors[cur_ip] +end diff --git a/utils/http_codes.rb b/utils/http_codes.rb new file mode 100644 index 0000000..35be317 --- /dev/null +++ b/utils/http_codes.rb @@ -0,0 +1,12 @@ +HTTP_CLIENT_ERROR_RANGE = 400..499 +HTTP_SERVER_ERROR_RANGE = 500..599 +HTTP_INFORMATIONAL_RANGE = 100..199 +HTTP_SUCCESS_RANGE = 200..299 + +def is_client_err?(code) + HTTP_CLIENT_ERROR_RANGE === code +end + +def is_server_err?(code) + +end \ No newline at end of file