Module: MotoRecall::Sampler::Util

Included in:
Runner, Sample
Defined in:
lib/moto_recall/sampler/util.rb

Instance Method Summary collapse

Instance Method Details

#fetch_with_caching_and_logging(key, label, &block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/moto_recall/sampler/util.rb', line 5

def fetch_with_caching_and_logging(key, label, &block)
  result = CACHE.read(key)
  if result.nil?
    result = fetch_with_logging(label, &block)
    CACHE.write(key, result)
  else
    log("Reading #{label} from cache... #{result.length} found", {
      color: :yellow
    })
  end
  result
end

#fetch_with_logging(label, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/moto_recall/sampler/util.rb', line 18

def fetch_with_logging(label, &block)
  print "Fetching #{label}... "

  t_0 = Time.now
  results = yield
  t_elapsed = (Time.now - t_0) * 1000

  log_colored_count(results.length, "found (#{t_elapsed}ms)")

  results
end

#log(message, opts = {}) ⇒ Object



36
37
38
# File 'lib/moto_recall/sampler/util.rb', line 36

def log(message, opts = {})
  puts message.colorize(opts.slice(:color))
end

#log_colored_count(count, message) ⇒ Object



30
31
32
33
34
# File 'lib/moto_recall/sampler/util.rb', line 30

def log_colored_count(count, message)
  log("#{count} #{message}", {
    color: count == 0 ? :red : :green
  })
end