lib/rspeed/splitter.rb in rspeed-0.2.0 vs lib/rspeed/splitter.rb in rspeed-0.3.0

- old
+ new

@@ -1,36 +1,33 @@ -# frozen_string_literal: true module RSpeed class Splitter DEFAULT_PATTERN = 'rspeed_*' def append(files = file_data) files.each do |time, file| - redis.lpush 'rspeed_tmp', { file: file, time: time.to_f }.to_json + redis.lpush('rspeed_tmp', { file: file, time: time.to_f }.to_json) end end def destroy(pattern = DEFAULT_PATTERN) keys(pattern).each { |key| redis.del key } end def diff - (actual_files + added_files).sort_by { |item| item[:time].to_f }.map do |item| - [item[:time], item[:file]] - end + actual_data + added_data end def first_pipe? pipe == 1 end def get(pattern) @get ||= begin return redis.lrange(pattern, 0, -1) if %w[rspeed rspeed_tmp].include?(pattern) - keys(pattern).map { |key| JSON.parse redis.get(key) } + keys(pattern).map { |key| JSON.parse(redis.get(key)) } end end def keys(pattern = DEFAULT_PATTERN) cursor = 0 @@ -57,21 +54,19 @@ def pipes result? ? ENV.fetch('RSPEED_PIPES') { 1 }.to_i : 1 end def rename - redis.rename 'rspeed_tmp', 'rspeed' + redis.rename('rspeed_tmp', 'rspeed') end def result? !keys('rspeed').empty? end - def save(data = rspeed_data) - split(data).each do |key, value| - redis.set key, value.to_json - end + def save(data = diff) + split(data).each { |key, value| redis.set(key, value.to_json) } end def split(data) json = {} @@ -94,46 +89,46 @@ json end private - def actual_files - rspeed_data.select { |item| actual_specs.include?(item[:file]) } + def actual_data + rspeed_data.select { |item| actual_files.include?(item[:file]) } end - def actual_specs - Dir['./spec/**/*_spec.rb'] + def actual_files + @actual_files ||= Dir['./spec/**/*_spec.rb'] end - def added_files + def added_data added_specs.map { |item| { file: item, time: 0 } } end def added_specs - actual_specs - saved_specs + actual_files - old_files end def file_data CSV.read('rspeed.csv') end + def old_files + rspeed_data.map { |item| item[:file] } + end + def redis @redis ||= ::Redis.new(db: ENV['RSPEED_DB'], host: ENV['RSPEED_HOST'], port: ENV.fetch('RSPEED_PORT') { 6379 }) end def removed_specs - saved_specs - actual_specs + old_files - actual_files end def removed_time removed_specs.map { |item| item[0].to_f }.sum end def rspeed_data - get('rspeed').map { |item| JSON.parse item, symbolize_names: true } - end - - def saved_specs - rspeed_data.map { |item| item[:file] } + @rspeed_data ||= get('rspeed').map { |item| JSON.parse(item, symbolize_names: true) } end end end