TT#68703 workaround slow contract_cnt queries of profiles

Change-Id: Ieedb868b268890ebf16e6f036ef68601e73f3bcd
changes/38/34238/5
Rene Krenn 6 years ago
parent 2052c466e9
commit 838cd09aca

@ -49,6 +49,7 @@ sub _profile_resultset_admin {
'me.status' => { '!=' => 'terminated' },
},
{ '+select' => [ { '' => \[ NGCP::Panel::Utils::Billing::get_contract_count_stmt() ] , -as => 'contract_cnt' },
{ '' => \[ NGCP::Panel::Utils::Billing::get_contract_exists_stmt() ] , -as => 'contract_exists' },
{ '' => \[ NGCP::Panel::Utils::Billing::get_package_count_stmt() ] , -as => 'package_cnt' }, ],
});
return $rs;
@ -62,6 +63,7 @@ sub _profile_resultset_reseller {
'me.status' => { '!=' => 'terminated' },
},
{ '+select' => [ { '' => \[ NGCP::Panel::Utils::Billing::get_contract_count_stmt() ] , -as => 'contract_cnt' },
{ '' => \[ NGCP::Panel::Utils::Billing::get_contract_exists_stmt() ] , -as => 'contract_exists' },
{ '' => \[ NGCP::Panel::Utils::Billing::get_package_count_stmt() ] , -as => 'package_cnt' }, ],
});
return $rs;
@ -315,7 +317,7 @@ sub terminate :Chained('base') :PathPart('terminate') :Args(0) {
try {
#todo: putting the profile fetch into a transaction wouldn't help since the count columns a prone to phantom reads...
unless($profile->get_column('contract_cnt') == 0) {
if($profile->get_column('contract_exists')) {
die(['Cannnot terminate billing profile that is still used in profile mappings', "showdetails"]);
}
unless($profile->get_column('package_cnt') == 0) {

@ -48,6 +48,7 @@ sub _network_resultset_admin {
'me.status' => { '!=' => 'terminated' },
},
{ '+select' => [ { '' => \[ NGCP::Panel::Utils::BillingNetworks::get_contract_count_stmt() ] , -as => 'contract_cnt' },
{ '' => \[ NGCP::Panel::Utils::Billing::get_contract_exists_stmt() ] , -as => 'contract_exists' },
{ '' => \[ NGCP::Panel::Utils::BillingNetworks::get_package_count_stmt() ] , -as => 'package_cnt' }, ],
});
}
@ -65,6 +66,7 @@ sub _network_resultset_reseller {
'me.status' => { '!=' => 'terminated' },
},
{ '+select' => [ { '' => \[ NGCP::Panel::Utils::BillingNetworks::get_contract_count_stmt() ] , -as => 'contract_cnt' },
{ '' => \[ NGCP::Panel::Utils::Billing::get_contract_exists_stmt() ] , -as => 'contract_exists' },
{ '' => \[ NGCP::Panel::Utils::BillingNetworks::get_package_count_stmt() ] , -as => 'package_cnt' }, ],
});
}
@ -223,7 +225,7 @@ sub terminate :Chained('base') :PathPart('terminate') :Args(0) {
try {
#todo: putting the network fetch into a transaction wouldn't help since the count columns a prone to phantom reads...
unless($network->get_column('contract_cnt') == 0) {
if($network->get_column('contract_exists')) {
die(['Cannnot terminate billing network that is still used in profile mappings', "showdetails"]);
}
unless($network->get_column('package_cnt') == 0) {

@ -71,6 +71,7 @@ sub _item_rs {
my $item_rs = $c->model('DB')->resultset('billing_networks')->search_rs({ 'me.status' => { '!=' => 'terminated' } });
my $search_xtra = {
'+select' => [ { '' => \[ NGCP::Panel::Utils::BillingNetworks::get_contract_count_stmt() ] , -as => 'contract_cnt' },
{ '' => \[ NGCP::Panel::Utils::BillingNetworks::get_contract_exists_stmt() ] , -as => 'contract_exists' },
{ '' => \[ NGCP::Panel::Utils::BillingNetworks::get_package_count_stmt() ] , -as => 'package_cnt' }, ],
};
if($c->user->roles eq "admin") {

@ -21,6 +21,7 @@ sub _item_rs {
my $item_rs = $c->model('DB')->resultset('billing_profiles');
my $search_xtra = {
'+select' => [ { '' => \[ NGCP::Panel::Utils::Billing::get_contract_count_stmt() ] , -as => 'contract_cnt' },
{ '' => \[ NGCP::Panel::Utils::Billing::get_contract_exists_stmt() ] , -as => 'contract_exists' },
{ '' => \[ NGCP::Panel::Utils::Billing::get_package_count_stmt() ] , -as => 'package_cnt' }, ],
};
if($c->user->roles eq "admin") {

@ -31,18 +31,19 @@ sub check_profile_update_item {
# return 0 unless &{$err_code}("The prepaid rating library is mandatory for a prepaid profile.",'prepaid_library');
#}
my $contract_exists = $old_item->get_column('contract_exists');
my $contract_cnt = $old_item->get_column('contract_cnt');
#my $package_cnt = $old_item->get_column('package_cnt');
if (($contract_cnt > 0)
if ($contract_exists
&& defined $new_resource->{interval_charge} && $old_item->interval_charge != $new_resource->{interval_charge}) {
return 0 unless &{$err_code}("Interval charge cannot be changed (profile linked to $contract_cnt contracts).",'interval_charge');
}
if (($contract_cnt > 0)
if ($contract_exists
&& defined $new_resource->{interval_free_time} && $old_item->interval_free_time != $new_resource->{interval_free_time}) {
return 0 unless &{$err_code}("Interval free time cannot be changed (profile linked to $contract_cnt contracts).",'interval_free_time');
}
if (($contract_cnt > 0)
if ($contract_exists
&& defined $new_resource->{interval_free_cash} && $old_item->interval_free_cash != $new_resource->{interval_free_cash}) {
return 0 unless &{$err_code}("Interval free cash cannot be changed (profile linked to $contract_cnt contracts).",'interval_free_cash');
}
@ -447,11 +448,11 @@ sub switch_prepaid {
}
sub get_contract_count_stmt {
sub get_contract_exists_stmt {
return <<EOS;
select
count(distinct c.id)
1
from billing.contracts_billing_profile_network_schedule cbpns
join billing.contracts_billing_profile_network cbpn on cbpns.profile_network_id = cbpn.id
join billing._v_actual_effective_start_time est on est.contract_id = cbpn.contract_id and cbpns.effective_start_time = est.effective_start_time
@ -459,11 +460,36 @@ join billing.contracts as c on est.contract_id = c.id
where
cbpn.billing_profile_id = me.id
and c.status != 'terminated'
limit 1
EOS
}
sub get_contract_count_stmt {
return <<EOS;
select
count(distinct c.id)
from `billing`.`contracts_billing_profile_network` cbpn
join `billing`.`contracts` c on c.id = cbpn.contract_id
where cbpn.`billing_profile_id` = `me`.`id`
and c.status != 'terminated'
and (cbpn.end_date is null or cbpn.end_date >= now())
EOS
}
sub get_package_count_stmt {
return "select count(distinct pp.id) from `billing`.`package_profile_sets` pps join `billing`.`profile_packages` pp on pp.id = pps.package_id where pps.`profile_id` = `me`.`id`"; # and pp.status != 'terminated'";
return <<EOS;
select
count(distinct pp.id)
from `billing`.`package_profile_sets` pps
join `billing`.`profile_packages` pp on pp.id = pps.package_id
where pps.`profile_id` = `me`.`id`
EOS
# and pp.status != 'terminated'";
}
sub get_datatable_cols {

@ -149,11 +149,11 @@ sub _validate_ip {
return (is_ipv4($ip) || is_ipv6($ip));
}
sub get_contract_count_stmt {
sub get_contract_exists_stmt {
return <<EOS;
select
count(distinct c.id)
1
from billing.contracts_billing_profile_network_schedule cbpns
join billing.contracts_billing_profile_network cbpn on cbpns.profile_network_id = cbpn.id
join billing._v_actual_effective_start_time est on est.contract_id = cbpn.contract_id and cbpns.effective_start_time = est.effective_start_time
@ -161,11 +161,37 @@ join billing.contracts as c on est.contract_id = c.id
where
cbpn.billing_network_id = me.id
and c.status != 'terminated'
limit 1
EOS
}
sub get_contract_count_stmt {
return <<EOS;
select
count(distinct c.id)
from `billing`.`contracts_billing_profile_network` cbpn
join `billing`.`contracts` c on c.id = cbpn.contract_id
where
cbpn.`billing_network_id` = `me`.`id`
and c.status != 'terminated'
and (cbpn.end_date is null or cbpn.end_date >= now())
EOS
}
sub get_package_count_stmt {
return "select count(distinct pp.id) from `billing`.`package_profile_sets` pps join `billing`.`profile_packages` pp on pp.id = pps.package_id where pps.`network_id` = `me`.`id`"; # and pp.status != 'terminated'";
return <<EOS;
select
count(distinct pp.id)
from `billing`.`package_profile_sets` pps
join `billing`.`profile_packages` pp on pp.id = pps.package_id
where pps.`network_id` = `me`.`id`
EOS
# and pp.status != 'terminated'";
}
sub get_datatable_cols {

Loading…
Cancel
Save