App-Prove-Plugin-Elasticsearch

 view release on metacpan or  search on metacpan

lib/App/Prove/Elasticsearch/Indexer.pm  view on Meta::CPAN

# ABSTRACT: Define what data is to be uploaded to elasticsearch, and handle it's uploading
# PODNAME: App::Prove::Elasticsearch::Indexer

package App::Prove::Elasticsearch::Indexer;
$App::Prove::Elasticsearch::Indexer::VERSION = '0.001';
use strict;
use warnings;

use App::Prove::Elasticsearch::Utils();

use Search::Elasticsearch();
use List::Util 1.33;

our $index = 'testsuite';

sub index {
    return $index;
}

our $max_query_size = 1000;
our $e;
our $bulk_helper;
our $idx;

sub check_index {
    my $conf = shift;

    my $port = $conf->{'server.port'} ? ':' . $conf->{'server.port'} : '';
    die "server must be specified" unless $conf->{'server.host'};
    die("port must be specified")  unless $port;
    my $serveraddress = "$conf->{'server.host'}$port";
    $e //= Search::Elasticsearch->new(
        nodes => $serveraddress,
    );

    #XXX for debugging
    #$e->indices->delete( index => $index );

    if (!$e->indices->exists(index => $index)) {
        $e->indices->create(
            index => $index,
            body  => {
                index => {
                    number_of_shards   => "3",
                    number_of_replicas => "2",
                    similarity         => {default => {type => "classic"}}
                },
                analysis => {
                    analyzer => {
                        default => {
                            type      => "custom",
                            tokenizer => "whitespace",
                            filter    => [
                                'lowercase', 'std_english_stop', 'custom_stop'
                            ]
                        }
                    },
                    filter => {
                        std_english_stop => {
                            type      => "stop",
                            stopwords => "_english_"

lib/App/Prove/Elasticsearch/Indexer.pm  view on Meta::CPAN

        $hit->{_source}->{platform} = [ $hit->{_source}->{platform} ]
          if ref($hit->{_source}->{platform}) ne 'ARRAY';
        next if (scalar(@{$opts{versions}}) && !$hit->{_source}->{version});
        next
          unless List::Util::any { $hit->{_source}->{version} eq $_ }
        @{$opts{versions}};
        next if (scalar(@{$opts{platforms}}) && !$hit->{_source}->{platform});
        next unless List::Util::all {
            my $p = $_;
            grep { $_ eq $p } @{$hit->{_source}->{platform}}
        }
        @{$opts{platforms}};
        next unless $hit->{_source}->{name} eq $opts{case};

        $attempts++;

        #Merge the existing defects with the ones we are adding in
        $hit->{defect} //= [];
        my @df_merged =
          List::Util::uniq((@{$hit->{defect}}, @{$opts{defects}}));

        my %update = (
            index => $index,
            id    => $hit->{_id},
            type  => 'result',
            body  => {
                doc => {
                    defect => \@df_merged,
                },
            }
        );
        $update{body}{doc}{status} = $opts{status} if $opts{status};

        my $res = $e->update(%update);

        print "Associated cases to document $hit->{_id}\n"
          if $res->{result} eq 'updated';
        if (!grep { $res->{result} eq $_ } qw{updated noop}) {
            print
              "Something went wrong associating cases to document $hit->{_id}!\n$res->{result}\n";
            $failures++;
        }
    }

    print "No cases matching your query could be found.  No action was taken.\n"
      unless $attempts;

    return $failures;
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

App::Prove::Elasticsearch::Indexer - Define what data is to be uploaded to elasticsearch, and handle it's uploading

=head1 VERSION

version 0.001

=head1 SYNOPSIS

    App::Prove::Elasticsearch::Indexer::check_index({ 'server.host' => 'zippy.test', 'server.port' => 9600 });

=head1 VARIABLES

=head2 index (STRING)

The name of the elasticsearch index used.
If you are subclassing this, be aware that the Searcher plugin will rely on this.

=head2 max_query_size

Number of items returned by queries.
Defaults to 1000.

=head1 SUBROUTINES

=head2 check_index

Returns 1 if the index needed to be created, 0 if it's already OK.
Dies if the server cannot be reached, or the index creation fails.

=head2 index_results

Index a test result (see L<App::Prove::Elasticsearch::Parser> for the input).

=head2 bulk_index_results(@results)

Helper method for migration scripts.
Uploads an array of results in bulk such as would be fed to index_results.

It is up to the caller to chunk inputs as is appropriate for your installation.

=head2 associate_case_with_result(%config)

Associate an indexed result with a tracked defect.

Requires configuration to be inside of ENV vars already.

Arguments Hash:

=over 4

=item B<case STRING>     - case to associate defect to

=item B<defects ARRAY>   - defects to associate with case

=item B<platforms ARRAY> - filter out any results not having these platforms

=item B<versions ARRAY>  - filter out any results not having these versions

=back

=head1 SPECIAL THANKS



( run in 1.370 second using v1.01-cache-2.11-cpan-b16cb0d3907 )