<commit_before>#
# CDR-Stats License
# http://www.cdr-stats.org
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (C) 2011-2013 Star2Billing S.L.
#
# The Initial Developer of the Original Code is
# <NAME> <<EMAIL>>
#

from voip_billing.models import VoIPRetailPlan
from voip_billing.function_def import prefix_list_string
from collections import namedtuple
from cache_utils.decorators import cached



def rate_engine(voipplan_id, dest_number):
    """
    To determine the cost of the voip call and get provider/gateway
    to use to deliver the call.
    """

    if not voipplan_id or not dest_number:
        return []

    dest_prefix = prefix_list_string(str(dest_number))
    if not dest_prefix:
        return []

    rate_tuples = rate_call_prefix(voipplan_id, dest_prefix)

    rates = []
    if rate_tuples:
        Rate = namedtuple('Rate', ['id', 'cpid', 'cr_prefix', 'rt_prefix', 'rrid', 'carrier_rate',\
                    'retail_rate', 'crid', 'provider_id', 'gateway_id', 'sum_metric'])
        rate = Rate(*rate_tuples)
        rates.append(rate)
    return rates


@cached(60)
def rate_call_prefix(voipplan_id, dest_prefix):
    #Build SQL query to rate calls
    sql = 'SELECT rpid as id, cpid, cr_prefix, prefix as rt_prefix, rrid, \
        carrier_rate, retail_rate, crid, provider_id, gateway_id, sum_metric \
        FROM ( \
            SELECT DISTINCT allsellrates.*, \
                voip_carrier_rate.id AS crid, \
                voip_carrier_rate.prefix AS cr_prefix, \
                voip_provider.id AS provider_id, \
                voip_provider.gateway_id AS gateway_id, \
                voip_carrier_rate.carrier_rate,\
                (voip_carrier_plan.metric + \
                 allsellrates.metric + voip_provider.metric)\
                 AS sum_metric, \
                 voip_carrier_plan.id AS cpid \
            FROM ( \
            SELECT DISTINCT voip_retail_plan.id AS rpid, \
                voip_retail_rate.prefix, \
                voip_retail_rate.id AS rrid, \
                voip_retail_rate.retail_rate AS retail_rate, \
                voip_retail_plan.metric AS metric \
                FROM voip_retail_rate, \
                     voip_retail_plan, \
                     voipplan_voipretailplan \
                WHERE voip_retail_rate.prefix IN (%s) \
                AND voip_retail_plan.id = \
                    voip_retail_rate.voip_retail_plan_id \
                AND voipplan_voipretailplan.voipplan_id = %s \
                AND voipplan_voipretailplan.voipretailplan_id \
                    = voip_retail_plan.id \
                ORDER BY prefix DESC, retail_rate ASC\
                )  AS allsellrates, dialcode_prefix,\<commit_msg>improve readability of rating query<commit_after>#
# CDR-Stats License
# http://www.cdr-stats.org
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (C) 2011-2013 Star2Billing S.L.
#
# The Initial Developer of the Original Code is
# <NAME> <<EMAIL>>
#

from voip_billing.models import VoIPRetailPlan
from voip_billing.function_def import prefix_list_string
from collections import namedtuple
from cache_utils.decorators import cached



def rate_engine(voipplan_id, dest_number):
    """
    To determine the cost of the voip call and get provider/gateway
    to use to deliver the call.
    """

    if not voipplan_id or not dest_number:
        return []

    dest_prefix = prefix_list_string(str(dest_number))
    if not dest_prefix:
        return []

    rate_tuples = rate_call_prefix(voipplan_id, dest_prefix)

    rates = []
    if rate_tuples:
        Rate = namedtuple('Rate', ['id', 'cpid', 'cr_prefix', 'rt_prefix', 'rrid', 'carrier_rate',\
                    'retail_rate', 'crid', 'provider_id', 'gateway_id', 'sum_metric'])
        rate = Rate(*rate_tuples)
        rates.append(rate)
    return rates


@cached(60)
def rate_call_prefix(voipplan_id, dest_prefix):
    #Build SQL query to rate calls
    sql = 'SELECT rpid as id, cpid, cr_prefix, prefix as rt_prefix, rrid, \
        carrier_rate, retail_rate, crid, provider_id, gateway_id, sum_metric \
        FROM ( \
            SELECT DISTINCT allsellrates.*, \
                voip_carrier_rate.id AS crid, voip_carrier_rate.prefix AS cr_prefix, \
                voip_provider.id AS provider_id, voip_provider.gateway_id AS gateway_id, \
                voip_carrier_rate.carrier_rate,\
                (voip_carrier_plan.metric + allsellrates.metric + voip_provider.metric) AS sum_metric, \
                voip_carrier_plan.id AS cpid \
            FROM ( \
            SELECT DISTINCT voip_retail_plan.id AS rpid, \
                voip_retail_rate.prefix, voip_retail_rate.id AS rrid, \
                voip_retail_rate.retail_rate AS retail_rate, voip_retail_plan.metric AS metric \
                FROM voip_retail_rate, voip_retail_plan, voipplan_voipretailplan \
                WHERE voip_retail_rate.prefix IN (%s) \
                    AND voip_retail_plan.id = voip_retail_rate.voip_retail_plan_id \
                    AND voipplan_voipretailplan.voipplan_id = %s \
                    AND voipplan_voipretailplan.voipretailplan_id = voip_retail_plan.id \
                ORDER BY prefix DESC, retail_rate ASC\
                )  AS allsellrates, dialcode_prefix,\
{'fraction_non_alphanumeric': 0.08112937812723374, 'fraction_numerical': 0.005360972122944961, 'mean_word_length': 2.3216617210682493, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5920636', 'n_tokens_mistral': 1870, 'n_tokens_neox': 1742, 'n_words': 443}