File Coverage

blib/lib/Kevin/Commands/Util.pm
Criterion Covered Total %
statement 3 27 11.1
branch 0 26 0.0
condition 0 3 0.0
subroutine 1 5 20.0
pod n/a
total 4 61 6.5


line stmt bran cond sub pod time code
1              
2             package Kevin::Commands::Util;
3             $Kevin::Commands::Util::VERSION = '0.4.0';
4 1     1   10 use Mojo::Base -strict;
  1         4  
  1         8  
5              
6             # Borrowed from https://github.com/docker/go-units/blob/master/duration.go
7              
8             sub _human_duration {
9 0     0     my $seconds = shift;
10              
11 0 0         return 'Less than a second' if $seconds < 1;
12 0 0         return '1 second' if $seconds == 1;
13 0 0         return sprintf '%d seconds', $seconds if $seconds < 60;
14              
15 0           my $minutes = int($seconds / 60);
16 0 0         return 'About a minute' if $minutes == 1;
17 0 0         return sprintf '%d minutes', $minutes if $minutes < 46;
18              
19 0           my $hours = int($seconds / (60 * 60) + 0.5);
20 0 0         return 'About an hour' if $hours == 1;
21 0 0         return sprintf '%d hours', $hours if $hours < 48;
22 0 0         return sprintf '%d days', $hours / 24 if $hours < 24 * 7 * 2;
23 0 0         return sprintf '%d weeks', $hours / (24 * 7) if $hours < 24 * 30 * 2;
24 0 0         return sprintf '%d months', $hours / (24 * 3) if $hours < 24 * 365 * 2;
25              
26 0           return sprintf '%d years', $hours / (24 * 365);
27             }
28              
29             sub _created_since {
30 0     0     _human_duration(shift) . ' ago';
31             }
32              
33             sub _running_since {
34 0     0     'Up ' . _human_duration(shift);
35             }
36              
37             sub _job_status {
38 0     0     my ($info, $now) = (shift, shift);
39              
40 0           my $state = $info->{state};
41 0 0         if ($state eq 'active') {
42             return 'Waiting ' . _human_duration($now - $info->{delayed})
43 0 0         if $info->{delayed};
44              
45 0           return 'Running';
46             }
47              
48 0 0 0       if ($state eq 'failed' || $state eq 'finished') {
49             return
50 0           ucfirst $state . ' ' . _human_duration($now - $info->{finished}) . ' ago';
51             }
52              
53 0           return 'Inactive';
54             }
55              
56             1;
57              
58             __END__