ProgressStatus.pm

filter: Statementstmt 85.7% 78 / 91 Branchbran 53.3% 16 / 30 Conditioncond 25.0% 3 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 94.1% 16 / 17 Podpod n/a totaltotal 71.9% 113 / 157 CC 21 SCAR 22.6
CC
21
Cov
73%
__ANON__
30.0
__ANON__
13.5
_progress_status_file
5.5
CRAP 9.6
↑ Summary ProgressStatus/Object.pm »
1 # ABSTRACT: Keeps track of progress status
2  
3 package Dancer2::Plugin::ProgressStatus;
4 2 $Dancer2::Plugin::ProgressStatus::VERSION = '0.018';
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Digest::MD5 qw/md5_hex/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use File::Path qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use JSON qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use Dancer2::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Dancer2::Plugin::ProgressStatus::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Dancer2::Core::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 9 ▶sub _progress_status_file {
Subroutine _progress_status_file: called CC=4 SCAR=17.1
19 9 my ( $dsl, $name ) = @_;
20  
21 my $dir = $dsl->app->config->{'plugins'}{ProgressStatus}{dir}
22 9 ▶ or croak 'No ProgressStatus plugin settings in config';
Branch: 1 branch
BranchTrueFalseTotal
my $dir = $dsl->app->config->{"plugins"}{"ProgressStatus"}{"dir"} or croak("No ProgressStatus plugin settings in config") 9 0 9
23 9 ▶ if ( !-d $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 0 9 9
24 0 ▶ File::Path::make_path($dir) or die "Cannot create path $dir";
Branch: 1 branch
BranchTrueFalseTotal
File::Path::make_path($dir) or die "Cannot create path $dir" 0 0 0
25 }
26  
27 9 return Path::Tiny::path($dir, md5_hex($name));
28 }
29  
30  
31 1 ▶on_plugin_import {
Subroutine __ANON__: called CC=1 SCAR=0.0
32 1 my $dsl = shift;
33  
34 # determine if there is a prefix?
35  
36 # Register the route for fetching messages
37 # We must specify the content_type and encoding here because this plugin
38 # only works for json, and the D2 config might have other settings
39 $dsl->app->add_route(
40 method => 'get',
41 regexp => '/_progress_status/:name',
42 4 ▶ code => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
43 4 my $context = shift;
44 4 my $data = _get_progress_status_data($dsl, $context->request->params->{'name'});
45  
46 4 $context->response->status(200);
47 4 $context->response->content(JSON->new->utf8->encode($data));
48 4 $context->response->content_type('application/json');
49 4 $context->response->is_encoded(1);
50  
51 4 return $context->response;
52 },
53 1 );
54 2 };
55  
56 4 ▶sub _get_progress_status_data {
Subroutine _get_progress_status_data: called CC=2 SCAR=7.1
57 4 my ($dsl, $name) = @_;
58  
59 4 my $file = $dsl->_progress_status_file($name);
60 4 ▶ if ( !$file->is_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file->is_file) 0 4 4
61 return {
62 0 error => "No such progress status $name",
63 status => 'error',
64 };
65 }
66 4 my $data = JSON->new->utf8(0)->decode($file->slurp_utf8());
67 4 delete $data->{pid};
68  
69 4 return $data;
70 }
71  
72  
73 5 ▶register start_progress_status => sub {
Subroutine __ANON__: called CC=12 SCAR=26.1
74 5 my ($dsl, $args) = @_;
75  
76 5 ▶ if ( !ref($args) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $args) 3 2 5
77 3 $args = { name => $args };
78 }
79  
80 5 my $progress_id = $dsl->params->{progress_id};
81 5 my $name = delete $args->{name};
82 5 ▶ if ( !$name && !$progress_id ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name or $progress_id) 0 5 5
Condition66%2/3
$name or $progress_id
l!l&&r!l&&!r
410
MC/DC0%0/2
$name or $progress_id
$name $progress_id
Truth table66%2/3
A or B
A= $nameB= $progress_id
ABresult
1X1
011
000
83 0 croak 'Must supply name and/or progress_id';
84 }
85 5 ▶ $name .= $progress_id if $progress_id;
Branch: 1 branch
BranchTrueFalseTotal
if $progress_id 1 4 5
86  
87 5 my $file = $dsl->_progress_status_file($name);
88 5 ▶ if ( $file->is_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->is_file) 1 4 5
89 1 my $d = JSON->new->utf8(0)->decode($file->slurp_utf8());
90 1 my $in_progress = $d->{in_progress};
91  
92 1 ▶ if ( $in_progress && $d->{pid} != $$ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($in_progress and $d->{'pid'} != $$) { } 0 1 1
elsif ($in_progress) { } 1 0 1
Condition33%1/3
$in_progress and $d->{"pid"} != $$
!ll&&!rl&&r
010
MC/DC0%0/2
$in_progress and $d->{"pid"} != $$
$in_progress $d->{"pid"} != $$
Truth table33%1/3
A and B
A= $in_progressB= $d->{"pid"} != $$
ABresult
0X0
100
111
93 0 ▶ if ( kill(0, $d->{pid}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill 0, $d->{"pid"}) 0 0 0
94 0 die "Progress status $name already exists for a running process, cannot create a new one\n";
95 }
96 }
97 elsif ( $in_progress ) {
98 1 die "Progress status $name already exists\n";
99 }
100 }
101  
102 my %objargs = (
103 20 ▶ _on_save => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
104 20 my ($obj, $is_finished) = @_;
105 20 ▶ my $data = JSON->new->utf8(0)->encode({
Branch: 1 branch
BranchTrueFalseTotal
$is_finished ? : 4 16 20
106 start_time => $obj->start_time,
107 current_time => $obj->current_time,
108 total => $obj->total,
109 count => $obj->count,
110 messages => $obj->messages,
111 in_progress => $is_finished ? JSON::false : JSON::true,
112 status => $obj->status,
113 pid => $$,
114 });
115  
116 20 $file->spew_utf8($data);
117 },
118 4 );
119  
120 4 foreach my $key (qw/total count status messages/) {
121 16 ▶ if ( $args->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{$key}) 2 14 16
122 2 $objargs{$key} = $args->{$key};
123 }
124 }
125  
126 4 my $obj = Dancer2::Plugin::ProgressStatus::Object->new(%objargs);
127 4 $obj->save();
128 4 return $obj;
129 2 };
130  
131 0 ▶register is_progress_running => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
132 0 my ( $dsl, $name ) = @_;
133 0 my $file = $dsl->_progress_status_file($name);
134  
135 0 ▶ if ( $file->exists ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->exists) 0 0 0
136 0 my $d = JSON->new->utf8(0)->decode($file->slurp_utf8());
137 0 my $in_progress = $d->{in_progress};
138  
139 0 ▶ if ( $in_progress && $d->{pid} != $$ && kill(0, $d->{pid}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_progress and $d->{"pid"} != $$ and kill 0, $d->{"pid"}) 0 0 0
Condition0%0/6
$in_progress and $d->{"pid"} != $$ and kill 0, $d->{"pid"}
!ll&&!rl&&r
000
$in_progress and $d->{"pid"} != $$
!ll&&!rl&&r
000
MC/DC0%0/3
$in_progress and $d->{"pid"} != $$ and kill 0, $d->{"pid"}
$in_progress $d->{"pid"} != $$ kill 0, $d->{"pid"}
Truth table0%0/4
A and B and C
A= $in_progressB= $d->{"pid"} != $$C= kill 0, $d->{"pid"}
ABCresult
0XX0
10X0
1100
1111
140 0 return 1;
141 }
142 }
143 0 return 0;
144 2 };
145  
146  
147 2 register_plugin;
148  
149  
150 2 1;
151  
152 __END__
↑ Summary ProgressStatus/Object.pm »