feat(date): fix invalid datetime string, add error handling
Signed-off-by: Miguel Nogueira <me@nogueira.codes>
This commit is contained in:
parent
4703e28e32
commit
87706021c3
16
main.rb
16
main.rb
|
@ -42,8 +42,11 @@ end
|
||||||
# Returns the date in the current line
|
# Returns the date in the current line
|
||||||
def get_line_date(line)
|
def get_line_date(line)
|
||||||
pattern = /\[(.*?)\]/
|
pattern = /\[(.*?)\]/
|
||||||
match = line.match(pattern)[0] if line.match(pattern)
|
if line.match(pattern)
|
||||||
|
match = line.match(pattern)[0]
|
||||||
|
else
|
||||||
|
raise StandardError("Invalid datetime data detected!")
|
||||||
|
end
|
||||||
# [12/Apr/2023:13:56:41 +0100] -> 12/Apr/2023:13:56:41 +0100 -> Apr/2023
|
# [12/Apr/2023:13:56:41 +0100] -> 12/Apr/2023:13:56:41 +0100 -> Apr/2023
|
||||||
Date.parse(match.gsub('[', '').gsub(']', '')).strftime('%b/%Y')
|
Date.parse(match.gsub('[', '').gsub(']', '')).strftime('%b/%Y')
|
||||||
end
|
end
|
||||||
|
@ -110,7 +113,12 @@ lines.each do |line|
|
||||||
ip = get_line_ip(line).to_s
|
ip = get_line_ip(line).to_s
|
||||||
code = get_line_code(line)
|
code = get_line_code(line)
|
||||||
ua = get_line_ua(line)
|
ua = get_line_ua(line)
|
||||||
date = get_line_date(line)
|
|
||||||
|
begin
|
||||||
|
date = get_line_date(line)
|
||||||
|
rescue StandardError
|
||||||
|
date = 'N/A'
|
||||||
|
end
|
||||||
|
|
||||||
if is_client_err?(code.to_i)
|
if is_client_err?(code.to_i)
|
||||||
if client_errors[ip]
|
if client_errors[ip]
|
||||||
|
@ -153,7 +161,7 @@ end
|
||||||
monthly_visits.each do |ip, visits|
|
monthly_visits.each do |ip, visits|
|
||||||
puts "IP #{ip} had the following visits:"
|
puts "IP #{ip} had the following visits:"
|
||||||
visits.each do |date, count|
|
visits.each do |date, count|
|
||||||
puts " #{date}: #{count} visits"
|
puts "#{date}: #{count} visits"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue