blib/lib/MYDan/Collector/Show.pm untested

filter: Statementstmt 0.0% 0 / 118 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 195 CC 18 SCAR 43.3
CC
18
Cov
0%
curr
182.0
new
20.0
show
6.0
CRAP 75.6
1 package MYDan::Collector::Show;
2 use strict;
3 use warnings;
4  
5 use Carp;
6 use YAML::XS;
7  
8 use Digest::MD5;
9 use Sys::Hostname;
10  
11 use threads;
12 use Thread::Queue;
13  
14 use Data::Dumper;
15 use POSIX;
16  
17 use Socket;
18 use IO::Handle;
19  
20 sub new
21 {
22 my ( $class, %this ) = @_;
23  
24 $this{name} = '' unless $this{name} && $this{name} =~ /^[\w_]+$/;
25  
26 map{
27 $this{$_} = "$this{$_}_$this{name}" if $this{name};
28 confess "no $_\n" unless $this{$_} && -d $this{$_};
29 }qw( logs data );
30  
31 return bless \%this, ref $class || $class;
32 }
33  
34 sub show
35 {
36 my ( $this, @show ) = @_;
37 @show ? $this->hist( @show ): $this->curr();
38 return $this;
39 }
40  
41 sub curr
42 {
43 my ( $this, $data, $time ) = shift;
44  
45 if( $this->{sock} || $this->{ring} )
46 {
47 $data = sprintf "$this->{data}/%s.sock", $this->{sock} ? 'output' : 'ring';
48 unless( -S $data )
49 {
50 warn "no sock\n"; return $this;
51 }
52  
53 socket(my $sock, PF_UNIX, SOCK_STREAM, 0);
54 connect($sock, sockaddr_un($data)) or die "Connect: $!\n";
55 $sock->autoflush(1);
56 $data = join '', my @buf = <$sock>;
57 close $sock;
58 $data = eval{ YAML::XS::Load $data };
59 if( $@ ) { warn "syntax err:$@\n"; return $this; }
60 $time = POSIX::strftime( "%Y-%m-%d_%H:%M:%S", localtime );
61 }
62 else
63 {
64 $data = "$this->{data}/output";
65
66 unless( -f $data )
67 {
68 warn "no data\n"; return $this;
69 }
70  
71 $time = POSIX::strftime( "%Y-%m-%d_%H:%M:%S", localtime( (stat $data)[9] ) );
72  
73 $data = eval{ YAML::XS::LoadFile $data };
74 if( $@ ) { warn "syntax err:$@\n"; return $this; }
75 }
76  
77 # unless ( $data = $data->{data} )
78 # {
79 # warn "no data in yaml\n";return $this;
80 # }
81  
82 if( ref $data eq 'HASH' )
83 {
84 for( keys %$data )
85 {
86 my $stat = $data->{$_};
87 if( ref $stat ne 'ARRAY' )
88 {
89 warn "syntax err:$_\n";next;
90 }
91 map{ map{ printf "$time\t%s\n", join "\t", map{defined $_ ? $_:''}@$_ }@$_;print "\n"; }@$stat;
92 printf "\n%s\n\n", join ',', map{ $_->[0][0] }@$stat;
93 }
94 }
95 else
96 {
97 for my $s ( @$data )
98 {
99 next unless ref $s eq 'ARRAY';
100 my ( $d, $t ) = @$s;
101 next unless ref $d eq 'ARRAY';
102 map{map{ print "$t\t";map{ print defined $_ ? $_:'',"\t"}@$_; print "\n";}@$_}@$d;
103 }
104 }
105 return $this;
106 }
107  
108 sub hist
109 {
110 my $this = shift;
111 my $logs = $this->{logs};
112 my %show = map{ $_ => 1 }@_;
113  
114 my %hist = map{ $_ => ( stat $_ )[9] }grep{ /\/output\.\d+$/ }glob "$logs/output.*";
115  
116 map{
117 my $time = POSIX::strftime( "%Y-%m-%d_%H:%M:%S", localtime( $hist{$_} ) );
118  
119 eval{
120  
121 my $data = YAML::XS::LoadFile $_;
122 # $data = $data->{data};
123 for my $stat( values %$data )
124 {
125 next unless ref $stat eq 'ARRAY';
126 map{ my $t = $stat->[$_]; map{ printf "$time\t%s\n", join "\t", @$_;}@$t; }
127 grep{ $show{$stat->[$_][0][0]} } 0 .. @$stat -1;
128
129 }
130 };
131
132  
133 }sort{ $hist{$b} <=> $hist{$a} }keys %hist;
134
135 return $this;
136 }
137  
138 1;