<commit_before>"""
Allocation Strategy represented as a Purely-Python object
"""
from dateutil.relativedelta import relativedelta

from django.utils import timezone
from allocation.models import \
    AllocationRecharge, IgnoreStatusRule, MultiplySizeCPU,\
    Allocation, TimeUnit
from allocation.models import Instance as AllocInstance


class PythonAllocationStrategy(object):

    """
    PythonAllocationStrategy is powered by CORE: Identity, Allocation
    Start date and End date REQUIRED
    Interval *may* be removed in a future release..

    To be implemented:
    * Refresh Behavior
    * Counting Behavior
    * Rules Behavior
    * ???
    """

    def __init__(self, counting_behavior,
                 recharge_behaviors=[], rule_behaviors=[]):
        self.counting_behavior = counting_behavior
        self.recharge_behaviors = recharge_behaviors
        self.rule_behaviors = rule_behaviors

    def get_instance_list(self, identity):
        from service.monitoring import _core_instances_for
        # Retrieve the core that could have an impact..
        core_instances = _core_instances_for(
            identity,
            self.counting_behavior.start_date)
        # Convert Core Models --> Allocation/core Models
        alloc_instances = [AllocInstance.from_core(
            inst, self.counting_behavior.start_date)
            for inst in core_instances]
        return alloc_instances

    def apply(self, identity, core_allocation):
        instances = self.get_instance_list(identity)

        credits = []
        for behavior in self.recharge_behaviors:
            if core_allocation:
                credits.extend(
                    behavior.get_allocation_credits(
                        unit=TimeUnit.minute,
                        amount=core_allocation.threshold))
            else:
                # Unlimited for 'no allocation'
                credits.extend(<commit_msg>Avoid 500-crash when a core instance is missing an instance source<commit_after>"""
Allocation Strategy represented as a Purely-Python object
"""
from dateutil.relativedelta import relativedelta

from django.utils import timezone
from allocation.models import \
    AllocationRecharge, IgnoreStatusRule, MultiplySizeCPU,\
    Allocation, TimeUnit
from allocation.models import Instance as AllocInstance

from threepio import logger

class PythonAllocationStrategy(object):

    """
    PythonAllocationStrategy is powered by CORE: Identity, Allocation
    Start date and End date REQUIRED
    Interval *may* be removed in a future release..

    To be implemented:
    * Refresh Behavior
    * Counting Behavior
    * Rules Behavior
    * ???
    """

    def __init__(self, counting_behavior,
                 recharge_behaviors=[], rule_behaviors=[]):
        self.counting_behavior = counting_behavior
        self.recharge_behaviors = recharge_behaviors
        self.rule_behaviors = rule_behaviors

    def get_instance_list(self, identity):
        from service.monitoring import _core_instances_for
        # Retrieve the core that could have an impact..
        core_instances = _core_instances_for(
            identity,
            self.counting_behavior.start_date)
        # Convert Core Models --> Allocation/core Models
        alloc_instances = []
        for inst in core_instances:
            try:
                alloc_instances.append(
                    AllocInstance.from_core(inst, self.counting_behavior.start_date)
                )
            except Exception as exc:
                logger.exception(exc)
        return alloc_instances

    def apply(self, identity, core_allocation):
        instances = self.get_instance_list(identity)

        credits = []
        for behavior in self.recharge_behaviors:
            if core_allocation:
                credits.extend(
                    behavior.get_allocation_credits(
                        unit=TimeUnit.minute,
                        amount=core_allocation.threshold))
            else:
                # Unlimited for 'no allocation'
                credits.extend(
{'fraction_non_alphanumeric': 0.06179499754781756, 'fraction_numerical': 0.0007356547327121138, 'mean_word_length': 2.3935108153078204, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2986547', 'n_tokens_mistral': 1100, 'n_tokens_neox': 1041, 'n_words': 279}