49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
|
# File 'app/controllers/api/v2/host_reports_controller.rb', line 49
def create
result = nil
params[:host_report].delete(:version)
raise("Unknown host: #{@hostname}") unless params[:host_report][:host_id]
the_body = params[:host_report].delete(:body)
if the_body && !the_body.is_a?(String)
logger.warn "Report body not as a string, serializing JSON"
the_body = JSON.pretty_generate(the_body)
end
keywords = params[:host_report].delete(:keywords)
report_keyword_ids = []
telemetry_duration_histogram(:host_report_create_keywords, :ms) do
if keywords.present?
keywords_to_insert = keywords.each_with_object([]) do |n, ks|
ks << { name: n }
end
ReportKeyword.upsert_all(keywords_to_insert, unique_by: :name)
report_keyword_ids = ReportKeyword.where(name: keywords).distinct.pluck(:id)
end
end
@host_report = HostReport.new(host_report_params.merge(body: the_body, report_keyword_ids: report_keyword_ids))
telemetry_duration_histogram(:host_report_create, :ms) do
result = @host_report.save
end
telemetry_duration_histogram(:host_report_create_refresh, :ms) do
time = Time.parse(params[:host_report][:reported_at]).utc
@host.update_attribute(:last_report, time) if @host.last_report.nil? || @host.last_report.utc < time
@host.refresh_statuses([HostStatus::HostReportStatus])
end
process_response result
rescue StandardError => e
render_exception(e, :status => :unprocessable_entity)
end
|