class Coverband::Collectors::ViewTracker

Public Instance Methods

report_views_tracked() click to toggle source
# File lib/coverband-service-client.rb, line 277
def report_views_tracked
  reported_time = Time.now.to_i
  if views_to_record.any?
    relative_views = views_to_record.map! do |view|
      roots.each do |root|
        view = view.gsub(/#{root}/, '')
      end
      view
    end
    save_tracked_views(views: relative_views, reported_time: reported_time)
  end
  self.views_to_record = []
rescue StandardError => 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: view_tracker failed to store, error #{e.class.name}" if Coverband.configuration.verbose || COVERBAND_ENABLE_DEV_MODE
end

Private Instance Methods

api_key() click to toggle source
# File lib/coverband-service-client.rb, line 297
def api_key
  ENV['COVERBAND_API_KEY'] || Coverband.configuration.api_key
end
logger() click to toggle source
# File lib/coverband-service-client.rb, line 301
def logger
  Coverband.configuration.logger
end
save_tracked_views(views:, reported_time:) click to toggle source
# File lib/coverband-service-client.rb, line 305
def save_tracked_views(views:, reported_time:)
  uri = URI("#{COVERBAND_SERVICE_URL}/api/collector")
  req = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json', 'Coverband-Token' => api_key)
  data = {
    collection_type: 'view_tracker_delta',
    collection_data: {
      tags: {
        runtime_env: COVERBAND_ENV
      },
      collection_time: reported_time,
      tracked_views: views
    }
  }
  # puts "sending #{data}"
  req.body = { remote_uuid: SecureRandom.uuid, data: data }.to_json
  res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
    http.request(req)
  end
rescue StandardError => e
  logger&.error "Coverband: Error while saving coverage #{e}" if Coverband.configuration.verbose || COVERBAND_ENABLE_DEV_MODE
end