File Coverage

blib/lib/Rex/WebUI/Model/LogBook.pm
Criterion Covered Total %
statement 9 44 20.4
branch 0 16 0.0
condition n/a
subroutine 3 11 27.2
pod 0 7 0.0
total 12 78 15.3


line stmt bran cond sub pod time code
1              
2             package Rex::WebUI::Model::LogBook;
3              
4 1     1   7 use strict;
  1         3  
  1         44  
5              
6 1     1   6 use Data::Dumper;
  1         2  
  1         81  
7 1     1   941 use DBIx::Foo qw(:all);
  1         24479  
  1         918  
8              
9             sub new
10             {
11 0     0 0   my ($class, $dbh) = @_;
12              
13 0           my $self = {
14             '_dbh' => $dbh,
15             };
16              
17 0           return bless $self, $class;
18             }
19              
20             sub dbh
21             {
22 0     0 0   return shift->{_dbh};
23             }
24              
25             sub add
26             {
27 0     0 0   my ($self, $data) = @_;
28              
29 0 0         die "Need a task_name" unless $data->{task_name};
30 0 0         die "Need a server" unless $data->{server};
31 0 0         die "Need a userid" unless $data->{userid};
32              
33 0 0         if (ref($data->{server}) eq 'ARRAY') {
34              
35 0           $data->{server} = join ", ", map { $_->{name} } @{$data->{server}};
  0            
  0            
36             }
37              
38 0           my $check = $self->dbh->selectrow_hashref("select max(jobid) from logbook");
39              
40 0 0         $self->_create_logbook_table unless $check;
41              
42 0           my $jobid = $self->dbh_do("insert into logbook (userid, task_name, server, statusid, jobid) values (?, ?, ?, ?, ?)", $data->{userid}, $data->{task_name}, $data->{server}, 0, undef);
43              
44 0 0         $jobid = "TS" . time if !$jobid; # in case db not working, use timestamp as a jobid
45              
46 0           return $jobid;
47             }
48              
49             sub update_status
50             {
51 0     0 0   my ($self, $jobid, $statusid) = @_;
52              
53 0           return $self->dbh_do("update logbook set statusid = ? where jobid = ?", $statusid, $jobid);
54             }
55              
56             sub set_pid
57             {
58 0     0 0   my ($self, $jobid, $pid) = @_;
59              
60 0           $self->dbh_do("update logbook set pid = ? where jobid = ?", $pid, $jobid);
61             }
62              
63             sub _create_logbook_table
64             {
65 0     0     my $self = shift;
66              
67 0           $self->dbh_do("create table logbook (jobid INTEGER PRIMARY KEY AUTOINCREMENT, userid int not null, task_name varchar(100), server varchar(100), statusid int)");
68             }
69              
70             sub running_tasks
71             {
72 0     0 0   my $self = shift;
73              
74 0           my $rows = $self->selectall("select l.*, s.status, u.username from logbook l join status s on l.statusid = s.statusid join users u on u.userid = l.userid where l.statusid in (0, 1)");
75              
76             # check if the tasks are still alive
77 0           foreach my $row (@$rows) {
78              
79 0 0         if ($row->{pid}) {
80 0 0         unless (my $check = kill 0, $row->{pid}) {
81 0           $row->{status} = 'DEAD' ;
82 0           $self->update_status($row->{jobid}, 3);
83             }
84             }
85             else {
86 0           $row->{status} = 'MISSING PID';
87             }
88             }
89              
90 0           return $rows;
91             }
92              
93             sub recent_tasks
94             {
95 0     0 0   my $self = shift;
96              
97 0           my $rows = $self->selectall("select l.*, s.status, u.username from logbook l join status s on l.statusid = s.statusid join users u on u.userid = l.userid where l.statusid > 1 order by l.jobid desc limit 20");
98              
99 0           return $rows;
100             }
101              
102             1;