Add more functions

This commit is contained in:
Miguel Nogueira 2024-05-16 11:35:08 +01:00
parent 34babd7810
commit 9d678a2e7d
2 changed files with 26 additions and 10 deletions

View File

@ -2,7 +2,7 @@
## Objetivos do programa ## Objetivos do programa
- **Objetivo 1**: Número de contactos que um determinado IP realizou - **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 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 3**: Agrupar por tipo de cliente e saber que tipos de clientes contactam mais o site

34
main.rb
View File

@ -1,15 +1,38 @@
# frozen_string_literal: true # frozen_string_literal: true
require 'date'
logfile = File.open("teambuilder.pt.log") logfile = File.open("teambuilder.pt.log")
data = logfile.read data = logfile.read
lines = data.split("\n") lines = data.split("\n")
# Gets the IP address from the string # 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) def get_line_ip(line)
pattern = /\b(?:\d{1,3}\.){3}\d{1,3}\b/ pattern = /\b(?:\d{1,3}\.){3}\d{1,3}\b/
line.match(pattern)[0] if line.match(pattern) line.match(pattern)[0] if line.match(pattern)
end 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 # Gets the number of times an IP contacted the site
def times_appeared_single(ips, ip_to_check) def times_appeared_single(ips, ip_to_check)
counter = 0 counter = 0
@ -21,18 +44,11 @@ def times_appeared_single(ips, ip_to_check)
[counter] [counter]
end end
def get_code_by_ip(ip_address)
end
def get_user_agent(ip)
end
ips = [] ips = []
lines.each do | line | lines.each do | line |
previous_ip = "" previous_ip = ""
ips << get_line_ip(line) ips << get_line_ip(line)
puts get_line_ua(line)
end end
puts times_appeared_single(ips, "3.94.209.170")