Commit 4e639d71 by Tấn Trần Thanh

refactor code

parent 63e50323
gem 'addressable', '~> 2.5', '>= 2.5.2'
gem 'descendants_tracker', '~> 0.0.4'
gem 'faraday', '~> 0.17.6'
gem 'hashie', '~> 3.5', '>= 3.5.7'
gem 'oauth2', '~> 1.2'
gem 'github_api', '~> 0.19.0'
= workflow_report
Description goes here
This plugin is designed to produce monthly workflow reports with precise customization options. Users can select a specific month, year, and team of interest. Upon making these selections, the plugin generates a detailed report that is exclusively based on the chosen month and year.
class WorkflowReportController < ApplicationController
include WorkflowReport
def index
@thead = ['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']
github = Github.new oauth_token: "your github token"
kuruma_project_ids = [139, 138, 94, 90, 93, 120, 128, 147, 121, 116]
@result = Array.new(@thead.length)
root_ids = Issue.select('issues.root_id')
.joins('INNER JOIN projects ON projects.id = issues.project_id')
.joins('LEFT OUTER JOIN time_entries ON issues.id = time_entries.issue_id')
.joins('INNER JOIN enabled_modules ON enabled_modules.project_id = projects.id')
.where('projects.status <> 9')
.where("projects.id IN (#{kuruma_project_ids.join(',')})")
.where(['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 ?))',
2023,
3,
DateTime.new(2023, 3).beginning_of_day,
DateTime.new(2023, 3, -1).end_of_day
])
.distinct.pluck(:root_id)
return unless root_ids.length > 0
sum_hours_records = Issue.select('issues.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('INNER JOIN time_entries ON issues.id = time_entries.issue_id')
.joins('LEFT OUTER JOIN custom_values c1 ON c1.customized_id = issues.id and c1.custom_field_id=22')
.joins('LEFT OUTER JOIN custom_values c2 ON c2.customized_id = issues.id and c2.custom_field_id=23')
.joins('LEFT OUTER JOIN custom_values stg ON stg.customized_id = issues.id and stg.custom_field_id=27')
.joins('LEFT OUTER JOIN custom_values prod ON prod.customized_id = issues.id and prod.custom_field_id=28')
.where("issues.root_id IN (#{root_ids.join(',')})")
.where('issues.tracker_id <> 1')
.group('issues.root_id')
raw_tasks_records = Issue.select('issues.root_id, issues.id, tracker_id, issues.subject, issues.due_date, issues.created_on, issues.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('INNER JOIN projects ON projects.id = issues.project_id')
.joins('INNER JOIN issue_statuses ON issue_statuses.id = issues.status_id')
.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=16')
.joins('LEFT OUTER JOIN custom_values pr ON pr.customized_id = issues.id and pr.custom_field_id=18')
.where("issues.root_id IN (#{root_ids.join(',')})")
.where('issues.tracker_id <> 1')
.order('issues.root_id')
@result.map! { |item| item = [] }
raw_tasks_records.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 = ActiveRecord::Base.connection.execute("select journalized_id, old_value, value, created_on, notes FROM journals
INNER JOIN journal_details ON journals.id = journal_id WHERE journalized_id IN (#{issue_ids.join(',')})
AND prop_key = 'estimated_hours' ORDER BY journalized_id, journals.id").to_a
@result[0] << root_id
@result[1] << record.first[:project].gsub(/[^[:print:]]/, '')
@result[2] << record.first[:subject].gsub(/[^[:print:]]/, '')
@result[3] << record.first[:target_version]
issue_created_on = record.min_by { |i| i[:created_on] if i[:created_on].present? }
if issue_created_on.present?
@result[4] << issue_created_on[:created_on]&.strftime('%Y-%m-%d %H:%M:%S')
else
@result[4] << nil
end
issue_closed_on = record.max_by { |i| i[:closed_on] if i[:closed_on].present? }
if issue_closed_on.present?
@result[5] << issue_closed_on[:closed_on]&.strftime('%Y-%m-%d %H:%M:%S')
else
@result[5] << nil
end
issue_due_dates = record.map(&:due_date).compact
issue_due_date = issue_due_dates.max_by { |a| a }
if issue_due_date.present?
@result[6] << issue_due_date&.strftime('%Y-%m-%d %H:%M:%S')
else
@result[6] << nil
end
@result[7] << record.first[:status]
sum_estimated_hours = record.map { |i| i[:estimated_hours] }.compact.sum
@result[8] << sum_estimated_hours
if sum_hours_record.present?
@result[9] << sum_hours_record[:hours]
@result[10] << (sum_hours_record[:hours] - sum_estimated_hours)
else
@result[9] << nil
@result[10] << nil
end
if journals.length > 0
number_of_est_changes = journals.count { |j| !j[1].nil? } || nil
est_changes = journals.reject { |j| j[1].nil? }.select { |e| e[4].present? }
notes = est_changes.inject("") { |all, i| all + i[4] + "\n" } || nil
original_est_hours = journals.group_by(&:shift).inject(0) { |sum, i| sum + i[1][0][1].to_f unless i.nil? }
@result[11] << original_est_hours
@result[12] << number_of_est_changes
@result[13] << notes
else
@result[11] << nil
@result[12] << nil
@result[13] << nil
end
pull_request = ''
jp_request = ''
record.each do |issue|
if issue.tracker_id == 12
pull_request = issue.pr
jp_request = issue.jp_request
end
process = get_process(issue.subject.gsub(/[^[:print:]]/, ''))
index_1 = 14 # first index for estimation detail
index_2 = 21 # first index for actual detail
case process
when '1. Requirement'
@result[index_1] << issue.estimated_hours if issue.estimated_hours.present?
@result[index_2] << +issue.spent_hours if issue.spent_hours > 0
when '2. Design'
@result[index_1 + 1] << issue.estimated_hours if issue.estimated_hours.present?
@result[index_2 + 1] << issue.spent_hours if issue.spent_hours > 0
when '3. Coding'
@result[index_1 + 2] << issue.estimated_hours if issue.estimated_hours.present?
@result[index_2 + 2] << issue.spent_hours if issue.spent_hours > 0
when '4. Testing'
@result[index_1 + 3] << issue.estimated_hours if issue.estimated_hours.present?
@result[index_2 + 3] << issue.spent_hours if issue.spent_hours > 0
when '5. Bug fixing'
@result[index_1 + 4] << issue.estimated_hours if issue.estimated_hours.present?
@result[index_2 + 4] << issue.spent_hours if issue.spent_hours > 0
when '6. Release'
@result[index_1 + 5] << issue.estimated_hours if issue.estimated_hours.present?
@result[index_2 + 5] << issue.spent_hours if issue.spent_hours > 0
else
if issue.estimated_hours.present?
# if @result[index_1+ 6 ].present? // recheck
# @result[index_1+ 6 ] << worksheet[row, index_1 + 6].to_f + issue.estimated_hours
# else
@result[index_1 + 6] << issue.estimated_hours
# end
end
if issue.spent_hours > 0
# if @result[index_2 + 6].present?
# @result[index_2 + 6] << worksheet[row, index_2 + 6].to_f + issue.spent_hours
# else
@result[index_2 + 6] << issue.spent_hours
# end
end
end
end
if sum_hours_record.nil?
# testcases
@result[28] << nil
# internal bug
@result[29] << nil
# stg bug
@result[30] << nil
# prod bug
@result[31] << nil
else
# testcases
@result[28] << sum_hours_record[:testcases] if sum_hours_record[:testcases].to_i >= 0
# internal bug
@result[29] << sum_hours_record[:bugs] if sum_hours_record[:bugs].to_i >= 0
# stg bug
@result[30] << sum_hours_record[:stg_bugs] if sum_hours_record[:stg_bugs].to_i >= 0
# prod bug
@result[31] << sum_hours_record[:prod_bugs] if sum_hours_record[:prod_bugs].to_i >= 0
if jp_request.present?
jp_request = jp_request.strip
@result[32] << jp_request
jp_request_arr = URI(jp_request).path.split('/').reject { |element| element.blank? }
next unless jp_request_arr.length == 4 && jp_request_arr[3].to_i > 0
issue_detail = github.issues.find user: jp_request_arr[0], repo: jp_request_arr[1], number: jp_request_arr[3]
@result[33] << issue_detail.comments if issue_detail.success?
end
# pr detail
if pull_request.present?
pr_comments = 0
pr_review_comments = 0
pr_commits = 0
pr_additions = 0
pr_deletions = 0
pr_changed_files = 0
prs = pull_request.split("\r\n").compact
prs.each do |link|
pr_link_arr = URI(link.strip).path.split('/').compact_blank
next unless pr_link_arr.length == 4
pr_detail = github.pull_requests.find user: pr_link_arr[0], repo: pr_link_arr[1], number: pr_link_arr[3]
if pr_detail.success?
pr_comments += pr_detail.comments
pr_review_comments += pr_detail.review_comments
pr_commits += pr_detail.commits
pr_additions += pr_detail.additions
pr_deletions += pr_detail.deletions
pr_changed_files += pr_detail.changed_files
end
@result[34] << pr_comments
@result[35] << pr_review_comments
@result[36] << pr_commits
@result[37] << pr_changed_files
@result[38] << pr_additions
@result[39] << pr_deletions
end
end
end
end
@thead = TABLE_HEADER
@result = WorkflowReport.build_report(2023, 7, KURUMA_PROJECT_IDS)
end
private
def get_process(subject)
name = ''
if !subject.nil? && index = subject.strip.index(' -')
subject = subject.strip
id = subject[0, 1]
name = subject[0, index]
if id.to_i != 0
if ["3. Code review"].include? name
name = "3. Coding"
elsif ["4. Create Test case"].include? name
name = "4. Testing"
else
name
end
else
if name.include? "Requirement"
name = "1. " + name
elsif name.include? "Design"
name = "2. " + name
elsif ["Coding", "Code review"].include? name
name = "3. Coding"
elsif ["Testing", "Create Test case"].include? name
name = "4. Testing"
elsif name.include? "Bug fixing"
name = "5. " + name
elsif name.include? "Release"
name = "6. " + name
end
end
end
name.strip
end
end
github_token: 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
......@@ -11,5 +11,7 @@ Redmine::Plugin.register :workflow_report do
version '0.0.1'
url 'http://example.com/path/to/plugin'
author_url 'http://example.com/about'
configfile = File.join(File.dirname(__FILE__), 'config', 'settings.yml')
$workflow_report_config = YAML::load_file(configfile)
menu :top_menu, :workflow_report, { controller: 'workflow_report', action: 'index' }, caption: 'Workflow Report'
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)
return unless root_ids.length.positive?
sum_hours_records = find_sum_hours_records(root_ids)
raw_tasks = 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
result[0] << root_id
result[1] << record.first[:project].gsub(/[^[:print:]]/, '')
result[2] << record.first[:subject].gsub(/[^[:print:]]/, '')
result[3] << record.first[:target_version]
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') : '')
closed_on_issues = record.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_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].present? ? record.first[:status] : '')
sum_estimated_hours = record.map { |i| i[:estimated_hours] }.compact.sum
result[8] << sum_estimated_hours
if sum_hours_record.present?
result[9] << sum_hours_record[:hours]
result[10] << (sum_hours_record[:hours] - sum_estimated_hours)
else
result[9] << ''
result[10] << ''
end
if journals.length.positive?
number_of_est_changes = journals.count { |j| !j[1].nil? } || ''
est_changes = journals.reject { |j| j[1].nil? }.select { |e| e[4].present? }
notes = est_changes.inject('') { |all, i| "#{all}#{i[4]}\n" } || ''
original_est_hours = journals.group_by(&:shift).inject(0) { |sum, i| sum + i[1][0][1].to_f unless i.nil? } || ''
result[11] << original_est_hours
result[12] << number_of_est_changes
result[13] << notes
else
result[11] << ''
result[12] << ''
result[13] << ''
end
pull_request = ''
jp_request = ''
record.each do |issue|
if issue.tracker_id == 12
pull_request = issue.pr
jp_request = issue.jp_request
end
process = get_process(issue.subject.gsub(/[^[:print:]]/, ''))
index_1 = EST_DETAIL_FIRST_COL # first index for estimation detail
index_2 = ACTUAL_TIME_DETAIL_FIRST_COL # first index for actual detail
case process
when '1. Requirement'
result[index_1].push(issue.estimated_hours.present? ? issue.estimated_hours : '')
result[index_2].push(issue.spent_hours.positive? ? issue.spent_hours : '')
when '2. Design'
result[index_1 + 1].push(issue.estimated_hours.present? ? issue.estimated_hours : '')
result[index_2 + 1].push(issue.spent_hours.positive? ? issue.spent_hours : '')
when '3. Coding'
result[index_1 + 2].push(issue.estimated_hours.present? ? issue.estimated_hours : '')
result[index_2 + 2].push(issue.spent_hours.positive? ? issue.spent_hours : '')
when '4. Testing'
result[index_1 + 3].push(issue.estimated_hours.present? ? issue.estimated_hours : '')
result[index_2 + 3].push(issue.spent_hours.positive? ? issue.spent_hours : '')
when '5. Bug fixing'
result[index_1 + 4].push(issue.estimated_hours.present? ? issue.estimated_hours : '')
result[index_2 + 4].push(issue.spent_hours.positive? ? issue.spent_hours : '')
when '6. Release'
result[index_1 + 5].push(issue.estimated_hours.present? ? issue.estimated_hours : '')
result[index_2 + 5].push(issue.spent_hours.positive? ? issue.spent_hours : '')
else
result[index_1 + 6].push(issue.estimated_hours.present? ? issue.estimated_hours : '')
result[index_2 + 6].push(issue.spent_hours.positive? ? issue.spent_hours : '')
end
end
if sum_hours_record.nil?
# testcases
result[28] << ''
# internal bug
result[29] << ''
# stg bug
result[30] << ''
# prod bug
result[31] << ''
else
# testcases
result[28] << sum_hours_record[:testcases] if sum_hours_record[:testcases].to_i >= 0
# internal bug
result[29] << sum_hours_record[:bugs] if sum_hours_record[:bugs].to_i >= 0
# stg bug
result[30] << sum_hours_record[:stg_bugs] if sum_hours_record[:stg_bugs].to_i >= 0
# prod bug
result[31] << sum_hours_record[:prod_bugs] if sum_hours_record[:prod_bugs].to_i >= 0
end
if jp_request.present?
jp_request = jp_request.strip
result[32] << jp_request
jp_request_arr = URI(jp_request).path.split('/').reject(&:blank?)
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 : '')
end
else
result[32] << ''
result[33] << ''
end
# pr detail
next unless pull_request.present?
pr_comments = 0
pr_review_comments = 0
pr_commits = 0
pr_additions = 0
pr_deletions = 0
pr_changed_files = 0
prs = pull_request.split("\r\n").compact
prs.each do |link|
pr_link_arr = URI(link.strip).path.split('/').compact_blank
next unless pr_link_arr.length == 4
pr_detail = github.pull_requests.find user: pr_link_arr[0], repo: pr_link_arr[1], number: pr_link_arr[3]
if pr_detail.success?
pr_comments += pr_detail.comments
pr_review_comments += pr_detail.review_comments
pr_commits += pr_detail.commits
pr_additions += pr_detail.additions
pr_deletions += pr_detail.deletions
pr_changed_files += pr_detail.changed_files
end
result[34] << pr_comments
result[35] << pr_review_comments
result[36] << pr_commits
result[37] << pr_changed_files
result[38] << pr_additions
result[39] << pr_deletions
end
end
result
end
private
def get_process(subject)
name = ''
if !subject.nil? && index = subject.strip.index(' -')
subject = subject.strip
id = subject[0, 1]
name = subject[0, index]
if id.to_i != 0
if ["3. Code review"].include? name
name = "3. Coding"
elsif ["4. Create Test case"].include? name
name = "4. Testing"
else
name
end
else
if name.include? "Requirement"
name = "1. " + name
elsif name.include? "Design"
name = "2. " + name
elsif ["Coding", "Code review"].include? name
name = "3. Coding"
elsif ["Testing", "Create Test case"].include? name
name = "4. Testing"
elsif name.include? "Bug fixing"
name = "5. " + name
elsif name.include? "Release"
name = "6. " + name
end
end
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