App-Prove-Plugin-Elasticsearch

 view release on metacpan or  search on metacpan

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

    #It's normal to have multiple platforms in a document.
    foreach my $plat (@{$opts{platforms}}) {
        push(@{$q{body}{query}{bool}{must}}, {match => {platform => $plat}});
    }

    #It's NOT normal to have multiple versions in a document.
    foreach my $version (@{$opts{versions}}) {
        push(
            @{$q{body}{query}{bool}{should}},
            {match => {version => $version}}
        );
    }

    #Paginate the query, TODO short-circuit when we stop getting results?
    my $hits = App::Prove::Elasticsearch::Utils::do_paginated_query(
        $e, $max_query_size,
        %q
    );
    return 0 unless scalar(@$hits);

    #Now, update w/ the defect.
    my $failures = 0;
    my $attempts = 0;
    foreach my $hit (@$hits) {
        $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.



( run in 2.047 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )