예제 #1
0
 def make_object(self, data):
     from connect.models import UsageFile
     return UsageFile(**data)
예제 #2
0
    def process_request(self, request):
        """Generate UsageFile for each active Asset"""

        # store each processed request for debug
        self.usages.append(request)

        today = datetime.utcnow() - timedelta(minutes=10)
        name_format = 'Report for {asset} {date}'

        project = self.get_project(request)
        if not project:
            return

        stop_report_time = self.get_stop_report_time(request, project)
        start_report_time = self.get_start_report_time(request, project)
        if request.status in ['suspended', 'terminated'] \
                and not stop_report_time:
            LOG.info(
                "%s-%s: asset usage reporting was stopped without "
                "stop report time label", request.id, project.id)
            return

        last_report_time, confirmed = self.get_last_report_time(
            request, project)
        report_time = last_report_time + timedelta(days=1)
        report_time = report_time.replace(hour=0,
                                          minute=0,
                                          second=0,
                                          microsecond=0)

        if self.project_id is not None and self.project_id != project.id:
            return

        # check that previous report has passed validation
        if confirmed is False:
            usage_files = UsageFileAutomation()
            try:
                report_date = last_report_time.strftime('%Y-%m-%d')
                report_name = name_format.format(asset=request.id,
                                                 date=report_date)
                found = usage_files.list(
                    dict(
                        usage_files.filters(),
                        name=report_name,
                        status__in=None,
                        limit=10,
                    ))

                found = [f for f in found or [] if f.status != 'deleted']

                if found:
                    if len(found) > 2:
                        raise Exception("Found multiple reports with name %s"
                                        "" % report_name)

                    report = found[0]

                    if report.status in ('processing', 'draft', 'uploading'):
                        LOG.info("%s-%s: usage report '%s' is being processed",
                                 request.id, project.id, report_name)
                        return

                    if report.status in ('invalid', 'rejected'):
                        # we have to wait when user remove invalid report
                        LOG.error("%s-%s: failed usage report '%s' found",
                                  request.id, project.id, report_name)
                        return

                    self.update_last_report_time(project,
                                                 last_report_time,
                                                 confirmed=True)
                else:
                    report_time = last_report_time

            except ServerError:
                # this section is useless but left for future development
                raise

        if request.status in ['suspended', 'terminated']:
            if stop_report_time and stop_report_time > last_report_time \
                    and stop_report_time <= report_time:
                LOG.info("%s-%s: sending last report (%s)", request.id,
                         project.id, request.status)
                report_time = stop_report_time

        if start_report_time and start_report_time > last_report_time \
                and start_report_time < report_time:
            last_report_time = start_report_time

        if report_time > today and self.project_id is None:
            LOG.info("%s-%s: usage is already reported", request.id,
                     project.id)
            return

        usage_file = UsageFile(
            name=name_format.format(asset=request.id,
                                    date=report_time.strftime('%Y-%m-%d')),
            product=Product(id=request.product.id),
            contract=Contract(id=request.contract.id),
            description=name_format.format(
                asset=request.id, date=report_time.strftime('%Y-%m-%d')),
        )

        # report for each day since last report date
        LOG.info("%s-%s: creating report from %s to %s", request.id,
                 project.id, last_report_time, report_time)
        items = {item.mpn: item for item in request.items}
        usage_records = self.collect_usage_records(items, project,
                                                   last_report_time,
                                                   report_time)
        self.submit_usage(usage_file=usage_file, usage_records=usage_records)

        if report_time > today:
            # when project id is specified we allow to send usage for today
            # but don't update last report time
            return

        self.update_last_report_time(project, report_time)