Beispiel #1
0
    def get_by_violation_key_name(self,
                                  key_id,
                                  current_page=1,
                                  page_size=10,
                                  domain=None,
                                  page_filter=None,
                                  callback=None):
        reviews = Review.get_by_violation_key_name(
            db=self.db,
            key_id=key_id,
            current_page=current_page,
            page_size=page_size,
            domain_filter=domain.name if domain else None,
            page_filter=page_filter,
        )

        reviews_data = []
        for item in reviews:
            reviews_data.append({
                'uuid': item.review_uuid,
                'page': {
                    'uuid': item.page_uuid,
                    'url': item.url,
                    'completedAt': item.completed_date
                },
                'domain': item.domain_name,
            })

        callback({'reviews': reviews_data})
Beispiel #2
0
    def get_by_violation_key_name(self, key_id, current_page=1, page_size=10, domain=None, page_filter=None, callback=None):
        reviews = Review.get_by_violation_key_name(
            db=self.db,
            key_id=key_id,
            current_page=current_page,
            page_size=page_size,
            domain_filter=domain.name if domain else None,
            page_filter=page_filter,
        )

        reviews_data = []
        for item in reviews:
            reviews_data.append({
                'uuid': item.review_uuid,
                'page': {
                    'uuid': item.page_uuid,
                    'url': item.url,
                    'completedAt': item.completed_date
                },
                'domain': item.domain_name,
            })

        callback({
            'reviews': reviews_data
        })
Beispiel #3
0
    def save_review(cls, page_uuid, review_data, db, fact_definitions, violation_definitions, cache, publish):
        from holmes.models import Page

        page = Page.by_uuid(page_uuid, db)
        last_review = page.last_review

        review = Review(
            domain_id=page.domain.id,
            page_id=page.id,
            is_active=True,
            is_complete=False,
            completed_date=datetime.utcnow(),
            uuid=uuid4(),
        )

        db.add(review)

        for fact in review_data['facts']:
            name = fact['key']
            key = fact_definitions[name]['key']
            review.add_fact(key, fact['value'])

        for violation in review_data['violations']:
            name = violation['key']
            key = violation_definitions[name]['key']
            review.add_violation(
                key,
                violation['value'],
                violation['points'],
                page.domain
            )

        for i in range(3):
            db.begin(subtransactions=True)
            try:
                page.expires = review_data['expires']
                page.last_modified = review_data['lastModified']
                page.score = 0
                page.last_review_uuid = review.uuid
                page.last_review = review
                page.last_review_date = review.completed_date
                page.violations_count = len(review_data['violations'])
                db.commit()
                break
            except Exception:
                err = sys.exc_info()[1]
                if 'Deadlock found' in str(err):
                    logging.error('Deadlock happened! Trying again (try number %d)! (Details: %s)' % (i, str(err)))
                else:
                    db.rollback()
                    raise

        review.is_complete = True

        if not last_review:
            cache.increment_active_review_count(page.domain)

            cache.increment_violations_count(
                page.domain,
                increment=page.violations_count
            )

            cache.increment_next_jobs_count(-1)

        else:
            old_violations_count = len(last_review.violations)
            new_violations_count = len(review.violations)

            cache.increment_violations_count(
                page.domain,
                increment=new_violations_count - old_violations_count
            )

            for i in range(3):
                db.begin(subtransactions=True)
                try:
                    for violation in last_review.violations:
                        violation.review_is_active = False
                    last_review.is_active = False
                    db.commit()
                    break
                except Exception:
                    err = sys.exc_info()[1]
                    if 'Deadlock found' in str(err):
                        logging.error('Deadlock happened! Trying again (try number %d)! (Details: %s)' % (i, str(err)))
                    else:
                        db.rollback()
                        raise

        publish(dumps({
            'type': 'new-review',
            'reviewId': str(review.uuid)
        }))