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



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.



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

def errors
  @errors
end

#telemetryObject (readonly)

TODO support multiple metrics and adding total time



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

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



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

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

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



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

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

#debug_payload(prefix, data) ⇒ Object



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

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

#debug_payload?Boolean



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

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

#errors?Boolean



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

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



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

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

#log_error(message) ⇒ Object



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

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

#measure(metric) ⇒ Object



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

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

#spool_reportObject



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

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

#telemetry_as_stringObject



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

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