Class: TraceDeflator

Inherits:
Object show all
Defined in:
lib/stratagem/extensions/trace_compression.rb

Overview

it’s not elegant, but it compresses stack traces in a json friendly manner

Class Method Summary collapse

Class Method Details

.create_word_map(compressed) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/stratagem/extensions/trace_compression.rb', line 4

def create_word_map(compressed)
  word_counts = {}
  compressed.each do |column|
    column.each do |row|
      word_counts[row[0]] ||= 0
      word_counts[row[0]] += 1
    end
  end

  map = word_counts.keys.select {|key| (key.length) > 2 && (word_counts[key] > 1) }.sort {|a,b| word_counts[b] <=> word_counts[a]}
end

.deflate(trace) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/stratagem/extensions/trace_compression.rb', line 25

def deflate(trace)
  tokenized = trace.map {|s| s.split('/') }
  longest = tokenized.inject(0) {|memo,a| a.size > memo ? a.size : memo }
  tokenized.each {|a|
    a << "" until a.size >= longest
  }
  compressed = tokenized.transpose.map do |column|
    compressed_column = column.inject([]) {|new_col,col|
      new_col << [col,0] if (new_col.size == 0)
      prev = new_col.last
      prev[0] == col ? prev[1] += 1 : new_col << [col,1]
      new_col
    }
  end
  
  word_map = create_word_map(compressed)
  sub_in_word_map(compressed, word_map)

  compressed = compressed.map {|column|
    column.map {|row|
      "#{row[1]}*#{row[0]}"
    }.join("\n")
  }
  
  {:a => word_map, :b => compressed}
end

.sub_in_word_map(compressed, word_map) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/stratagem/extensions/trace_compression.rb', line 16

def sub_in_word_map(compressed,word_map)
  compressed.map! do |column|
    column.map! do |row|
      sub_index = word_map.index(row[0])
      sub_index ? [sub_index,row[1]] : row
    end
  end
end