示例#1
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'])
示例#2
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 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 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
示例#5
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
示例#7
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
示例#8
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'])
示例#9
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'])
示例#10
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
示例#11
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
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
示例#13
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'])