module LogfileInterval::ParsedLine::Parser

Attributes

regex[R]

Public Instance Methods

add_column(options) click to toggle source
# File lib/logfile_interval/parsed_line/parser.rb, line 24
def add_column(options)
  validate_column_options(options)
  options = sanitize_column_options(options)

  name = options[:name]
  columns[name] = options

  define_method(name) do
    @data[name]
  end
end
columns() click to toggle source
# File lib/logfile_interval/parsed_line/parser.rb, line 8
def columns
  @columns ||= {}
end
create_record(line) click to toggle source
# File lib/logfile_interval/parsed_line/parser.rb, line 84
def create_record(line)
  record = new(line)
  return record if record.valid?
  return nil
end
each(&block) click to toggle source
# File lib/logfile_interval/parsed_line/parser.rb, line 96
def each(&block)
  columns.each(&block)
end
parse(line) click to toggle source
# File lib/logfile_interval/parsed_line/parser.rb, line 52
def parse(line)
  raise ConfigurationError, 'There must be at least 1 configured column' unless columns.any?
  raise ConfigurationError, 'A regex must be set' unless regex

  match_data = regex.match(line)
  return nil unless match_data

  data = { skip: false }
  columns.each do |name, options|
    val = match_data[options[:pos]]
    data[name] = convert(val, options[:conversion])
  end

  skip_columns.each do |options|
    val = match_data[options[:pos]]
    if val =~ options[:regex]
      data[:skip] = true
      break
    end
  end

  skip_columns_with_exceptions.each do |options|
    val = match_data[options[:pos]]
    if val =~ options[:regex]
      data[:skip_with_exceptions] = true
      break
    end
  end

  data
end
set_column_custom_options(column_name, options) click to toggle source
# File lib/logfile_interval/parsed_line/parser.rb, line 90
def set_column_custom_options(column_name, options)
  raise ArgumentError, "Invalid column name: #{column_name}" unless columns.has_key?(column_name)
  columns[column_name][:custom_options] = options
end
set_regex(regex) click to toggle source
# File lib/logfile_interval/parsed_line/parser.rb, line 20
def set_regex(regex)
  @regex = regex
end
skip(options) click to toggle source
# File lib/logfile_interval/parsed_line/parser.rb, line 36
def skip(options)
  unless options[:pos] && options[:regex]
    raise ConfigurationError, "skip option must include pos and regex"
  end

  skip_columns << { pos: options[:pos], regex: options[:regex] }
end
skip_columns() click to toggle source
# File lib/logfile_interval/parsed_line/parser.rb, line 12
def skip_columns
  @skip_columns ||= []
end
skip_columns_with_exceptions() click to toggle source
# File lib/logfile_interval/parsed_line/parser.rb, line 16
def skip_columns_with_exceptions
  @skip_columns_with_exceptions ||= []
end
skip_with_exceptions(options) click to toggle source
# File lib/logfile_interval/parsed_line/parser.rb, line 44
def skip_with_exceptions(options)
  unless options[:pos] && options[:regex]
    raise ConfigurationError, "skip option must include pos and regex"
  end

  skip_columns_with_exceptions << { pos: options[:pos], regex: options[:regex] }
end

Private Instance Methods

convert(val, conversion) click to toggle source
# File lib/logfile_interval/parsed_line/parser.rb, line 133
def convert(val, conversion)
  case conversion
  when :integer then val.to_i
  when :float   then val.to_f
  else val
  end
end
sanitize_column_options(options) click to toggle source
# File lib/logfile_interval/parsed_line/parser.rb, line 118
def sanitize_column_options(options)
  options[:name]       = options[:name].to_sym
  if options.has_key?(:group_by)
    if options[:group_by].to_sym != options[:name]
      options[:group_by] = options[:group_by].to_sym
    else
      options.delete(:group_by)
    end
  end
  options[:conversion] = options.fetch(:conversion, :string)
  options[:aggregator_class] = Aggregator::Base.klass(options[:aggregator])
  options.delete(:aggregator)
  options
end
validate_column_options(options) click to toggle source
# File lib/logfile_interval/parsed_line/parser.rb, line 102
def validate_column_options(options)
  validate_option(options, :name)
  validate_option(options, :pos)
  validate_option(options, :aggregator)
  if options[:name].to_s == 'skip'
    raise ConfigurationError, "'skip' is a reserved column name"
  end
  unless Aggregator::Base.exist?(options[:aggregator]) || options[:aggregator] == :timestamp
    raise ConfigurationError, "aggregator must be one of #{Aggregator::Base.all.join(', ')}"
  end
end
validate_option(options, key, errmsg = nil) click to toggle source
# File lib/logfile_interval/parsed_line/parser.rb, line 114
def validate_option(options, key, errmsg = nil)
  raise ConfigurationError, errmsg || "#{key} is a mandatory column option" unless options.has_key?(key)
end