New logic

This commit is contained in:
Miguel Nogueira 2024-05-16 16:37:02 +01:00
parent 63181a656a
commit 7df3e94185
2 changed files with 23 additions and 12 deletions

23
main.rb
View File

@ -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

12
utils/http_codes.rb Normal file
View File

@ -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