Skip to content

Instantly share code, notes, and snippets.

Created May 2, 2015 20:38
Show Gist options
  • Save anonymous/22a8ab4d8f1600a1bc3c to your computer and use it in GitHub Desktop.
Save anonymous/22a8ab4d8f1600a1bc3c to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'date'
session = Hash.new
time = Array.new
parseapache = /.*\[(.*) .*\].* ([a-zA-Z0-9]+)$/
File.open("03-26").each do |line|
matches = parseapache.match(line)
if matches
if !(session[matches[2]])
session[matches[2]] = Array.new
session[matches[2]] << DateTime.strptime(matches[1], "%d/%b/%Y:%H:%M:%S")
else
session[matches[2]] << DateTime.strptime(matches[1], "%d/%b/%Y:%H:%M:%S")
end
end
end
session.map { |k, v| [k, v.sort] }.to_h
def sessionize(session)
session.each do |key, value|
outcome = (value[-1] - value[0])
outcome_sec = (outcome * 24 * 60 * 60).to_i
if outcome_sec > 1800
index1 = 0
index2 = 1
value.each do |x, y|
outcome = (y - x)
outcome_sec = (outcome * 24 * 60 * 60).to_i
if (outcome_sec > 1800)
random = SecureRandom.hex
session[random] = Array.new
session[random] << value[index2]..value[-1]
session[key] = value[0]..value[index1]
sessionize(session)
end
index1 += 1
index2 += 1
end
end
end
end
overall = 0
sessions = 0
session.each do |key, value|
outcome = (value[-1] - value[0])
outcome_sec = (outcome * 24 * 60 * 60).to_i
puts "#{key} = #{outcome_sec}"
overall += outcome_sec
sessions +=1
end
puts "amount: #{sessions}"
puts "total duration: #{overall} sec"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment