fix: change projec trend rule and fix data

This commit is contained in:
yystopf 2021-10-15 14:36:50 +08:00
parent ffa0715374
commit 2a09fadf71
5 changed files with 56 additions and 5 deletions

View File

@ -216,6 +216,12 @@ class IssuesController < ApplicationController
if @issue.previous_changes[:due_date].present?
previous_changes.merge!(due_date: [@issue.previous_changes[:due_date][0].to_s, @issue.previous_changes[:due_date][1].to_s])
end
if @issue.previous_changes[:status_id].present? && @issue.previous_changes[:status_id][1] == 5
@issue.project_trends.create(user_id: current_user.id, project_id: @project.id, action_type: ProjectTrend::CLOSE)
end
if @issue.previous_changes[:status_id].present? && @issue.previous_changes[:status_id][0] == 5
@issue.project_trends.where(action_type: ProjectTrend::CLOSE).destroy_all
end
SendTemplateMessageJob.perform_later('IssueChanged', current_user.id, @issue&.id, previous_changes)
SendTemplateMessageJob.perform_later('IssueAssigned', current_user.id, @issue&.id) if @issue.previous_changes[:assigned_to_id].present?
end
@ -338,6 +344,12 @@ class IssuesController < ApplicationController
if i.previous_changes[:due_date].present?
previous_changes.merge!(due_date: [i.previous_changes[:due_date][0].to_s, i.previous_changes[:due_date][1].to_s])
end
if i.previous_changes[:status_id].present? && i.previous_changes[:status_id][1] == 5
i.project_trends.create(user_id: current_user.id, project_id: @project.id, action_type: ProjectTrend::CLOSE)
end
if i.previous_changes[:status_id].present? && i.previous_changes[:status_id][0] == 5
i.project_trends.where(action_type: ProjectTrend::CLOSE).destroy_all
end
SendTemplateMessageJob.perform_later('IssueChanged', current_user.id, i&.id, previous_changes)
SendTemplateMessageJob.perform_later('IssueAssigned', current_user.id, i&.id) if i.previous_changes[:assigned_to_id].present?
end

View File

@ -3,7 +3,7 @@ class ProjectTrendsController < ApplicationController
before_action :check_project_public
def index
project_trends = @project.project_trends.includes(:user, trend: :user)
project_trends = @project.project_trends.preload(:user, trend: :user)
check_time = params[:time] #时间的筛选
check_type = params[:type] #动态类型的筛选,目前已知的有 Issue, PullRequest, Version
@ -18,7 +18,7 @@ class ProjectTrendsController < ApplicationController
@project_issues_count = project_trends.where(trend_type: "Issue", action_type: "create").size
@project_open_issues_count = @project_issues_count - @project_close_issues_count
@project_pr_count = project_trends.where(trend_type: "PullRequest", action_type: "close").size
@project_pr_count = project_trends.where(trend_type: "PullRequest", action_type: ["close", "merge"]).size
@project_pr_all_count = project_trends.where(trend_type: "PullRequest", action_type: "create").size
@project_new_pr_count = @project_pr_all_count - @project_pr_count
if check_type.present?
@ -26,7 +26,13 @@ class ProjectTrendsController < ApplicationController
end
if check_status.present?
project_trends = project_trends.where(action_type: check_status.to_s.strip)
if check_status == "delay" || check_status == "close"
project_trends = project_trends.where(action_type: ["close", "merge"])
else
project_trends = project_trends.where(action_type: ["create"]).where.not(trend_id: project_trends.where(action_type: ["close", "merge"]).pluck(:trend_id))
end
else
project_trends = project_trends.where(action_type: "create")
end
project_trends = project_trends.order("created_at desc")

View File

@ -130,6 +130,7 @@ class PullRequestsController < ApplicationController
begin
colsed = PullRequests::CloseService.call(@owner, @repository, @pull_request, current_user)
if colsed === true
@pull_request.project_trends.create!(user: current_user, project: @project,action_type: ProjectTrend::CLOSE)
SendTemplateMessageJob.perform_later('PullRequestClosed', current_user.id, @pull_request.id)
normal_status(1, "已拒绝")
else
@ -171,7 +172,8 @@ class PullRequestsController < ApplicationController
end
if success_condition && @pull_request.merge!
@pull_request.project_trend_status!
# @pull_request.project_trend_status!
@pull_request.project_trends.create!(user: current_user, project: @project,action_type: ProjectTrend::MERGE)
@issue&.custom_journal_detail("merge", "", "该合并请求已被合并", current_user&.id)
SendTemplateMessageJob.perform_later('PullRequestMerged', current_user.id, @pull_request.id)
normal_status(1, "合并成功")

View File

@ -20,7 +20,8 @@
class ProjectTrend < ApplicationRecord
CLOSE = 'close'
CREATE = 'create'
MERGE = 'merge'
belongs_to :project
belongs_to :trend, polymorphic: true, optional: true
belongs_to :user

View File

@ -0,0 +1,30 @@
namespace :refactor_project_trend do
desc "refactor project trend data record"
task issue_and_pull_request: :environment do
puts "========DELETE all old data begin========"
old_data_count = ProjectTrend.where(trend_type: ["PullRequest","Issue"]).destroy_all.size
puts "========DELETE all old data #{old_data_count}========"
puts "========DELETE all old data end========"
puts "========CREATE new issue data begin========"
issue_count = 0
Issue.issue_issue.find_each do |issue|
issue_count += 1
issue.project_trends.create(user_id: issue.assigned_to_id || issue.author_id, project_id: issue.project_id, action_type: ProjectTrend::CLOSE, created_at: issue.updated_on) if issue.status_id == 5
issue.project_trends.create(user_id: issue.author_id, project_id: issue.project_id, action_type: ProjectTrend::CREATE, created_at: issue.created_on)
end
puts "========CREATE new issue data #{issue_count}========"
puts "========CREATE new issue data end========"
puts "========CREATE new pull_request data begin========"
pull_request_count = 0
PullRequest.find_each do |pull_request|
pull_request_count += 1
pull_request.project_trends.create(user_id: pull_request.user_id, project_id: pull_request.project_id, action_type: ProjectTrend::MERGE, created_at: pull_request.updated_at) if pull_request.status == PullRequest::MERGED
pull_request.project_trends.create(user_id: pull_request.user_id, project_id: pull_request.project_id, action_type: ProjectTrend::CLOSE, created_at: pull_request.updated_at) if pull_request.status == PullRequest::CLOSED
pull_request.project_trends.create(user_id: pull_request.user_id, project_id: pull_request.project_id, action_type: ProjectTrend::CREATE, created_at: pull_request.created_at)
end
puts "========CREATE new pull_request data #{pull_request_count}========"
puts "========CREATE new pull_request data end========"
end
end