Skip to content

Commit aadfdc6

Browse files
committed
add hit_total function to cope with ES6 total values
Elasticsearch 6 return hits.total as an object. Add a function that can cope with both forms, and use it in all of the places we retrieve the total.
1 parent 9fe8771 commit aadfdc6

File tree

17 files changed

+67
-41
lines changed

17 files changed

+67
-41
lines changed

lib/MetaCPAN/API/Model/Cover.pm

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package MetaCPAN::API::Model::Cover;
22

33
use MetaCPAN::Moose;
44

5+
use MetaCPAN::Util qw(hit_total);
6+
57
with 'MetaCPAN::API::Model::Role::ES';
68

79
sub find_release_coverage {
@@ -17,7 +19,7 @@ sub find_release_coverage {
1719
size => 999,
1820
}
1921
);
20-
$res->{hits}{total} or return {};
22+
hit_total($res) or return {};
2123

2224
return +{
2325
%{ $res->{hits}{hits}[0]{_source} },

lib/MetaCPAN/Query/Author.pm

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ package MetaCPAN::Query::Author;
22

33
use MetaCPAN::Moose;
44

5-
use Ref::Util qw( is_arrayref );
5+
use MetaCPAN::Util qw(hit_total);
6+
use Ref::Util qw( is_arrayref );
67

78
with 'MetaCPAN::Query::Role::Common';
89

@@ -27,7 +28,7 @@ sub by_ids {
2728
return {
2829
authors => \@authors,
2930
took => $authors->{took},
30-
total => $authors->{hits}{total},
31+
total => hit_total($authors),
3132
};
3233
}
3334

@@ -49,7 +50,7 @@ sub by_user {
4950
return {
5051
authors => \@authors,
5152
took => $authors->{took},
52-
total => $authors->{hits}{total},
53+
total => hit_total($authors),
5354
};
5455
}
5556

@@ -93,7 +94,7 @@ sub search {
9394
return +{
9495
authors => \@authors,
9596
took => $ret->{took},
96-
total => $ret->{hits}{total},
97+
total => hit_total($ret),
9798
};
9899
}
99100

@@ -124,7 +125,7 @@ sub prefix_search {
124125
return +{
125126
authors => \@authors,
126127
took => $ret->{took},
127-
total => $ret->{hits}{total},
128+
total => hit_total($ret),
128129
};
129130
}
130131

lib/MetaCPAN/Query/Contributor.pm

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package MetaCPAN::Query::Contributor;
22

33
use MetaCPAN::Moose;
44

5+
use MetaCPAN::Util qw(hit_total);
6+
57
with 'MetaCPAN::Query::Role::Common';
68

79
sub find_release_contributors {
@@ -24,7 +26,7 @@ sub find_release_contributors {
2426
size => 999,
2527
}
2628
);
27-
$res->{hits}{total} or return {};
29+
hit_total($res) or return {};
2830

2931
return +{
3032
contributors => [ map { $_->{_source} } @{ $res->{hits}{hits} } ] };

lib/MetaCPAN/Query/Cover.pm

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package MetaCPAN::Query::Cover;
22

33
use MetaCPAN::Moose;
44

5+
use MetaCPAN::Util qw(hit_total);
6+
57
with 'MetaCPAN::Query::Role::Common';
68

79
sub find_release_coverage {
@@ -17,7 +19,7 @@ sub find_release_coverage {
1719
size => 999,
1820
}
1921
);
20-
$res->{hits}{total} or return {};
22+
hit_total($res) or return {};
2123

2224
return +{
2325
%{ $res->{hits}{hits}[0]{_source} },

lib/MetaCPAN/Query/Distribution.pm

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package MetaCPAN::Query::Distribution;
22

33
use MetaCPAN::Moose;
44

5+
use MetaCPAN::Util qw(hit_total);
6+
57
with 'MetaCPAN::Query::Role::Common';
68

79
sub get_river_data_by_dist {
@@ -21,7 +23,7 @@ sub get_river_data_by_dist {
2123
size => 999,
2224
}
2325
);
24-
$res->{hits}{total} or return {};
26+
hit_total($res) or return {};
2527

2628
return +{ river => +{ $dist => $res->{hits}{hits}[0]{_source}{river} } };
2729
}
@@ -43,7 +45,7 @@ sub get_river_data_by_dists {
4345
size => 999,
4446
}
4547
);
46-
$res->{hits}{total} or return {};
48+
hit_total($res) or return {};
4749

4850
return +{
4951
river => +{

lib/MetaCPAN/Query/Favorite.pm

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package MetaCPAN::Query::Favorite;
22

33
use MetaCPAN::Moose;
44

5+
use MetaCPAN::Util qw(hit_total);
6+
57
with 'MetaCPAN::Query::Role::Common';
68

79
sub agg_by_distributions {
@@ -76,7 +78,7 @@ sub by_user {
7678
size => $size,
7779
}
7880
);
79-
return {} unless $favs->{hits}{total};
81+
return {} unless hit_total($favs);
8082
my $took = $favs->{took};
8183

8284
my @favs = map { $_->{_source} } @{ $favs->{hits}{hits} };
@@ -106,7 +108,7 @@ sub by_user {
106108
);
107109
$took += $no_backpan->{took};
108110

109-
if ( $no_backpan->{hits}{total} ) {
111+
if ( hit_total($no_backpan) ) {
110112
my %has_no_backpan = map { $_->{_source}{distribution} => 1 }
111113
@{ $no_backpan->{hits}{hits} };
112114

@@ -171,7 +173,7 @@ sub recent {
171173
return +{
172174
favorites => \@favs,
173175
took => $favs->{took},
174-
total => $favs->{hits}{total}
176+
total => hit_total($favs),
175177
};
176178
}
177179

@@ -187,7 +189,7 @@ sub users_by_distribution {
187189
size => 1000,
188190
}
189191
);
190-
return {} unless $favs->{hits}{total};
192+
return {} unless hit_total($favs);
191193

192194
my @plusser_users = map { $_->{_source}{user} } @{ $favs->{hits}{hits} };
193195

lib/MetaCPAN/Query/File.pm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package MetaCPAN::Query::File;
22

33
use MetaCPAN::Moose;
44

5-
use MetaCPAN::Util qw( true false );
5+
use MetaCPAN::Util qw( hit_total true false );
66

77
with 'MetaCPAN::Query::Role::Common';
88

@@ -301,7 +301,7 @@ sub interesting_files {
301301
} );
302302

303303
$return->{took} = $data->{took};
304-
$return->{total} = $data->{hits}{total};
304+
$return->{total} = hit_total($data);
305305

306306
return $return
307307
unless $return->{total};

lib/MetaCPAN/Query/Mirror.pm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package MetaCPAN::Query::Mirror;
22

33
use MetaCPAN::Moose;
4+
use MetaCPAN::Util qw( hit_total );
45

56
with 'MetaCPAN::Query::Role::Common';
67

@@ -57,7 +58,7 @@ sub search {
5758

5859
return {
5960
mirrors => $data,
60-
total => $ret->{hits}{total},
61+
total => hit_total($ret),
6162
took => $ret->{took}
6263
};
6364
}

lib/MetaCPAN/Query/Release.pm

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ package MetaCPAN::Query::Release;
22

33
use MetaCPAN::Moose;
44

5-
use MetaCPAN::Util qw( single_valued_arrayref_to_scalar true false );
5+
use MetaCPAN::Util
6+
qw( hit_total single_valued_arrayref_to_scalar true false );
67

78
with 'MetaCPAN::Query::Role::Common';
89

@@ -184,7 +185,7 @@ sub get_contributors {
184185
}
185186
);
186187

187-
if ( $check_author->{hits}{total} ) {
188+
if ( hit_total($check_author) ) {
188189
$contrib->{pauseid}
189190
= uc $check_author->{hits}{hits}[0]{_source}{pauseid};
190191
}
@@ -365,7 +366,7 @@ sub by_author_and_name {
365366
return {
366367
took => $ret->{took},
367368
release => $data,
368-
total => $ret->{hits}{total}
369+
total => hit_total($ret),
369370
};
370371
}
371372

@@ -412,7 +413,7 @@ sub by_author_and_names {
412413

413414
return {
414415
took => $ret->{took},
415-
total => $ret->{hits}{total},
416+
total => hit_total($ret),
416417
releases => \@releases,
417418
};
418419
}
@@ -450,7 +451,7 @@ sub by_author {
450451

451452
return {
452453
releases => $data,
453-
total => $ret->{hits}{total},
454+
total => hit_total($ret),
454455
took => $ret->{took}
455456
};
456457
}
@@ -486,7 +487,7 @@ sub latest_by_distribution {
486487
return {
487488
release => $data,
488489
took => $ret->{took},
489-
total => $ret->{hits}{total}
490+
total => hit_total($ret),
490491
};
491492
}
492493

@@ -546,7 +547,7 @@ sub all_by_author {
546547
return {
547548
took => $ret->{took},
548549
releases => $data,
549-
total => $ret->{hits}{total}
550+
total => hit_total($ret),
550551
};
551552
}
552553

@@ -617,7 +618,7 @@ sub versions {
617618

618619
return {
619620
releases => $data,
620-
total => $ret->{hits}{total},
621+
total => hit_total($ret),
621622
took => $ret->{took}
622623
};
623624
}
@@ -812,7 +813,7 @@ sub _get_depended_releases {
812813

813814
return +{
814815
data => [ map { $_->{_source} } @{ $depended->{hits}{hits} } ],
815-
total => $depended->{hits}{total},
816+
total => hit_total($depended),
816817
took => $depended->{took},
817818
};
818819
}
@@ -880,7 +881,7 @@ sub recent {
880881

881882
return {
882883
releases => $data,
883-
total => $ret->{hits}{total},
884+
total => hit_total($ret),
884885
took => $ret->{took}
885886
};
886887
}
@@ -969,7 +970,7 @@ sub modules {
969970

970971
return {
971972
files => \@files,
972-
total => $ret->{hits}{total},
973+
total => hit_total($ret),
973974
took => $ret->{took}
974975
};
975976
}
@@ -1153,7 +1154,7 @@ sub find_download_url {
11531154
search_type => 'dfs_query_then_fetch',
11541155
);
11551156

1156-
return unless $res->{hits}{total};
1157+
return unless hit_total($res);
11571158

11581159
my @checksums;
11591160

lib/MetaCPAN/Query/Search.pm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use Hash::Merge qw( merge );
77
use List::Util qw( min uniq );
88
use Log::Contextual qw( :log :dlog );
99
use MetaCPAN::Types::TypeTiny qw( Object Str );
10-
use MetaCPAN::Util qw( true false );
10+
use MetaCPAN::Util qw( hit_total true false );
1111
use MooseX::StrictConstructor;
1212

1313
with 'MetaCPAN::Query::Role::Common';
@@ -113,7 +113,7 @@ sub _search_expanded {
113113

114114
my $return = {
115115
results => $results,
116-
total => $es_results->{hits}->{total},
116+
total => hit_total($es_results),
117117
took => $es_results->{took},
118118
collapsed => false,
119119
};

lib/MetaCPAN/Script/CVE.pm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use namespace::autoclean;
66
use Cpanel::JSON::XS qw( decode_json );
77
use Log::Contextual qw( :log :dlog );
88
use MetaCPAN::Types::TypeTiny qw( Bool Str Uri );
9-
use MetaCPAN::Util qw( numify_version true false );
9+
use MetaCPAN::Util qw( hit_total numify_version true false );
1010
use Path::Tiny qw( path );
1111
use Ref::Util qw( is_arrayref );
1212

@@ -177,7 +177,7 @@ sub index_cve_data {
177177
},
178178
);
179179

180-
if ( $releases->{hits}{total} ) {
180+
if ( hit_total($releases) ) {
181181
## no critic (ControlStructures::ProhibitMutatingListFunctions)
182182
@matches = map { $_->[0] }
183183
sort { $a->[1] <=> $b->[1] }

lib/MetaCPAN/Script/Cover.pm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use Cpanel::JSON::XS qw( decode_json );
77
use Log::Contextual qw( :log :dlog );
88
use MetaCPAN::Types::TypeTiny qw( Bool Str Uri );
99
use Path::Tiny qw( path );
10-
use MetaCPAN::Util qw( true false );
10+
use MetaCPAN::Util qw( hit_total true false );
1111

1212
with 'MetaCPAN::Role::Script', 'MooseX::Getopt';
1313

@@ -71,7 +71,7 @@ sub index_cover_data {
7171
query => { term => { name => $release } },
7272
},
7373
);
74-
if ( $rel_check->{hits}{total} ) {
74+
if ( hit_total($rel_check) ) {
7575
log_info { "Adding release info for '" . $release . "'" };
7676
}
7777
else {

lib/MetaCPAN/Util.pm

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use Sub::Exporter -setup => {
2929
fix_pod
3030
fix_version
3131
generate_sid
32+
hit_total
3233
numify_version
3334
pod_lines
3435
strip_pod
@@ -97,6 +98,15 @@ sub author_dir {
9798
return $dir;
9899
}
99100

101+
sub hit_total {
102+
my $res = shift;
103+
my $total = $res && $res->{hits} && $res->{hits}{total};
104+
if ( ref $total ) {
105+
return $total->{value};
106+
}
107+
return $total;
108+
}
109+
100110
# TODO: E<escape>
101111
sub strip_pod {
102112
my $pod = shift;

0 commit comments

Comments
 (0)