MT#9329 Use DB to count statistic for calls.

Optimization in case when calls dispersion is low.
changes/94/294/1
Irina Peshinskaya 11 years ago
parent 76129383f5
commit bf9ed81fd2

@ -25,43 +25,43 @@ sub index :Chained('/') :PathPart('calls') :Args(0) {
} }
sub calls_matrix_ajax :Chained('/') :PathPart('calls/ajax') :Args(0) { sub calls_matrix_ajax :Chained('/') :PathPart('calls/ajax') :Args(0) {
my ( $self, $c ) = @_; my ( $self, $c ) = @_;
my $matrix = [];
my $countries = [];
my $from = $c->req->params->{from}; my $matrix = [];
my $to = $c->req->params->{to}; my $countries = [];
my $parse_time = DateTime::Format::Strptime->new(pattern => '%F'); my $from = $c->req->params->{from};
my $to = $c->req->params->{to};
my $parse_time = DateTime::Format::Strptime->new(pattern => '%F');
my $from_epoch; my $from_epoch;
if($from) { if($from) {
$from_epoch = $parse_time->parse_datetime($from)->epoch(); $from_epoch = $parse_time->parse_datetime($from)->epoch();
} else { } else {
$from_epoch = NGCP::Panel::Utils::DateTime::current_local->truncate(to => 'day')->epoch(); $from_epoch = NGCP::Panel::Utils::DateTime::current_local->truncate(to => 'day')->epoch();
} }
my $to_epoch; my $to_epoch;
if($to) { if($to) {
$to_epoch = $parse_time->parse_datetime($to)->add(days => 1)->epoch(); $to_epoch = $parse_time->parse_datetime($to)->add(days => 1)->epoch();
} else { } else {
$to_epoch = NGCP::Panel::Utils::DateTime::current_local->truncate(to => 'day')->add(days => 1)->epoch(); $to_epoch = NGCP::Panel::Utils::DateTime::current_local->truncate(to => 'day')->add(days => 1)->epoch();
} }
my $rs = $c->model('DB')->resultset('cdr')->search({ my $rs = $c->model('DB')->resultset('cdr')->search({
-and => [ -and => [
start_time => { '>=' => $from_epoch }, start_time => { '>=' => $from_epoch },
start_time => { '<=' => $to_epoch }, start_time => { '<=' => $to_epoch },
], ],
}, { }, {
select => [qw/source_cli destination_user_in/], select => [qw/source_cli destination_user_in/,
}); { count => '*', -as => 'cnt' },
],
my $n = $rs->count; group_by => [qw/source_cli destination_user_in/],
});
my $id_counter = 0;
my $id_table = {};
while(my $ref = $rs->next) { my $id_counter = 0;
my $id_table = {};
my $i = 0;
while(my $ref = $rs->next) {
next unless($ref->source_cli && $ref->source_cli =~ /^\d{5,}$/ && next unless($ref->source_cli && $ref->source_cli =~ /^\d{5,}$/ &&
$ref->destination_user_in && $ref->destination_user_in =~ /^\d{5,}$/); $ref->destination_user_in && $ref->destination_user_in =~ /^\d{5,}$/);
my $s = Number::Phone->new($ref->source_cli); my $s = Number::Phone->new($ref->source_cli);
@ -87,22 +87,20 @@ sub calls_matrix_ajax :Chained('/') :PathPart('calls/ajax') :Args(0) {
$matrix->[$sid] = []; $matrix->[$sid] = [];
} }
unless(defined $matrix->[$sid]->[$did]) { unless(defined $matrix->[$sid]->[$did]) {
$matrix->[$sid]->[$did] = 1; $matrix->[$sid]->[$did] = 0 + $ref->get_column('cnt');
} else { } else {
$matrix->[$sid]->[$did]++; $matrix->[$sid]->[$did] += $ref->get_column('cnt');
} }
} }
my $count = @{ $countries };
my $count = @{ $countries }; for(my $i = 0; $i < $count; ++$i) {
for(my $i = 0; $i < $count; ++$i) {
unless(defined $matrix->[$i]) { unless(defined $matrix->[$i]) {
$matrix->[$i] = []; $matrix->[$i] = [];
$matrix->[$i]->[$count-1] = undef; $matrix->[$i]->[$count-1] = undef;
} elsif(@{ $matrix->[$i] } != $count) { } elsif(@{ $matrix->[$i] } != $count) {
$matrix->[$i]->[$count-1] = undef; $matrix->[$i]->[$count-1] = undef;
} }
} }
my $data = { my $data = {
countries => $countries, countries => $countries,
calls => $matrix, calls => $matrix,

Loading…
Cancel
Save