Module: Kaisoku::TestMap::HotFiles

Included in:
Kaisoku::TestMap
Defined in:
lib/kaisoku/test_map/hot_files.rb

Instance Method Summary collapse

Instance Method Details

#hot_files(threshold: 20, limit: 20, adapter: nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/kaisoku/test_map/hot_files.rb', line 6

def hot_files(threshold: 20, limit: 20, adapter: nil)
  adapter_clause = adapter ? 'AND entities.adapter = ?' : ''
  bind = []
  bind << adapter.to_s if adapter
  bind << threshold.to_i
  bind << limit.to_i

  sql = <<~SQL
    SELECT deps.dep_file AS file, COUNT(DISTINCT deps.entity_id) AS entity_count
    FROM deps
    INNER JOIN entities ON entities.id = deps.entity_id
    WHERE substr(deps.dep_file, 1, 2) != '__'
      AND instr(deps.dep_file, '#') = 0
    #{adapter_clause}
    GROUP BY deps.dep_file
    HAVING COUNT(DISTINCT deps.entity_id) >= ?
    ORDER BY entity_count DESC, deps.dep_file ASC
    LIMIT ?
  SQL

  @db.execute(sql, bind).map do |row|
    { file: row['file'], entity_count: row['entity_count'].to_i }
  end
end