Analytics teams track engagement metrics, product managers need DAU/MAU reports, and SaaS companies monitor usage for billing. This drill teaches you to parse Apache access logs to extract IP addresses and usernames, group activity by date, count unique users per day, and calculate 30-day active users. You'll work with complex nested hash structures and date arithmetic.
Apache log format: IP - username [date] "request" status size
Use regex to extract the three parts before the bracket
Hash.new { |h, k| h[k] = Hash.new(0) } creates nested hash with counters
Date.strptime(date_str, '%d/%b/%Y') parses Apache date format
Use .strftime('%Y-%m-%d') to convert to standard format
Count unique users with hash.keys.length
Get all unique users across days with .flat_map(&:keys).uniq
track_active_users('apache.log')
Date: 2024-10-01, Active users: 2 Date: 2024-10-02, Active users: 1 30-day active users: 3
track_active_users('apache.log')
Date: 2024-10-01, Active users: 2 30-day active users: 2
track_active_users('apache.log')
Date: 2024-10-01, Active users: 1 30-day active users: 1
Console output will appear here...
Are you sure?
You're making great progress