| 1 | # ABSTRACT: karr-foundation read-only overview -- multi-board status dashboard | ||
| 2 | |||
| 3 | package App::karr::Foundation::Overview; | ||
| 4 | our $VERSION = '0.602'; | ||
| 5 | use Moo; | ||
| 6 | use Time::Piece; | ||
| 7 | use Try::Tiny; | ||
| 8 | |||
| 9 | |||
| 10 | |||
| 11 | has foundation => ( | ||
| 12 | is => 'ro', | ||
| 13 | weak_ref => 1, | ||
| 14 | required => 1, | ||
| 15 | ); | ||
| 16 | |||
| 17 | # --------------------------------------------------------------------------- | ||
| 18 | # Overview (read-only dashboard) | ||
| 19 | # --------------------------------------------------------------------------- | ||
| 20 | |||
| 21 | sub _print_overview { | ||
| 22 | my ( $self, $repos ) = @_; | ||
| 23 | for my $repo (@$repos) { | ||
| 24 | my $karr = $self->foundation->_load_karr($repo); | ||
| 25 | my %states = $self->foundation->_task_states($repo); | ||
| 26 | |||
| 27 | my %count; | ||
| 28 | my ( @in_progress, @blocked ); | ||
| 29 | for my $id ( sort { $a <=> $b } keys %states ) { | ||
| 30 | my $st = $states{$id}; | ||
| 31 | $count{ $st->{status} // 'unknown' }++; | ||
| 32 | push @in_progress, $id if ( $st->{status} // '' ) eq 'in-progress'; | ||
| 33 | push @blocked, $id if $st->{blocked}; | ||
| 34 | } | ||
| 35 | |||
| 36 | # The board's own opt-out (refs/karr/config: foundation.enabled) is the | ||
| 37 | # dominant fact about a board, so it leads the flag list — and suppresses | ||
| 38 | # the 'agent' flag, because that agent will never run here. | ||
| 39 | my $disabled = $self->foundation->_board_disabled($repo); | ||
| 40 | |||
| 41 | my @flags; | ||
| 42 | push @flags, 'disabled' if $disabled; | ||
| 43 | push @flags, 'agent-running' if $self->foundation->_lock_held($repo); | ||
| 44 | if ( $self->foundation->_cooldown_active($repo) ) { | ||
| 45 | my $until = $self->foundation->_state_get( $repo, 'cooldown_until' ) // 0; | ||
| 46 | # Why, not just how long: a board sitting in cooldown is a board doing | ||
| 47 | # nothing, and the only other record of the reason is a COMMON-ERROR | ||
| 48 | # line somewhere in .karr.log (#160). last_error belongs to the run that | ||
| 49 | # set this deadline — the next run that is not a common error drops it. | ||
| 50 | my $why = $self->foundation->_state_get( $repo, 'last_error' ); | ||
| 51 | push @flags, 'cooldown ' . ( $until - time ) . 's' | ||
| 52 | . ( defined $why ? " ($why)" : '' ); | ||
| 53 | } | ||
| 54 | # Which agent, not just that there is one: a fleet with several agent | ||
| 55 | # definitions is the case this exists for, and "agent" alone answers the | ||
| 56 | # least interesting question about a board. A named agent that is not | ||
| 57 | # currently available says so here too, because from the outside that board | ||
| 58 | # and a board in cooldown are doing the same nothing. | ||
| 59 | my ( $agent_error, $waiting ); | ||
| 60 | unless ( $disabled ) { | ||
| 61 | my ( $cmd, $agent, $wait ); | ||
| 62 | try { ( $cmd, $agent, $wait ) = $self->foundation->_resolve_agent( $repo, $karr ) } | ||
| 63 | catch { $agent_error = "$_"; chomp $agent_error }; | ||
| 64 | if ( defined $agent_error ) { | ||
| 65 | push @flags, 'agent-error'; | ||
| 66 | } | ||
| 67 | elsif ( defined $wait ) { | ||
| 68 | # Routed by the assignment, and told to wait (#210): every agent in | ||
| 69 | # this board's fallback chain is failing, or the chain says WAIT. Its | ||
| 70 | # own flag rather than no flag at all, because a board waiting for an | ||
| 71 | # agent to come back and a board nobody configured an agent for look | ||
| 72 | # identical from here and are fixed by different things. | ||
| 73 | push @flags, 'agent-waiting'; | ||
| 74 | $waiting = $wait; | ||
| 75 | } | ||
| 76 | elsif ( defined $cmd ) { | ||
| 77 | my $flag = 'agent'; | ||
| 78 | if ( $agent ) { | ||
| 79 | $flag .= ':' . $agent->{name}; | ||
| 80 | $flag .= ' failing' | ||
| 81 | unless $self->foundation->_agents->available( $agent->{name} ); | ||
| 82 | } | ||
| 83 | push @flags, $flag; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | my $total = keys %states; | ||
| 88 | printf "%s\n", $repo->basename; | ||
| 89 | printf " %d tasks", $total; | ||
| 90 | print ' [' . join( ', ', @flags ) . ']' if @flags; | ||
| 91 | print "\n"; | ||
| 92 | if (%count) { | ||
| 93 | printf " %s\n", join( ' ', map { "$_:$count{$_}" } sort keys %count ); | ||
| 94 | } | ||
| 95 | printf " disabled: %s\n", $disabled->{reason} // 'no reason given' | ||
| 96 | if $disabled; | ||
| 97 | printf " agent-error: %s\n", $agent_error if defined $agent_error; | ||
| 98 | printf " waiting: %s\n", $waiting if defined $waiting; | ||
| 99 | printf " in-progress: %s\n", join( ', ', map { "#$_" } @in_progress ) if @in_progress; | ||
| 100 | printf " blocked: %s\n", join( ', ', map { "#$_" } @blocked ) if @blocked; | ||
| 101 | print "\n"; | ||
| 102 | } | ||
| 103 | $self->_print_agents; | ||
| 104 | $self->_print_questions; | ||
| 105 | return; | ||
| 106 | } | ||
| 107 | |||
| 108 | # --------------------------------------------------------------------------- | ||
| 109 | # Agent availability (local config, per machine) | ||
| 110 | # --------------------------------------------------------------------------- | ||
| 111 | |||
| 112 | sub _print_agents { | ||
| 113 | my ( $self ) = @_; | ||
| 114 | my $agents = $self->foundation->_agents; | ||
| 115 | my @names = try { $agents->names } catch { | ||
| 116 | warn "karr-foundation: $_"; | ||
| 117 | (); | ||
| 118 | }; | ||
| 119 | return unless @names; | ||
| 120 | |||
| 121 | my $width = 0; | ||
| 122 | for my $n ( @names ) { $width = length $n if length $n > $width } | ||
| 123 | |||
| 124 | print "Agents\n"; | ||
| 125 | for my $name ( @names ) { | ||
| 126 | my $def = $agents->definitions->{$name}; | ||
| 127 | # Which one is the fleet's judgement layer, said here because this is the | ||
| 128 | # block that answers "what can run on this machine" -- and a coordination | ||
| 129 | # agent that is failing is why no new plan has appeared (#210). | ||
| 130 | printf " %-*s %s%s\n", $width, $name, _availability_line( $agents, $name ), | ||
| 131 | ( ( $def->{role} // '' ) eq 'coordinator' ? ' (coordinator)' : '' ); | ||
| 132 | next unless $self->foundation->verbose; | ||
| 133 | printf " %-*s kind: %s\n", $width, '', $def->{kind}; | ||
| 134 | # The description is prose and is printed as written -- it is the selection | ||
| 135 | # criterion a language model reads, not a field, so nothing here reformats | ||
| 136 | # or truncates it. | ||
| 137 | if ( defined $def->{description} && length $def->{description} ) { | ||
| 138 | my $text = $def->{description}; | ||
| 139 | $text =~ s/\s+\z//; | ||
| 140 | printf " %-*s %s\n", $width, '', $_ for split /\n/, $text; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | print "\n"; | ||
| 144 | return; | ||
| 145 | } | ||
| 146 | |||
| 147 | # --------------------------------------------------------------------------- | ||
| 148 | # The question mailbox (fleet-wide, in the hub) | ||
| 149 | # --------------------------------------------------------------------------- | ||
| 150 | |||
| 151 | # An open question is a chain waiting for somebody, and the somebody is whoever | ||
| 152 | # reads this. Printed for the same reason the agent block is: a question nobody | ||
| 153 | # sees is a chain nobody unblocks, and the id printed here is the one argument | ||
| 154 | # `karr-foundation answer` needs. Settled questions are not shown -- the mailbox | ||
| 155 | # is what is outstanding, not an archive. | ||
| 156 | sub _print_questions { | ||
| 157 | my ( $self ) = @_; | ||
| 158 | my $mailbox = $self->foundation->_questions or return; | ||
| 159 | my @open = try { $mailbox->open_questions } catch { | ||
| 160 | warn "karr-foundation: $_"; | ||
| 161 | (); | ||
| 162 | }; | ||
| 163 | return unless @open; | ||
| 164 | |||
| 165 | print "Open questions\n"; | ||
| 166 | for my $q ( @open ) { | ||
| 167 | my $r = $mailbox->resolve($q) || {}; | ||
| 168 | printf " #%s %s\n", $q->{id}, $q->{question}; | ||
| 169 | my @facts; | ||
| 170 | push @facts, 'options: ' . join( ', ', @{ $q->{options} } ) if $q->{options}; | ||
| 171 | # What happens if this stays unanswered, said in the same line, because | ||
| 172 | # that is the difference between a question somebody must answer and one | ||
| 173 | # that answers itself in an hour. | ||
| 174 | push @facts, | ||
| 175 | ( $r->{state} // '' ) eq 'overdue' | ||
| 176 | ? 'overdue: ' . $q->{policy} | ||
| 177 | . ( defined $r->{answer} ? " ($r->{answer})" : '' ) | ||
| 178 | : $q->{policy} . ( defined $q->{deadline} ? " after $q->{deadline}" : '' ); | ||
| 179 | printf " %s\n", join( ' ', @facts ); | ||
| 180 | } | ||
| 181 | print "\n"; | ||
| 182 | return; | ||
| 183 | } | ||
| 184 | |||
| 185 | # ok / failing since X / next attempt at Y -- the three states karr keeps, said | ||
| 186 | # in the order somebody reads them in. | ||
| 187 | sub _availability_line { | ||
| 188 | my ( $agents, $name ) = @_; | ||
| 189 | my $av = $agents->availability( $name ); | ||
| 190 | if ( ( $av->{state} // 'ok' ) eq 'failing' ) { | ||
| 191 | my $line = 'failing since ' . _stamp( $av->{failing_since} ) | ||
| 192 | . ', next attempt at ' . _stamp( $av->{next_attempt} ); | ||
| 193 | $line .= " ($av->{last_error})" if defined $av->{last_error}; | ||
| 194 | return $line; | ||
| 195 | } | ||
| 196 | # An agent that came back carries the last outage with it: that record is | ||
| 197 | # what the fixed-interval probe exists to leave behind, and a rhythm nobody | ||
| 198 | # ever sees is a rhythm nobody reads. | ||
| 199 | my $last = ( $av->{recovered} // [] )->[-1]; | ||
| 200 | return 'ok' unless $last && defined $last->{seconds}; | ||
| 201 | return 'ok (last outage ' . _duration( $last->{seconds} ) | ||
| 202 | . ', back at ' . _stamp( $last->{recovered_at} ) . ')'; | ||
| 203 | } | ||
| 204 | |||
| 205 | sub _stamp { | ||
| 206 | my ( $epoch ) = @_; | ||
| 207 | return '?' unless defined $epoch && $epoch =~ /\A[0-9]+\z/; | ||
| 208 | return localtime( $epoch )->strftime('%Y-%m-%dT%H:%M:%S'); | ||
| 209 | } | ||
| 210 | |||
| 211 | sub _duration { | ||
| 212 | my ( $s ) = @_; | ||
| 213 | return "${s}s" if $s < 90; | ||
| 214 | return int( $s / 60 ) . 'm' if $s < 5400; | ||
| 215 | return sprintf '%.1fh', $s / 3600; | ||
| 216 | } | ||
| 217 | |||
| 218 | 1; | ||
| 219 | |||
| 220 | __END__ |