| 1 | 5 | ▶ | use strictures 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | package Mojito::Role::DB::Elasticsearch; | ||||||||||
| 3 | 5 | $Mojito::Role::DB::Elasticsearch::VERSION = '0.25'; | |||||||||
| 4 | 5 | ▶ | use Moo::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 5 | ▶ | use Mojito::Model::Config; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 5 | ▶ | use Search::Elasticsearch; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 5 | ▶ | use Data::Dumper::Concise; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 5 | with('Mojito::Role::DB::OID'); | |||||||||
| 10 | |||||||||||
| 11 | has 'db_name' => ( | ||||||||||
| 12 | is => 'rw', | ||||||||||
| 13 | lazy => 1, | ||||||||||
| 14 | # Set a test DB when RELEASE_TESTING | ||||||||||
| 15 | 0 | ▶ | default => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 16 | $ENV{RELEASE_TESTING} | ||||||||||
| 17 | ? 'mojito_test' | ||||||||||
| 18 | 0 | ▶ | : Mojito::Model::Config->new->config->{es_index}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 19 | }, | ||||||||||
| 20 | 5 | clearer => 'clear_db_name', | |||||||||
| 21 | ); | ||||||||||
| 22 | has 'db' => ( | ||||||||||
| 23 | is => 'lazy', | ||||||||||
| 24 | 0 | ▶ | builder => sub { Search::Elasticsearch->new(nodes => [$_[0]->db_host]) }, | ||||||||
|
Subroutine
_build_db: not called CC=1 SCAR=6.9
| |||||||||||
| 25 | 5 | clearer => 'clear_db', | |||||||||
| 26 | ); | ||||||||||
| 27 | 5 | has 'collection' => ( | |||||||||
| 28 | is => 'ro', | ||||||||||
| 29 | lazy => 1, | ||||||||||
| 30 | builder => '_build_collection', | ||||||||||
| 31 | clearer => 'clear_collection', | ||||||||||
| 32 | ); | ||||||||||
| 33 | has 'collection_name' => ( | ||||||||||
| 34 | is => 'rw', | ||||||||||
| 35 | lazy => 1, | ||||||||||
| 36 | 0 | ▶ | default => sub { 'notes' }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 37 | 5 | clearer => 'clear_collection_name', | |||||||||
| 38 | ); | ||||||||||
| 39 | has 'collection_size' => ( | ||||||||||
| 40 | is => 'lazy', | ||||||||||
| 41 | 0 | ▶ | builder => sub { 100 }, | ||||||||
|
Subroutine
_build_collection_size: not called CC=1 SCAR=6.9
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 42 | 5 | ); | |||||||||
| 43 | has 'db_host' => ( | ||||||||||
| 44 | is => 'lazy', | ||||||||||
| 45 | 0 | ▶ | builder => sub { 'localhost:9200' }, | ||||||||
|
Subroutine
_build_db_host: not called CC=1 SCAR=6.9
| |||||||||||
| 46 | 5 | ); | |||||||||
| 47 | |||||||||||
| 48 | 0 | ▶ | sub _build_collection { | ||||||||
|
Subroutine
_build_collection: not called CC=3 SCAR=24.8
| |||||||||||
| 49 | 0 | my $self = shift; | |||||||||
| 50 | 0 | ▶ | if (not defined $self->db) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 0 | $self->clear_db; | |||||||||
| 52 | } | ||||||||||
| 53 | 0 | my $body = { | |||||||||
| 54 | query => {match_all => {}}, | ||||||||||
| 55 | }; | ||||||||||
| 56 | 0 | my $collection_name =$self->collection_name; | |||||||||
| 57 | # If we're the notes collection then sort by last_modified | ||||||||||
| 58 | 0 | ▶ | if ($collection_name eq 'notes') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 0 | $body->{sort} = [{last_modified => {order => 'desc'}}]; | |||||||||
| 60 | } | ||||||||||
| 61 | 0 | my $results = $self->db->search( | |||||||||
| 62 | index => $self->db_name, | ||||||||||
| 63 | type => $collection_name, | ||||||||||
| 64 | body => $body, | ||||||||||
| 65 | size => $self->collection_size, | ||||||||||
| 66 | ); | ||||||||||
| 67 | 0 | return $results; | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | |||||||||||
| 71 | 5 | 1; | |||||||||