Module: Polyrun::SpecQuality::Merge

Defined in:
lib/polyrun/spec_quality/merge.rb

Class Method Summary collapse

Class Method Details

.aggregate_hot_lines(examples) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/polyrun/spec_quality/merge.rb', line 55

def aggregate_hot_lines(examples)
   = Hash.new { |h, k| h[k] = {"examples" => [], "total_hits" => 0} }
  examples.each do |example_loc, row|
    Array(row["lines"]).each do |entry|
      path, line_no, delta = entry
      key = "#{path}:#{line_no}"
      [key]["examples"] << example_loc
      [key]["total_hits"] += delta.to_i
    end
  end
  .transform_values do |v|
    v["example_count"] = v["examples"].uniq.size
    v
  end
end

.build_merged_payload(examples, fragment_count) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/polyrun/spec_quality/merge.rb', line 29

def build_merged_payload(examples, fragment_count)
  hot_lines = aggregate_hot_lines(examples)
  {
    "examples" => examples,
    "hot_lines" => hot_lines,
    "shard_summary" => shard_summary(examples),
    "meta" => {
      "polyrun_version" => Polyrun::VERSION,
      "fragment_count" => fragment_count,
      "example_count" => examples.size
    }
  }
end

.merge_and_write(paths, output_path) ⇒ Object



71
72
73
74
75
# File 'lib/polyrun/spec_quality/merge.rb', line 71

def merge_and_write(paths, output_path)
  merged = merge_files(paths)
  File.write(output_path, JSON.pretty_generate(merged))
  merged
end

.merge_file_into!(examples, path) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/polyrun/spec_quality/merge.rb', line 16

def merge_file_into!(examples, path)
  File.foreach(path) do |line|
    line = line.strip
    next if line.empty?

    row = JSON.parse(line)
    key = row["example"].to_s
    next if key.empty?

    examples[key] = row
  end
end

.merge_files(paths) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/polyrun/spec_quality/merge.rb', line 8

def merge_files(paths)
  examples = {}
  paths.each do |path|
    merge_file_into!(examples, path)
  end
  build_merged_payload(examples, paths.size)
end

.shard_summary(examples) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/polyrun/spec_quality/merge.rb', line 43

def shard_summary(examples)
  by_shard = Hash.new { |h, k| h[k] = {"examples" => 0, "zero_hit" => 0, "line_churn" => 0} }
  examples.each do |_loc, row|
    shard = row["polyrun_shard_index"]
    shard = shard.nil? ? "?" : shard.to_s
    by_shard[shard]["examples"] += 1
    by_shard[shard]["zero_hit"] += 1 if row["unique_lines"].to_i.zero?
    by_shard[shard]["line_churn"] += row["line_churn"].to_i
  end
  by_shard
end