Skip to content

Commit

Permalink
Fixes #18893 - Improve latest events widget performance
Browse files Browse the repository at this point in the history
  • Loading branch information
tbrisker authored and ohadlevy committed Mar 23, 2017
1 parent 9ca77d0 commit 9313f6a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
9 changes: 8 additions & 1 deletion app/services/dashboard/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ def latest_events
@latest_events ||= ConfigReport.authorized(:view_config_reports).my_reports.interesting
.where("#{ConfigReport.quoted_table_name}.#{con.quote_column_name('host_id')} IN (#{ids})")
.search_for('reported > "7 days ago"')
.limit(9).includes(:host)
.reorder(:reported_at => :desc)
.limit(9)
.preload(:host)
end

def latest_events?
#exists? removes the order field causing the db to use the wrong index
latest_events.limit(1).present?
end

private
Expand Down
2 changes: 1 addition & 1 deletion app/views/dashboard/_reports_widget.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<h4 class="header">
<%= _("Latest Events") %>
</h4>
<% if @data.latest_events.exists? %>
<% if @data.latest_events? %>
<table class="<%= table_css_classes 'table-fixed reports-table' %>">
<thead>
<tr>
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20170315154334_add_report_time_type_host_index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddReportTimeTypeHostIndex < ActiveRecord::Migration
def change
add_index :reports, [:reported_at, :host_id, :type]
end
end

0 comments on commit 9313f6a

Please sign in to comment.