| 1 | package App::Ordo::Command::Server::List; | ||
| 2 | use Moo; | ||
| 3 | use feature qw(say); | ||
| 4 | use utf8; | ||
| 5 | use open ':std', ':utf8'; | ||
| 6 | |||
| 7 | use Term::ANSIColor qw(colored); | ||
| 8 | use Text::Table::Tiny 1.02 qw(generate_table); | ||
| 9 | |||
| 10 | |||
| 11 | extends 'App::Ordo::Command::Base'; | ||
| 12 | |||
| 13 | sub name { "server list" } | ||
| 14 | sub summary { "List all worker servers" } | ||
| 15 | sub usage { "" } | ||
| 16 | sub aliases { ['server ls'] } | ||
| 17 | |||
| 18 | sub option_spec { {} } | ||
| 19 | |||
| 20 | sub execute { | ||
| 21 | my ($self) = @_; | ||
| 22 | my $res = $self->api->call('find_monitor', {}); | ||
| 23 | |||
| 24 | unless ($res->{success} && $res->{servers}) { | ||
| 25 | say colored(["bold yellow"], "No servers found"); | ||
| 26 | return; | ||
| 27 | } | ||
| 28 | |||
| 29 | my $rows = [ [qw(ID NAME HOST USER CPU MEM DISK PING UPDATED)] ]; | ||
| 30 | for my $s (@{$res->{servers} || []}) { | ||
| 31 | push @$rows, [ | ||
| 32 | $s->{id}, | ||
| 33 | $s->{name} || '-', | ||
| 34 | $s->{host} || '-', | ||
| 35 | $s->{user} || '-', | ||
| 36 | $s->{cpu} ? sprintf("%.1f", $s->{cpu}) : '-', | ||
| 37 | $s->{total_memory} ? sprintf("%.1f%%", ($s->{used_memory}/$s->{total_memory})*100) : '-', | ||
| 38 | $s->{total_disk} ? sprintf("%.1f%%", ($s->{used_disk}/$s->{total_disk})*100) : '-', | ||
| 39 | $s->{ping} ? sprintf("%.0fms", $s->{ping}) : '-', | ||
| 40 | $s->{update_time} ? time - $s->{update_time} . 's ago' : '-', | ||
| 41 | ]; | ||
| 42 | } | ||
| 43 | say generate_table(rows => $rows, header_row => 1, style => 'boxrule'); | ||
| 44 | } | ||
| 45 | |||
| 46 | 1; |