Пример #1
0
def instance_allocation_source_changed_event_is_fired_before_statushistory_is_created(
        context):
    for row in context.table:
        payload = {}
        payload['allocation_source_name'] = context.allocation_sources_name[
            row["allocation_source_id"]]
        payload['instance_id'] = str(context.instance[row['instance_id']])

        name = 'instance_allocation_source_changed'
        ts = context.current_time
        entity_id = str(row["username"])

        event = EventTable(name=name,
                           payload=payload,
                           timestamp=ts,
                           entity_id=entity_id)
        event.save()

        # test the obj was created
        obj = InstanceAllocationSourceSnapshot.objects.filter(
            allocation_source__name=payload['allocation_source_name'],
            instance__provider_alias=payload['instance_id'])

        assert (len(obj) == 1)

        # create status history
        args = context.instance_history_args[row['instance_id']]
        time_created = ts + timedelta(seconds=3)
        launch_instance_history(args['instance'], args['cpu'],
                                args['provider'], args['status'], time_created)
def instance_allocation_source_changed_event_is_fired_before_statushistory_is_created(
    context
):
    for row in context.table:
        payload = {}
        payload['allocation_source_name'] = context.allocation_sources_name[
            row["allocation_source_id"]]
        payload['instance_id'] = str(context.instance[row['instance_id']])

        name = 'instance_allocation_source_changed'
        ts = context.current_time
        entity_id = str(row["username"])

        event = EventTable(
            name=name, payload=payload, timestamp=ts, entity_id=entity_id
        )
        event.save()

        # test the obj was created
        obj = InstanceAllocationSourceSnapshot.objects.filter(
            allocation_source__name=payload['allocation_source_name'],
            instance__provider_alias=payload['instance_id']
        )

        assert (len(obj) == 1)

        # create status history
        args = context.instance_history_args[row['instance_id']]
        time_created = ts + timedelta(seconds=3)
        launch_instance_history(
            args['instance'], args['cpu'], args['provider'], args['status'],
            time_created
        )
Пример #3
0
def _assign_user_allocation_source(source_to_assign, user_to_assign):
    assert isinstance(source_to_assign, AllocationSource)
    assert isinstance(user_to_assign, AtmosphereUser)
    username_to_assign = user_to_assign.username
    payload = {'allocation_source_name': source_to_assign.name}
    event = EventTable(name='user_allocation_source_created',
                       entity_id=username_to_assign,
                       payload=payload)
    event.save()
Пример #4
0
    def test_user_allocation_source_assigned(self):
        new_allocation_source = {
            'source_id': str(uuid.uuid4()),
            'name': 'TestAllocationSourceAssociateScenario',
            'compute_allowed': 50000
        }
        new_event = EventTable.create_event(name='allocation_source_created',
                                            payload=new_allocation_source,
                                            entity_id=new_allocation_source['source_id'])
        user = UserFactory.create()
        new_user_allocation_source = {
            'source_id': new_allocation_source['source_id'],
            'username': user.username
        }

        # Make sure no allocation_source_assigned event for this user and source exists
        event_count_before = EventTable.objects.filter(
            name='user_allocation_source_assigned',
            payload__username=user.username,
            payload__source_id=new_user_allocation_source['source_id']
        ).count()
        self.assertEqual(event_count_before, 0)

        # Make sure that no Allocation Source and User combination exists
        user_allocation_source_count_before = UserAllocationSource.objects.filter(
            allocation_source__source_id=new_user_allocation_source['source_id'],
            user=user).count()
        self.assertEqual(user_allocation_source_count_before, 0)

        # Add an event 'allocation_source_created' with our test source name
        new_event = EventTable.create_event(name='user_allocation_source_assigned',
                                            payload=new_user_allocation_source,
                                            entity_id=new_user_allocation_source['username'])

        # Make sure we added the event successfully
        event_count_after = EventTable.objects.filter(
            name='user_allocation_source_assigned',
            payload__username=user.username,
            payload__source_id=new_user_allocation_source['source_id']
        ).count()
        self.assertEqual(event_count_after, 1)

        # Make sure that there is now an Allocation Source with the test name
        user_allocation_source_count_after = UserAllocationSource.objects.filter(
            allocation_source__source_id=new_user_allocation_source['source_id'],
            user=user).count()
        self.assertEqual(user_allocation_source_count_after, 1)

        user_allocation_source = UserAllocationSource.objects.filter(
            allocation_source__source_id=new_user_allocation_source['source_id'],
            user=user).first()
        self.assertEqual(user_allocation_source.allocation_source.compute_allowed,
                         new_allocation_source['compute_allowed'])
        self.assertEqual(user_allocation_source.allocation_source.source_id, new_allocation_source['source_id'])
        self.assertEqual(user_allocation_source.allocation_source.name, new_allocation_source['name'])
def _assign_user_allocation_source(source_to_assign, user_to_assign):
    assert isinstance(source_to_assign, AllocationSource)
    assert isinstance(user_to_assign, AtmosphereUser)
    username_to_assign = user_to_assign.username
    payload = {'allocation_source_name': source_to_assign.name}
    event = EventTable(
        name='user_allocation_source_created',
        entity_id=username_to_assign,
        payload=payload
    )
    event.save()
Пример #6
0
    def _delete_user_allocation_source(self, request_data):

        username = request_data.get('username')
        payload = {}
        payload['allocation_source_name'] = request_data.get('allocation_source_name')

        delete_event = EventTable(
            name='user_allocation_source_deleted',
            entity_id=username,
            payload=payload)

        delete_event.save()
    def _delete_user_allocation_source(self, request_data):

        username = request_data.get('username')
        payload = {}
        payload['allocation_source_name'] = request_data.get(
            'allocation_source_name')

        delete_event = EventTable(name='user_allocation_source_deleted',
                                  entity_id=username,
                                  payload=payload)

        delete_event.save()
Пример #8
0
def _create_allocation_source(name):
    payload = {
        'uuid': str(uuid.uuid4()),
        'allocation_source_name': name,
        'compute_allowed': 168,  # TODO: Make this configurable
        'renewal_strategy': 'default'
    }
    event = EventTable(
        name='allocation_source_created_or_renewed',
        entity_id=name,
        payload=payload
    )
    event.save()
Пример #9
0
def _create_allocation_source(name):
    default_compute_allowed = getattr(settings,
                                      'ALLOCATION_SOURCE_COMPUTE_ALLOWED', 336)
    payload = {
        'uuid': str(uuid.uuid4()),
        'allocation_source_name': name,
        'compute_allowed':
        default_compute_allowed,  # TODO: Make this a plugin configurable
        'renewal_strategy': 'default'
    }
    event = EventTable(name='allocation_source_created_or_renewed',
                       entity_id=name,
                       payload=payload)
    event.save()
Пример #10
0
def _create_allocation_source(name):
    default_compute_allowed = getattr(settings, 'ALLOCATION_SOURCE_COMPUTE_ALLOWED', 168)
    payload = {
        'uuid': str(uuid.uuid4()),
        'allocation_source_name': name,
        'compute_allowed': default_compute_allowed,  # TODO: Make this a plugin configurable
        'renewal_strategy': 'default'
    }
    event = EventTable(
        name='allocation_source_created_or_renewed',
        entity_id=name,
        payload=payload
    )
    event.save()
Пример #11
0
def step_impl(context):
    for row in context.table:
        allocation_source_name = context.allocation_sources_name[row['allocation_source_id']]
        new_compute_allowed = int(row['new_compute_allowed'])

        payload = {}
        payload['allocation_source_name'] = allocation_source_name
        payload['compute_allowed'] = new_compute_allowed
        name = 'allocation_source_compute_allowed_changed'
        ts = context.time_at_the_end_of_calculation_check

        event = EventTable(name=name, entity_id=allocation_source_name, payload=payload, timestamp=ts)
        event.save()

        assert AllocationSource.objects.get(name=allocation_source_name).compute_allowed == new_compute_allowed
Пример #12
0
def compute_allowed_is_increased_for_allocation_source(context):
    for row in context.table:
        allocation_source_name = context.allocation_sources_name[row['allocation_source_id']]
        new_compute_allowed = int(row['new_compute_allowed'])

        payload = {}
        payload['allocation_source_name'] = allocation_source_name
        payload['compute_allowed'] = new_compute_allowed
        name = 'allocation_source_compute_allowed_changed'
        ts = context.time_at_the_end_of_calculation_check

        event = EventTable(name=name, entity_id=allocation_source_name, payload=payload, timestamp=ts)
        event.save()

        assert AllocationSource.objects.get(name=allocation_source_name).compute_allowed == new_compute_allowed
    def _create_instance_allocation_source(self, request_data, request_user):

        payload = {}
        payload['instance_id'] = request_data.get('instance_id')
        payload['allocation_source_name'] = request_data.get(
            'allocation_source_name')
        username = request_user.username

        creation_event = EventTable(name='instance_allocation_source_changed',
                                    entity_id=username,
                                    payload=payload)

        creation_event.save()
        return InstanceAllocationSourceSnapshot.objects.filter(
            allocation_source__name=payload['allocation_source_name'],
            instance__provider_alias=payload['instance_id']).last()
 def test_create_event(self):
     event_count = EventTable.objects.count()
     self.assertEqual(event_count, 0)
     user = UserFactory.create()
     alloc_src = AllocationSource.objects.create(
         name='DefaultAllocation', compute_allowed=1000
     )    # UUID assigned by default.
     UserAllocationSource.objects.create(
         user=user, allocation_source=alloc_src
     )
     event_payload = {
         'allocation_source_id': str(alloc_src.uuid),
         'compute_used':
             100.00,    # 100 hours used ( a number, not a string!)
         'global_burn_rate': 2.00,    # 2 hours used each hour
     }
     new_event = EventTable.create_event(
         name='allocation_source_snapshot',
         payload=event_payload,
         entity_id=str(alloc_src.uuid)
     )
     event_count = EventTable.objects.count()
     self.assertEqual(event_count, 2)
     events = EventTable.objects.all()
     self.assertEqual(new_event, events[1])
     self.assertEqual(events[0].name, 'allocation_source_threshold_met')
     self.assertEqual(events[0].entity_id, str(alloc_src.uuid))
     self.assertEqual(
         events[0].payload, {
             'actual_value': 10,
             'allocation_source_id': str(alloc_src.uuid),
             'threshold': 10
         }
     )
    def _create_instance_allocation_source(self, request_data, request_user):

        payload = {}
        payload['instance_id'] = request_data.get('instance_id')
        payload['allocation_source_name'] = request_data.get('allocation_source_name')
        username = request_user.username

        creation_event = EventTable(
            name='instance_allocation_source_changed',
            entity_id=username,
            payload=payload)

        creation_event.save()
        return InstanceAllocationSourceSnapshot.objects.filter(
            allocation_source__name=payload['allocation_source_name'],
            instance__provider_alias=payload['instance_id']).last()
Пример #16
0
 def test_create_event(self):
     event_count = EventTable.objects.count()
     self.assertEqual(event_count, 0)
     user = UserFactory.create()
     alloc_src = AllocationSource.objects.create(
         name='DefaultAllocation',
         compute_allowed=1000)  # UUID assigned by default.
     UserAllocationSource.objects.create(user=user,
                                         allocation_source=alloc_src)
     event_payload = {
         'allocation_source_id': str(alloc_src.uuid),
         'compute_used':
         100.00,  # 100 hours used ( a number, not a string!)
         'global_burn_rate': 2.00,  # 2 hours used each hour
     }
     new_event = EventTable.create_event(name='allocation_source_snapshot',
                                         payload=event_payload,
                                         entity_id=str(alloc_src.uuid))
     event_count = EventTable.objects.count()
     self.assertEqual(event_count, 2)
     events = EventTable.objects.all()
     self.assertEqual(new_event, events[1])
     self.assertEqual(events[0].name, 'allocation_source_threshold_met')
     self.assertEqual(events[0].entity_id, str(alloc_src.uuid))
     self.assertEqual(
         events[0].payload, {
             'actual_value': 10,
             'allocation_source_id': str(alloc_src.uuid),
             'threshold': 10
         })
    def _create_user_allocation_source(self, request_data):

        payload = {}
        username = request_data.get('username')
        allocation_source_name = request_data.get('allocation_source_name')
        payload['allocation_source_name'] = allocation_source_name

        creation_event = EventTable(name='user_allocation_source_created',
                                    entity_id=username,
                                    payload=payload)

        creation_event.save()

        # allocation_source = get_allocation_source_object(request_data['source_id'])

        return UserAllocationSource.objects.filter(
            allocation_source__name=allocation_source_name,
            user__username=username).last()
Пример #18
0
    def _create_user_allocation_source(self, request_data):

        payload = {}
        username = request_data.get('username')
        allocation_source_name = request_data.get('allocation_source_name')
        payload['allocation_source_name'] = allocation_source_name

        creation_event = EventTable(
            name='user_allocation_source_created',
            entity_id=username,
            payload=payload)

        creation_event.save()

        # allocation_source = get_allocation_source_object(request_data['source_id'])

        return UserAllocationSource.objects.filter(
            allocation_source__name=allocation_source_name,
            user__username=username).last()
Пример #19
0
    def test_can_create_infinite_allocation_source(self):
        """Can I create an allocation source with infinite compute_allowed"""
        client = APIClient()
        client.force_authenticate(user=self.user_without_sources)
        payload = {
            'uuid': str(uuid.uuid4()),
            'allocation_source_name': 'TG-INF990002',
            'compute_allowed': -1,
            'renewal_strategy': 'default'
        }

        from core.models import EventTable, AllocationSource, AllocationSourceSnapshot
        creation_event = EventTable(
            name='allocation_source_created_or_renewed',
            entity_id=payload['allocation_source_name'],
            payload=payload)

        creation_event.save()

        allocation_source = AllocationSource.objects.get(name='TG-INF990002')
        expected_values = {'name': 'TG-INF990002', 'compute_allowed': -1}
        self.assertDictContainsSubset(expected_values,
                                      allocation_source.__dict__)
        assert isinstance(allocation_source, AllocationSource)
        self.assertEqual(allocation_source.compute_allowed, -1)
        update_snapshot_cyverse()
        self.assertEqual(allocation_source.time_remaining(),
                         decimal.Decimal('Infinity'))
        self.assertEqual(allocation_source.is_over_allocation(), False)
        allocation_snapshot = allocation_source.snapshot
        assert isinstance(allocation_snapshot, AllocationSourceSnapshot)
        self.assertEqual(allocation_snapshot.compute_allowed, -1)
        self.assertEqual(allocation_snapshot.compute_used, 0)
        allocation_snapshot.compute_used = 9999999
        allocation_snapshot.save()
        self.assertEqual(allocation_source.time_remaining(),
                         decimal.Decimal('Infinity'))
        self.assertEqual(allocation_source.is_over_allocation(), False)
Пример #20
0
    def test_can_create_infinite_allocation_source(self):
        """Can I create an allocation source with infinite compute_allowed"""
        client = APIClient()
        client.force_authenticate(user=self.user_without_sources)
        payload = {
            'uuid': str(uuid.uuid4()),
            'allocation_source_name': 'TG-INF990002',
            'compute_allowed': -1,
            'renewal_strategy': 'default'}

        from core.models import EventTable, AllocationSource, AllocationSourceSnapshot
        creation_event = EventTable(
            name='allocation_source_created_or_renewed',
            entity_id=payload['allocation_source_name'],
            payload=payload)

        creation_event.save()

        allocation_source = AllocationSource.objects.get(name='TG-INF990002')
        expected_values = {
            'name': 'TG-INF990002',
            'compute_allowed': -1
        }
        self.assertDictContainsSubset(expected_values, allocation_source.__dict__)
        assert isinstance(allocation_source, AllocationSource)
        self.assertEqual(allocation_source.compute_allowed, -1)
        update_snapshot_cyverse()
        self.assertEqual(allocation_source.time_remaining(), decimal.Decimal('Infinity'))
        self.assertEqual(allocation_source.is_over_allocation(), False)
        allocation_snapshot = allocation_source.snapshot
        assert isinstance(allocation_snapshot, AllocationSourceSnapshot)
        self.assertEqual(allocation_snapshot.compute_allowed, -1)
        self.assertEqual(allocation_snapshot.compute_used, 0)
        allocation_snapshot.compute_used = 9999999
        allocation_snapshot.save()
        self.assertEqual(allocation_source.time_remaining(), decimal.Decimal('Infinity'))
        self.assertEqual(allocation_source.is_over_allocation(), False)
 def save(self):
     # Properly structure the event data as a payload
     serialized_data = self.validated_data
     return_data = self.data
     entity_id = serialized_data['identity'].created_by.username
     event_payload = {
         'update_method': return_data['update_method'],
         'quota': return_data['quota'],
         'identity': return_data['identity']['uuid'],
         'timestamp': return_data['timestamp']
     }
     # Create the event in EventTable
     event = EventTable.create_event(
         name="quota_assigned", entity_id=entity_id, payload=event_payload
     )
     return event
Пример #22
0
 def save(self):
     # Properly structure the event data as a payload
     serialized_data = self.validated_data
     return_data = self.data
     entity_id = serialized_data['identity'].created_by.username
     event_payload = {
         'update_method': return_data['update_method'],
         'quota': return_data['quota'],
         'identity': return_data['identity']['uuid'],
         'timestamp': return_data['timestamp']
     }
     # Create the event in EventTable
     event = EventTable.create_event(name="quota_assigned",
                                     entity_id=entity_id,
                                     payload=event_payload)
     return event
    def save(self):
        serialized_data = self.validated_data
        return_data = self.data
        entity_id = serialized_data['identity'].created_by.username
        event_payload = {
            'update_method': 'resource_request',
            'resource_request': return_data['resource_request'],
            'approved_by': serialized_data['approved_by'].username,
            'quota': return_data['quota'],
            'identity': return_data['identity']['uuid'],
            'timestamp': return_data['timestamp']
        }

        event = EventTable.create_event(
            name="quota_assigned", entity_id=entity_id, payload=event_payload
        )
        return event
Пример #24
0
    def save(self):
        serialized_data = self.validated_data
        return_data = self.data
        entity_id = serialized_data['identity'].created_by.username
        event_payload = {
            'update_method': 'resource_request',
            'resource_request': return_data['resource_request'],
            'approved_by': serialized_data['approved_by'].username,
            'quota': return_data['quota'],
            'identity': return_data['identity']['uuid'],
            'timestamp': return_data['timestamp']
        }

        event = EventTable.create_event(name="quota_assigned",
                                        entity_id=entity_id,
                                        payload=event_payload)
        return event
Пример #25
0
    def test_allocation_source_created(self):
        new_allocation_source = {
            'source_id': str(uuid.uuid4()),
            'name': 'TestAllocationSourceCreateScenario',
            'compute_allowed': 50000
        }

        # Make sure no allocation_source_created event for this source exists
        event_count_before = EventTable.objects.filter(
            name='allocation_source_created',
            payload__name='TestAllocationSourceCreateScenario'
        ).count()
        self.assertEqual(event_count_before, 0)

        # Make sure that no Allocation Source with our test source name exists
        allocation_source_count_before = AllocationSource.objects.filter(
            name=new_allocation_source['name']).count()
        self.assertEqual(allocation_source_count_before, 0)

        allocation_source_count_before = AllocationSource.objects.filter(
            source_id=new_allocation_source['source_id']).count()
        self.assertEqual(allocation_source_count_before, 0)

        # Add an event 'allocation_source_created' with our test source name
        new_event = EventTable.create_event(name='allocation_source_created',
                                            payload=new_allocation_source,
                                            entity_id=new_allocation_source['source_id'])

        # Make sure we added the event successfully
        event_count_after = EventTable.objects.filter(
            name='allocation_source_created',
            payload__name='TestAllocationSourceCreateScenario'
        ).count()
        self.assertEqual(event_count_after, 1)

        # Make sure that there is now an Allocation Source with the test name
        allocation_source_count_after = AllocationSource.objects.filter(
            source_id=new_allocation_source['source_id'],
            name=new_allocation_source['name']).count()
        self.assertEqual(allocation_source_count_after, 1)

        allocation_source = AllocationSource.objects.filter(
            source_id=new_allocation_source['source_id'],
            name=new_allocation_source['name']).first()
        self.assertEqual(allocation_source.compute_allowed, new_allocation_source['compute_allowed'])
Пример #26
0
    def test_allocation_source_created(self):
        new_allocation_source = {
            'source_id': str(uuid.uuid4()),
            'name': 'TestAllocationSourceCreateScenario',
            'compute_allowed': 50000
        }

        # Make sure no allocation_source_created event for this source exists
        event_count_before = EventTable.objects.filter(
            name='allocation_source_created',
            payload__name='TestAllocationSourceCreateScenario').count()
        self.assertEqual(event_count_before, 0)

        # Make sure that no Allocation Source with our test source name exists
        allocation_source_count_before = AllocationSource.objects.filter(
            name=new_allocation_source['name']).count()
        self.assertEqual(allocation_source_count_before, 0)

        allocation_source_count_before = AllocationSource.objects.filter(
            source_id=new_allocation_source['source_id']).count()
        self.assertEqual(allocation_source_count_before, 0)

        # Add an event 'allocation_source_created' with our test source name
        new_event = EventTable.create_event(
            name='allocation_source_created',
            payload=new_allocation_source,
            entity_id=new_allocation_source['source_id'])

        # Make sure we added the event successfully
        event_count_after = EventTable.objects.filter(
            name='allocation_source_created',
            payload__name='TestAllocationSourceCreateScenario').count()
        self.assertEqual(event_count_after, 1)

        # Make sure that there is now an Allocation Source with the test name
        allocation_source_count_after = AllocationSource.objects.filter(
            source_id=new_allocation_source['source_id'],
            name=new_allocation_source['name']).count()
        self.assertEqual(allocation_source_count_after, 1)

        allocation_source = AllocationSource.objects.filter(
            source_id=new_allocation_source['source_id'],
            name=new_allocation_source['name']).first()
        self.assertEqual(allocation_source.compute_allowed,
                         new_allocation_source['compute_allowed'])
Пример #27
0
def listen_before_allocation_snapshot_changes(sender, instance, raw, **kwargs):
    """
    DEV NOTE: This is a *pre_save* signal. As such, the arguments are slightly different and the object in the database matching the data will be the "before", while the data coming into the function should be considered the "after". For more details about pre_save signals: https://docs.djangoproject.com/en/dev/ref/signals/#pre-save

    This listener expects:
    EventType - 'allocation_source_snapshot'
    EventPayload - {
        "allocation_source_id": "37623",
        "compute_used":100.00,  # 100 hours used ( a number, not a string!)
        "global_burn_rate":2.00,  # 2 hours used each hour
    }
    The method should result in an up-to-date snapshot of AllocationSource usage.
    """

    event = instance
    if event.name != 'allocation_source_snapshot':
        return None
    # Circular dep...
    from core.models import EventTable

    payload = event.payload
    allocation_source_id = payload['allocation_source_id']
    new_compute_used = payload['compute_used']
    threshold_values = getattr(settings, "ALLOCATION_SOURCE_WARNINGS", [])
    source = AllocationSource.objects.filter(
        source_id=allocation_source_id).first()
    if new_compute_used == 0:
        return
    if not source:
        return
    if source.compute_allowed in [None, 0]:
        return
    prev_snapshot = AllocationSourceSnapshot.objects.filter(
        allocation_source__source_id=allocation_source_id).first()
    if not prev_snapshot:
        prev_compute_used = 0
    else:
        prev_compute_used = float(prev_snapshot.compute_used)
    prev_percentage = int(100.0 * prev_compute_used / source.compute_allowed)
    current_percentage = int(100.0 * new_compute_used / source.compute_allowed)
    print "Souce: %s (%s) Previous:%s - New:%s" % (
        source.name, allocation_source_id, prev_percentage, current_percentage)
    percent_event_triggered = None
    # Compare 'Now snapshot' with Previous snapshot. Have we "crossed a threshold?"
    # If yes:
    # # Check if we have already fired the `allocation_source_threshold_met` event
    # # If not:
    # # # Fire the `allocation_source_threshold_met` event
    for test_threshold in threshold_values:
        if prev_percentage < test_threshold \
                and current_percentage >= test_threshold:
            percent_event_triggered = test_threshold
    if not percent_event_triggered:
        return
    print "Email Event triggered for %s users: %s" % (source.all_users.count(),
                                                      percent_event_triggered)
    prev_email_event = EventTable.objects\
        .filter(name="allocation_source_threshold_met")\
        .filter(entity_id=allocation_source_id,
                payload__threshold=percent_event_triggered)
    if prev_email_event:
        return
    new_payload = {
        "threshold": percent_event_triggered,
        "allocation_source_id": allocation_source_id,
        "actual_value": current_percentage
    }
    EventTable.create_event(name="allocation_source_threshold_met",
                            entity_id=allocation_source_id,
                            payload=new_payload)
    return
Пример #28
0
    def test_user_allocation_source_assigned(self):
        new_allocation_source = {
            'source_id': str(uuid.uuid4()),
            'name': 'TestAllocationSourceAssociateScenario',
            'compute_allowed': 50000
        }
        new_event = EventTable.create_event(
            name='allocation_source_created',
            payload=new_allocation_source,
            entity_id=new_allocation_source['source_id'])
        user = UserFactory.create()
        new_user_allocation_source = {
            'source_id': new_allocation_source['source_id'],
            'username': user.username
        }

        # Make sure no allocation_source_assigned event for this user and source exists
        event_count_before = EventTable.objects.filter(
            name='user_allocation_source_assigned',
            payload__username=user.username,
            payload__source_id=new_user_allocation_source['source_id']).count(
            )
        self.assertEqual(event_count_before, 0)

        # Make sure that no Allocation Source and User combination exists
        user_allocation_source_count_before = UserAllocationSource.objects.filter(
            allocation_source__source_id=new_user_allocation_source[
                'source_id'],
            user=user).count()
        self.assertEqual(user_allocation_source_count_before, 0)

        # Add an event 'allocation_source_created' with our test source name
        new_event = EventTable.create_event(
            name='user_allocation_source_assigned',
            payload=new_user_allocation_source,
            entity_id=new_user_allocation_source['username'])

        # Make sure we added the event successfully
        event_count_after = EventTable.objects.filter(
            name='user_allocation_source_assigned',
            payload__username=user.username,
            payload__source_id=new_user_allocation_source['source_id']).count(
            )
        self.assertEqual(event_count_after, 1)

        # Make sure that there is now an Allocation Source with the test name
        user_allocation_source_count_after = UserAllocationSource.objects.filter(
            allocation_source__source_id=new_user_allocation_source[
                'source_id'],
            user=user).count()
        self.assertEqual(user_allocation_source_count_after, 1)

        user_allocation_source = UserAllocationSource.objects.filter(
            allocation_source__source_id=new_user_allocation_source[
                'source_id'],
            user=user).first()
        self.assertEqual(
            user_allocation_source.allocation_source.compute_allowed,
            new_allocation_source['compute_allowed'])
        self.assertEqual(user_allocation_source.allocation_source.source_id,
                         new_allocation_source['source_id'])
        self.assertEqual(user_allocation_source.allocation_source.name,
                         new_allocation_source['name'])
def listen_before_allocation_snapshot_changes(sender, instance, raw, **kwargs):
    """
    DEV NOTE: This is a *pre_save* signal. As such, the arguments are slightly
    different and the object in the database matching the data will be the
    "before", while the data coming into the function should be considered the
    "after". For more details about pre_save signals:
    https://docs.djangoproject.com/en/dev/ref/signals/#pre-save

    This listener expects:
    EventType - 'allocation_source_snapshot'
    EventPayload - {
        "allocation_source_name": "37623",
        "compute_used":100.00,  # 100 hours used ( a number, not a string!)
        "global_burn_rate":2.00,  # 2 hours used each hour
    }
    The method should result in an up-to-date snapshot of AllocationSource usage.
    """

    event = instance
    if event.name != 'allocation_source_snapshot':
        return None
    # Circular dep...
    from core.models import EventTable

    payload = event.payload
    allocation_source_name = payload['allocation_source_name']
    new_compute_used = payload['compute_used']
    threshold_values = getattr(settings, "ALLOCATION_SOURCE_WARNINGS", [])
    source = AllocationSource.objects.filter(name=allocation_source_name).last()
    if new_compute_used == 0:
        return
    if not source:
        return
    if source.compute_allowed in [None, 0]:
        return
    prev_snapshot = AllocationSourceSnapshot.objects.filter(
        allocation_source=source
    ).first()
    ##CHANGED
    # prev_snapshot = AllocationSourceSnapshot.objects.filter(allocation_source__name=allocation_source_name).last()
    if not prev_snapshot:
        prev_compute_used = 0
    else:
        prev_compute_used = float(prev_snapshot.compute_used)
    prev_percentage = int(100.0 * prev_compute_used / source.compute_allowed)
    current_percentage = int(100.0 * new_compute_used / source.compute_allowed)
    print "Souce: %s (%s) Previous:%s - New:%s" % (
        source.name, allocation_source_name, prev_percentage, current_percentage
    )
    percent_event_triggered = None
    # Compare 'Now snapshot' with Previous snapshot. Have we "crossed a threshold?"
    # If yes:
    # # Check if we have already fired the `allocation_source_threshold_met` event
    # # If not:
    # # # Fire the `allocation_source_threshold_met` event
    for test_threshold in threshold_values:
        if prev_percentage < test_threshold \
                and current_percentage >= test_threshold:
            percent_event_triggered = test_threshold
    if not percent_event_triggered:
        return
    print "Email Event triggered for %s users: %s" % (
        source.all_users.count(), percent_event_triggered
    )
    prev_email_event = EventTable.objects \
        .filter(name="allocation_source_threshold_met") \
        .filter(entity_id=allocation_source_name,
                payload__threshold=percent_event_triggered)
    if prev_email_event:
        return
    new_payload = {
        "threshold": percent_event_triggered,
        "allocation_source_name": allocation_source_name,
        "actual_value": current_percentage
    }
    EventTable.create_event(
        name="allocation_source_threshold_met",
        entity_id=allocation_source_name,
        payload=new_payload
    )
    return
Пример #30
0
def listen_before_allocation_snapshot_changes(sender, instance, raw, **kwargs):
    """
    This listener expects:
    EventType - 'allocation_source_snapshot'
    EventPayload - {
        "allocation_source_id": "37623",
        "compute_used":100.00,  # 100 hours used ( a number, not a string!)
        "global_burn_rate":2.00,  # 2 hours used each hour
    }
    The method should result in an up-to-date snapshot of AllocationSource usage.
    """

    event = instance
    if event.name != 'allocation_source_snapshot':
        return None
    # Circular dep...
    from core.models import EventTable

    payload = event.payload
    allocation_source_id = payload['allocation_source_id']
    new_compute_used = payload['compute_used']
    threshold_values = getattr(settings, "ALLOCATION_SOURCE_WARNINGS", [])
    source = AllocationSource.objects.filter(
        source_id=allocation_source_id).first()
    if new_compute_used == 0:
        return
    if not source:
        return
    if source.compute_allowed in [None, 0]:
        return
    prev_snapshot = AllocationSourceSnapshot.objects.filter(
        allocation_source__source_id=allocation_source_id).first()
    if not prev_snapshot:
        prev_compute_used = 0
    else:
        prev_compute_used = float(prev_snapshot.compute_used)
    prev_percentage = int(100.0 * prev_compute_used / source.compute_allowed)
    current_percentage = int(100.0 * new_compute_used / source.compute_allowed)
    print "Previous:%s - New:%s" % (prev_percentage, current_percentage)
    percent_event_triggered = None
    # Compare 'Now snapshot' with Previous snapshot. Have we "crossed a threshold?"
    # If yes:
    # # Check if we have already fired the `allocation_source_threshold_met` event
    # # If not:
    # # # Fire the `allocation_source_threshold_met` event
    for test_threshold in threshold_values:
        if prev_percentage < test_threshold \
                and current_percentage >= test_threshold:
            percent_event_triggered = test_threshold
    print "Event triggered: %s" % percent_event_triggered
    if not percent_event_triggered:
        return
    prev_email_event = EventTable.objects\
        .filter(name="allocation_source_threshold_met")\
        .filter(entity_id=allocation_source_id,
                payload__threshold=percent_event_triggered)
    if prev_email_event:
        return
    new_payload = {
        "threshold": percent_event_triggered,
        "allocation_source_id": allocation_source_id,
        "actual_value": current_percentage
    }
    EventTable.create_event(name="allocation_source_threshold_met",
                            entity_id=allocation_source_id,
                            payload=new_payload)
    return