# frozen_string_literal: true require 'date' require 'set' logfile = File.open("teambuilder.pt.log") data = logfile.read lines = data.split("\n") # Gets the IP address from the string # Note: sometimes the function can fetch the UA version as an IP address def get_line_ip(line) pattern = /\b(?:\d{1,3}\.){3}\d{1,3}\b/ line.match(pattern)[0] if line.match(pattern) end # Returns the date in the current line def get_line_date(line) pattern = /\[(.*?)\]/ match = line.match(pattern)[0] if line.match(pattern) Date.parse(match.gsub("[", "").gsub("]", "")) end def get_line_code(line) pattern = /\s(\d{3})\s/ line.match(pattern)[1] if line.match(pattern) end def get_line_ua(line) pattern = /"([^"]*)"$/ user_agent = line.match(pattern)[0] if line.match(pattern) user_agent.gsub('"', '') end # Gets the number of times an IP contacted the site def times_appeared_single(ips, ip_to_check) counter = 0 ips.each do |ip| if ip == ip_to_check counter += 1 end end counter end 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 = {} all_ips = [] lines.each do | line | all_ips << get_line_ip(line) end unique_ips = sort_unique_ip(all_ips) 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