Commit 6786d5a8 by Tấn Trần Thanh

refactor code

parent 97fc4079
Pipeline #1577 canceled with stages
in 0 seconds
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
scope :find_root_ids, ->(year, month, project_ids) {
joins(:project, project: :enabled_modules)
.joins('LEFT OUTER JOIN time_entries ON issues.id = time_entries.issue_id')
.where.not(projects: { status: 9 })
.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])
.distinct.pluck(: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)
}
scope :find_journal, ->(issue_ids) {
Journal.joins(: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)
}
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']
KURUMA_PROJECT_IDS = [139, 138, 94, 90, 93, 120, 128, 147, 121, 116]
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
EST_DETAIL_FIRST_COL = 14
ACTUAL_TIME_DETAIL_FIRST_COL = 21
class << self
def build_report(year, month, project_ids)
github = Github.new oauth_token: $workflow_report_config['github_token']
result = TABLE_HEADER.length.times.map{[]}
root_ids = find_root_ids(year: year, month: month, project_ids: project_ids)
result = TABLE_HEADER.length.times.map { [] }
root_ids = WorkflowReportIssue.find_root_ids(year, month, project_ids)
return unless root_ids.length.positive?
sum_hours_records = find_sum_hours_records(root_ids)
raw_tasks = raw_tasks_records(root_ids)
sum_hours_records = WorkflowReportIssue.find_sum_hours_records(root_ids)
raw_tasks = WorkflowReportIssue.raw_tasks_records(root_ids)
raw_tasks.group_by(&:root_id).each do |root_id, record|
sum_hours_record = sum_hours_records.find { |hr| hr.root_id == root_id }
issue_ids = record.map(&:id)
journals = Journal.joins(: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).to_a
journals = WorkflowReportIssue.find_journal(issue_ids).to_a
result[0] << root_id
result[1] << record.first[:project].gsub(/[^[:print:]]/, '')
......@@ -205,38 +195,5 @@ module WorkflowReport
end
name.strip
end
def find_root_ids(year: 2023, month: 3, project_ids: [])
Issue.joins(:project, project: :enabled_modules)
.joins('LEFT OUTER JOIN time_entries ON issues.id = time_entries.issue_id')
.where.not(projects: { status: 9 })
.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])
.distinct.pluck(:root_id)
end
def find_sum_hours_records(root_ids)
Issue.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)
end
def raw_tasks_records(root_ids)
Issue.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
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