| 1 | package Monitoring::Spooler::Cmd::Command::list; | ||
| 2 | $Monitoring::Spooler::Cmd::Command::list::VERSION = '0.05'; | ||
| 3 | BEGIN { | ||
| 4 | $Monitoring::Spooler::Cmd::Command::list::AUTHORITY = 'cpan:TEX'; | ||
| 5 | } | ||
| 6 | # ABSTRACT: list all queued notifications | ||
| 7 | |||
| 8 | use 5.010_000; | ||
| 9 | use mro 'c3'; | ||
| 10 | use feature ':5.10'; | ||
| 11 | |||
| 12 | use Moose; | ||
| 13 | use namespace::autoclean; | ||
| 14 | |||
| 15 | # use IO::Handle; | ||
| 16 | # use autodie; | ||
| 17 | # use MooseX::Params::Validate; | ||
| 18 | # use Carp; | ||
| 19 | # use English qw( -no_match_vars ); | ||
| 20 | # use Try::Tiny; | ||
| 21 | |||
| 22 | # extends ... | ||
| 23 | extends 'Monitoring::Spooler::Cmd::Command'; | ||
| 24 | # has ... | ||
| 25 | has 'all' => ( | ||
| 26 | 'is' => 'ro', | ||
| 27 | 'isa' => 'Bool', | ||
| 28 | 'default' => 0, | ||
| 29 | 'traits' => [qw(Getopt)], | ||
| 30 | 'cmd_aliases' => 'a', | ||
| 31 | 'documentation' => 'When set prints all tables/queues', | ||
| 32 | ); | ||
| 33 | |||
| 34 | has 'group_id' => ( | ||
| 35 | 'is' => 'ro', | ||
| 36 | 'isa' => 'Int', | ||
| 37 | 'traits' => [qw(Getopt)], | ||
| 38 | 'cmd_aliases' => 'g', | ||
| 39 | 'documentation' => 'Restrict output to this Group ID', | ||
| 40 | ); | ||
| 41 | # with ... | ||
| 42 | # initializers ... | ||
| 43 | |||
| 44 | # your code here ... | ||
| 45 | sub execute { | ||
| 46 | my $self = shift; | ||
| 47 | |||
| 48 | # just print the queue content if invoked w/o options | ||
| 49 | # we may also print all tables if the user asks for them | ||
| 50 | my $sql = 'SELECT id, group_id, type, message, ts FROM msg_queue'; | ||
| 51 | my @args = (); | ||
| 52 | if($self->group_id()) { | ||
| 53 | $sql .= ' WHERE group_id = ?'; | ||
| 54 | push(@args, $self->group_id()); | ||
| 55 | } | ||
| 56 | $sql .= ' ORDER BY id'; | ||
| 57 | my $sth = $self->dbh()->prepare($sql); | ||
| 58 | $sth->execute(@args); | ||
| 59 | |||
| 60 | my $msg_ref = {}; | ||
| 61 | while(my ($id, $group_id, $type, $message) = $sth->fetchrow_array()) { | ||
| 62 | push(@{$msg_ref->{$group_id}}, { | ||
| 63 | 'id' => $id, | ||
| 64 | 'group_id' => $group_id, | ||
| 65 | 'type' => $type, | ||
| 66 | 'message' => $message, | ||
| 67 | }); | ||
| 68 | } | ||
| 69 | $sth->finish(); | ||
| 70 | |||
| 71 | if(scalar(keys %$msg_ref)) { | ||
| 72 | foreach my $group_id (sort keys %$msg_ref) { | ||
| 73 | print "Messages waiting in Queue for Group ".$group_id."\n"; | ||
| 74 | foreach my $msg (@{$msg_ref->{$group_id}}) { | ||
| 75 | printf("[%4d] %s %s\n", $msg->{'id'}, $msg->{'type'}, $msg->{'message'}); | ||
| 76 | } | ||
| 77 | } | ||
| 78 | } else { | ||
| 79 | print "Queue is empty\n"; | ||
| 80 | } | ||
| 81 | |||
| 82 | if($self->all()) { | ||
| 83 | my $sql = 'SELECT id, name FROM groups ORDER BY name'; | ||
| 84 | my $sth = $self->dbh()->prepare($sql); | ||
| 85 | $sth->execute(); | ||
| 86 | while(my ($id, $name) = $sth->fetchrow_array()) { | ||
| 87 | printf("%s (%i)\n", $name, $id); | ||
| 88 | } | ||
| 89 | $sth->finish(); | ||
| 90 | |||
| 91 | $sql = 'SELECT group_id, until FROM paused_groups ORDER BY group_id'; | ||
| 92 | $sth = $self->dbh()->prepare($sql); | ||
| 93 | $sth->execute(); | ||
| 94 | while(my ($id, $until) = $sth->fetchrow_array()) { | ||
| 95 | printf("%i => %s\n", $id, localtime($until)); | ||
| 96 | } | ||
| 97 | $sth->finish(); | ||
| 98 | |||
| 99 | $sql = 'SELECT group_id,type,notify_from,notify_to FROM notify_interval ORDER BY group_id'; | ||
| 100 | $sth = $self->dbh()->prepare($sql); | ||
| 101 | $sth->execute(); | ||
| 102 | while(my ($group_id, $type, $notify_from, $notify_to) = $sth->fetchrow_array()) { | ||
| 103 | printf("%i %s %s - %s\n", $group_id, $type, $notify_from, $notify_to); | ||
| 104 | } | ||
| 105 | $sth->finish(); | ||
| 106 | } | ||
| 107 | |||
| 108 | return 1; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub abstract { | ||
| 112 | return "List the notification queue"; | ||
| 113 | } | ||
| 114 | |||
| 115 | no Moose; | ||
| 116 | __PACKAGE__->meta->make_immutable; | ||
| 117 | |||
| 118 | 1; | ||
| 119 | |||
| 120 | __END__ |