MT#62339 /api/subscriberregistrations add total_count support

* Panel::Utils::RedisLocationResultSet:
  - add total_count() that returns total count of all entries
    without pagination
  - add total count aggregation during _find(), _row_from_mapkey(),
    _scan() that aggregates total entries that can be shown without
    pagination
* SubscriberRegistrations:
  - return correct total_count of all possible entries that can
    be shown (without pagination)
  - fix a 500 error when 'subscriber_id' contains a non-existing
    subscriber

Change-Id: I914356a809a005c21bb662498ca78dd35affc0ee
(cherry picked from commit d1280015c4)
mr14.1
Kirill Solomko 1 month ago
parent 54928a008f
commit b9e5ce5140

@ -91,10 +91,17 @@ sub GET :Allow {
my $rows = $c->request->params->{rows} // 10;
{
my $items = $self->item_rs($c, undef, {page => $page, rows => $rows});
(my $total_count, $items, my $items_rows) = $self->paginate_order_collection($c, $items);
my ($items_count, $total_count, $items_rows);
my (@embedded, @links);
my $form = $self->get_form($c);
$self->expand_prepare_collection($c);
if ($items) {
($items_count, $items, $items_rows) = $self->paginate_order_collection($c, $items);
$total_count = $items->total_count();
$self->expand_prepare_collection($c);
} else {
$items_rows = [];
$total_count = 0;
}
for my $item (@$items_rows) {
my $halitem = $self->hal_from_item($c, $item, $form);
next unless($halitem);
@ -104,7 +111,7 @@ sub GET :Allow {
href => sprintf('/%s%s', $c->request->path, $item->id),
);
}
$self->expand_collection_fields($c, \@embedded);
$self->expand_collection_fields($c, \@embedded) if $items;
push @links,
Data::HAL::Link->new(
relation => 'curies',

@ -24,7 +24,7 @@ sub _item_rs {
unless (defined $filter) {
$filter = {};
if ($c->req->param('subscriber_id')) {
my $sub = $c->model('DB')->resultset('voip_subscribers')->find($c->req->param('subscriber_id'));
my $sub = $c->model('DB')->resultset('voip_subscribers')->find($c->req->param('subscriber_id')) // return;
my $prov_subscriber = $sub->provisioning_voip_subscriber;
my @usernames = ($prov_subscriber->username);
my $devid_aliases = $prov_subscriber->voip_dbaliases->search(

@ -32,6 +32,12 @@ has _rows => (
default => sub {[]}
);
has _total_count => (
is => 'rw',
isa => sub { die "$_[0] must be int" unless defined $_[0] && is_int($_[0]) },
default => 0,
);
has _query_done => (
is => 'rw',
isa => sub { die "$_[0] must be int" unless defined $_[0] && is_int($_[0]) },
@ -60,6 +66,12 @@ sub count {
return $count;
}
sub total_count {
my $self = shift;
return $self->_total_count;
}
sub first {
my ($self) = @_;
return $self->_rows->[0];
@ -111,14 +123,14 @@ sub find {
if (exists $filter->{reseller_id} && $filter->{reseller_id} != $subscribers_reseller->get_column('reseller_id')) {
return;
}
$self->_total_count(1);
return NGCP::Panel::Utils::RedisLocationResultSource->new(_data => \%entry);
}
sub search {
my ($self, $filter, $opt) = @_;
$filter //= {};
$filter //= $self->_unalias($filter // {});
$filter = $self->_unalias($filter // {});
my $new_rs = $self->meta->clone_object($self);
unless ($new_rs->_query_done) {
if ($filter->{id}) {
@ -195,6 +207,8 @@ sub _rows_from_mapkey {
my $res = $self->_row_from_key($key, $filter);
push @rows, $res if $res;
}
my $total_count = $self->_total_count;
$self->_total_count($total_count + $#rows+1);
return \@rows;
}
@ -323,6 +337,9 @@ sub _scan {
my $keys = shift @{$res};
my $keys_count = $#$keys+1;
my $total_count = $self->_total_count;
$self->_total_count($total_count + $keys_count);
my $offset = $fetched_keys_count - ($page-1)*$rows;
$fetched_keys_count += $keys_count;

Loading…
Cancel
Save