Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PART 1] Create a "priority only" distribution #14691

Closed
Closed
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
3222a99
Priority distributions wip
hschallhorn Jul 13, 2020
22a4912
merge
hschallhorn Jul 13, 2020
a45df4b
Fix tests
hschallhorn Jul 13, 2020
1cee714
make docs!
hschallhorn Jul 13, 2020
2fdbc09
Code climate
hschallhorn Jul 13, 2020
b653340
Merge branch 'master' into hschallhorn/14604-distribute-priority-only
hschallhorn Jul 13, 2020
25c9606
Allow unlimited priority legacy appeals to be distributed
hschallhorn Jul 13, 2020
3bd0698
Fix bug on wrong number of hearing cases distributed
hschallhorn Jul 14, 2020
fb7e038
Create priority only distributions
hschallhorn Jul 14, 2020
df2fd01
Merge branch 'master' into hschallhorn/14604-priority-only-distribution
hschallhorn Jul 14, 2020
0932ca5
Merge branch 'master' into hschallhorn/14604-distribute-priority-only
hschallhorn Jul 14, 2020
904c52f
Suggestions
hschallhorn Jul 14, 2020
a0a26e5
Update to priority_push
hschallhorn Jul 14, 2020
98c954f
Update to priority_push
hschallhorn Jul 14, 2020
12013e3
Fix schema
hschallhorn Jul 14, 2020
49db6c4
Merge branch 'hschallhorn/14604-distribute-priority-only' into hschal…
hschallhorn Jul 14, 2020
3d8b6b2
Code Climate
hschallhorn Jul 14, 2020
b902ddc
Updates
hschallhorn Jul 14, 2020
4c47117
Updates
hschallhorn Jul 14, 2020
b75e3a9
fix specs
hschallhorn Jul 14, 2020
04535b7
Merge branch 'master' into hschallhorn/14604-priority-only-distribution
hschallhorn Jul 15, 2020
0b8f944
Merge branch 'master' into hschallhorn/14604-priority-only-distribution
hschallhorn Jul 15, 2020
ef4f5ba
Allow unlimited priority legacy appeals to be distributed
hschallhorn Jul 13, 2020
4e05c00
Fix bug on wrong number of hearing cases distributed
hschallhorn Jul 14, 2020
5d96969
Create priority only distributions
hschallhorn Jul 14, 2020
ed9081d
Code Climate
hschallhorn Jul 14, 2020
ca76476
Updates
hschallhorn Jul 14, 2020
046454e
Updates
hschallhorn Jul 14, 2020
10b1656
fix specs
hschallhorn Jul 14, 2020
edb04dd
Merge branch 'hschallhorn/14604-priority-only-distribution' of https:…
hschallhorn Jul 15, 2020
d562a24
Rename ama distribution
hschallhorn Jul 16, 2020
78e9e7a
Merge branch 'master' into hschallhorn/14604-priority-only-distribution
hschallhorn Jul 16, 2020
463d33c
Merge branch 'master' into hschallhorn/14604-priority-only-distribution
hschallhorn Jul 21, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/controllers/distributions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def enqueue_distribution_job(distribution)

def render_errors(error)
errors = error.record.errors.details.values.flatten.map { |error_details| error_details[:error] }
return render_single(pending_distribution) if errors.include? :pending_distribution
return render_single(pending_distributions.first) if errors.include? :pending_distribution

render_403_error(errors)
end
Expand Down Expand Up @@ -101,7 +101,7 @@ def json_error(error)
end
end

def pending_distribution
def pending_distributions
Distribution.pending_for_judge(judge)
end

Expand Down
20 changes: 18 additions & 2 deletions app/models/concerns/ama_case_distribution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@ def docket_coordinator
@docket_coordinator ||= DocketCoordinator.new
end

def priority_push_distribution(limit = nil)
@appeals = []
@rem = 0

if limit.nil?
# Distribute priority appeals that are tied to judges (not genpop) with no limit.
distribute_appeals(:legacy, nil, priority: true, genpop: "not_genpop")
distribute_appeals(:hearing, nil, priority: true, genpop: "not_genpop")
else
# Distribute <limit> number of cases, regardless of docket type, oldest first.
oldest_priority_appeals_by_docket(limit).each do |docket, number_of_cases|
distribute_appeals(docket, number_of_cases, priority: true)
end
end
end

def ama_distribution
@appeals = []
@rem = batch_size
Expand Down Expand Up @@ -59,7 +75,7 @@ def ama_distribution

def ama_statistics
{
batch_size: batch_size,
batch_size: priority_push? ? @appeals.count : batch_size,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

priority push does not work off of batch size, it either distributes without limit or distributes the provided limit.

total_batch_size: total_batch_size,
priority_count: priority_count,
direct_review_due_count: direct_review_due_count,
Expand All @@ -75,7 +91,7 @@ def ama_statistics
end

def distribute_appeals(docket, num, priority: false, genpop: "any", range: nil, bust_backlog: false)
return [] unless num > 0
return [] unless num.nil? || num > 0

if range.nil? && !bust_backlog
appeals = dockets[docket].distribute_appeals(self, priority: priority, genpop: genpop, limit: num)
Expand Down
16 changes: 10 additions & 6 deletions app/models/distribution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class Distribution < CaseflowRecord

validates :judge, presence: true
validate :validate_user_is_judge, on: :create
validate :validate_number_of_unassigned_cases, on: :create
validate :validate_days_waiting_of_unassigned_cases, on: :create
validate :validate_number_of_unassigned_cases, on: :create, unless: :priority_push?
validate :validate_days_waiting_of_unassigned_cases, on: :create, unless: :priority_push?
validate :validate_judge_has_no_pending_distributions, on: :create

enum status: { pending: "pending", started: "started", error: "error", completed: "completed" }
Expand All @@ -20,13 +20,15 @@ class Distribution < CaseflowRecord
CASES_PER_ATTORNEY = 3
ALTERNATIVE_BATCH_SIZE = 15

scope :priority_push, -> { where(priority_push: true) }

class << self
def pending_for_judge(judge)
find_by(status: %w[pending started], judge: judge)
where(status: %w[pending started], judge: judge)
end
end

def distribute!
def distribute!(limit = nil)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allows us to stipulate a limit for priority push cases, when we get to the "aim to hit priority target" portion of the job

return unless %w[pending error].include? status

if status == "error"
Expand All @@ -41,7 +43,7 @@ def distribute!
multi_transaction do
ActiveRecord::Base.connection.execute "SET LOCAL statement_timeout = #{transaction_time_out}"

ama_distribution
priority_push? ? priority_push_distribution(limit) : ama_distribution

update!(status: "completed", completed_at: Time.zone.now, statistics: ama_statistics)
end
Expand Down Expand Up @@ -74,7 +76,9 @@ def validate_days_waiting_of_unassigned_cases
end

def validate_judge_has_no_pending_distributions
errors.add(:judge, :pending_distribution) if self.class.pending_for_judge(judge)
if self.class.pending_for_judge(judge).where(priority_push: priority_push).exists?
errors.add(:judge, :pending_distribution)
end
end

def judge_tasks
Expand Down
8 changes: 8 additions & 0 deletions app/models/dockets/hearing_request_docket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ def distribute_appeals(distribution, priority: false, genpop: "any", limit: 1)
base_relation: base_relation, genpop: genpop, judge: distribution.judge
).call

# genpop 'any' returns 2 arrays of the limited base relation. This means if we only request 2 cases, appeals is a
# 2x2 array containing 4 cases overall and we will end up distributing 4 cases rather than 2. Instead, reinstate the
# limit here by filtering out the newest cases
if genpop == "any"
appeals_to_reject = appeals.flatten.sort_by(&:ready_for_distribution_at).drop(limit)
appeals = [appeals.first - appeals_to_reject, appeals.last - appeals_to_reject]
end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found a bug! Detailed here


HearingRequestCaseDistributor.new(
appeals: appeals, genpop: genpop, distribution: distribution, priority: priority
).call
Expand Down
5 changes: 3 additions & 2 deletions app/models/vacols/case_docket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class DocketNumberCentennialLoop < StandardError; end
#{JOIN_ASSOCIATED_VLJS_BY_PRIOR_DECISIONS}
)
where ((VLJ = ? and 1 = ?) or (VLJ is null and 1 = ?))
and rownum <= ?
and (rownum <= ? or 1 = ?)
"

SELECT_NONPRIORITY_APPEALS = "
Expand Down Expand Up @@ -324,7 +324,8 @@ def self.distribute_priority_appeals(judge, genpop, limit, dry_run = false)
judge.vacols_attorney_id,
(genpop == "any" || genpop == "not_genpop") ? 1 : 0,
(genpop == "any" || genpop == "only_genpop") ? 1 : 0,
limit
limit,
limit.nil? ? 1 : 0
Copy link
Contributor Author

@hschallhorn hschallhorn Jul 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allows nil limit to be passed to distribute_priority_appeals, indicating that any and all cases should be distributed

])

distribute_appeals(fmtd_query, judge, dry_run)
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20200713173228_add_priority_to_distributions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class AddPriorityToDistributions < ActiveRecord::Migration[5.2]
def up
add_column :distributions, :priority_push, :boolean, comment: "Whether or not this distribution is a priority-appeals-only push to judges via a weekly job (not manually requested)"
change_column_default :distributions, :priority_push, false
end

def down
remove_column :distributions, :priority_push
end
end
10 changes: 10 additions & 0 deletions db/migrate/20200713174152_backfill_distribution_priority.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class BackfillDistributionPriority < ActiveRecord::Migration[5.2]
disable_ddl_transaction!

def change
Distribution.unscoped.in_batches do |relation|
relation.update_all priority_push: false
sleep(0.1)
end
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_07_10_232644) do
ActiveRecord::Schema.define(version: 2020_07_13_174152) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -439,6 +439,7 @@
t.datetime "created_at", null: false
t.datetime "errored_at", comment: "when the Distribution job suffered an error"
t.integer "judge_id"
t.boolean "priority_push", default: false, comment: "Whether or not this distribution is a priority-appeals-only push to judges via a weekly job (not manually requested)"
t.datetime "started_at", comment: "when the Distribution job commenced"
t.json "statistics"
t.string "status"
Expand Down
7 changes: 4 additions & 3 deletions docs/schema/caseflow.csv
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ distributions,created_at,datetime ∗,x,,,,,
distributions,errored_at,datetime,,,,,,when the Distribution job suffered an error
distributions,id,integer (8) PK,x,x,,,,
distributions,judge_id,integer FK,,,x,,,
distributions,priority_push,boolean,,,,,,Whether or not this distribution is a priority-appeals-only push to judges via a weekly job (not manually requested)
distributions,started_at,datetime,,,,,,when the Distribution job commenced
distributions,statistics,json,,,,,,
distributions,status,string,,,,,,
Expand Down Expand Up @@ -687,18 +688,18 @@ legacy_issues,vacols_sequence_id,integer ∗,x,,,,,The sequence ID of the lega
legacy_issue_optins,,,,,,,,"When a VACOLS issue from a legacy appeal is opted-in to AMA, this table keeps track of the related request_issue, and the status of processing the opt-in, or rollback if the request issue is removed from a Decision Review."
legacy_issue_optins,created_at,datetime ∗,x,,,,,"When a Request Issue is connected to a VACOLS issue on a legacy appeal, and the Veteran has agreed to withdraw their legacy appeals, a legacy_issue_optin is created at the time the Decision Review is successfully intaken. This is used to indicate that the legacy issue should subsequently be opted into AMA in VACOLS. "
legacy_issue_optins,error,string,,,,,,
legacy_issue_optins,folder_decision_date,date,,,,,,Decision date on case record folder
legacy_issue_optins,id,integer (8) PK,x,x,,,,
legacy_issue_optins,legacy_issue_id,integer (8) FK,,,x,,x,"The legacy issue being opted in, which connects to the request issue"
legacy_issue_optins,optin_processed_at,datetime,,,,,,"The timestamp for when the opt-in was successfully processed, meaning it was updated in VACOLS as opted into AMA."
legacy_issue_optins,original_disposition_code,string,,,,,,The original disposition code of the VACOLS issue being opted in. Stored in case the opt-in is rolled back.
legacy_issue_optins,original_disposition_date,date,,,,,,The original disposition date of the VACOLS issue being opted in. Stored in case the opt-in is rolled back.
legacy_issue_optins,original_legacy_appeal_decision_date,date,,,,,,The original disposition date of a legacy appeal being opted in
legacy_issue_optins,original_legacy_appeal_disposition_code,string,,,,,,The original disposition code of legacy appeal being opted in
legacy_issue_optins,request_issue_id,integer (8) ∗ FK,x,,x,,x,The request issue connected to the legacy VACOLS issue that has been opted in.
legacy_issue_optins,rollback_created_at,datetime,,,,,,"Timestamp for when the connected request issue is removed from a Decision Review during edit, indicating that the opt-in needs to be rolled back."
legacy_issue_optins,rollback_processed_at,datetime,,,,,,Timestamp for when a rolled back opt-in has successfully finished being rolled back.
legacy_issue_optins,updated_at,datetime ∗,x,,,,x,Automatically populated when the record is updated.
legacy_issue_optins,original_legacy_appeal_decision_date,datetime,,,,,,The original disposition date of a legacy appeal being opted in.
legacy_issue_optins,original_legacy_appeal_disposition_code,string,,,,,,The original disposition code of legacy appeal being opted in.
legacy_issue_optins,folder_decision_date,date,,,,,,Decision date on case record folder.
messages,,,,,,,,
messages,created_at,datetime ∗,x,,,,,
messages,detail_id,integer FK,,,x,,x,ID of the related object
Expand Down
Loading