Commit db3cc886 by Tấn Trần Thanh

improve 50% performance

parent 0479aed5
Pipeline #1602 failed with stages
in 0 seconds
......@@ -2,7 +2,7 @@ class WorkflowReportIssue < Issue
belongs_to :workflow_report_version, class_name: 'Version', foreign_key: 'fixed_version_id'
has_many :workflow_report_custom_values, class_name: 'WorkflowReportCustomValue', foreign_key: 'customized_id'
scope :find_root_ids, ->(year, month, project_ids) {
scope :raw_tasks_records, -> (year, month, project_ids) {
includes(:project, :workflow_report_version, :workflow_report_custom_values, :time_entries, :status, project: :enabled_modules)
.where.not(projects: { status: 9 }, issues: { tracker_id: 1 })
.where(projects: { id: project_ids }, enabled_modules: { name: 'time_tracking' })
......
class WorkflowReportJournal < Journal
scope :find_journal_by_issue_ids, ->(issue_ids) {
joins(:details)
scope :find_journal_by_issue_ids, -> (issue_ids) {
includes(:details)
.where(journals: { journalized_id: issue_ids }, journal_details: { prop_key: 'estimated_hours' })
.order(:journalized_id, :id)
.pluck(:journalized_id, :old_value, :value, :created_on, :notes)
......
......@@ -16,32 +16,30 @@ module WorkflowReport
github = Github.new oauth_token: $workflow_report_config['github_token']
error_links = []
result = TABLE_HEADER.length.times.map { [] }
raw_tasks = WorkflowReportIssue.find_root_ids(year, month, project_ids)
raw_tasks = WorkflowReportIssue.raw_tasks_records(year, month, project_ids)
return { workflow_report: [], error_links: [] } if raw_tasks.empty?
raw_tasks.group_by(&:root_id).each do |root_id, record|
issue_ids = record.map(&:id)
raw_tasks.group_by(&:root_id).each do |root_id, issues|
issue_ids = issues.map(&:id)
root_issue = WorkflowReportIssue.find(root_id)
journals = WorkflowReportJournal.find_journal_by_issue_ids(issue_ids).to_a
result[0] << root_id
result[1] << record.first.project.name.gsub(/[^[:print:]]/, '')
subject = WorkflowReportIssue.find(record.first.root_id)&.subject || record.first[:subject]
result[2] << subject.gsub(/[^[:print:]]/, '')
result[3] << record.first.workflow_report_version.name
issue_created_on = record.min_by { |i| i[:created_on] if i[:created_on].present? }
result[1] << issues.first.project.name.gsub(/[^[:print:]]/, '')
result[2] << root_issue&.subject.gsub(/[^[:print:]]/, '')
result[3] << issues.first.workflow_report_version.name
issue_created_on = issues.min_by { |i| i[:created_on] if i[:created_on].present? }
result[4].push(issue_created_on.present? ? issue_created_on.created_on&.strftime('%Y-%m-%d %H:%M:%S') : '')
closed_on_issues = record.map(&:closed_on).compact
closed_on_issues = issues.map(&:closed_on).compact
issue_closed_on = closed_on_issues.max_by { |close_on| close_on }
result[5].push(issue_closed_on.present? ? issue_closed_on.strftime('%Y-%m-%d %H:%M:%S') : '')
issue_due_dates = record.map(&:due_date).compact
issue_due_dates = issues.map(&:due_date).compact
issue_due_date = issue_due_dates.max_by { |due_date| due_date }
result[6].push(issue_due_date.present? ? issue_due_date&.strftime('%Y-%m-%d %H:%M:%S') : '')
result[7].push(record.first.status.name.present? ? record.first.status.name : '')
sum_estimated_hours = record.map { |i| i.estimated_hours }.compact.sum
result[7].push(root_issue.status.name.present? ? root_issue.status.name : '')
sum_estimated_hours = issues.map(&:estimated_hours).compact.sum
result[8] << sum_estimated_hours
actual_time = record.map { |item| item.time_entries.sum(:hours) }.sum
actual_time = issues.map { |issue| issue.time_entries.sum(:hours) }.sum
if actual_time.present?
result[9] << actual_time
result[10] << (actual_time - sum_estimated_hours)
......@@ -62,7 +60,7 @@ module WorkflowReport
end
pull_request = ''
jp_request = ''
record.each do |issue|
issues.each do |issue|
if issue.tracker_id == 12
pull_request = issue.custom_values.find_by(custom_field_id: PR_FIELD_ID).value
jp_request = issue.custom_values.find_by(custom_field_id: JP_REQUEST_FIELD_ID).value
......@@ -80,6 +78,10 @@ module WorkflowReport
end
end
else
6.times do |index|
build_est_to_actual_time!(result, EST_DETAIL_FIRST_COL, 0, index)
build_est_to_actual_time!(result, ACTUAL_TIME_DETAIL_FIRST_COL, 0, index)
end
build_est_to_actual_time!(result, EST_DETAIL_FIRST_COL, issue.estimated_hours.to_f, 6)
build_est_to_actual_time!(result, ACTUAL_TIME_DETAIL_FIRST_COL, issue.spent_hours.to_f, 6)
end
......@@ -89,7 +91,7 @@ module WorkflowReport
(28..31).each { |i| result[i] << '' }
else
BUGS.each_with_index do |(_key, custom_id), index|
result[BUG_COLS + index] << find_max_value_by_custom_field_id(record, custom_id)
result[BUG_COLS + index] << find_max_value_by_custom_field_id(issues, custom_id)
end
end
if jp_request.strip.blank?
......@@ -181,16 +183,16 @@ module WorkflowReport
def build_est_to_actual_time!(result, column, time, index)
if result[column + index][result[0].length - 1].present?
result[column + index][result[0].length - 1] = result[column + index][result[0].length - 1] + time
result[column + index][result[0].length - 1] = result[column + index][result[0].length - 1] + time.round(2)
else
result[column + index].push(time)
result[column + index].push(time.round(2))
end
end
def find_max_value_by_custom_field_id(record, custom_field_id)
def find_max_value_by_custom_field_id(issues, custom_field_id)
max_value = 0
issue_custom = []
record.each do |issue|
issues.each do |issue|
filtered_values = issue.workflow_report_custom_values.select { |custom_value| custom_value.custom_field_id == custom_field_id }
next if filtered_values.empty?
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment