-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |