Skip to content

Commit

Permalink
Merge pull request #100 from tcarac/csv_export
Browse files Browse the repository at this point in the history
feat: Add CSV Export
  • Loading branch information
igorkasyanchuk authored Oct 28, 2024
2 parents f5a6879 + a2387a1 commit 3a234dc
Show file tree
Hide file tree
Showing 14 changed files with 206 additions and 40 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ test/dummy/storage/
test/dummy/tmp/

*.gem

coverage/
coverage/
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
- Unreleased

- 1.3.0
- added csv export

- 1.2.3
- typo fix https://github.com/igorkasyanchuk/rails_performance/pull/91
- added ignored_paths https://github.com/igorkasyanchuk/rails_performance/pull/96
Expand Down
3 changes: 3 additions & 0 deletions app/assets/images/download.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 7 additions & 7 deletions app/controllers/rails_performance/base_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module RailsPerformance
class BaseController < ActionController::Base
layout 'rails_performance/layouts/rails_performance'
include RailsPerformance::Concerns::CsvExportable
layout "rails_performance/layouts/rails_performance"

before_action :verify_access

Expand All @@ -12,10 +13,9 @@ class BaseController < ActionController::Base

private

def verify_access
result = RailsPerformance.verify_access_proc.call(self)
redirect_to('/', error: 'Access Denied', status: 401) unless result
end

def verify_access
result = RailsPerformance.verify_access_proc.call(self)
redirect_to("/", error: "Access Denied", status: 401) unless result
end
end
end
end
29 changes: 29 additions & 0 deletions app/controllers/rails_performance/concerns/csv_exportable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require "csv"
module RailsPerformance
module Concerns
module CsvExportable
extend ActiveSupport::Concern

def export_to_csv(filename, data)
return if data.blank?

send_data generate_csv(data),
filename: "#{filename}_#{Time.zone.today}.csv",
type: "text/csv",
disposition: "attachment"
end

private

def generate_csv(data)
CSV.generate(headers: true) do |csv|
headers = data.first.keys
csv << headers.map(&:to_s)
data.each do |entry|
csv << headers.map { |header| entry[header].to_s }
end
end
end
end
end
end
90 changes: 68 additions & 22 deletions app/controllers/rails_performance/rails_performance_controller.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
require_relative './base_controller.rb'
require_relative "base_controller"

module RailsPerformance
class RailsPerformanceController < RailsPerformance::BaseController

protect_from_forgery except: :recent

if RailsPerformance.enabled
def index
@datasource = RailsPerformance::DataSource.new(**prepare_query, type: :requests)
@datasource = RailsPerformance::DataSource.new(
**prepare_query, type: :requests
)
db = @datasource.db

@throughput_report_data = RailsPerformance::Reports::ThroughputReport.new(db).data
@response_time_report_data = RailsPerformance::Reports::ResponseTimeReport.new(db).data
end

def summary
@datasource = RailsPerformance::DataSource.new(**prepare_query, type: :requests)
@datasource = RailsPerformance::DataSource.new(
**prepare_query, type: :requests
)
db = @datasource.db

@throughput_report_data = RailsPerformance::Reports::ThroughputReport.new(db).data
@response_time_report_data = RailsPerformance::Reports::ResponseTimeReport.new(db).data
@data = RailsPerformance::Reports::BreakdownReport.new(db, title: "Requests").data

@data = RailsPerformance::Reports::BreakdownReport.new(
db, title: "Requests"
).data
respond_to do |format|
format.js {}
format.any { render plain: "Doesn't open in new window. Wait until full page load." }
format.any do
render plain: "Doesn't open in new window. Wait until full page load."
end
end
end

Expand All @@ -33,24 +39,44 @@ def trace
@data = RailsPerformance::Reports::TraceReport.new(request_id: params[:id]).data
respond_to do |format|
format.js {}
format.any { render plain: "Doesn't open in new window. Wait until full page load." }
format.any do
render plain: "Doesn't open in new window. Wait until full page load."
end
end
end

def crashes
@datasource = RailsPerformance::DataSource.new(**prepare_query({status_eq: 500}), type: :requests)
@datasource = RailsPerformance::DataSource.new(
**prepare_query({ status_eq: 500 }), type: :requests
)
db = @datasource.db
@data = RailsPerformance::Reports::CrashReport.new(db).data

respond_to do |format|
format.html
format.csv do
export_to_csv "error_report", @data
end
end
end

def requests
@datasource = RailsPerformance::DataSource.new(**prepare_query, type: :requests)
@datasource = RailsPerformance::DataSource.new(**prepare_query,
type: :requests)
db = @datasource.db
@data = RailsPerformance::Reports::RequestsReport.new(db, group: :controller_action_format, sort: :count).data
@data = RailsPerformance::Reports::RequestsReport.new(db,
group: :controller_action_format, sort: :count).data
respond_to do |format|
format.html
format.csv do
export_to_csv "requests_report", @data
end
end
end

def recent
@datasource = RailsPerformance::DataSource.new(**prepare_query, type: :requests)
@datasource = RailsPerformance::DataSource.new(**prepare_query,
type: :requests)
db = @datasource.db
@data = RailsPerformance::Reports::RecentRequestsReport.new(db).data(params[:from_timei])

Expand All @@ -73,51 +99,72 @@ def recent
# "email"=>nil,
# "user_agent"=>"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"}]

respond_to do |page|
page.html
page.js
respond_to do |format|
format.html
format.js
format.csv do
export_to_csv "recent_requests_report", @data
end
end
end

def slow
@datasource = RailsPerformance::DataSource.new(**prepare_query, type: :requests)
@datasource = RailsPerformance::DataSource.new(**prepare_query,
type: :requests)
db = @datasource.db
@data = RailsPerformance::Reports::SlowRequestsReport.new(db).data

respond_to do |format|
format.html
format.csv do
export_to_csv "slow_requests_report", @data
end
end
end

def sidekiq
@datasource = RailsPerformance::DataSource.new(**prepare_query, type: :sidekiq)
@datasource = RailsPerformance::DataSource.new(
**prepare_query, type: :sidekiq
)
db = @datasource.db
@throughput_report_data = RailsPerformance::Reports::ThroughputReport.new(db).data
@response_time_report_data = RailsPerformance::Reports::ResponseTimeReport.new(db).data
@recent_report_data = RailsPerformance::Reports::RecentRequestsReport.new(db).data
end

def delayed_job
@datasource = RailsPerformance::DataSource.new(**prepare_query, type: :delayed_job)
@datasource = RailsPerformance::DataSource.new(
**prepare_query, type: :delayed_job
)
db = @datasource.db
@throughput_report_data = RailsPerformance::Reports::ThroughputReport.new(db).data
@response_time_report_data = RailsPerformance::Reports::ResponseTimeReport.new(db).data
@recent_report_data = RailsPerformance::Reports::RecentRequestsReport.new(db).data
end

def custom
@datasource = RailsPerformance::DataSource.new(**prepare_query, type: :custom)
@datasource = RailsPerformance::DataSource.new(
**prepare_query, type: :custom
)
db = @datasource.db
@throughput_report_data = RailsPerformance::Reports::ThroughputReport.new(db).data
@response_time_report_data = RailsPerformance::Reports::ResponseTimeReport.new(db).data
@recent_report_data = RailsPerformance::Reports::RecentRequestsReport.new(db).data
end

def grape
@datasource = RailsPerformance::DataSource.new(**prepare_query, type: :grape)
@datasource = RailsPerformance::DataSource.new(
**prepare_query, type: :grape
)
db = @datasource.db
@throughput_report_data = RailsPerformance::Reports::ThroughputReport.new(db).data
@recent_report_data = RailsPerformance::Reports::RecentRequestsReport.new(db).data
end

def rake
@datasource = RailsPerformance::DataSource.new(**prepare_query, type: :rake)
@datasource = RailsPerformance::DataSource.new(
**prepare_query, type: :rake
)
db = @datasource.db
@throughput_report_data = RailsPerformance::Reports::ThroughputReport.new(db).data
@recent_report_data = RailsPerformance::Reports::RecentRequestsReport.new(db).data
Expand All @@ -129,6 +176,5 @@ def prepare_query(query = params)
RailsPerformance::Rails::QueryBuilder.compose_from(query)
end
end

end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= link_to request.original_url + '.csv', class: 'icon download_icon' do %>
<%= icon('download') %>
<% end %>
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
<div class="card">
<div class="card-content">
<h2 class="subtitle">Crash Report</h2>
<div class="columns is-vcentered">
<div class="column">
<h2 class="subtitle">Crash Report</h2>
</div>
<div class="column is-narrow">
<%= render "export" %>
</div>
</div>
<table class="table is-fullwidth is-hoverable is-narrow">
<thead>
<tr>
Expand Down
14 changes: 8 additions & 6 deletions app/views/rails_performance/rails_performance/recent.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
<div class="column">
<h2 class="subtitle">Recent Requests (last <%= RailsPerformance.recent_requests_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"/>
Auto-update
</label>
<div class="column is-narrow">
<%= render "export" %>
</div>
</div>

<div class="has-text-right">
<label id="autoupdate_label">
<input id="autoupdate" type="checkbox" />
Auto-update
</label>
</div>
<table id="recent" class="table is-fullwidth is-hoverable is-narrow">
<thead>
<tr>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
<div class="card">
<div class="card-content">
<h2 class="subtitle">Requests (path, total number, average response time)</h2>
<div class="columns is-vcentered">
<div class="column">
<h2 class="subtitle">Requests (path, total number, average response time)</h2>
</div>
<div class="column is-narrow">
<%= render "export" %>
</div>
</div>
<table class="table is-fullwidth is-hoverable is-narrow">
<thead>
<tr>
Expand Down
3 changes: 3 additions & 0 deletions app/views/rails_performance/rails_performance/slow.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
<div class="column">
<h2 class="subtitle">Slow Requests (last <%= RailsPerformance.slow_requests_time_window / 60 %> minutes + slower than <%= RailsPerformance.slow_requests_threshold %>ms)<h2>
</div>
<div class="column is-narrow">
<%= render "export" %>
</div>
</div>

<table id="recent" class="table is-fullwidth is-hoverable is-narrow">
Expand Down
1 change: 0 additions & 1 deletion app/views/rails_performance/shared/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
<div class="navbar-end">
<div class="navbar-item">
<div class="buttons">
<%#= link_to 'Export to CSV', '#', target: '_blank', class: "button is-primary" %>
<%= link_to RailsPerformance.home_link, class: "button is-light home_icon" do %>
<%= icon('home') %>
<% end %>
Expand Down
6 changes: 6 additions & 0 deletions app/views/rails_performance/stylesheets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@
height: 24px;
}

.download_icon svg {
width: 24px;
height: 24px;
color: red;
}

.user-agent-icon svg {
width: 12px;
height: 12px;
Expand Down
Loading

0 comments on commit 3a234dc

Please sign in to comment.