From 63181a656a753e48ed9d503be102f30471503210 Mon Sep 17 00:00:00 2001 From: Miguel Nogueira Date: Thu, 16 May 2024 12:47:23 +0100 Subject: [PATCH] Add visitor count logic --- main.rb | 44 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/main.rb b/main.rb index 11869d7..c920f57 100644 --- a/main.rb +++ b/main.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'date' +require 'set' logfile = File.open("teambuilder.pt.log") data = logfile.read @@ -41,14 +42,45 @@ def times_appeared_single(ips, ip_to_check) counter += 1 end end - [counter] + counter end -ips = [] +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 | - previous_ip = "" - ips << get_line_ip(line) - - puts get_line_ua(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