Class: Coverband::Collectors::AbstractTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/coverband/collectors/abstract_tracker.rb

Overview

This abstract class makes it easy to track any used/unused with timestamp set of usage

Direct Known Subclasses

RouteTracker, TranslationTracker, ViewTracker

Constant Summary collapse

REPORT_ROUTE =
"/"
TITLE =
"abstract"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ AbstractTracker

Returns a new instance of AbstractTracker.

Raises:

  • (NotImplementedError)


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

def initialize(options = {})
  raise NotImplementedError, "#{self.class.name} requires a newer version of Rails" unless self.class.supported_version?
  raise "Coverband: #{self.class.name} initialized before configuration!" if !Coverband.configured? && ENV["COVERBAND_TEST"] == "test"

  @ignore_patterns = Coverband.configuration.ignore
  @store = options.fetch(:store) { Coverband.configuration.store }
  @logger = options.fetch(:logger) { Coverband.configuration.logger }
  @target = options.fetch(:target) do
    concrete_target
  end

  @one_time_timestamp = false

  @logged_keys = Set.new
  @keys_to_record = Set.new
end

Instance Attribute Details

#ignore_patternsObject (readonly)

Returns the value of attribute ignore_patterns.



16
17
18
# File 'lib/coverband/collectors/abstract_tracker.rb', line 16

def ignore_patterns
  @ignore_patterns
end

#loggerObject (readonly)

Returns the value of attribute logger.



16
17
18
# File 'lib/coverband/collectors/abstract_tracker.rb', line 16

def logger
  @logger
end

#storeObject (readonly)

Returns the value of attribute store.



16
17
18
# File 'lib/coverband/collectors/abstract_tracker.rb', line 16

def store
  @store
end

#targetObject

Returns the value of attribute target.



15
16
17
# File 'lib/coverband/collectors/abstract_tracker.rb', line 15

def target
  @target
end

Class Method Details

.supported_version?Boolean

This is the basic rails version supported, if there is something more unique over ride in subclass

Returns:

  • (Boolean)


121
122
123
# File 'lib/coverband/collectors/abstract_tracker.rb', line 121

def self.supported_version?
  defined?(Rails::VERSION) && Rails::VERSION::STRING.split(".").first.to_i >= 7
end

Instance Method Details

#all_keysObject



61
62
63
# File 'lib/coverband/collectors/abstract_tracker.rb', line 61

def all_keys
  target.uniq
end

#as_jsonObject



70
71
72
73
74
75
76
# File 'lib/coverband/collectors/abstract_tracker.rb', line 70

def as_json
  used_keys = self.used_keys
  {
    unused_keys: unused_keys(used_keys),
    used_keys: used_keys
  }.to_json
end

#clear_key!(key) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/coverband/collectors/abstract_tracker.rb', line 95

def clear_key!(key)
  return unless key
  return unless redis_store

  puts "#{tracker_key} key #{key}"
  redis_store.hdel(tracker_key, key)
  @logged_keys.delete(key)
end

#keys_to_recordObject



39
40
41
# File 'lib/coverband/collectors/abstract_tracker.rb', line 39

def keys_to_record
  @keys_to_record.to_a
end

#logged_keysObject



35
36
37
# File 'lib/coverband/collectors/abstract_tracker.rb', line 35

def logged_keys
  @logged_keys.to_a
end

#reset_recordingsObject



88
89
90
91
92
93
# File 'lib/coverband/collectors/abstract_tracker.rb', line 88

def reset_recordings
  return unless redis_store

  redis_store.del(tracker_key)
  redis_store.del(tracker_time_key)
end

#routeObject



125
126
127
# File 'lib/coverband/collectors/abstract_tracker.rb', line 125

def route
  self.class::REPORT_ROUTE
end

#save_reportObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/coverband/collectors/abstract_tracker.rb', line 104

def save_report
  return unless redis_store

  redis_store.set(tracker_time_key, Time.now.to_i) unless @one_time_timestamp || tracker_time_key_exists?
  @one_time_timestamp = true
  reported_time = Time.now.to_i
  if @keys_to_record.any?
    redis_store.hset(tracker_key, @keys_to_record.each_with_object({}) { |key, h| h[key.to_s] = reported_time })
  end
  @keys_to_record.clear
rescue => e
  # we don't want to raise errors if Coverband can't reach redis.
  # This is a nice to have not a bring the system down
  logger&.error "Coverband: #{self.class.name} failed to store, error #{e.class.name} info #{e.message}"
end

#titleObject



129
130
131
# File 'lib/coverband/collectors/abstract_tracker.rb', line 129

def title
  self.class::TITLE
end

#track_key(key) ⇒ Object

This method is called on every translation usage



46
47
48
49
50
51
52
53
# File 'lib/coverband/collectors/abstract_tracker.rb', line 46

def track_key(key)
  if key
    if newly_seen_key?(key)
      @logged_keys << key
      @keys_to_record << key if track_key?(key)
    end
  end
end

#tracking_sinceObject



78
79
80
81
82
83
84
85
86
# File 'lib/coverband/collectors/abstract_tracker.rb', line 78

def tracking_since
  return "N/A" unless redis_store

  if (tracking_time = redis_store.get(tracker_time_key))
    Time.at(tracking_time.to_i).iso8601
  else
    "N/A"
  end
end

#unused_keys(used_keys = nil) ⇒ Object



65
66
67
68
# File 'lib/coverband/collectors/abstract_tracker.rb', line 65

def unused_keys(used_keys = nil)
  recently_used_keys = used_keys || self.used_keys
  all_keys.reject { |k| recently_used_keys.key?(k.to_s) }
end

#used_keysObject



55
56
57
58
59
# File 'lib/coverband/collectors/abstract_tracker.rb', line 55

def used_keys
  return {} unless redis_store

  redis_store.hgetall(tracker_key)
end