Class: AbrtProxy::HostReport

Inherits:
Object
  • Object
show all
Includes:
Proxy::Log
Defined in:
lib/smart_proxy_abrt/abrt_lib.rb

Defined Under Namespace

Classes: AggregatedReport, Error

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fname) ⇒ HostReport

Returns a new instance of HostReport.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/smart_proxy_abrt/abrt_lib.rb', line 111

def initialize(fname)
  contents = IO.read(fname)
  json = JSON.parse(contents)

  [:report, :reported_at, :host].each do |field|
    if !json.has_key?(field.to_s)
      raise AbrtProxy::Error::SyntaxError, "Report #{fname} missing field #{field}"
    end
  end

  report = json["report"]
  hash = HostReport.duphash report
  ar = AggregatedReport.new(json["report"], 1, hash, json["reported_at"])
  @reports = [ar]
  # index the array elements by duphash, if they have one
  @by_hash = {}
  @by_hash[hash] = ar unless hash.nil?
  @files = [fname]
  @host = json["host"]
end

Instance Attribute Details

#by_hashObject (readonly)

Returns the value of attribute by_hash.



109
110
111
# File 'lib/smart_proxy_abrt/abrt_lib.rb', line 109

def by_hash
  @by_hash
end

#filesObject (readonly)

Returns the value of attribute files.



109
110
111
# File 'lib/smart_proxy_abrt/abrt_lib.rb', line 109

def files
  @files
end

#hostObject (readonly)

Returns the value of attribute host.



109
110
111
# File 'lib/smart_proxy_abrt/abrt_lib.rb', line 109

def host
  @host
end

#reportsObject (readonly)

Returns the value of attribute reports.



109
110
111
# File 'lib/smart_proxy_abrt/abrt_lib.rb', line 109

def reports
  @reports
end

Class Method Details

.load_from_spoolObject



182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/smart_proxy_abrt/abrt_lib.rb', line 182

def self.load_from_spool
  reports = []
  report_files = Dir[File.join(HostReport.spooldir, "ureport-*")]
  report_files.each do |fname|
    begin
      reports << new(fname)
    rescue => e
      logger.error "Failed to parse report #{fname}: #{e}"
    end
  end
  reports
end

.save(host, report, reported_at = nil) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/smart_proxy_abrt/abrt_lib.rb', line 163

def self.save(host, report, reported_at=nil)
  # create the spool dir if it does not exist
  FileUtils.mkdir_p HostReport.spooldir

  reported_at ||= Time.now.utc
  on_disk_report = { "host" => host, "report" => report , "reported_at" => reported_at.to_s }

  # write report to temporary file
  temp_fname = unique_filename "new-"
  File.open temp_fname, File::WRONLY|File::CREAT|File::EXCL do |tmpfile|
    tmpfile.write(on_disk_report.to_json)
  end

  # rename it
  final_fname = unique_filename("ureport-" + DateTime.now.strftime("%FT%T") + "-")
  File.link temp_fname, final_fname
  File.unlink temp_fname
end

Instance Method Details

#merge(other) ⇒ Object

Raises:



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/smart_proxy_abrt/abrt_lib.rb', line 132

def merge(other)
  raise HostReport::Error, "Host names do not match" unless @host == other.host

  other.reports.each do |ar|
    if !ar.hash.nil? && @by_hash.has_key?(ar.hash)
      # we already have this report, just increment the counter
      found_report = @by_hash[ar.hash]
      found_report.count += ar.count
      found_report.reported_at = [found_report.reported_at, ar.reported_at].min
    else
      # we either don't have this report or it has no hash
      @reports << ar
      @by_hash[ar.hash] = ar unless ar.hash.nil?
    end
  end
  @files += other.files
end

#send_to_foremanObject



150
151
152
153
154
# File 'lib/smart_proxy_abrt/abrt_lib.rb', line 150

def send_to_foreman
  foreman_report = create_foreman_report
  logger.debug "Sending #{foreman_report}"
  Proxy::HttpRequest::ForemanRequest.new.send_request("/api/abrt_reports", foreman_report.to_json)
end


156
157
158
159
160
161
# File 'lib/smart_proxy_abrt/abrt_lib.rb', line 156

def unlink
  @files.each do |fname|
    logger.debug "Deleting #{fname}"
    File.unlink(fname)
  end
end