From 897fb7ece0b10736524991a7421f6fce20e9225a Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Wed, 5 Feb 2025 10:52:16 +0100 Subject: Add read_logs.rb scripts --- bin/read_logs.rb | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100755 bin/read_logs.rb (limited to 'bin') diff --git a/bin/read_logs.rb b/bin/read_logs.rb new file mode 100755 index 0000000..76627bc --- /dev/null +++ b/bin/read_logs.rb @@ -0,0 +1,59 @@ +#!/usr/bin/env ruby + +require 'date' +require 'zlib' +require 'pathname' + +class Visit + attr_accessor :page, :date + + def initialize(match_data) + @date = Date.iso8601 match_data[1] + @page = match_data[2].strip + end +end + +def numeric_extension(filename) + if filename.extname == '.gz' + filename.sub_ext('').extname[1..-1].to_i + elsif filename.extname == '.log' + 0 + else + filename.extname[1..-1].to_i + end +end + +entries = Dir.new('log') + .entries + .reject { |entry| entry.start_with? '.' } + .map { |entry| Pathname.new entry } + .sort { |a, b| numeric_extension(b) <=> numeric_extension(a) } + +def read_lines(stream) + if ARGV.length > 0 + regex = /([0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}).+#{ARGV[0]}(.+) / + else + regex = /([0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2})(.+) / + end + + stream + .map { |line| line.match regex } + .reject { |line| line.nil? } + .map { |match_data| Visit.new match_data } +end + +lines_in_all_files = entries.flat_map do |log| + log_path = Pathname.new('log') + log + + if log_path.extname == '.gz' + File.open log_path.to_s do |file| + read_lines Zlib::GzipReader.new(file).readlines + end + else + read_lines File.readlines(log_path.to_s) + end +end + +lines_in_all_files.each do |visit| + puts "#{visit.date.strftime} (#{visit.page})" +end -- cgit v1.2.3