Skip to content

Commit cfcafd2

Browse files
committed
Chapter 4: Data visualization
1 parent 66a833a commit cfcafd2

6 files changed

Lines changed: 104 additions & 0 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require_dependency "active_monitoring/application_controller"
2+
3+
module ActiveMonitoring
4+
class DashboardController < ApplicationController
5+
def show
6+
@dashboard = Dashboard.new
7+
end
8+
end
9+
end
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module ActiveMonitoring
2+
class Dashboard
3+
LIMIT = 10
4+
5+
def initialize(date = Date.current)
6+
@date = date
7+
end
8+
9+
def percentile(value)
10+
response_metrics.percentile(value)
11+
end
12+
13+
def slow_sql_queries
14+
sql_metrics.order(:value).limit(LIMIT)
15+
end
16+
17+
private
18+
19+
attr_reader :date
20+
21+
def sql_metrics
22+
metrics.where(name: "sql.active_record")
23+
end
24+
25+
def response_metrics
26+
metrics.where(name: "process_action.action_controller")
27+
end
28+
29+
def metrics
30+
Metric.where(created_at: date.beginning_of_day..date.end_of_day)
31+
end
32+
end
33+
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
module ActiveMonitoring
22
class Metric < ApplicationRecord
3+
def self.percentile(value)
4+
order(:value).offset(count * value / 10 - 1).limit(1).pluck(:value).first
5+
end
36
end
47
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<h1>Dashboard</h1>
2+
3+
<h2>Response time</h2>
4+
<ul>
5+
<li>90th Percentile: <%= @dashboard.percentile(9) %><li>
6+
<li>50th Percentile: <%= @dashboard.percentile(5) %><li>
7+
</ul>
8+
9+
<h2>Slow queriess</h2>
10+
<table>
11+
<tr>
12+
<th>SQL</th>
13+
<th>Location</th>
14+
<th>Time</th>
15+
</tr>
16+
<% @dashboard.slow_sql_queries.each do |metric| %>
17+
<tr>
18+
<td><%= metric.sql_query %></td>
19+
<td><%= metric.location %></td>
20+
<td><%= metric.value %></td>
21+
</tr>
22+
<% end %>
23+
<table>

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
ActiveMonitoring::Engine.routes.draw do
2+
resource :dashboard, controller: :dashboard, only: :show
23
end

spec/requests/dashboard_spec.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
require "rails_helper"
2+
3+
RSpec.describe "ActiveMonitoring Dashboard", type: :request do
4+
scenario "renders percentile" do
5+
create_metrics("process_action.action_controller", 10)
6+
7+
get active_monitoring.dashboard_path
8+
9+
expect(response.body).to include("90th Percentile: 9")
10+
expect(response.body).to include("50th Percentile: 5")
11+
end
12+
13+
scenario "renders percentile" do
14+
ActiveMonitoring::Metric.create!(
15+
value: 100,
16+
name: "sql.active_record",
17+
sql_query: "SELECT * FROM books;",
18+
location: "BooksController#show"
19+
)
20+
21+
get active_monitoring.dashboard_path
22+
23+
expect(response.body).to include("SELECT * FROM books;")
24+
expect(response.body).to include("BooksController#show")
25+
expect(response.body).to include("100")
26+
end
27+
28+
private
29+
30+
def create_metrics(name, count)
31+
1.upto(count) do |i|
32+
ActiveMonitoring::Metric.create!(value: i, name: name)
33+
end
34+
end
35+
end

0 commit comments

Comments
 (0)