blib/lib/App/RecordStream/Operation/fromps.pm

filter: Statementstmt 20.6% 12 / 58 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 28.5% 4 / 14 Podpod n/a totaltotal 17.7% 16 / 90 CC 9 SCAR 22.7
CC
9
Cov
17%
stream_done
30.0
init
6.0
get_process_table
6.0
CRAP 9.6
« blib/lib/App/RecordStream/Site.pm ↑ Summary blib/lib/App/RecordStream/KeySpec.pm »
1 package App::RecordStream::Operation::fromps;
2  
3 our $VERSION = "4.0.23";
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use base qw(App::RecordStream::Operation);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use App::RecordStream::OptionalRequire qw(Proc::ProcessTable);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 App::RecordStream::OptionalRequire::require_done();
12  
13 sub init {
14 0 ▶ my $this = shift;
Subroutine init: not called CC=2 SCAR=17.9
15 0 my $args = shift;
16  
17 0 my $process_table = $this->get_process_table();
18  
19 0 my @fields;
20  
21 my $spec = {
22 0 ▶ 'key|k|field|f=s' => sub { push @fields, split(',', $_[1]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
23 0 };
24  
25 0 $this->parse_options($args, $spec);
26  
27 0 ▶ if ( scalar @fields < 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @fields < 1) 0 0 0
28 # Silly Proc::ProcessTable has been known to give undefs sometimes for some
29 # reason. We throw them on the ground.
30 0 @fields = grep { defined($_) } $process_table->fields();
31 }
32  
33 0 $this->{'FIELDS'} = \@fields;
34 }
35  
36 sub get_process_table {
37 0 ▶ my $this = shift;
Subroutine get_process_table: not called CC=2 SCAR=17.9
38 0 ▶ $this->{'PROCESS_TABLE'} ||= Proc::ProcessTable->new();
Condition0%0/3
$this->{"PROCESS_TABLE"} ||= "Proc::ProcessTable"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$this->{"PROCESS_TABLE"} ||= "Proc::ProcessTable"->new
$this->{"PROCESS_TABLE"} "Proc::ProcessTable"->new
Truth table0%0/3
A ||= B
A= $this->{"PROCESS_TABLE"}B= "Proc::ProcessTable"->new
ABresult
1X1
011
000
39 0 return $this->{'PROCESS_TABLE'}
40 }
41  
42 sub set_process_table {
43 0 ▶ my $this = shift;
Subroutine set_process_table: not called CC=1 SCAR=6.9
44 0 my $table = shift;
45 0 $this->{'PROCESS_TABLE'} = $table;
46 }
47  
48 sub set_converter {
49 0 ▶ my $this = shift;
Subroutine set_converter: not called CC=1 SCAR=6.9
50 0 my $func = shift;
51 0 $this->{'CONVERTER'} = $func;
52 }
53  
54 sub get_converter {
55 0 ▶ my $this = shift;
Subroutine get_converter: not called CC=2 SCAR=17.9
56 0 ▶ $this->{'CONVERTER'} ||= sub { return (getpwuid($_[0]))[0] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
$this->{"CONVERTER"} ||= sub { return (getpwuid $_[0])[0]; }
l!l&&r!l&&!r
000
MC/DC0%0/2
$this->{"CONVERTER"} ||= sub { return (getpwuid $_[0])[0]; }
$this->{"CONVERTER"} sub { return (getpwuid $_[0])[0]; }
Truth table0%0/3
A ||= B
A= $this->{"CONVERTER"}B= sub { return (getpwuid $_[0])[0]; }
ABresult
1X1
011
000
57 0 return $this->{'CONVERTER'};
58 }
59  
60 sub wants_input {
61 0 ▶ return 0;
Subroutine wants_input: not called CC=1 SCAR=6.9
62 }
63  
64 sub stream_done {
65 0 ▶ my $this = shift;
Subroutine stream_done: not called CC=5 SCAR=34.0
66  
67 0 my $table = $this->get_process_table();
68 0 my $fields = $this->{'FIELDS'};
69  
70 0 foreach my $proc (@{$table->table()}) {
71 0 my $record = App::RecordStream::Record->new();
72 0 foreach my $field (@$fields) {
73 0 my $value = $proc->{$field};
74  
75 0 ▶ if ( $field eq 'uid' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field eq "uid") 0 0 0
76 0 $value = $this->get_converter()->($value);
77 }
78  
79 0 ▶ $record->{$field} = $value if ( defined $value );
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 0 0 0
80 }
81  
82 0 $this->push_record($record);
83 }
84 }
85  
86 sub usage {
87 0 ▶ my $this = shift;
Subroutine usage: not called CC=2 SCAR=17.9
88  
89 0 my @fields = Proc::ProcessTable->new()->fields();
90 0 my $all_fields = join (', ', grep { defined } @fields);
91  
92 0 my $options = [
93 [ 'keys <fields>', 'Fields to output. May be specified multiple times, may be comma separated. Default to all fields These are Proc::ProcessTable keys, and thus may not be keyspecs or groups'],
94 ];
95  
96 0 my $args_string = $this->options_string($options);
97 0 ▶ my $default_fields = $ENV{GENERATING_STATIC_DOC} ? <<STATIC : <<DYNAMIC;
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'GENERATING_STATIC_DOC'} ? : 0 0 0
98 Default fields for Linux:
99 __FORMAT_TEXT__
100 uid, gid, pid, fname, ppid, pgrp, sess, ttynum, flags, minflt, cminflt,
101 majflt, cmajflt, utime, stime, cutime, cstime, priority, start, size, rss,
102 wchan, time, ctime, state, euid, suid, fuid, egid, sgid, fgid, pctcpu,
103 pctmem, cmndline, exec, cwd
104 __FORMAT_TEXT__
105  
106 Default fields for OS X:
107 __FORMAT_TEXT__
108 pid, ppid, pgrp, uid, gid, euid, egid, suid, sgid, priority, size, rss,
109 flags, nice, sess, time, stime, utime, start, wchan, ttydev, ttynum, pctcpu,
110 pctmem, state, cmndline, fname
111 __FORMAT_TEXT__
112 STATIC
113 Default fields:
114 __FORMAT_TEXT__
115 $all_fields
116 __FORMAT_TEXT__
117 DYNAMIC
118 0 chomp $default_fields;
119  
120 0 return <<USAGE;
121 Usage: recs-fromps <args>
122 __FORMAT_TEXT__
123 Prints out JSON records converted from the process table.
124 __FORMAT_TEXT__
125  
126 $args_string
127  
128 $default_fields
129  
130 Examples:
131 Get records for the process table
132 recs-fromps
133 Only get uid and pid
134 recs-fromps --keys uid,pid
135 USAGE
136 }
137  
138 1;
« blib/lib/App/RecordStream/Site.pm ↑ Summary blib/lib/App/RecordStream/KeySpec.pm »