示例#1
0
    def save_review(cls, page_uuid, review_data, db, search_provider,
                    fact_definitions, violation_definitions, cache, publish,
                    config):
        from holmes.models import Page, Request

        page = Page.by_uuid(page_uuid, db)

        if page is None:
            return

        if review_data['requests']:
            Request.save_requests(db, publish, page, review_data['requests'])

        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)

        page.expires = review_data['expires']
        page.last_modified = review_data['lastModified']
        page.last_review_uuid = review.uuid
        page.last_review = review
        page.last_review_date = review.completed_date
        page.violations_count = len(review_data['violations'])

        review.is_complete = True

        if not last_review:
            cache.increment_active_review_count(page.domain)
        else:
            for violation in last_review.violations:
                violation.review_is_active = False

            last_review.is_active = False

        Review.delete_old_reviews(db, config, page)

        search_provider.index_review(review)

        publish(dumps({'type': 'new-review', 'reviewId': str(review.uuid)}))
示例#2
0
    def save_review(
        cls,
        page_uuid,
        review_data,
        db,
        search_provider,
        fact_definitions,
        violation_definitions,
        cache,
        publish,
        config,
    ):
        from holmes.models import Page, Request

        page = Page.by_uuid(page_uuid, db)

        if page is None:
            return

        if review_data["requests"]:
            Request.save_requests(db, publish, page, review_data["requests"])

        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)

        page.expires = review_data["expires"]
        page.last_modified = review_data["lastModified"]
        page.last_review_uuid = review.uuid
        page.last_review = review
        page.last_review_date = review.completed_date
        page.violations_count = len(review_data["violations"])

        review.is_complete = True

        if not last_review:
            cache.increment_active_review_count(page.domain)
        else:
            for violation in last_review.violations:
                violation.review_is_active = False

            last_review.is_active = False

        Review.delete_old_reviews(db, config, page)

        search_provider.index_review(review)

        publish(dumps({"type": "new-review", "reviewId": str(review.uuid)}))
示例#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)
        }))
示例#4
0
    def save_review(cls, page_uuid, review_data, db, search_provider, fact_definitions, violation_definitions, cache, publish, config):
        from holmes.models import Page

        page = Page.by_uuid(page_uuid, db)

        if page is None:
            return

        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
            )

        page.expires = review_data['expires']
        page.last_modified = review_data['lastModified']
        page.last_review_uuid = review.uuid
        page.last_review = review
        page.last_review_date = review.completed_date
        page.violations_count = len(review_data['violations'])

        review.is_complete = True

        if not last_review:
            cache.increment_active_review_count(page.domain)
        else:
            for violation in last_review.violations:
                violation.review_is_active = False

            last_review.is_active = False

        Review.delete_old_reviews(db, config, page)

        search_provider.index_review(review)

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