Example #1
0
def cost_post_save(sender, instance, raw, using, **kwargs):
    changed = False
    if 'venture_id' in instance.dirty_fields:
        changed = True
    if 'expire' in instance.dirty_fields:
        changed = True
    if 'cost' in instance.dirty_fields:
        old_cost = instance.dirty_fields['cost'] or 0
        if not -1 < instance.cost - old_cost < 1:
            # Ignore changes due to rounding errors
            changed = True
    if changed:
        HistoryCost.start_span(extra=instance, end=instance.expire)
Example #2
0
def cost_post_save(sender, instance, raw, using, **kwargs):
    changed = False
    if 'venture_id' in instance.dirty_fields:
        changed = True
    if 'expire' in instance.dirty_fields:
        changed = True
    if 'cost' in instance.dirty_fields:
        old_cost = instance.dirty_fields['cost'] or 0
        if not -1 < instance.cost - old_cost < 1:
            # Ignore changes due to rounding errors
            changed = True
    if changed:
        if instance.expire:
            start = min(datetime.date.today(), instance.expire)
        else:
            start = datetime.datetime.now()
        HistoryCost.start_span(extra=instance, start=start, end=instance.expire)
Example #3
0
def openstack(**kwargs):
    if settings.OPENSTACK_URL is None:
        return False, 'not configured.', kwargs
    tenants = collections.defaultdict(lambda: collections.defaultdict(dict))
    end = kwargs.get('end') or datetime.datetime.today().replace(
        hour=0, minute=0, second=0, microsecond=0)
    start = kwargs.get('start') or end - datetime.timedelta(days=1)
    for region in getattr(settings, 'OPENSTACK_REGIONS', ['']):
        stack = OpenStack(
            settings.OPENSTACK_URL,
            settings.OPENSTACK_USER,
            settings.OPENSTACK_PASS,
            region=region,
        )
        for data in stack.simple_tenant_usage(start, end):
            tenants[data['tenant_id']][region].update(data)
    for url, query in getattr(settings, 'OPENSTACK_EXTRA_QUERIES', []):
        for data in stack.query(
                query,
                url=url,
                start=start.strftime('%Y-%m-%dT%H:%M:%S'),
                end=end.strftime('%Y-%m-%dT%H:%M:%S'),
        ):
            tenants[data['tenant_id']][url].update(data)
    for tenant_id, regions in tenants.iteritems():
        dev = make_tenant(tenant_id)
        dev.historycost_set.filter(start=start).delete()
        margin_in_percent = dev.get_margin() or 0
        total_cost = 0
        for region, data in regions.iteritems():
            cost = make_components(data, dev, region)
            total_cost += cost * (1 + margin_in_percent / 100)
        hc = HistoryCost(
            device=dev,
            venture=dev.venture,
            start=start,
            end=end,
            daily_cost=total_cost,
        )
        hc.save()
    return True, 'loaded from %s to %s.' % (start, end), kwargs
Example #4
0
def cost_pre_delete(sender, instance, using, **kwargs):
    HistoryCost.end_span(extra=instance)