Class: Processor

Inherits:
Object
  • Object
show all
Includes:
Proxy::Log
Defined in:
lib/smart_proxy_host_reports/processor.rb

Overview

TODO: move everything into a module

Direct Known Subclasses

AnsibleProcessor, PuppetProcessor

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json_body: true) ⇒ Processor

Returns a new instance of Processor.



21
22
23
24
25
# File 'lib/smart_proxy_host_reports/processor.rb', line 21

def initialize(*, json_body: true)
  @keywords_set = {}
  @errors = []
  @json_body = json_body
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



70
71
72
# File 'lib/smart_proxy_host_reports/processor.rb', line 70

def errors
  @errors
end

#telemetryObject (readonly)

TODO support multiple metrics and adding total time



81
82
83
# File 'lib/smart_proxy_host_reports/processor.rb', line 81

def telemetry
  @telemetry
end

Class Method Details

.new_processor(format, data, json_body: true) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/smart_proxy_host_reports/processor.rb', line 10

def self.new_processor(format, data, json_body: true)
  case format
  when "puppet"
    PuppetProcessor.new(data, json_body: json_body)
  when "ansible"
    AnsibleProcessor.new(data, json_body: json_body)
  else
    NotImplementedError.new
  end
end

Instance Method Details

#add_keywords(*keywords) ⇒ Object



60
61
62
63
64
# File 'lib/smart_proxy_host_reports/processor.rb', line 60

def add_keywords(*keywords)
  keywords.each do |keyword|
    @keywords_set[keyword] = true
  end
end

#build_report_root(format:, version:, host:, reported_at:, status:, proxy:, body:, keywords:) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/smart_proxy_host_reports/processor.rb', line 35

def build_report_root(format:, version:, host:, reported_at:, status:, proxy:, body:, keywords:)
  {
    "host_report" => {
      "format" => format,
      "version" => version,
      "host" => host,
      "reported_at" => reported_at,
      "status" => status,
      "proxy" => proxy,
      "body" => @json_body ? body.to_json : body,
      "keywords" => keywords,
    },
  }
  # TODO add metric with total time
end

#debug_payload(prefix, data) ⇒ Object



55
56
57
58
# File 'lib/smart_proxy_host_reports/processor.rb', line 55

def debug_payload(prefix, data)
  return unless debug_payload?
  logger.debug { "#{prefix}: #{data.pretty_inspect}" }
end

#debug_payload?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/smart_proxy_host_reports/processor.rb', line 51

def debug_payload?
  Proxy::HostReports::Plugin.settings.debug_payload
end

#errors?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/smart_proxy_host_reports/processor.rb', line 76

def errors?
  @errors&.any?
end

#generated_report_idObject



27
28
29
# File 'lib/smart_proxy_host_reports/processor.rb', line 27

def generated_report_id
  @generated_report_id ||= SecureRandom.uuid
end

#hostname_from_configObject



31
32
33
# File 'lib/smart_proxy_host_reports/processor.rb', line 31

def hostname_from_config
  @hostname_from_config ||= Proxy::HostReports::Plugin.settings.override_hostname
end

#keywordsObject



66
67
68
# File 'lib/smart_proxy_host_reports/processor.rb', line 66

def keywords
  @keywords_set.keys.to_a rescue []
end

#log_error(message) ⇒ Object



72
73
74
# File 'lib/smart_proxy_host_reports/processor.rb', line 72

def log_error(message)
  @errors << message.to_s
end

#measure(metric) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/smart_proxy_host_reports/processor.rb', line 83

def measure(metric)
  t1 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  yield
ensure
  t2 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  @telemetry ||= {}
  @telemetry[metric] = (t2 - t1) * 1000
end

#spool_reportObject



100
101
102
103
# File 'lib/smart_proxy_host_reports/processor.rb', line 100

def spool_report
  super
  logger.debug "Spooled #{report_id}: #{telemetry_as_string}"
end

#telemetry_as_stringObject



92
93
94
95
96
97
98
# File 'lib/smart_proxy_host_reports/processor.rb', line 92

def telemetry_as_string
  result = []
  telemetry.each do |key, value|
    result << "#{key}=#{value.round(1)}ms"
  end
  result.join(", ")
end