Sha256: 4349f791a07c8eef1d75e541f1e943a950e55e3f79654428b027427a6eafd33e

Contents?: true

Size: 783 Bytes

Versions: 2

Compression:

Stored size: 783 Bytes

Contents

# frozen_string_literal: true

require 'logger'

class Csvsql::Tracker
  attr_reader :stats, :logger

  def self.tracker
    @tracker ||= new
  end

  def self.tracker=(t)
    @tracker = t
  end

  def self.commit(*args, &block)
    tracker.commit(*args, &block)
  end

  def initialize(logger = Logger.new('/dev/null'))
    @stats = {}
    @logger = logger
  end

  def commit(id, output: true, &block)
    id = id.to_s
    old = stats[id]
    stats[id] = get_stat

    if block
      block.call.tap { commit(id) }
    elsif output && old
      logger.info("[#{id}] #{compare_stat(old, stats[id])}")
    end
  end

  private

  def get_stat
    { time: Time.now }
  end

  def compare_stat(old, new)
    "Time cost: #{((new[:time] - old[:time]) * 1000000).to_i / 1000}ms"
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
csvsql-0.2.0 lib/csvsql/tracker.rb
csvsql-0.1.5 lib/csvsql/tracker.rb