From a6d47b5eb477a51c549ac6cf86a26c3a034fbffc Mon Sep 17 00:00:00 2001 From: Rene Krenn Date: Fri, 13 Dec 2019 10:01:13 +0100 Subject: [PATCH] TT#72650 cdr-generator: fix random subscriber selection if the number of exisiting subscribers (used as caller/callee in created cdrs) is low, there is a higher risk that it can fail to randomize one. Change-Id: Id8f37f259e07f1b50251008a207a3e6122a628ea (cherry picked from commit 71d805440ad533d384aa149cfd8eddb04f79fb3e) --- .../Dao/Trunk/billing/voip_subscribers.pm | 57 ++++++++++--------- .../Migration/UPCAT/settings.debug.cfg | 2 +- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/lib/NGCP/BulkProcessor/Dao/Trunk/billing/voip_subscribers.pm b/lib/NGCP/BulkProcessor/Dao/Trunk/billing/voip_subscribers.pm index 7cd6f68..68419bb 100644 --- a/lib/NGCP/BulkProcessor/Dao/Trunk/billing/voip_subscribers.pm +++ b/lib/NGCP/BulkProcessor/Dao/Trunk/billing/voip_subscribers.pm @@ -224,36 +224,19 @@ sub find_minmaxid { } -sub find_random { - - my ($xa_db,$excluding_id,$states,$reseller_id,$min_id,$max_id,$load_recursive) = @_; +sub _get_find_random_stmt { - if (not defined $min_id or not defined $max_id) { - ($min_id,$max_id) = find_minmaxid($xa_db,$states,$reseller_id); - } - - check_table(); - my $db = &$get_db(); - $xa_db //= $db; + my ($db,$rand,$above,$excluding_id,$states,$reseller_id) = @_; my $table = $db->tableidentifier($tablename); - - die("less than 2 subscriber to randomize") if ($max_id - $min_id) < 2; - my $rand = $min_id + int(rand($max_id - $min_id) + 0.5); - if (defined $excluding_id) { - while ($rand == $excluding_id) { - $rand = $min_id + int(rand($max_id - $min_id) + 0.5); - } - } - my $stmt = 'SELECT r1.* FROM ' . $table . ' AS r1' . - ' JOIN (SELECT ? AS id) AS r2'; + my $stmt = 'SELECT r1.* FROM ' . $table . ' AS r1'; my @params = (); - push(@params,$rand); if ($reseller_id) { $stmt .= ' INNER JOIN ' . $db->tableidentifier(NGCP::BulkProcessor::Dao::Trunk::billing::contracts::gettablename()) . ' AS contract ON r1.contract_id = contract.id' . ' INNER JOIN ' . $db->tableidentifier(NGCP::BulkProcessor::Dao::Trunk::billing::contacts::gettablename()) . ' AS contact ON contract.contact_id = contact.id'; } - $stmt .= ' WHERE r1.id >= r2.id'; # AND r1.id <= ?'; - #push(@params,$max_id); + $stmt .= ' WHERE r1.id >= ?' if $above; + $stmt .= ' WHERE r1.id <= ?' unless $above; + push(@params,$rand); if (defined $states and 'HASH' eq ref $states) { foreach my $in (keys %$states) { @@ -278,12 +261,34 @@ sub find_random { push(@params,$reseller_id); } } - $stmt .= ' ORDER BY r1.id ASC LIMIT 1'; + $stmt .= ' ORDER BY r1.id ASC' if $above; + $stmt .= ' ORDER BY r1.id DESC' unless $above; + $stmt .= ' LIMIT 1'; - my $rows = $xa_db->db_get_all_arrayref($stmt,@params); + return ($stmt,@params); +} + +sub find_random { + + my ($xa_db,$excluding_id,$states,$reseller_id,$min_id,$max_id,$load_recursive) = @_; + + if (not defined $min_id or not defined $max_id) { + ($min_id,$max_id) = find_minmaxid($xa_db,$states,$reseller_id); + } + + check_table(); + my $db = &$get_db(); + $xa_db //= $db; + + die("less than 2 subscriber to randomize") if ($max_id - $min_id) < 1; + my $rand = $min_id + int(rand($max_id - $min_id) + 0.5); + + my $rows; + $rows = $xa_db->db_get_all_arrayref(_get_find_random_stmt($db,$rand,1,$excluding_id,$states,$reseller_id)); + $rows = $xa_db->db_get_all_arrayref(_get_find_random_stmt($db,$rand,0,$excluding_id,$states,$reseller_id)) unless scalar @$rows; my $result = buildrecords_fromrows($rows,$load_recursive)->[0]; - die($stmt,join("-",@params)) unless $result; + #die($stmt,join("-",@params)) unless $result; return $result; diff --git a/lib/NGCP/BulkProcessor/Projects/Migration/UPCAT/settings.debug.cfg b/lib/NGCP/BulkProcessor/Projects/Migration/UPCAT/settings.debug.cfg index 7b953cc..3bd9fb2 100644 --- a/lib/NGCP/BulkProcessor/Projects/Migration/UPCAT/settings.debug.cfg +++ b/lib/NGCP/BulkProcessor/Projects/Migration/UPCAT/settings.debug.cfg @@ -38,7 +38,7 @@ provision_ccs_subscriber_multithreading = 1 provision_ccs_subscriber_numofthreads = 2 ccs_reseller_name = default ccs_billing_profile_name = Default Billing Profile -ccs_domain = test1574213010.example.org +ccs_domain = test1575968867.example.org #10.15.19.193 #d20.upc.at #in-ivr.upc.at