<commit_before>package TreeBot::TreeHerder;
use Moo;

use HTTP::Request;
use JSON::XS qw(decode_json);
use POE qw(Component::Client::HTTP);
use Try::Tiny;

has irc       => ( is => 'rw' );
has resultset => ( is => 'rw' );

my $_instance;
sub instance {
    my $class = shift;
    return defined $_instance ? $_instance : ($_instance = $class->new(@_));
}

sub init {
    my ($self, $irc) = @_;
    POE::Component::Client::HTTP->spawn(
        Agent           => 'TreeBot/0.1',
        Alias           => 'ua',
        FollowRedirects => 5,
        Timeout         => 30,
    );
    $self->irc($irc);
}

sub poll {
    my ($self, $kernel) = @_;

    $self->resultset(undef);

    # grab resultset
    print "requesting https://treeherder.mozilla.org/api/project/bmo-master/resultset\n";
    $kernel->post(
        'ua', 'request', 'response',
        HTTP::Request->new( GET => 'https://treeherder.mozilla.org/api/project/bmo-master/resultset' ),
        'resultset'
    );

    # clean up old revisions
    $self->_cleanup();
}

sub response {
    my ($self, $kernel, $gen_args, $call_args) = @_[OBJECT, KERNEL, ARG0, ARG1];

    # decode json
    my $response;
    try {
        $response = decode_json($call_args->[0]->decoded_content);
    } catch {
        warn "Failed to decode json: $_\n";
        warn $call_args->[0]->as_string(), "\n";
        return;
    };

    # pass to handler
    if ($gen_args->[1] eq 'resultset') {
        $self->resultset_handler($kernel, $response);
    }
    elsif ($gen_args->[1] =~ /^status\.(\d+)/) {
        $self->status_handler($kernel, $1, $response);
    }
}

sub resultset_handler {
    my ($self, $kernel, $response) = @_;

    try {
        $self->resultset($response->{results});
        foreach my $result (@{ $response->{results} }) {
            my $id = $result->{id};
            if ($self->_exists($id)) {
                $self->_touch($id);
            }
            else {
                print "requesting https://treeherder.mozilla.org/api/project/bmo-master/resultset/$id/status\n";
                $kernel->post(
                    'ua', 'request', 'response',
                    HTTP::Request->new( GET => "https://treeherder.mozilla.org/api/project/bmo-master/resultset/$id/status" ),
                    "status.$id"
                );
            }
        }
    } catch {
        my $error = $_;
        $error =~ s/(^\s+|\s+$)//g;
        warn "$error\n";
    };
}

sub status_handler {
    my ($self, $kernel, $id, $response) = @_;
    return if $response->{running};

    try {<commit_msg>Append forward slash to Treeherder API URLs to avoid 301 redirects<commit_after>package TreeBot::TreeHerder;
use Moo;

use HTTP::Request;
use JSON::XS qw(decode_json);
use POE qw(Component::Client::HTTP);
use Try::Tiny;

has irc       => ( is => 'rw' );
has resultset => ( is => 'rw' );

my $_instance;
sub instance {
    my $class = shift;
    return defined $_instance ? $_instance : ($_instance = $class->new(@_));
}

sub init {
    my ($self, $irc) = @_;
    POE::Component::Client::HTTP->spawn(
        Agent           => 'TreeBot/0.1',
        Alias           => 'ua',
        FollowRedirects => 5,
        Timeout         => 30,
    );
    $self->irc($irc);
}

sub poll {
    my ($self, $kernel) = @_;

    $self->resultset(undef);

    # grab resultset
    print "requesting https://treeherder.mozilla.org/api/project/bmo-master/resultset/\n";
    $kernel->post(
        'ua', 'request', 'response',
        HTTP::Request->new( GET => 'https://treeherder.mozilla.org/api/project/bmo-master/resultset/' ),
        'resultset'
    );

    # clean up old revisions
    $self->_cleanup();
}

sub response {
    my ($self, $kernel, $gen_args, $call_args) = @_[OBJECT, KERNEL, ARG0, ARG1];

    # decode json
    my $response;
    try {
        $response = decode_json($call_args->[0]->decoded_content);
    } catch {
        warn "Failed to decode json: $_\n";
        warn $call_args->[0]->as_string(), "\n";
        return;
    };

    # pass to handler
    if ($gen_args->[1] eq 'resultset') {
        $self->resultset_handler($kernel, $response);
    }
    elsif ($gen_args->[1] =~ /^status\.(\d+)/) {
        $self->status_handler($kernel, $1, $response);
    }
}

sub resultset_handler {
    my ($self, $kernel, $response) = @_;

    try {
        $self->resultset($response->{results});
        foreach my $result (@{ $response->{results} }) {
            my $id = $result->{id};
            if ($self->_exists($id)) {
                $self->_touch($id);
            }
            else {
                print "requesting https://treeherder.mozilla.org/api/project/bmo-master/resultset/$id/status/\n";
                $kernel->post(
                    'ua', 'request', 'response',
                    HTTP::Request->new( GET => "https://treeherder.mozilla.org/api/project/bmo-master/resultset/$id/status/" ),
                    "status.$id"
                );
            }
        }
    } catch {
        my $error = $_;
        $error =~ s/(^\s+|\s+$)//g;
        warn "$error\n";
    };
}

sub status_handler {
    my ($self, $kernel, $id, $response) = @_;
    return if $response->{running};

    try {
{'fraction_non_alphanumeric': 0.18304692025487546, 'fraction_numerical': 0.005213361652828732, 'mean_word_length': 2.0778371954842543, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 71, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '75134', 'n_tokens_mistral': 1757, 'n_tokens_neox': 1693, 'n_words': 342}