Class: Coverband::Adapters::WebServiceStore

Inherits:
Base
  • Object
show all
Defined in:
lib/coverband/adapters/web_service_store.rb

Overview

WebServiceStore: store a checkpoint of coverage to a remote service

Constant Summary

Constants inherited from Base

Base::ABSTRACT_KEY, Base::DATA_KEY, Base::FILE_HASH, Base::FIRST_UPDATED_KEY, Base::LAST_UPDATED_KEY

Instance Attribute Summary collapse

Attributes inherited from Base

#type

Instance Method Summary collapse

Methods inherited from Base

#covered_files, #get_coverage_report, #size_in_mib

Constructor Details

#initialize(coverband_url, opts = {}) ⇒ WebServiceStore

Returns a new instance of WebServiceStore.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/coverband/adapters/web_service_store.rb', line 11

def initialize(coverband_url, opts = {})
  super()
  require "socket"
  require "securerandom"
  @coverband_url = coverband_url
  @process_type = opts.fetch(:process_type) { $PROGRAM_NAME&.split("/")&.last || Coverband.configuration.process_type }
  @hostname = opts.fetch(:hostname) { ENV["DYNO"] || Socket.gethostname.force_encoding("utf-8").encode }
  @hostname = @hostname.delete("'", "").delete("", "")
  @runtime_env = opts.fetch(:runtime_env) { Coverband.configuration.coverband_env }
  @failed_coverage_reports = []
end

Instance Attribute Details

#coverband_urlObject (readonly)

Returns the value of attribute coverband_url.



9
10
11
# File 'lib/coverband/adapters/web_service_store.rb', line 9

def coverband_url
  @coverband_url
end

#hostnameObject (readonly)

Returns the value of attribute hostname.



9
10
11
# File 'lib/coverband/adapters/web_service_store.rb', line 9

def hostname
  @hostname
end

#pidObject (readonly)

Returns the value of attribute pid.



9
10
11
# File 'lib/coverband/adapters/web_service_store.rb', line 9

def pid
  @pid
end

#process_typeObject (readonly)

Returns the value of attribute process_type.



9
10
11
# File 'lib/coverband/adapters/web_service_store.rb', line 9

def process_type
  @process_type
end

#runtime_envObject (readonly)

Returns the value of attribute runtime_env.



9
10
11
# File 'lib/coverband/adapters/web_service_store.rb', line 9

def runtime_env
  @runtime_env
end

Instance Method Details

#clear!Object



27
28
29
30
# File 'lib/coverband/adapters/web_service_store.rb', line 27

def clear!
  # done via service UI
  raise "not supported via service"
end

#clear_file!(filename) ⇒ Object



32
33
34
35
# File 'lib/coverband/adapters/web_service_store.rb', line 32

def clear_file!(filename)
  # done via service UI
  raise "not supported via service"
end

#coverage(local_type = nil, opts = {}) ⇒ Object

Fetch coverband coverage via the API This would allow one to explore from the service and move back to the open source without having to reset coverage



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/coverband/adapters/web_service_store.rb', line 48

def coverage(local_type = nil, opts = {})
  return if Coverband.configuration.service_disabled_dev_test_env?

  local_type ||= opts.key?(:override_type) ? opts[:override_type] : type
  env_filter = opts.key?(:env_filter) ? opts[:env_filter] : "production"
  uri = URI("#{coverband_url}/api/coverage?type=#{local_type}&env_filter=#{env_filter}")
  req = Net::HTTP::Get.new(uri, "content-type" => "application/json", "Coverband-Token" => Coverband.configuration.api_key)
  res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
    http.request(req)
  end
  JSON.parse(res.body)
rescue => e
  logger&.error "Coverband: Error while retrieving coverage #{e}" if Coverband.configuration.verbose || Coverband.configuration.service_dev_mode
end

#loggerObject



23
24
25
# File 'lib/coverband/adapters/web_service_store.rb', line 23

def logger
  Coverband.configuration.logger
end

#raw_storeObject



92
93
94
# File 'lib/coverband/adapters/web_service_store.rb', line 92

def raw_store
  raise "not supported via service"
end

#save_report(report) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/coverband/adapters/web_service_store.rb', line 63

def save_report(report)
  return if report.empty?

  # We set here vs initialize to avoid setting on the primary process vs child processes
  @pid ||= ::Process.pid

  # TODO: do we need dup
  # TODO: we don't need upstream timestamps, server will track first_seen
  Thread.new do
    data = expand_report(report.dup)
    full_package = {
      collection_type: "coverage_delta",
      collection_data: {
        tags: {
          process_type: process_type,
          app_loading: type == Coverband::EAGER_TYPE,
          runtime_env: runtime_env,
          pid: pid,
          hostname: hostname
        },
        file_coverage: data
      }
    }

    save_coverage(full_package)
    retry_failed_reports
  end&.join
end

#sizeObject

NOTE: Should support nil to mean not supported the size feature doesn’t really makde sense for the service



39
40
41
# File 'lib/coverband/adapters/web_service_store.rb', line 39

def size
  0
end