blib/lib/Mojolicious/Plugin/Webtail.pm

filter: Statementstmt 89.2% 83 / 93 Branchbran 55.0% 11 / 20 Conditioncond 75.0% 6 / 8 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 72.2% 13 / 18 Podpod 100% 1 / 1 totaltotal 80.5% 116 / 144 CC 16 SCAR 11.6
CC
16
Cov
83%
DESTROY
6.0
__ANON__
5.4
register
4.1
CRAP 3.2
↑ Summary bin/mwtail »
1 package Mojolicious::Plugin::Webtail;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 our $VERSION = '0.07';
6  
7 4 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 ▶use constant DEFAULT_TAIL_OPTIONS => '-f -n 0';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 has 'template' => <<'TEMPLATE';
14 <html><head>
15 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
16 <title><%= $file %> - Webtail</title>
17 %= stylesheet begin
18 /* Stolen from https://github.com/r7kamura/webtail */
19 * {
20 margin: 0;
21 padding: 0;
22 }
23 body {
24 margin: 1em 0;
25 color: #ddd;
26 background: #111;
27 }
28 pre {
29 padding: 0 1em;
30 line-height: 1.25;
31 font-family: "Monaco", "Consolas", monospace;
32 }
33 #message {
34 position:fixed;
35 top:1em;
36 right:1em;
37 }
38 % end
39 %= javascript 'https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js'
40 %= javascript begin
41 $(function() {
42 var autoscroll = true;
43 // press 's' key to toggle autoscroll
44 $(window).keydown(function(e) { if (e.keyCode == 83 ) autoscroll = (autoscroll) ? false : true });
45  
46 var ws = new (WebSocket || MozWebSocket)('<%= $ws_url %>');
47 var timer_id;
48 ws.onopen = function() {
49 console.log('Connection opened');
50 timer_id = setInterval(
51 function() {
52 console.log('Connection keepalive');
53 ws.send('keepalive');
54 },
55 1000 * 240
56 );
57 };
58 ws.onmessage = function(msg) {
59 if (msg.data == '\n' && $('pre:last').text() == '\n') return;
60 $('<pre>').text(msg.data).appendTo('body');
61 if (autoscroll) $('html, body').scrollTop($(document).height());
62  
63 % if ($webtailrc) {
64 // webtailrc
65 <%== $webtailrc %>
66 % }
67 };
68 ws.onclose = function() {
69 console.warn('Connection closed');
70 clearInterval(timer_id);
71 };
72 ws.onerror = function(msg) {
73 console.error(msg.data);
74 };
75 });
76 % end
77 </head><body>
78 <div id="message">press 's' to toggle autoscroll</div>
79 </body></html>
80 TEMPLATE
81  
82 4 has 'file';
83 4 has 'webtailrc';
84 0 ▶has 'tail_opts' => sub { DEFAULT_TAIL_OPTIONS };
Subroutine __ANON__: not called CC=1 SCAR=1.2
85  
86 4 has '_tail_stream';
87 2 ▶has '_clients' => sub { +{} };
Subroutine __ANON__: called CC=1 SCAR=0.0
88  
89 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
90 0 my $self = shift;
91 0 ▶ $self->_tail_stream->close if $self->_tail_stream;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_tail_stream 0 0 0
92 }
93  
94 2 ▶sub _prepare_stream {
Subroutine _prepare_stream: called CC=4 SCAR=14.1
95 2 my ( $self, $app ) = @_;
96  
97 2 ▶ return if ( $self->_tail_stream );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_tail_stream 0 2 2
98  
99 2 my ( $fh, $pid );
100 2 my $read_from = 'STDIN';
101 2 ▶ if ( $self->file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->file) { } 1 1 2
102 1 require Text::ParseWords;
103 1 my @opts = Text::ParseWords::shellwords( $self->tail_opts );
104 1 my @cmd = ('tail', @opts, $self->file);
105 1 ▶ $pid = open( $fh, '-|', @cmd ) or Carp::croak "fork failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
$pid = open($fh, "-|", @cmd) or Carp::croak("fork failed: $!") 1 0 1
106 1 $read_from = join ' ', @cmd;
107 }
108 else {
109 1 $fh = *STDIN;
110 }
111 2 $app->log->debug("reading from: $read_from");
112  
113 2 my $stream = Mojo::IOLoop::Stream->new($fh)->timeout(0);
114 2 my $stream_id = Mojo::IOLoop->stream($stream);
115 2 ▶ $stream->on( read => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
116 2 my ($stream, $chunk) = @_;
117 2 for my $key (keys %{ $self->_clients }) {
118 2 my $tx = $self->_clients->{$key};
119 2 ▶ next unless $tx->is_websocket;
Branch: 1 branch
BranchTrueFalseTotal
unless $tx->is_websocket 0 2 2
120 2 $tx->send( Encode::decode_utf8($chunk) );
121 2 $app->log->debug( sprintf('sent %s', $key ) );
122 }
123 2 } );
124 0 ▶ $stream->on( error => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
125 0 $app->log->error( sprintf('error %s', $_[1] ) );
126 0 Mojo::IOLoop->remove($stream_id);
127 0 $self->_tail_stream(undef);
128 2 });
129 1 ▶ $stream->on( close => sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
130 1 $app->log->debug('close tail stream');
131 1 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 1 0 1
132 1 ▶ kill 'TERM', $pid if ( kill 0, $pid );
Branch: 1 branch
BranchTrueFalseTotal
if kill 0, $pid 1 0 1
133 1 waitpid( $pid, 0 );
134 };
135 1 Mojo::IOLoop->remove($stream_id);
136 1 $self->_tail_stream(undef);
137 2 });
138  
139 2 $self->_tail_stream($stream);
140 2 $app->log->debug( sprintf('connected tail stream %s', $stream_id ) );
141 }
142  
143 3 ▶sub register {
Subroutine register: called CC=4 SCAR=14.1
Pod documented
144 3 my $plugin = shift;
145 3 my ( $app, $args ) = @_;
146  
147 3 ▶ $plugin->file( $args->{file} || '' );
Condition100%2/2
$args->{'file'} || ''
l!l
12
MC/DC100%1/1
$args->{'file'} || ''
$args->{'file'}
Truth table100%2/2
A
A= $args->{'file'}
Aresult
11
00
148 3 ▶ $plugin->webtailrc( $args->{webtailrc} || '' );
Condition100%2/2
$args->{'webtailrc'} || ''
l!l
12
MC/DC100%1/1
$args->{'webtailrc'} || ''
$args->{'webtailrc'}
Truth table100%2/2
A
A= $args->{'webtailrc'}
Aresult
11
00
149 3 ▶ $plugin->tail_opts( $args->{tail_opts} || DEFAULT_TAIL_OPTIONS );
Condition50%1/2
$args->{'tail_opts'} || '-f -n 0'
l!l
03
MC/DC0%0/1
$args->{'tail_opts'} || '-f -n 0'
$args->{'tail_opts'}
Truth table50%1/2
A
A= $args->{'tail_opts'}
Aresult
11
00
150  
151 $app->hook(
152 3 ▶ before_dispatch => sub {
Subroutine __ANON__: called CC=5 SCAR=16.8
153 3 my $c = shift;
154 3 my $path = $c->req->url->path;
155  
156 3 ▶ return unless ($c->req->url->path =~ m|^/webtail/?$|);
Branch: 1 branch
BranchTrueFalseTotal
unless $c->req->url->path =~ m[^/webtail/?$]u 0 3 3
157  
158 3 ▶ if ( $c->tx->is_websocket ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->tx->is_websocket) 2 1 3
159 2 $plugin->_prepare_stream($app);
160 2 my $tx = $c->tx;
161 2 $plugin->_clients->{"$tx"} = $tx;
162 2 $c->app->log->debug( sprintf('connected %s', "$tx" ) );
163 0 ▶ Mojo::IOLoop->stream( $tx->connection )->timeout(300)->on( timeout => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
164 0 $c->finish;
165 0 delete $plugin->_clients->{"$tx"};
166 0 $c->app->log->debug( sprintf('timeout %s', $tx ) );
167 2 });
168 0 ▶ $c->on( message => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
169 0 $c->app->log->debug( sprintf('message "%s" from %s', $_[1], $tx ) );
170 2 } );
171 2 ▶ $c->on( finish => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
172 2 delete $plugin->_clients->{"$tx"};
173 2 $c->app->log->debug( sprintf('finish %s', $tx ) );
174 2 } );
175 2 $c->res->headers->content_type('text/event-stream');
176 2 return;
177 }
178  
179 1 my $ws_url = $c->req->url->to_abs->scheme('ws')->to_string;
180 $c->render(
181 inline => $plugin->template,
182 ws_url => $ws_url,
183 webtailrc => ( $plugin->webtailrc ) ? Mojo::File->new( $plugin->webtailrc )->slurp : '',
184 1 ▶ file => $args->{file} || 'STDIN',
Branch: 1 branch
BranchTrueFalseTotal
$plugin->webtailrc ? : 1 0 1
Condition50%1/2
$args->{'file'} || 'STDIN'
l!l
01
MC/DC0%0/1
$args->{'file'} || 'STDIN'
$args->{'file'}
Truth table50%1/2
A
A= $args->{'file'}
Aresult
11
00
185 );
186 },
187 3 );
188 3 return $app;
189 }
190  
191 4 1;
192 __END__
↑ Summary bin/mwtail »