Exemplo n.º 1
0
def total_usage(username, start_date, allocation_source_name=None,end_date=None, burn_rate=False, email=None):
    """ 
        This function outputs the total allocation usage in hours
    """
    from service.allocation_logic import create_report
    if not end_date:
        end_date = timezone.now()
    user_allocation = create_report(start_date,end_date,user_id=username,allocation_source_name=allocation_source_name)
    if email:
        return user_allocation
    total_allocation = 0.0
    for data in user_allocation:
        #print data['instance_id'], data['allocation_source'], data['instance_status_start_date'], data['instance_status_end_date'], data['applicable_duration']
        if not data['allocation_source']=='N/A':
            total_allocation += data['applicable_duration']
    compute_used_total = round(total_allocation/3600.0,2)
    if compute_used_total > 0:
        logger.info("Total usage for User %s with AllocationSource %s from %s-%s = %s"
                    % (username, allocation_source_name, start_date, end_date, compute_used_total))
    if burn_rate:
        burn_rate_total = 0 if len(user_allocation)<1 else user_allocation[-1]['burn_rate']
        if burn_rate_total != 0:
            logger.info("User %s with AllocationSource %s Burn Rate: %s"
                        % (username, allocation_source_name, burn_rate_total))
        return [compute_used_total, burn_rate_total]
    return compute_used_total
Exemplo n.º 2
0
def total_usage(username, start_date, allocation_source_name=None,end_date=None, burn_rate=False, email=None):
    """ 
        This function outputs the total allocation usage in hours
    """
    from service.allocation_logic import create_report
    if not end_date:
        end_date = timezone.now()
    user_allocation = create_report(start_date,end_date,user_id=username,allocation_source_name=allocation_source_name)
    if email:
        return user_allocation
    total_allocation = 0.0
    for data in user_allocation:
        #print data['instance_id'], data['allocation_source'], data['instance_status_start_date'], data['instance_status_end_date'], data['applicable_duration']
        if not data['allocation_source']=='N/A':
            total_allocation += data['applicable_duration']
    compute_used_total = round(total_allocation/3600.0,2)
    if compute_used_total > 0:
        logger.info("Total usage for User %s with AllocationSource %s from %s-%s = %s"
                    % (username, allocation_source_name, start_date, end_date, compute_used_total))
    if burn_rate:
        burn_rate_total = 0 if len(user_allocation)<1 else user_allocation[-1]['burn_rate']
        if burn_rate_total != 0:
            logger.info("User %s with AllocationSource %s Burn Rate: %s"
                        % (username, allocation_source_name, burn_rate_total))
        return [compute_used_total, burn_rate_total]
    return compute_used_total
Exemplo n.º 3
0
def update_snapshot(start_date=None, end_date=None):
    if not settings.USE_ALLOCATION_SOURCE:
        return False
    end_date = end_date or timezone.now()
    # TODO: Read this start_date from last 'reset event' for each allocation source
    start_date = start_date or '2016-09-01 00:00:00.0-05'
    all_data = create_report(start_date, end_date)

    user_allocation_snapshots = {}
    unique_usernames = set()

    for row in all_data:
        key = (row['allocation_source'], row['username'])
        compute_used, instance_burn_rates = user_allocation_snapshots.get(
            key, (0.0, {}))
        new_compute_used = compute_used + float(row['applicable_duration'])
        new_instance_burn_rate = int(get_instance_burn_rate_from_row(row))
        instance_burn_rates['instance_id'] = new_instance_burn_rate
        user_allocation_snapshots[key] = (new_compute_used,
                                          instance_burn_rates)

        unique_usernames.add(row['username'])

    allocation_source_ids = {
        obj['name']: obj['id']
        for obj in AllocationSource.objects.all().values('name', 'id')
    }
    relevant_users = {
        obj['username']: obj['id']
        for obj in AtmosphereUser.objects.filter(
            username__in=unique_usernames).values('username', 'id')
    }

    allocation_source_burn_rates = collections.Counter()
    for key, snapshot_numbers in user_allocation_snapshots.iteritems():
        allocation_source_name, username = key
        compute_used, instance_burn_rates = snapshot_numbers
        try:
            allocation_source_id = allocation_source_ids[
                allocation_source_name]
        except KeyError:
            # This allocation source does not exist in our database yet. Create it? Skip for now. Could be 'N/A' as well
            continue
        user_allocation_burn_rate = sum(instance_burn_rates.values())
        snapshot, created = UserAllocationSnapshot.objects.update_or_create(
            allocation_source_id=allocation_source_id,
            user_id=relevant_users[username],
            defaults={
                'compute_used': round(compute_used / 3600, 2),
                'burn_rate': user_allocation_burn_rate
            })
        allocation_source_burn_rates[
            allocation_source_name] += user_allocation_burn_rate

    tas_api_obj = TASAPIDriver()
    allocation_source_usage_from_tas = tas_api_obj.get_all_projects()
    for project in allocation_source_usage_from_tas:
        allocation_source_name = project['chargeCode']
        try:
            allocation_source_id = allocation_source_ids[
                allocation_source_name]
        except KeyError:
            # This allocation source does not exist in our database yet. Create it? Skip for now.
            continue
        compute_used = project['allocations'][-1]['computeUsed']
        snapshot, created = AllocationSourceSnapshot.objects.update_or_create(
            allocation_source_id=allocation_source_id,
            defaults={
                'compute_used':
                compute_used,
                'global_burn_rate':
                allocation_source_burn_rates.get(allocation_source_name, 0)
            })
    return True