Skip to content

Commit

Permalink
added rendering PDF with insights
Browse files Browse the repository at this point in the history
  • Loading branch information
Bogdanov Anton committed Jul 11, 2024
1 parent b77645f commit 58e5b55
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 50 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## Unreleased
### Added
- rendering PDF with insights

### Modified
- Notification and Webhook models
- edit form of notifications and webhooks
Expand Down
18 changes: 12 additions & 6 deletions app/javascript/components/Company/Company.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,18 @@ export const Company = ({
if (pageState.insightTypes.length === 0) return <p>There are no selected insight attributes 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/companies/${uuid}/insights.pdf`}
>Download insights PDF</a>
</>
);
};

Expand Down
2 changes: 2 additions & 0 deletions app/models/insight.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Insight < ApplicationRecord
average_review_seconds average_merge_seconds average_open_pr_comments bad_reviews_count
].freeze
DECIMAL_ATTRIBUTES = %i[average_open_pr_comments].freeze
TIME_ATTRIBUTES = %i[average_review_seconds average_merge_seconds].freeze
PERCENTILE_ATTRIBUTES = %i[review_involving].freeze

SHORT_ATTRIBUTE_NAMES = {
required_reviews_count: 'Required reviews',
Expand Down
33 changes: 0 additions & 33 deletions app/serializers/helpers/insight.rb

This file was deleted.

41 changes: 36 additions & 5 deletions app/serializers/insight_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,54 @@
# frozen_string_literal: true

class InsightSerializer < ApplicationSerializer
include Helpers::Insight

attributes :id, :values, :entity

def id = SecureRandom.hex

def values
context[:insight_fields].index_with do |insight_field|
value = object[insight_field]
{
value: Insight::DECIMAL_ATTRIBUTES.include?(insight_field.to_sym) ? value.to_f : value,
ratio_value: context[:ratio_enabled] ? compare_with_previous_period(object, insight_field, context) : nil
value: convert_value(object[insight_field], insight_field.to_sym),
ratio_value: context[:ratio_enabled] ? compare_with_previous_period(insight_field) : nil
}.compact
end
end

def entity
context[:no_entity] ? nil : (Rails.cache.read("entity_payload_#{object.entity_id}_v1") || Entity::EMPTY_PAYLOAD)
end

private

def convert_value(value, insight_field)
Insight::DECIMAL_ATTRIBUTES.include?(insight_field) ? value.to_f : value
end

def compare_with_previous_period(insight_field)
previous_insight =
if context[:previous_insights].is_a?(Array)
context[:previous_insights].find { |previous_insight| previous_insight.entity_id == object.entity_id }
else
context[:previous_insights]
end
return change_value(previous_insight, insight_field) if context[:ratio_type] == 'change'

ratio_value(previous_insight, insight_field)
end

def ratio_value(previous_insight, insight_field)
return 0 if previous_insight.nil?

previous_period = previous_insight[insight_field].to_f
return 0 if previous_period.zero?

((object[insight_field].to_f - previous_period) * 100 / previous_period).to_i
end

def change_value(previous_insight, insight_field)
method_name = ::Insight::DECIMAL_ATTRIBUTES.include?(insight_field) ? :to_f : :to_i
return object[insight_field].send(method_name) if previous_insight.nil?

object[insight_field].send(method_name) - previous_insight[insight_field].send(method_name)
end
end
18 changes: 12 additions & 6 deletions app/services/reports/insights/pdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ def to_pdf(insights:, insight_fields:)
move_down(18)

font 'Courier', size: 8
headers_data = ['', 'Developer', *render_headers_data(insight_fields)]
headers_data = ['Developer', *render_headers_data(insight_fields)]
insights.map! { |insight| render_insight(insight, insight_fields) }

table(
[
headers_data + insights
headers_data,
*insights
],
**table_options
)
Expand All @@ -32,16 +33,19 @@ def render_headers_data(insight_fields)

def render_insight(insight, insight_fields)
[
{
image: URI.parse(insight.dig('entity', 'avatar_url')).open, image_height: 12, image_width: 12
},
insight.dig('entity', 'login'),
*render_insight_values(insight['values'].symbolize_keys, insight_fields)
]
end

def render_insight_values(values, insight_fields)
insight_fields.map { |insight_field| values.dig(insight_field, 'value') }
insight_fields.map do |insight_field|
value = values.dig(insight_field, 'value')
next "#{value}%" if Insight::PERCENTILE_ATTRIBUTES.include?(insight_field)
next time_representer.call(value: value.to_i) if Insight::TIME_ATTRIBUTES.include?(insight_field)

value
end
end

def table_options
Expand All @@ -54,6 +58,8 @@ def table_options
}
}
end

def time_representer = ::Pullmetry::Container['services.converters.seconds_to_text']
end
end
end

0 comments on commit 58e5b55

Please sign in to comment.