Skip to content

Commit

Permalink
Chapter 4: Data visualization
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisBr committed Apr 4, 2020
1 parent 66a833a commit cfcafd2
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 0 deletions.
9 changes: 9 additions & 0 deletions app/controllers/active_monitoring/dashboard_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require_dependency "active_monitoring/application_controller"

module ActiveMonitoring
class DashboardController < ApplicationController
def show
@dashboard = Dashboard.new
end
end
end
33 changes: 33 additions & 0 deletions app/models/active_monitoring/dashboard.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module ActiveMonitoring
class Dashboard
LIMIT = 10

def initialize(date = Date.current)
@date = date
end

def percentile(value)
response_metrics.percentile(value)
end

def slow_sql_queries
sql_metrics.order(:value).limit(LIMIT)
end

private

attr_reader :date

def sql_metrics
metrics.where(name: "sql.active_record")
end

def response_metrics
metrics.where(name: "process_action.action_controller")
end

def metrics
Metric.where(created_at: date.beginning_of_day..date.end_of_day)
end
end
end
3 changes: 3 additions & 0 deletions app/models/active_monitoring/metric.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module ActiveMonitoring
class Metric < ApplicationRecord
def self.percentile(value)
order(:value).offset(count * value / 10 - 1).limit(1).pluck(:value).first
end
end
end
23 changes: 23 additions & 0 deletions app/views/active_monitoring/dashboard/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<h1>Dashboard</h1>

<h2>Response time</h2>
<ul>
<li>90th Percentile: <%= @dashboard.percentile(9) %><li>
<li>50th Percentile: <%= @dashboard.percentile(5) %><li>
</ul>

<h2>Slow queriess</h2>
<table>
<tr>
<th>SQL</th>
<th>Location</th>
<th>Time</th>
</tr>
<% @dashboard.slow_sql_queries.each do |metric| %>
<tr>
<td><%= metric.sql_query %></td>
<td><%= metric.location %></td>
<td><%= metric.value %></td>
</tr>
<% end %>
<table>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
ActiveMonitoring::Engine.routes.draw do
resource :dashboard, controller: :dashboard, only: :show
end
35 changes: 35 additions & 0 deletions spec/requests/dashboard_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require "rails_helper"

RSpec.describe "ActiveMonitoring Dashboard", type: :request do
scenario "renders percentile" do
create_metrics("process_action.action_controller", 10)

get active_monitoring.dashboard_path

expect(response.body).to include("90th Percentile: 9")
expect(response.body).to include("50th Percentile: 5")
end

scenario "renders percentile" do
ActiveMonitoring::Metric.create!(
value: 100,
name: "sql.active_record",
sql_query: "SELECT * FROM books;",
location: "BooksController#show"
)

get active_monitoring.dashboard_path

expect(response.body).to include("SELECT * FROM books;")
expect(response.body).to include("BooksController#show")
expect(response.body).to include("100")
end

private

def create_metrics(name, count)
1.upto(count) do |i|
ActiveMonitoring::Metric.create!(value: i, name: name)
end
end
end

0 comments on commit cfcafd2

Please sign in to comment.