| 1 | # ABSTRACT: The one definition of which card karr pick may hand out, and in what order | ||
| 2 | |||
| 3 | package App::karr::Role::PickRules; | ||
| 4 | our $VERSION = '0.602'; | ||
| 5 | use Moo::Role; | ||
| 6 | use App::karr::Config; | ||
| 7 | |||
| 8 | |||
| 9 | # Composed, not required: claim expiry is half the eligibility rule, and its | ||
| 10 | # RFC3339 parser exists once (App::karr::Role::ClaimTimeout, ticket #57). A | ||
| 11 | # consumer that also composes ClaimTimeout itself -- App::karr::Cmd::Pick does, | ||
| 12 | # for the separate lock_timeout parse -- is applying the same role twice, which | ||
| 13 | # Role::Tiny resolves to one application rather than a method conflict. | ||
| 14 | with 'App::karr::Role::ClaimTimeout'; | ||
| 15 | |||
| 16 | sub pickable { | ||
| 17 | my ( $self, $task, %filter ) = @_; | ||
| 18 | return 0 unless $task; | ||
| 19 | |||
| 20 | my $timeout = defined $filter{timeout} ? $filter{timeout} : $self->claim_timeout_secs; | ||
| 21 | |||
| 22 | # Held back, whatever the filters say (ticket k306): backlog is where a | ||
| 23 | # card is filed and sorted, and nobody takes it until someone promotes it. | ||
| 24 | # Outside the filter branch below on purpose -- a --status that names | ||
| 25 | # backlog is refused by karr pick before it gets here, and an in-process | ||
| 26 | # caller that passes one anyway must not get a backlog card out of it. | ||
| 27 | return 0 if $self->store->is_held_back_status( $task->status ); | ||
| 28 | |||
| 29 | if ( $filter{statuses} ) { | ||
| 30 | my %allowed = map { $_ => 1 } @{ $filter{statuses} }; | ||
| 31 | return 0 unless $allowed{ $task->status }; | ||
| 32 | } | ||
| 33 | else { | ||
| 34 | # The board's own terminal status, not a hardcoded 'done': a board | ||
| 35 | # imported from kanban-md can end in `shipped`, and pick used to hand | ||
| 36 | # those finished cards straight back out (ticket #67). | ||
| 37 | return 0 if $self->store->is_terminal_status( $task->status ); | ||
| 38 | } | ||
| 39 | |||
| 40 | # One claim test in karr, and this is a call to it rather than a copy of it | ||
| 41 | # (ticket #252). These were three lines spelled out here -- has_claimed_by, | ||
| 42 | # length, and the expiry parse -- which was fine while `karr pick` was the | ||
| 43 | # only caller and became the #59/#198 failure the moment `karr list | ||
| 44 | # --unclaimed` needed the same answer: two spellings of the claim test | ||
| 45 | # drift, and then a list calls a card unclaimed that pick treats as held. | ||
| 46 | # | ||
| 47 | # It could not be borrowed by calling pickable itself, because the whole of | ||
| 48 | # what used to stand here is the claim, and the next line is not: blocked is | ||
| 49 | # pick's rule, not part of being claimed, and kanban-md's IsUnclaimed | ||
| 50 | # (internal/board/filter.go) does not ask it either. That is why the claim | ||
| 51 | # test moved out and this line stayed behind it. The held-back test at the | ||
| 52 | # top is pick's rule on the same terms (k306): a backlog card holds no | ||
| 53 | # claim, so `list --unclaimed` lists it and pick still refuses it. The list | ||
| 54 | # and pick share the claim half of this method and nothing more -- the | ||
| 55 | # pickable set is `list --unclaimed --not-blocked` minus backlog, a parting | ||
| 56 | # decided in ticket k309 rather than left to drift. | ||
| 57 | # | ||
| 58 | # What moved is exactly what was here, `claimed_by: ""` included -- see | ||
| 59 | # App::karr::Role::ClaimTimeout/claim_held for the reasoning that came with | ||
| 60 | # it. | ||
| 61 | return 0 if $self->claim_held( $task, $timeout ); | ||
| 62 | return 0 if $task->has_blocked; | ||
| 63 | |||
| 64 | if ( $filter{tags} ) { | ||
| 65 | my %wanted = map { $_ => 1 } @{ $filter{tags} }; | ||
| 66 | return 0 unless grep { $wanted{$_} } @{ $task->tags }; | ||
| 67 | } | ||
| 68 | |||
| 69 | return 1; | ||
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | sub pick_rank { | ||
| 74 | my ( $self, @tasks ) = @_; | ||
| 75 | |||
| 76 | # Both axes are driven by the board's configured lists -- not by a | ||
| 77 | # hardcoded table that only knew the four default priorities and classes. | ||
| 78 | # A board imported from kanban-md can name anything (ticket #149: a | ||
| 79 | # `blocker` priority beat a `critical` one on a non-default board); ranking | ||
| 80 | # against the hardcoded table gave the wrong card out while | ||
| 81 | # `karr list --sort priority` showed the right one right next to it. | ||
| 82 | # | ||
| 83 | # Convention, matching kanban-md's pick.go: lower class index = more urgent | ||
| 84 | # class; higher priority index = more urgent priority. So the sort key for | ||
| 85 | # priority is `(max - priority_index)` -- most-urgent-last in the config | ||
| 86 | # list comes out first. A priority the board does not list at all sorts | ||
| 87 | # below every listed one; a class the board does not list sorts where | ||
| 88 | # `standard` does -- and `$std_cls_idx` below decides what that means on a | ||
| 89 | # board that does not list `standard` either. | ||
| 90 | # | ||
| 91 | # Between class and priority sits kanban-md's one exception, and it is not | ||
| 92 | # a general due-date rule: only where both cards carry `fixed-date` does | ||
| 93 | # the date come first (ticket #233). | ||
| 94 | my $cfg = App::karr::Config->from_merged( $self->store->effective_config ); | ||
| 95 | my @priorities = $cfg->priorities; | ||
| 96 | my @classes = $cfg->classes; | ||
| 97 | my %pri_idx; $pri_idx{ $priorities[$_] } = $_ for 0 .. $#priorities; | ||
| 98 | my %cls_idx; $cls_idx{ $classes[$_] } = $_ for 0 .. $#classes; | ||
| 99 | my $max_pri = $#priorities; | ||
| 100 | |||
| 101 | # `// 0`, not kanban-md's -1, and that is a decision rather than an | ||
| 102 | # oversight (ticket #240): kanban-md's classOrder returns | ||
| 103 | # cfg.ClassIndex("standard") as it stands, so on a board whose `classes` | ||
| 104 | # never names `standard` the unknown class comes back -1 and outranks | ||
| 105 | # every configured class. Here it lands level with the board's first class | ||
| 106 | # instead. See this method's POD for the reasoning; a change to -1 is | ||
| 107 | # visible in t/198-pick-rules-shared.t, subtest 'an unknown class on a | ||
| 108 | # board without `standard`'. | ||
| 109 | my $std_cls_idx = $cls_idx{standard} // 0; | ||
| 110 | |||
| 111 | return sort { | ||
| 112 | ( ( $cls_idx{ $a->class } // $std_cls_idx ) <=> ( $cls_idx{ $b->class } // $std_cls_idx ) ) | ||
| 113 | || $self->_fixed_date_due_cmp( $a, $b ) | ||
| 114 | || ( ( $max_pri - ( $pri_idx{ $a->priority } // -1 ) ) | ||
| 115 | <=> ( $max_pri - ( $pri_idx{ $b->priority } // -1 ) ) ) | ||
| 116 | || $a->id <=> $b->id | ||
| 117 | } @tasks; | ||
| 118 | } | ||
| 119 | |||
| 120 | |||
| 121 | # kanban-md's sortPickCandidates asks the due date only when both candidates | ||
| 122 | # are `fixed-date` -- the class of service whose whole point is that a date, | ||
| 123 | # not an urgency rating, decides. Against any other class (and between two | ||
| 124 | # cards of any other class) the class index and then priority decide exactly as | ||
| 125 | # before, so this returns 0 and the chain above carries on. | ||
| 126 | # | ||
| 127 | # The ordering within the exception is kanban-md's compareDue | ||
| 128 | # (internal/board/sort.go): earlier date first, a card with no due date last -- | ||
| 129 | # not first and not level -- and two cards it cannot separate (neither dated, | ||
| 130 | # or both dated the same day) fall through to priority. | ||
| 131 | # | ||
| 132 | # `has_due` is the whole emptiness test: L<App::karr::Task/BUILD> normalizes a | ||
| 133 | # `due:` that is present but empty back to unset on the parse path, so nothing | ||
| 134 | # reaches here has_due-true-but-blank (ticket #98). Dates are the bare | ||
| 135 | # `YYYY-MM-DD` kanban-md's date.Date accepts and karr validates | ||
| 136 | # (L<App::karr::Config/validate_due>), so a string compare is chronological. | ||
| 137 | sub _fixed_date_due_cmp { | ||
| 138 | my ( $self, $left, $right ) = @_; | ||
| 139 | |||
| 140 | # A card with no class at all is not fixed-date, the same way kanban-md's | ||
| 141 | # empty-string class is not. | ||
| 142 | my $fixed = App::karr::Config->FIXED_DATE_CLASS; | ||
| 143 | return 0 | ||
| 144 | unless ( $left->class // '' ) eq $fixed | ||
| 145 | && ( $right->class // '' ) eq $fixed; | ||
| 146 | |||
| 147 | my $l = $left->has_due ? $left->due : undef; | ||
| 148 | my $r = $right->has_due ? $right->due : undef; | ||
| 149 | return 0 unless defined $l || defined $r; | ||
| 150 | return 1 unless defined $l; | ||
| 151 | return -1 unless defined $r; | ||
| 152 | return $l cmp $r; | ||
| 153 | } | ||
| 154 | |||
| 155 | sub pick_candidates { | ||
| 156 | my ( $self, $tasks, %filter ) = @_; | ||
| 157 | return $self->pick_rank( grep { $self->pickable( $_, %filter ) } @$tasks ); | ||
| 158 | } | ||
| 159 | |||
| 160 | |||
| 161 | 1; | ||
| 162 | |||
| 163 | __END__ |