Skip to content

Commit

Permalink
render basis for PDF report
Browse files Browse the repository at this point in the history
  • Loading branch information
kortirso committed Jul 9, 2024
1 parent e3b6399 commit c974318
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 17 deletions.
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ gem 'recaptcha', require: 'recaptcha/rails'
gem 'rswag-api'
gem 'rswag-ui'

# PDF generating
gem 'prawn'
gem 'prawn-table'

group :development, :test do
gem 'bullet', git: 'https://github.com/flyerhzm/bullet', branch: 'main'
gem 'cypress-on-rails', '~> 1.0'
Expand Down
12 changes: 12 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ GEM
net-pop
net-smtp
marcel (1.0.4)
matrix (0.4.2)
method_source (1.1.0)
mini_mime (1.1.5)
minitest (5.24.1)
Expand Down Expand Up @@ -281,9 +282,16 @@ GEM
parser (3.3.3.0)
ast (~> 2.4.1)
racc
pdf-core (0.10.0)
pg (1.5.6)
pghero (3.5.0)
activerecord (>= 6)
prawn (2.5.0)
matrix (~> 0.4)
pdf-core (~> 0.10.0)
ttfunk (~> 1.8)
prawn-table (0.2.2)
prawn (>= 1.3.0, < 3.0.0)
psych (5.1.2)
stringio
public_suffix (6.0.0)
Expand Down Expand Up @@ -466,6 +474,8 @@ GEM
thor (1.3.1)
tilt (2.4.0)
timeout (0.4.1)
ttfunk (1.8.0)
bigdecimal (~> 3.1)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.5.0)
Expand Down Expand Up @@ -520,6 +530,8 @@ DEPENDENCIES
pagy (~> 8.0)
pg (~> 1.4)
pghero
prawn
prawn-table
puma (~> 6.0)
que (~> 2.3)
que-view (~> 0.3)
Expand Down
4 changes: 0 additions & 4 deletions app/assets/stylesheets/application.tailwind.css
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,6 @@
@apply bg-white;
}

.table.zebra tbody it:nth-of-type(2n) {
@apply bg-stone-200;
}

.form-field {
@apply mb-4;
}
Expand Down
35 changes: 28 additions & 7 deletions app/controllers/api/frontend/insights_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,33 @@ class InsightsController < Api::Frontend::BaseController
before_action :find_insightable

def index
render json: {
insights: InsightSerializer.new(
respond_to do |format|
format.json { render json: json_response, status: :ok }
format.pdf { send_data pdf_response, type: 'application/pdf', filename: 'insights.pdf' }
end
end

private

def json_response
{
insights: insights_json,
ratio_type: ratio_enabled? ? ratio_type : nil
}.compact
end

def pdf_response
Reports::Insights::Pdf.new(
page_size: Reports::Insights::Pdf::PAGE_SIZE,
page_layout: Reports::Insights::Pdf::PAGE_LAYOUT
).to_pdf(
insights: insights_json,
insight_fields: insight_fields
)
end

def insights_json
InsightSerializer.new(
actual_insights,
{
params: {
Expand All @@ -17,13 +42,9 @@ def index
ratio_type: ratio_type
}
}
).serializable_hash,
ratio_type: ratio_enabled? ? ratio_type : nil
}.compact, status: :ok
).serializable_hash
end

private

def find_insightable
find_company if params[:company_id]
find_repository if params[:repository_id]
Expand Down
18 changes: 12 additions & 6 deletions app/javascript/components/Repository/Repository.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,18 @@ export const Repository = ({
if (pageState.entities.length === 0) return <p>There are no insights yet</p>;

return (
<Insights
insightTypes={pageState.insightTypes}
entities={pageState.entities}
ratioType={pageState.ratioType}
mainAttribute={main_attribute}
/>
<>
<Insights
insightTypes={pageState.insightTypes}
entities={pageState.entities}
ratioType={pageState.ratioType}
mainAttribute={main_attribute}
/>
<a
className="btn-primary btn-small mt-4"
href={`/api/frontend/repositories/${uuid}/insights.pdf`}
>Download insights PDF</a>
</>
);
};

Expand Down
49 changes: 49 additions & 0 deletions app/services/reports/insights/pdf.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

module Reports
module Insights
class Pdf < Prawn::Document
PAGE_SIZE = 'A4'.freeze
PAGE_LAYOUT = :landscape

def to_pdf(insights:, insight_fields:)
text 'Insights report'
move_down(18)

font 'Courier', size: 8
data = []
data << ['', 'Developer', *headers_data(insight_fields)]

insights[:data].each do |insight|
data << [
{ image: URI.open(insight.dig(:attributes, :entity, :avatar_url)), image_height: 12, image_width: 12 },
insight.dig(:attributes, :entity, :login),
*insight_data(insight.dig(:attributes, :values), insight_fields)
]
end

table(
data,
header: true,
row_colors: ['ffffff', 'f3f4f6'],
cell_style: {
border_width: 1,
border_color: 'e5e7eb'
}
)

render
end

private

def headers_data(insight_fields)
insight_fields.map { |insight_field| Insight::SHORT_ATTRIBUTE_NAMES[insight_field] }
end

def insight_data(values, insight_fields)
insight_fields.map { |insight_field| values.dig(insight_field, :value) }
end
end
end
end
7 changes: 7 additions & 0 deletions db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

--
-- Name: public; Type: SCHEMA; Schema: -; Owner: -
--

-- *not* creating schema, since initdb creates it


--
-- Name: pg_stat_statements; Type: EXTENSION; Schema: -; Owner: -
--
Expand Down

0 comments on commit c974318

Please sign in to comment.