Commit cfcf0b0c by Tấn Trần Thanh

improve query better 50%

parent 1735d895
......@@ -45,9 +45,10 @@ class WorkflowReportController < ApplicationController
def export
team = params[:team]
project_ids = $workflow_report_config['teams'].select { |hash| hash.key?(team) }[0][team]
# result = Rails.cache.fetch("#{team}_#{params[:year]}_#{params[:month]}", expires_in: 1.hours) do
result= WorkflowReport.build_report(params[:year].to_i, params[:month].to_i, project_ids)
#end
result = Rails.cache.fetch("#{team}_#{params[:year]}_#{params[:month]}", expires_in: 1.hours) do
WorkflowReport.build_report(params[:year].to_i, params[:month].to_i, project_ids)
end
respond_to do |format|
format.js { render 'build_table', locals: { result: result[:workflow_report], error_links: result[:error_links], thead: TABLE_HEADER } }
end
......
class WorkflowReportIssue < Issue
TESTCASE_FIELD_ID = 22
BUGS_FIELD_ID = 23
STG_BUGS_FIELD_ID = 27
PROD_BUGS_FIELD_ID = 28
PR_FIELD_ID = 18
JP_REQUEST_FIELD_ID = 16
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) {
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(root_id: 59050)
.where(projects: { id: project_ids }, enabled_modules: { name: 'time_tracking' })
.where(['((time_entries.spent_on IS NOT NULL AND time_entries.tyear = ? AND time_entries.tmonth = ?) OR (issues.closed_on BETWEEN ? AND ?))',
year, month, DateTime.new(year, month).beginning_of_day, DateTime.new(year, month, -1).end_of_day])
.order(:root_id)
}
scope :find_sum_hours_records, ->(root_ids) {
select(:root_id, 'sum(time_entries.hours) as hours', 'max(IFNULL(c1.value,-1)) as testcases', 'max(IFNULL(c2.value,-1)) as bugs', 'max(IFNULL(stg.value,-1)) as stg_bugs', 'max(IFNULL(prod.value,-1)) as prod_bugs')
.joins(:time_entries)
.joins("LEFT OUTER JOIN custom_values c1 ON c1.customized_id = issues.id and c1.custom_field_id=#{TESTCASE_FIELD_ID}")
.joins("LEFT OUTER JOIN custom_values c2 ON c2.customized_id = issues.id and c2.custom_field_id=#{BUGS_FIELD_ID}")
.joins("LEFT OUTER JOIN custom_values stg ON stg.customized_id = issues.id and stg.custom_field_id=#{STG_BUGS_FIELD_ID}")
.joins("LEFT OUTER JOIN custom_values prod ON prod.customized_id = issues.id and prod.custom_field_id=#{PROD_BUGS_FIELD_ID}")
.where(issues: { root_id: root_ids })
.where.not(issues: { tracker_id: 1 })
.group(:root_id)
}
scope :raw_tasks_records, ->(root_ids) {
select(:root_id, :id, :tracker_id, :subject, :due_date, :created_on, :closed_on, :estimated_hours, 'issue_statuses.name as status', 'versions.name as target_version', 'projects.name as project', 'pr.value as pr', 'jr.value as jp_request')
.joins(:project, :status)
.joins('LEFT JOIN versions ON issues.fixed_version_id = versions.id')
.joins("LEFT OUTER JOIN custom_values jr ON jr.customized_id = issues.id and jr.custom_field_id=#{JP_REQUEST_FIELD_ID}")
.joins("LEFT OUTER JOIN custom_values pr ON pr.customized_id = issues.id and pr.custom_field_id=#{PR_FIELD_ID}")
.where(issues: { root_id: root_ids })
.where.not(issues: { tracker_id: 1 })
.order(:root_id)
}
end
module WorkflowReport
TABLE_HEADER = ['root_id', 'project', 'subject', 'target_version', 'created_on ', 'closed_on ', 'due_date ', 'status ', 'Estimated_hours (*final version)', 'Actual time', 'Diff', 'Estimated_hours (*Initial version)',
'Number of estimation changes', 'Note of estimation changes', '1. Requirement', '2. Design', '3. Coding', '4. Testing', '5. Bug fixing', '6. Release', 'Others', '1. Requirement', '2. Design', '3. Coding',
'4. Testing', '5. Bug fixing', '6. Release', 'Others', 'testcases', 'vn STG bug', 'Jp STG bug', 'Production', 'Issue', 'Issue comment', 'PR comment', 'Review comment', 'Commits', 'File changed', 'Addtion', 'Deletetion']
'4. Testing', '5. Bug fixing', '6. Release', 'Others', 'testcases', 'vn STG bug', 'Jp STG bug', 'Production', 'Issue', 'Issue comment', 'PR comment', 'Review comment', 'Commits', 'File changed', 'Addtion', 'Deletetion'].freeze
EST_DETAIL_FIRST_COL = 14
ACTUAL_TIME_DETAIL_FIRST_COL = 21
PR_CMT_COL = 34
PROCESS = ['1. Requirement', '2. Design', '3. Coding', '4. Testing', '5. Bug fixing', '6. Release', '']
BUGS = { testcases: 28, bugs: 29, stg_bugs: 30, prod_bugs: 31 }
PR_FIELD_ID = 18
JP_REQUEST_FIELD_ID = 16
BUG_COLS = 28
PROCESS = ['1. Requirement', '2. Design', '3. Coding', '4. Testing', '5. Bug fixing', '6. Release', ''].freeze
BUGS = { testcases: 22, bugs: 23, stg_bugs: 27, prod_bugs: 28 }.freeze
class << self
def build_report(year, month, project_ids)
......@@ -21,12 +24,11 @@ module WorkflowReport
raw_tasks.group_by(&:root_id).each do |root_id, record|
issue_ids = record.map(&:id)
sum_hours_record = record.first.time_entries.all
journals = WorkflowReportJournal.find_journal_by_issue_ids(issue_ids).to_a
result[0] << root_id
result[1] << record.first.project.name.gsub(/[^[:print:]]/, '')
result[2] << record.first[:subject].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[4].push(issue_created_on.present? ? issue_created_on.created_on&.strftime('%Y-%m-%d %H:%M:%S') : '')
......@@ -41,7 +43,7 @@ module WorkflowReport
result[7].push(record.first.status.name.present? ? record.first.status.name : '')
sum_estimated_hours = record.map { |i| i.estimated_hours }.compact.sum
result[8] << sum_estimated_hours
actual_time = record.map { |item| item.time_entries.all.map(&:hours).sum }.sum
actual_time = record.map { |item| item.time_entries.sum(:hours) }.sum
if actual_time.present?
result[9] << actual_time
result[10] << (actual_time - sum_estimated_hours)
......@@ -62,14 +64,10 @@ module WorkflowReport
end
pull_request = ''
jp_request = ''
test_case = 0
staging_bug = 0
jp_bug = 0
proc_bug = 0
record.each do |issue|
if issue.tracker_id == 12
pull_request = issue.custom_values.find_by(custom_field_id: 18).value
jp_request = issue.custom_values.find_by(custom_field_id: 16).value
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
end
process = get_process(issue.subject.gsub(/[^[:print:]]/, ''))
......@@ -87,20 +85,14 @@ module WorkflowReport
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
test = issue.custom_values.where(custom_field_id: 22)
end
if sum_hours_record.nil?
if sum_estimated_hours.zero?
# testcases, internal bug, stg bug, prod bug
(28..31).each { |i| result[i] << '' }
else
byebug
a=record.select{|item| p item.custom_values.where('custom_field_id= 22').length != 0}
# testcases, internal bug, stg bug, prod bug
# BUGS.each do |status, column|
# rs = record.map { |item| item.time_entries.all.map(&:hours).sum }.sum
# result[column].push(sum_hours_record[status].to_i >= 0 ? sum_hours_record[status] : '')
# end
BUGS.each_with_index do |(_key, custom_id), index|
result[BUG_COLS + index] << find_max_value_by_custom_field_id(record, custom_id)
end
end
if jp_request.present?
jp_request = jp_request.strip
......@@ -110,8 +102,10 @@ module WorkflowReport
if jp_request_arr.length >= 4 && jp_request_arr[3].to_i.positive?
issue_detail = github.issues.find user: jp_request_arr[0], repo: jp_request_arr[1], number: jp_request_arr[3]
result[33].push(issue_detail.success? ? issue_detail.comments : '')
else
result[33].push('') if result[33][result[0].length - 1].nil?
end
rescue => e
rescue StandardError => e
error_links << "##{root_id} Error occurred during the build with the ISSUE link:\r\n - #{jp_request} \r\n - #{e.message.gsub(/(?<=access_token=).*/, '123')}"
result[33].push('')
end
......@@ -138,7 +132,7 @@ module WorkflowReport
pr[:pr_additions] += pr_detail.additions
pr[:pr_deletions] += pr_detail.deletions
end
rescue => e
rescue StandardError => e
error_links << "##{root_id} Error occurred during the build with the PR link:\r\n - #{link} \r\n - #{e.message.gsub(/(?<=access_token=).*/, '123')}"
end
end
......@@ -194,5 +188,20 @@ module WorkflowReport
result[column + index].push(time)
end
end
def find_max_value_by_custom_field_id(record, custom_field_id)
max_value = 0
issue_custom = []
record.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?
issue_custom = filtered_values
end
max_value_in_record = issue_custom.map { |custom| custom.value.to_i }.max
[max_value, max_value_in_record.to_i].max
end
end
end
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