Skip to content

Commit

Permalink
fixme
Browse files Browse the repository at this point in the history
  • Loading branch information
exodist committed Apr 28, 2024
1 parent 14bf0d1 commit d972bca
Show file tree
Hide file tree
Showing 12 changed files with 282 additions and 271 deletions.
4 changes: 1 addition & 3 deletions lib/App/Yath/Command/db/publish.pm
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ sub run {

my $config = schema_config_from_settings($settings);

my $ydb = $settings->db;
my $yup = $settings->upload;
my $user = $yup->user || $ENV{USER};
my $user = $settings->yath->user;

my $is_term = -t STDOUT ? 1 : 0;

Expand Down
29 changes: 29 additions & 0 deletions lib/App/Yath/Command/db/recent.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package App::Yath::Command::db::recent;
use strict;
use warnings;

our $VERSION = '2.000000';

use parent 'App::Yath::Command::recent';
use Test2::Harness::Util::HashBase;

sub summary { "Show a list of recent runs in the database" }

sub description {
return <<" EOT";
This command will find the last several runs from a yath database.
EOT
}

sub get_data {
my $self = shift;
my ($project, $count, $user) = @_;

return $self->get_from_db($project, $count, $user) // die "Could not get data from the database.\n";
}

1;

__END__
=head1 POD IS AUTO-GENERATED
157 changes: 157 additions & 0 deletions lib/App/Yath/Command/recent.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package App::Yath::Command::recent;
use strict;
use warnings;

our $VERSION = '2.000000';

use Term::Table;
use Test2::Harness::Util::JSON qw/decode_json/;
use App::Yath::Schema::Util qw/schema_config_from_settings/;

use parent 'App::Yath::Command';
use Test2::Harness::Util::HashBase;

use Getopt::Yath;

include_options(
'App::Yath::Options::Yath',
'App::Yath::Options::Recent',
'App::Yath::Options::Server',
'App::Yath::Options::DB',
);

sub summary { "Show a list of recent runs on a yathui server" }

sub group { 'recent' }

sub cli_args { "" }

sub description {
return <<" EOT";
This command will find the last several runs from a yath server
EOT
}

sub run {
my $self = shift;

my $settings = $self->settings;

my $yath = $settings->yath;
my $recent = $settings->recent;

my ($is_term, $use_color) = (0, 0);
if (-t STDOUT) {
require Term::Table::Cell;
$is_term = 1;
$use_color = eval { require Term::ANSIColor; 1 };
}

my $project = $yath->project // die "--project is a required argument.\n";
my $count = $recent->max || 10;
my $user = $settings->yath->user;

my $data = $self->get_data($project, $count, $user) or die "Could not get any data.\n";

@$data = reverse @$data;

my $url = $settings->server->url;
$url =~ s{/$}{}g if $url;

my $header = [qw/Time Duration Status Pass Fail Retry/, "Run ID"];
push @$header => 'Link' if $url;

my $rows = [];
for my $run (@$data) {
push @$rows => [@{$run}{qw/added duration status passed failed retried run_id/}];

if ($url) {
push @{$rows->[-1]} => $run->{status} ne 'broken' ? "$url/view/$run->{run_id}" : "N/A";
}

my $color;
if ($run->{status} eq 'broken') { $color = "magenta" }
elsif ($run->{status} eq 'pending') { $color = "blue" }
elsif ($run->{status} eq 'running') { $color = "blue" }
elsif ($run->{status} eq 'canceled') { $color = "yellow" }
elsif ($run->{failed}) { $color = "bold red" }
elsif ($run->{retried}) { $color = "bold cyan" }
elsif ($run->{passed}) { $color = "bold green" }

if ($use_color && $color) {
$_ = Term::Table::Cell->new(value => $_, value_color => Term::ANSIColor::color($color), reset_color => Term::ANSIColor::color("reset"))
for @{$rows->[-1]};
}
}

my $table = Term::Table->new(
header => $header,
rows => $rows,
);

print "$_\n" for $table->render;

return 0;
}

sub get_data {
my $self = shift;
my ($project, $count, $user) = @_;

return $self->get_from_db($project, $count, $user)
|| $self->get_from_http($project, $count, $user);
}

sub get_from_db {
my $self = shift;
my ($project, $count, $user) = @_;

my $settings = $self->settings;
my $config = schema_config_from_settings($settings) or return undef;
my $schema = $config->schema or return undef;

my $runs = $schema->vague_run_search(
username => $user,
project_name => $project,
query => {},
attrs => {order_by => {'-desc' => 'added'}, rows => $count},
list => 1,
);

my $data = [];

while (my $run = $runs->next) {
push @$data => $run->TO_JSON;
}

return undef unless @$data;

return $data;
}

sub get_from_http {
my $self = shift;
my ($project, $count, $user) = @_;

my $settings = $self->settings;
my $server = $settings->server;

require HTTP::Tiny;
my $ht = HTTP::Tiny->new();
my $url = $server->url or return;
$url =~ s{/$}{}g;
$url .= "/recent/$project/$user";
my $res = $ht->get($url);

die "Could not get recent runs from '$url'\n$res->{status}: $res->{reason}\n$res->{content}\n"
unless $res->{success};

return decode_json($res->{content});
}

1;

__END__
=head1 POD IS AUTO-GENERATED
Loading

0 comments on commit d972bca

Please sign in to comment.