Exemplo n.º 1
0
class ScannerQueryRule(AbstractScannerRule):
    scanner = models.PositiveSmallIntegerField(
        choices=((YARA, 'yara'), ),  # For now code search only allows yara.
        default=YARA,
    )
    state = models.PositiveSmallIntegerField(choices=QUERY_RULE_STATES.items(),
                                             default=NEW)
    run_on_disabled_addons = models.BooleanField(
        default=False,
        help_text=_('Run this rule on add-ons that have been '
                    'force-disabled as well.'),
    )
    celery_group_result_id = models.UUIDField(default=None, null=True)
    task_count = models.PositiveIntegerField(default=0)

    class Meta(AbstractScannerRule.Meta):
        db_table = 'scanners_query_rules'

    def change_state_to(self, target):
        """Immediately change state of the rule in database or raise
        ImproperScannerQueryRuleStateError."""
        prereqs = {
            # New is the default state.
            NEW: (),
            # Scheduled should only happen through the admin. It's the
            # prerequisite to running the task.
            SCHEDULED: (NEW, ),
            # Running should only happen through the task, after we went
            # through the admin to schedule the query.
            RUNNING: (SCHEDULED, ),
            # Aborting can happen from various states.
            ABORTING: (NEW, SCHEDULED, RUNNING),
            # Aborted should only happen after aborting.
            ABORTED: (ABORTING, ),
            # Completed should only happen through the task
            COMPLETED: (RUNNING, ),
        }
        if self.state in prereqs[target]:
            self.update(state=target)
        else:
            raise ImproperScannerQueryRuleStateError()

    def _get_completed_tasks_count(self):
        if self.celery_group_result_id is not None:
            from olympia.amo.celery import app as celery_app

            result = celery_app.GroupResult.restore(
                str(self.celery_group_result_id))
            if result:
                return result.completed_count()
        return None

    def completion_rate(self):
        if self.state == RUNNING:
            completed_tasks_count = self._get_completed_tasks_count()
            if completed_tasks_count is not None and self.task_count:
                rate = (completed_tasks_count / self.task_count) * 100
                return '{:.2f}%'.format(rate)
        return None
Exemplo n.º 2
0
class ScannerQueryRule(AbstractScannerRule):
    scanner = models.PositiveSmallIntegerField(choices=(
        (YARA, 'yara'),  # For now code search only allows yara.
    ), default=YARA)
    state = models.PositiveSmallIntegerField(
        choices=QUERY_RULE_STATES.items(), default=NEW
    )
    run_on_disabled_addons = models.BooleanField(
        default=False, help_text=_('Run this rule on add-ons that have been '
                                   'force-disabled as well.'))

    class Meta(AbstractScannerRule.Meta):
        db_table = 'scanners_query_rules'

    def change_state_to(self, target):
        """Immediately change state of the rule in database or raise
        ImproperScannerQueryRuleStateError."""
        prereqs = {
            # New is the default state.
            NEW: (),
            # Scheduled should only happen through the admin. It's the
            # prerequisite to running the task.
            SCHEDULED: (NEW,),
            # Running should only happen through the task, after we went
            # through the admin to schedule the query.
            RUNNING: (SCHEDULED,),
            # Aborting can happen from various states.
            ABORTING: (NEW, SCHEDULED, RUNNING,),
            # Aborted should only happen after aborting.
            ABORTED: (ABORTING,),
            # Completed should only happen through the task
            COMPLETED: (RUNNING,),
        }
        if self.state in prereqs[target]:
            self.update(state=target)
        else:
            raise ImproperScannerQueryRuleStateError()
Exemplo n.º 3
0
class ScannerQueryRule(AbstractScannerRule):
    state = models.PositiveSmallIntegerField(choices=QUERY_RULE_STATES.items(),
                                             default=NEW)

    class Meta(AbstractScannerRule.Meta):
        db_table = 'scanners_query_rules'