lib/App/Yath/Command/recent.pm untested

filter: Statementstmt 0.0% 0 / 105 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 176 CC 16 SCAR 47.0
CC
16
Cov
0%
run
240.0
get_from_http
6.0
summary
2.0
CRAP 110.4
1 package App::Yath::Command::recent;
2 use strict;
3 use warnings;
4  
5 our $VERSION = '0.000150';
6  
7 use Term::Table;
8 use Test2::Harness::UI::Util qw/config_from_settings/;
9 use Test2::Harness::Util::JSON qw/decode_json/;
10  
11 use parent 'App::Yath::Command';
12 use Test2::Harness::Util::HashBase;
13  
14 use App::Yath::Options;
15  
16 option_group {prefix => 'yathui', category => "List Options"} => sub {
17 option max => (
18 type => 's',
19 long_examples => [' 10'],
20 default => 10,
21 description => 'Max number of recent runs to show',
22 );
23 };
24  
25 sub summary { "Show a list of recent YathUI runs" }
26  
27 sub group { 'log' }
28  
29 sub cli_args { "" }
30  
31 sub description {
32 return <<" EOT";
33 This command will find the last several runs from yathui or the yathui
34 database.
35  
36 This command gets the username from the "--yathui-user USER" option (Defaults to \$ENV{USER}).
37 This command gets the project from the "--yathui-project PROJECT" option.
38 This command uses the "--max NUM" option to set how many runs to show (Defaults to 10).
39  
40 EOT
41 }
42  
43 sub run {
44 my $self = shift;
45  
46 my $args = $self->args;
47 my $settings = $self->settings;
48  
49 my $yui = $settings->yathui;
50 my $ydb = $self->settings->prefix('yathui-db');
51 my $config = config_from_settings($settings);
52  
53 my ($is_term, $use_color) = (0, 0);
54 if (-t STDOUT) {
55 require Term::Table::Cell;
56 $is_term = 1;
57 $use_color = eval { require Term::ANSIColor; 1 };
58 }
59  
60 my $project = $yui->project // die "--yathui-project is a required argument.\n";
61 my $user = $yui->user // $ENV{USER};
62 my $count = $yui->max || 10;
63  
64 print "\nProject: $project\n User: $user\n Count: $count\n\n";
65  
66 my $data = $config->dbi_dsn
67 ? $self->get_from_db($settings, $config, $project, $user, $count)
68 : $self->get_from_http($settings, $yui, $project, $user, $count);
69  
70 @$data = reverse @$data;
71  
72 my $url = $yui->url;
73 $url =~ s{/$}{}g if $url;
74  
75 my $header = [qw/Time Duration Status Pass Fail Retry/, "Run ID"];
76 push @$header => 'Link' if $url;
77  
78 my $rows = [];
79 for my $run (@$data) {
80 push @$rows => [@{$run}{qw/added duration status passed failed retried run_id/}];
81  
82 if ($url) {
83 push @{$rows->[-1]} => $run->{status} ne 'broken' ? "$url/view/$run->{run_id}" : "N/A";
84 }
85  
86 my $color;
87 if ($run->{status} eq 'broken') { $color = "magenta" }
88 elsif ($run->{status} eq 'pending') { $color = "blue" }
89 elsif ($run->{status} eq 'running') { $color = "blue" }
90 elsif ($run->{status} eq 'canceled') { $color = "yellow" }
91 elsif ($run->{failed}) { $color = "bold red" }
92 elsif ($run->{retried}) { $color = "bold cyan" }
93 elsif ($run->{passed}) { $color = "bold green" }
94  
95 if ($use_color && $color) {
96 $_ = Term::Table::Cell->new(value => $_, value_color => Term::ANSIColor::color($color), reset_color => Term::ANSIColor::color("reset"))
97 for @{$rows->[-1]};
98 }
99 }
100  
101  
102 my $table = Term::Table->new(
103 header => $header,
104 rows => $rows,
105 );
106  
107 print "$_\n" for $table->render;
108  
109 return 0;
110 }
111  
112 sub get_from_http {
113 my $self = shift;
114 my ($settings, $yui, $project, $user, $count) = @_;
115  
116 require HTTP::Tiny;
117 my $ht = HTTP::Tiny->new();
118 my $url = $yui->url;
119 $url =~ s{/$}{}g;
120 $url .= "/recent/$project/$user";
121 my $res = $ht->get($url);
122  
123 die "Could not get recent runs from '$url'\n$res->{status}: $res->{reason}\n$res->{content}\n"
124 unless $res->{success};
125  
126 return decode_json($res->{content});
127 }
128  
129 sub get_from_db {
130 my $self = shift;
131 my ($settings, $config, $project, $user, $count) = @_;
132  
133 my $schema = $config->schema;
134 my $runs = $schema->vague_run_search(
135 username => $user,
136 project_name => $project,
137 query => {},
138 attrs => {order_by => {'-desc' => 'run_ord'}, rows => $count},
139 list => 1,
140 );
141  
142 my $data = [];
143  
144 while (my $run = $runs->next) {
145 push @$data => $run->TO_JSON;
146 }
147  
148 return $data;
149 }
150  
151  
152  
153 1;