class FlatKit::Stats

Constants

AllFields

Attributes

fields_to_stat[R]
reader[R]
stats_by_field[R]
stats_to_collect[R]
writer[R]

Public Class Methods

new(input:, input_fallback: "auto", output:, output_fallback: "auto", fields_to_stat: AllFields, stats_to_collect: FieldStats::CORE_STATS) click to toggle source
# File lib/flat_kit/stats.rb, line 13
def initialize(input:, input_fallback: "auto",
               output:, output_fallback: "auto",
               fields_to_stat: AllFields, stats_to_collect: FieldStats::CORE_STATS)

  @fields_to_stat   = fields_to_stat
  @stats_to_collect = stats_to_collect
  @stats_by_field   = Hash.new
  @record_count     = 0

  @reader = ::FlatKit::Reader.create_reader_from_path(path: input, fallback: input_fallback)
  @writer = ::FlatKit::Writer.create_writer_from_path(path: output, fallback: output_fallback,
                                                      reader_format: @reader.format_name)
end

Public Instance Methods

call() click to toggle source
# File lib/flat_kit/stats.rb, line 27
def call
  calculate_stats
  write_stat_records
  @writer.close
end
collecting_stats_on_field?(name) click to toggle source
# File lib/flat_kit/stats.rb, line 33
def collecting_stats_on_field?(name)
  return true if @fields_to_stat == AllFields
  return @fields_to_stat.include?(name)
end

Private Instance Methods

calculate_stats() click to toggle source
# File lib/flat_kit/stats.rb, line 40
def calculate_stats
  ::FlatKit.logger.debug "Calculating statistics on #{reader.source}"
  reader.each do |record|
    record.to_hash.each do |field_name, field_value|
      if collecting_stats_on_field?(field_name) then
        update_stats_for_field(name: field_name, value: field_value)
      end
    end
    @record_count += 1
  end
end
update_stats_for_field(name:, value:) click to toggle source
# File lib/flat_kit/stats.rb, line 52
def update_stats_for_field(name:, value:)
  field_stats = @stats_by_field[name] ||= FieldStats.new(name: name, stats_to_collect: @stats_to_collect)
  field_stats.update(value)
end
write_stat_records() click to toggle source
# File lib/flat_kit/stats.rb, line 57
def write_stat_records
  @stats_by_field.each do |name, stats|
    h = stats.to_hash.merge({"total_record_count" => @record_count })
    record = ::FlatKit::Jsonl::Record.new(data: nil, complete_structured_data: h)

    @writer.write(record)
  end
end