Exemplo n.º 1
0
    def get_allocation_dict(self):
        if not self.allocation:
            return {}
        #Don't move it up. Circular reference.
        from django.conf import settings
        from service.allocation import core_instance_time, get_burn_time,\
            delta_to_minutes, delta_to_hours, get_delta
        delta = get_delta(self, time_period=settings.FIXED_WINDOW)
        #Keeps the times on these calculations consistent!
        now_time = timezone.now()
        time_used, _ = core_instance_time(self.identity.created_by,
                                          self.identity.id,
                                          delta,
                                          now_time=now_time)
        hours_used = delta_to_hours(time_used)

        burn_time = get_burn_time(self.identity.created_by,
                                  self.identity.id,
                                  delta,
                                  timedelta(minutes=self.allocation.threshold),
                                  now_time=now_time)

        zero_time = now_time + burn_time if burn_time else None
        burned_per_hour = delta_to_hours(burn_time)

        allocation_dict = {
            "threshold": floor(self.allocation.threshold / 60),
            "current": hours_used,
            "delta": ceil(delta.total_seconds() / 60),
            "burn": burned_per_hour,
            "ttz": zero_time,
        }
        return allocation_dict
Exemplo n.º 2
0
 def get_allocation_dict(self):
     if not self.allocation:
         return {}
     #Don't move it up. Circular reference.
     from service.allocation import get_time, get_burn_time,\
         delta_to_minutes, delta_to_hours, get_delta
     delta = get_delta(self, time_period=relativedelta(day=1, months=1))
     time_used = get_time(self.identity.created_by,
                          self.identity.id,
                          delta)
     burn_time = get_burn_time(self.identity.created_by, self.identity.id,
                               delta,
                               timedelta(minutes=self.allocation.threshold))
     mins_consumed = delta_to_minutes(time_used)
     if burn_time:
         burn_time = delta_to_hours(burn_time)
     zero_time = datetime.now() + timedelta(
             minutes=(self.allocation.threshold - mins_consumed))
     allocation_dict = {
         "threshold": floor(self.allocation.threshold/60),
         "current": floor(mins_consumed/60),
         "delta": ceil(delta.total_seconds()/60),
         "burn": burn_time,
         "ttz": zero_time,
     }
     return allocation_dict
Exemplo n.º 3
0
    def get_allocation_dict(self):
        if not self.allocation:
            return {}
        #Don't move it up. Circular reference.
        from django.conf import settings
        from service.allocation import core_instance_time, get_burn_time,\
            delta_to_minutes, delta_to_hours, get_delta
        delta = get_delta(self, time_period=settings.FIXED_WINDOW)
        #Keeps the times on these calculations consistent!
        now_time = timezone.now()
        time_used, _ = core_instance_time(self.identity.created_by,
                             self.identity.id,
                             delta,
                             now_time=now_time)
        hours_used = delta_to_hours(time_used)

        burn_time = get_burn_time(self.identity.created_by, self.identity.id,
                                  delta,
                                  timedelta(minutes=self.allocation.threshold),
                                  now_time=now_time)

        zero_time = now_time + burn_time if burn_time else None
        burned_per_hour = delta_to_hours(burn_time)

        allocation_dict = {
            "threshold": floor(self.allocation.threshold/60),
            "current": hours_used,
            "delta": ceil(delta.total_seconds()/60),
            "burn": burned_per_hour,
            "ttz": zero_time,
        }
        return allocation_dict
Exemplo n.º 4
0
def monitor_instances_for_user(provider, username, instances):
    from core.models.instance import convert_esh_instance
    try:
        user = AtmosphereUser.objects.get(username=username)
    except AtmosphereUser.DoesNotExist:
        if instances:
            logger.warn("WARNING: User %s has %s instances, but does not"
            "exist on this database" % username, len(instances))
        return
    for identity in user.identity_set.filter(provider=provider):
        try:
            identity_id = identity.id
            #GATHER STATISTICS FIRST
            #This will be: Calculate time that user has used all instances within a
            #given delta, including the instances listed currently.
            time_period=relativedelta(day=1, months=1)
            allocation = get_allocation(username, identity_id)
            delta_time = get_delta(allocation, time_period)
            time_used = current_instance_time(
                    user, instances,
                    identity_id, delta_time)
            enforce_allocation(identity, user, time_used)
        except:
            logger.exception("Unable to monitor Identity:%s"
                         % (identity,))
Exemplo n.º 5
0
def check_usernames(provider_id, usernames):
    for username in usernames:
        ident = Identity.objects.get(provider__id=provider_id, created_by__username=username)
        print "Provider: %s - Username: %s" % (ident.provider, ident.created_by.username)
        total_time, instances_map = core_instance_time(ident.created_by, ident.id, get_delta(get_allocation(ident.created_by.username, ident.id),settings.FIXED_WINDOW, timezone.now()), [], now_time=timezone.now())
        instance_list = instances_map.keys()
        for instance in instance_list:
            print "Instance:%s Time Used:%s" % (instance.provider_alias, instance.active_time)
            for history in instances_map[instance]:
                if history.cpu_time > timezone.timedelta(0):
                    print "Status:%s %s*(%s - %s) = %s" % ( history.status.name, history.size.cpu, history.start_date, history.end_date, history.cpu_time)
        print "Total Time Given:%s Total time used: %s" % (timezone.timedelta(minutes=get_allocation(ident.created_by.username, ident.id).threshold),total_time)
Exemplo n.º 6
0
def monitor_instances_for_user(provider, username, instances,
                               print_logs=False):
    """
    """
    from core.models import IdentityMembership
    try:
        #Note: This username may or may not have an associated
        #Allocation/IdentityMembership
        user = AtmosphereUser.objects.get(username=username)
    except AtmosphereUser.DoesNotExist:
        #if instances:
        #    logger.warn("WARNING: User %s has %s instances, but does not"
        #                "exist on this database" % (username, len(instances)))
        return
    for identity in user.identity_set.filter(provider=provider):
        try:
            identity_id = identity.id
            #GATHER STATISTICS FIRST
            #This will be: Calculate time for all instances within a
            #given delta, including the instances listed currently.
            time_period = settings.FIXED_WINDOW
            allocation = get_allocation(username, identity_id)
            delta_time = get_delta(allocation, time_period)
            time_used, instance_status_map = current_instance_time(
                user, instances,
                identity_id, delta_time)
            if print_logs:
                print_table_row(instance_status_map, user,
                                allocation, time_used)
                return
            enforce_allocation(identity, user, time_used)
        except IdentityMembership.DoesNotExist:
            pass
            #if instances:
            #    logger.warn(
            #        "WARNING: User %s has %s instances, but does not"
            #        "exist on this database" % (username, len(instances)))
        except:
            logger.exception("Unable to monitor Identity:%s"
                             % (identity,))
Exemplo n.º 7
0
def monitor_instances_for_user(provider,
                               username,
                               instances,
                               print_logs=False):
    """
    """
    from core.models import IdentityMembership
    try:
        #Note: This username may or may not have an associated
        #Allocation/IdentityMembership
        user = AtmosphereUser.objects.get(username=username)
    except AtmosphereUser.DoesNotExist:
        #if instances:
        #    logger.warn("WARNING: User %s has %s instances, but does not"
        #                "exist on this database" % (username, len(instances)))
        return
    for identity in user.identity_set.filter(provider=provider):
        try:
            identity_id = identity.id
            #GATHER STATISTICS FIRST
            #This will be: Calculate time for all instances within a
            #given delta, including the instances listed currently.
            time_period = settings.FIXED_WINDOW
            allocation = get_allocation(username, identity_id)
            delta_time = get_delta(allocation, time_period)
            time_used, instance_status_map = current_instance_time(
                user, instances, identity_id, delta_time)
            if print_logs:
                print_table_row(instance_status_map, user, allocation,
                                time_used)
                return
            enforce_allocation(identity, user, time_used)
        except IdentityMembership.DoesNotExist:
            pass
            #if instances:
            #    logger.warn(
            #        "WARNING: User %s has %s instances, but does not"
            #        "exist on this database" % (username, len(instances)))
        except:
            logger.exception("Unable to monitor Identity:%s" % (identity, ))
Exemplo n.º 8
0
 def get_allocation_dict(self):
     if not self.allocation:
         return {}
     #Don't move it up. Circular reference.
     from service.allocation import get_time, get_burn_time,\
         delta_to_minutes, delta_to_hours, get_delta
     delta = get_delta(self, time_period=relativedelta(day=1, months=1))
     time_used = get_time(self.identity.created_by, self.identity.id, delta)
     burn_time = get_burn_time(self.identity.created_by, self.identity.id,
                               delta,
                               timedelta(minutes=self.allocation.threshold))
     mins_consumed = delta_to_minutes(time_used)
     if burn_time:
         burn_time = delta_to_hours(burn_time)
     zero_time = datetime.now() + timedelta(
         minutes=(self.allocation.threshold - mins_consumed))
     allocation_dict = {
         "threshold": floor(self.allocation.threshold / 60),
         "current": floor(mins_consumed / 60),
         "delta": ceil(delta.total_seconds() / 60),
         "burn": burn_time,
         "ttz": zero_time,
     }
     return allocation_dict