Skip to content

Commit

Permalink
Merge pull request #40 from igorkasyanchuk/autoupdate
Browse files Browse the repository at this point in the history
autoupdate Recent tab
  • Loading branch information
igorkasyanchuk authored May 14, 2022
2 parents 717a14a + 1de1fa3 commit f63f27d
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 36 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
- Unreleased
...

- 1.0.3
- autoupdate Recent tab with recent requests https://github.com/igorkasyanchuk/rails_performance/pull/40

- 1.0.2
- Add home button link customization https://github.com/igorkasyanchuk/rails_performance/pull/36
- Fix navbar toggle https://github.com/igorkasyanchuk/rails_performance/pull/38
Expand Down
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ gem 'mimemagic', '0.4.3'
# gem 'elasticsearch-model'
# gem 'elasticsearch-rails'
gem 'delayed_job_active_record'
gem "daemons"
gem "daemons"

gem 'puma'
10 changes: 8 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
rails_performance (1.0.2)
rails_performance (1.0.3)
rails
redis
redis-namespace
Expand Down Expand Up @@ -114,7 +114,7 @@ GEM
i18n (1.8.10)
concurrent-ruby (~> 1.0)
json (2.3.0)
loofah (2.17.0)
loofah (2.18.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
mail (2.7.1)
Expand All @@ -141,6 +141,8 @@ GEM
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
puma (5.6.4)
nio4r (~> 2.0)
racc (1.5.2)
rack (2.2.3)
rack-accept (0.4.5)
Expand Down Expand Up @@ -204,6 +206,8 @@ GEM
websocket-driver (0.7.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
wrapped_print (0.1.0)
activesupport
zeitwerk (2.4.2)

PLATFORMS
Expand All @@ -216,10 +220,12 @@ DEPENDENCIES
mimemagic (= 0.4.3)
otr-activerecord
pry
puma
rails_performance!
sidekiq (= 5.2.7)
simplecov
sqlite3
wrapped_print

BUNDLED WITH
2.2.3
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module RailsPerformance
class RailsPerformanceController < RailsPerformance::BaseController
include RailsPerformance::ApplicationHelper

protect_from_forgery except: :recent

if RailsPerformance.enabled
def index
@datasource = RailsPerformance::DataSource.new(**prepare_query, type: :requests)
Expand Down Expand Up @@ -51,7 +53,12 @@ def requests
def recent
@datasource = RailsPerformance::DataSource.new(**prepare_query, type: :requests)
db = @datasource.db
@data = RailsPerformance::Reports::RecentRequestsReport.new(db).data
@data = RailsPerformance::Reports::RecentRequestsReport.new(db).data(params[:from_timei])

respond_to do |page|
page.html
page.js
end
end

def sidekiq
Expand Down
28 changes: 27 additions & 1 deletion app/views/rails_performance/javascripts/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,30 @@ function showRTChart(div, data) {
}
}]
});
};
};

const recent = document.getElementById("recent")
const autoupdate = document.getElementById("autoupdate")

if(recent) {
const tbody = recent.querySelector("tbody")

setInterval(() => {
tr = tbody.children[0];
from_timei = tr.getAttribute("from_timei") || ''

if (!autoupdate.checked) {
return;
}

fetch(`/rails/performance/recent.js?from_timei=${from_timei}`, {
headers: {
"X-CSRF-Token": document.querySelector("[name='csrf-token']").content,
},
})
.then(res => res.text())
.then(html => {
tbody.innerHTML = html + tbody.innerHTML;
})
}, 3000);
}
25 changes: 25 additions & 0 deletions app/views/rails_performance/rails_performance/_recent_row.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<tr from_timei="<%= e[:datetimei] %>">
<td>
<% if e[:request_id].present? %>
<%= link_to rails_performance.rails_performance_trace_path(id: e[:request_id]), remote: true do %>
<span class="stats_icon_max"><%= icon 'activity' %></span>
<% end %>
<% end %>
</td>
<td><%= format_datetime e[:datetime] %></td>
<td><%= link_to e[:controller] + '#' + e[:action], rails_performance.rails_performance_summary_path({controller_eq: e[:controller], action_eq: e[:action]}), remote: true %></td>
<td><%= e[:method] %></td>
<td><%= e[:format] %></td>
<td><%= link_to_path(e) %></td>
<td><%= status_tag e[:status] %></td>
<td class="nowrap"><%= ms e[:duration] %></td>
<td class="nowrap"><%= ms e[:view_runtime] %></td>
<td class="nowrap"><%= ms e[:db_runtime] %></td>
<td>
<% if e[:request_id].present? %>
<%= link_to rails_performance.rails_performance_trace_path(id: e[:request_id]), remote: true do %>
<span class="stats_icon_max"><%= icon 'activity' %></span>
<% end %>
<% end %>
</td>
</tr>
41 changes: 14 additions & 27 deletions app/views/rails_performance/rails_performance/recent.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@

<div class="card">
<div class="card-content">
<h2 class="subtitle">Recent Requests (last <%= RailsPerformance::Reports::RecentRequestsReport::TIME_WINDOW / 60 %> minutes)<h2>
<div class="columns">
<div class="column">
<h2 class="subtitle">Recent Requests (last <%= RailsPerformance::Reports::RecentRequestsReport::TIME_WINDOW / 60 %> minutes)<h2>
</div>
<div class="column right-text is-size-5 has-text-right">
<label id="autoupdate_label">
<input id="autoupdate" type="checkbox" checked/>
Auto-update
</label>
</div>
</div>

<table class="table is-fullwidth is-hoverable is-narrow">
<table id="recent" class="table is-fullwidth is-hoverable is-narrow">
<thead>
<tr>
<th data-sort="string"></th>
Expand All @@ -27,33 +37,10 @@
</tr>
<% end %>
<% @data.each do |e| %>
<tr>
<td>
<% if e[:request_id].present? %>
<%= link_to rails_performance.rails_performance_trace_path(id: e[:request_id]), remote: true do %>
<span class="stats_icon_max"><%= icon 'activity' %></span>
<% end %>
<% end %>
</td>
<td><%= format_datetime e[:datetime] %></td>
<td><%= link_to e[:controller] + '#' + e[:action], rails_performance.rails_performance_summary_path({controller_eq: e[:controller], action_eq: e[:action]}), remote: true %></td>
<td><%= e[:method] %></td>
<td><%= e[:format] %></td>
<td><%= link_to_path(e) %></td>
<td><%= status_tag e[:status] %></td>
<td class="nowrap"><%= ms e[:duration] %></td>
<td class="nowrap"><%= ms e[:view_runtime] %></td>
<td class="nowrap"><%= ms e[:db_runtime] %></td>
<td>
<% if e[:request_id].present? %>
<%= link_to rails_performance.rails_performance_trace_path(id: e[:request_id]), remote: true do %>
<span class="stats_icon_max"><%= icon 'activity' %></span>
<% end %>
<% end %>
</td>
</tr>
<%= render 'recent_row', e: e %>
<% end %>
</tbody>
</table>
</div>
</div>

3 changes: 3 additions & 0 deletions app/views/rails_performance/rails_performance/recent.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<% @data.each do |e| %>
<%= render 'recent_row', e: e %>
<% end %>
4 changes: 4 additions & 0 deletions app/views/rails_performance/stylesheets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,8 @@ table th[data-sort] {

.very-small-text {
font-size: 8px;
}

#autoupdate_label {
cursor: pointer;
}
6 changes: 3 additions & 3 deletions lib/rails_performance/reports/recent_requests_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ def set_defaults
@sort ||= :datetimei
end

def data
time_agoi = TIME_WINDOW.ago.to_i
def data(from_timei = nil)
time_agoi = [TIME_WINDOW.ago.to_i, from_timei.to_i].reject(&:blank?).max
db.data
.collect{|e| e.record_hash}
.select{|e| e if e[sort] >= time_agoi}
.select{|e| e if e[sort] > time_agoi}
.sort{|a, b| b[sort] <=> a[sort]}
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rails_performance/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module RailsPerformance
VERSION = '1.0.2'
VERSION = '1.0.3'
SCHEMA = '1.0.1'
end
2 changes: 2 additions & 0 deletions rails_performance.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "mimemagic"
spec.add_development_dependency "delayed_job_active_record"
spec.add_development_dependency "daemons"
spec.add_development_dependency "wrapped_print"
spec.add_development_dependency "puma"
end
3 changes: 3 additions & 0 deletions test/rails_performance_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ def requests_report_data
setup_db
get '/rails/performance/recent'
assert_response :success

get '/rails/performance/recent', xhr: true
assert_response :success
end

test "should get sidekiq with params" do
Expand Down

0 comments on commit f63f27d

Please sign in to comment.