Exemplo n.º 1
0
    def collect_usage(self, tenant, mutex, end=None):
        # Collects usage for a given tenant from when they were last collected,
        #   up to the given end, and breaks the range into one hour windows.
        end = end or datetime.utcnow()

        time_label = TimeLabel(tenant.last_collected + timedelta(minutes=1))
        end_time_label = TimeLabel(end)
        usage = {}
        logbook.info(
            'collect_usage for {}, from {} till {} (last_collected: {})',
            tenant, time_label, end_time_label, tenant.last_collected)

        customer = Customer.get_by_tenant_id(tenant.tenant_id)
        if not customer:
            logbook.error("Customer for tenant {} not found", tenant)
            return usage

        while time_label < end_time_label:
            try:
                usages = self._collect_usage(tenant, time_label, customer)
                tenant.last_collected = time_label.datetime_range()[1]
                if usages:
                    db.session.add(customer)
                    total_cost = customer.calculate_usage_cost(usages)
                    customer.withdraw(total_cost)
                    if not conf.test:
                        db.session.commit()

                    usage[time_label] = [usage.to_dict()
                                         for usage in usages], total_cost
            except Exception:
                self.errors += 1
                import traceback

                traceback.print_exc()
                logbook.exception("Usage process failed for {} and {}", tenant,
                                  time_label)
                db.session.rollback()
                return usage

            time_label = time_label.next()
            mutex.update_ttl()
        return usage
Exemplo n.º 2
0
    def fake_usage(customer, start, finish, service_id, resource_id, volume, resource_name=None):
        from model import ServiceUsage
        from fitter.aggregation.timelabel import TimeLabel
        if customer.os_tenant_id is None:
            raise errors.TenantIsnotCreated()

        time_label = TimeLabel(start)
        finish_time_label = TimeLabel(finish)
        total_cost = Decimal(0)
        while time_label <= finish_time_label:
            st, fn = time_label.datetime_range()
            st = max(st, start)
            fn = min(fn, finish)
            service_usage = ServiceUsage(customer.os_tenant_id, service_id, time_label, resource_id,
                                         customer.tariff, volume, st, fn, resource_name=resource_name)
            db.session.add(service_usage)
            cost = customer.calculate_usage_cost([service_usage])
            customer.withdraw(cost)
            total_cost += cost
            time_label = time_label.next()
        return total_cost
Exemplo n.º 3
0
    def collect_usage(self, tenant, mutex, end=None):
        # Collects usage for a given tenant from when they were last collected,
        #   up to the given end, and breaks the range into one hour windows.
        end = end or datetime.utcnow()

        time_label = TimeLabel(tenant.last_collected + timedelta(minutes=1))
        end_time_label = TimeLabel(end)
        usage = {}
        logbook.info('collect_usage for {}, from {} till {} (last_collected: {})',
                     tenant, time_label, end_time_label, tenant.last_collected)

        customer = Customer.get_by_tenant_id(tenant.tenant_id)
        if not customer:
            logbook.error("Customer for tenant {} not found", tenant)
            return usage

        while time_label < end_time_label:
            try:
                usages = self._collect_usage(tenant, time_label, customer)
                tenant.last_collected = time_label.datetime_range()[1]
                if usages:
                    db.session.add(customer)
                    total_cost = customer.calculate_usage_cost(usages)
                    customer.withdraw(total_cost)
                    if not conf.test:
                        db.session.commit()

                    usage[time_label] = [usage.to_dict() for usage in usages], total_cost
            except Exception:
                self.errors += 1
                import traceback

                traceback.print_exc()
                logbook.exception("Usage process failed for {} and {}", tenant, time_label)
                db.session.rollback()
                return usage

            time_label = time_label.next()
            mutex.update_ttl()
        return usage