Class: XCCache::Cache::Cachemap

Inherits:
JSONRepresentable show all
Defined in:
lib/xccache/cache/cachemap.rb

Instance Attribute Summary

Attributes inherited from HashRepresentable

#path, #raw

Instance Method Summary collapse

Methods inherited from JSONRepresentable

#load, #save

Methods inherited from HashRepresentable

#[], #[]=, #initialize, #load, #merge!, #reload, #save

Constructor Details

This class inherits a constructor from XCCache::HashRepresentable

Instance Method Details

#cache_dataObject



10
11
12
# File 'lib/xccache/cache/cachemap.rb', line 10

def cache_data
  raw["cache"] ||= {}
end

#depgraph_dataObject



6
7
8
# File 'lib/xccache/cache/cachemap.rb', line 6

def depgraph_data
  raw["depgraph"] ||= {}
end

#get_cache_data(type) ⇒ Object



43
44
45
# File 'lib/xccache/cache/cachemap.rb', line 43

def get_cache_data(type)
  cache_data.select { |k, v| v == type && !k.end_with?(".xccache") }.keys
end

#missedObject



18
19
20
# File 'lib/xccache/cache/cachemap.rb', line 18

def missed
  get_cache_data(:missed)
end

#missed?(name) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/xccache/cache/cachemap.rb', line 14

def missed?(name)
  missed.include?(name)
end


30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/xccache/cache/cachemap.rb', line 30

def print_stats
  hit, missed, ignored = %i[hit missed ignored].map { |type| get_cache_data(type) }
  total_count = cache_data.count
  UI.message <<~DESC
    -------------------------------------------------------------------
    Cache stats
    • Hit (#{hit.count}/#{total_count}): #{hit.to_s.green.dark}
    • Missed (#{missed.count}/#{total_count}): #{missed.to_s.yellow.dark}
    • Ignored (#{ignored.count}/#{total_count}): #{ignored.to_s.dark}
    -------------------------------------------------------------------
  DESC
end

#statsObject



22
23
24
25
26
27
28
# File 'lib/xccache/cache/cachemap.rb', line 22

def stats
  %i[hit missed ignored].to_h do |type|
    count, total_count = get_cache_data(type).count, cache_data.count
    percent = total_count.positive? ? (count.to_f * 100 / total_count).round : 0
    [type, "#{percent}% (#{count}/#{total_count})"]
  end
end

#update_from_graph(graph) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/xccache/cache/cachemap.rb', line 47

def update_from_graph(graph)
  cache_data = graph["cache"].to_h do |k, v|
    next [k, :hit] if v
    next [k, :ignored] if Config.instance.ignore?(k)
    [k, :missed]
  end

  deps = graph["deps"]
  edges = deps.flat_map { |k, xs| xs.map { |v| { :source => k, :target => v } } }
  nodes = deps.keys.map do |k|
    {
      :id => k,
      :cache => cache_data[k],
      :type => ("agg" if k.end_with?(".xccache")),
      :binary => graph["cache"][k],
    }
  end
  self.raw = {
    "cache" => cache_data,
    "depgraph" => { "nodes" => nodes, "edges" => edges },
  }
  save
  print_stats
end