Module: RspecSprint::Comparator

Defined in:
lib/rspec_sprint/comparator.rb

Overview

Computes and formats the delta between a previous snapshot (raw JSON hash) and the current Normalizer::Snapshot.

Constant Summary collapse

NO_BASELINE_MSG =
"前回のスナップショットがありません。まず `bundle exec rspec-sprint doctor` を実行してベースラインを作成してください。\n" \
"(No prior snapshot. Run doctor once to create a baseline.)"

Class Method Summary collapse

Class Method Details

.format_delta(previous_data, current) ⇒ Object

previous_data: Hash parsed from a saved snapshot JSON (may be nil) current: Normalizer::Snapshot Returns a formatted string to append to the doctor report.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rspec_sprint/comparator.rb', line 17

def format_delta(previous_data, current)
  return NO_BASELINE_MSG if previous_data.nil?

  prev_duration   = previous_data[:suite_duration].to_f
  curr_duration   = current.suite_duration.to_f
  duration_delta  = curr_duration - prev_duration
  duration_pct    = prev_duration > 0 ? (duration_delta / prev_duration * 100).round(1) : 0.0

  prev_factory_pct = prev_duration > 0 ? (previous_data[:factory_time].to_f / prev_duration * 100).round(1) : 0.0
  curr_factory_pct = (current.factory_time_ratio * 100).round(1)

  prev_ts = format_timestamp(previous_data[:created_at])

  sign = duration_delta >= 0 ? "+" : ""
  "前回比 #{sign}#{duration_delta.round(1)}s (#{sign}#{duration_pct}%)  " \
    "factory time #{prev_factory_pct}% → #{curr_factory_pct}%  [#{prev_ts}]"
end

.format_timestamp(iso8601) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/rspec_sprint/comparator.rb', line 35

def format_timestamp(iso8601)
  return "unknown" if iso8601.nil?

  Time.parse(iso8601).strftime("%Y-%m-%d %H:%M")
rescue ArgumentError
  iso8601.to_s
end