def get(self, domain_name): domain = Domain.get_domain_by_name(domain_name, self.db) if not domain: self.set_status(404, 'Domain %s not found' % domain_name) return violation_defs = self.application.violation_definitions grouped_violations = self.girl.get('violation_count_by_category_for_domains') total = 0 violations = [] for item in grouped_violations.get(domain.id, []): key_name, key_category_id, count = item['key_name'], item['category_id'], item['violation_count'] violations.append({ 'categoryId': key_category_id, 'categoryName': violation_defs[key_name]['category'], 'count': count }) total += count result = { "domainId": domain.id, 'domainName': domain.name, 'domainURL': domain.url, 'total': total, 'violations': violations } self.write_json(result)
def get(self, domain_name): term = self.get_argument('term', None) current_page = int(self.get_argument('current_page', 1)) page_size = int(self.get_argument('page_size', 10)) domain = Domain.get_domain_by_name(domain_name, self.db) if not domain: self.set_status(404, self._('Domain %s not found') % domain_name) return reviews = yield self.application.search_provider.get_domain_active_reviews( domain=domain, current_page=current_page, page_size=page_size, page_filter=term, ) if 'error' in reviews: self.set_status(reviews['error']['status_code'], reviews['error']['reason']) self.finish() return if 'reviewsCount' not in reviews: if not term: reviews[ 'reviewsCount'] = yield self.cache.get_active_review_count( domain) else: reviews['reviewsCount'] = None self.write_json(reviews) self.finish()
def get(self, domain_name, key_category_id): domain = Domain.get_domain_by_name(domain_name, self.db) if not domain: self.set_status(404, 'Domain %s not found' % domain_name) return violation_defs = self.application.violation_definitions top_violations = yield self.cache.get_top_in_category_for_domain( domain, key_category_id, self.application.config.get('TOP_CATEGORY_VIOLATIONS_LIMIT') ) violations = [] for key_name, count in top_violations: violations.append({ 'title': violation_defs[key_name]['title'], 'count': count }) result = { "domainId": domain.id, 'domainName': domain.name, 'domainURL': domain.url, 'categoryId': key_category_id, 'violations': violations } self.write_json(result)
def get_count(self, key, domain_name, expiration, get_count_method): cache_key = '%s-%s' % (self.get_domain_name(domain_name), key) count = self.redis.get(cache_key) if count is not None: return int(count) domain = domain_name if domain and not isinstance(domain, Domain): domain = Domain.get_domain_by_name(domain_name, self.db) if domain is None: count = Page.get_page_count(self.db) else: count = get_count_method(domain) cache_key = '%s-%s' % (self.get_domain_name(domain), key) self.redis.setex( cache_key, expiration, value=int(count) ) return int(count)
def get(self, domain_name): domain = Domain.get_domain_by_name(domain_name, self.db) if not domain: self.set_status(404, self._('Domain %s not found') % domain_name) return prefs = DomainsViolationsPrefs.get_domains_violations_prefs_by_domain( self.db, domain.name ) violation_defs = self.application.violation_definitions result = [] for pref in prefs: key = violation_defs.get(pref.get('key')) if key is None: continue result.append({ 'key': pref.get('key'), 'title': key.get('default_value_description', None), 'category': key.get('category', None), 'value': pref.get('value'), 'default_value': key.get('default_value', None), 'unit': key.get('unit', None) }) self.write_json(result)
def post(self, domain_name): domain = Domain.get_domain_by_name(domain_name, self.db) if not domain: self.set_status(404, 'Domain %s not found' % domain_name) return domain.is_active = not domain.is_active
def test_can_set_domain_to_inactive(self): domain = DomainFactory.create(url="http://www.domain.com", name="domain.com", is_active=True) response = yield self.authenticated_fetch( '/domains/%s/change-status/' % domain.name, method='POST', body='') expect(response.code).to_equal(200) domain_from_db = Domain.get_domain_by_name(domain.name, self.db) expect(domain_from_db.is_active).to_be_false()
def post(self, domain_name): domain = Domain.get_domain_by_name(domain_name, self.db) if not domain: self.set_status(404, self._('Domain %s not found') % domain_name) return domain.is_active = not domain.is_active if not domain.is_active: yield self.cache.delete_limit_usage_by_domain(domain.url)
def test_can_set_domain_to_active(self): domain = DomainFactory.create(url="http://www.domain.com", name="domain.com", is_active=False) response = yield self.http_client.fetch( self.get_url(r'/domains/%s/change-status/' % domain.name), method='POST', body='' ) expect(response.code).to_equal(200) domain_from_db = Domain.get_domain_by_name(domain.name, self.db) expect(domain_from_db.is_active).to_be_true()
def test_can_set_domain_to_inactive(self): domain = DomainFactory.create(url="http://www.domain.com", name="domain.com", is_active=True) response = yield self.authenticated_fetch( '/domains/%s/change-status/' % domain.name, method='POST', body='' ) expect(response.code).to_equal(200) domain_from_db = Domain.get_domain_by_name(domain.name, self.db) expect(domain_from_db.is_active).to_be_false()
def handle(has_key): domain = domain_name if domain and not isinstance(domain, Domain): domain = Domain.get_domain_by_name(domain_name, self.db) if has_key: self.redis.incrby(key, increment, callback=callback) else: if domain is None: value = Page.get_page_count(self.db) + increment - 1 else: value = get_default_method(domain) + increment - 1 self.redis.set(key, value, callback=callback)
def get(self, key_name): current_page = int(self.get_argument('current_page', 1)) page_size = int(self.get_argument('page_size', 10)) domain_filter = self.get_argument('domain_filter', None) page_filter = self.get_argument('page_filter', None) domain = None if domain_filter is not None: domain = Domain.get_domain_by_name(domain_filter, self.db) if not domain: self.set_status(404, self._('Domain %s not found') % domain_filter) self.finish() return violations = self.application.violation_definitions if key_name not in violations: self.set_status(404, self._('Invalid violation key %s') % key_name) self.finish() return violation_title = violations[key_name]['title'] key_id = violations[key_name]['key'].id violation = yield self.application.search_provider.get_by_violation_key_name( key_id=key_id, current_page=current_page, page_size=page_size, domain=domain, page_filter=page_filter, ) if 'error' in violation: self.set_status(violation['error']['status_code'], violation['error']['reason']) self.finish() return if 'reviewsCount' not in violation: if not domain and not page_filter: violation['reviewsCount'] = Review.count_by_violation_key_name( self.db, key_id) else: violation['reviewsCount'] = None violation['title'] = violation_title self.write_json(violation) self.finish()
def get(self, domain_name): domain = Domain.get_domain_by_name(domain_name, self.db) if not domain: self.set_status(404, self._('Domain %s not found') % domain_name) return result = self.girl.get('domains_details') or [] data = next((l for l in result if l['name'] == domain_name), None) if not data: self.set_status(404, self._('Domain %s not found') % domain_name) return page_count = data.get('pageCount', 0) review_count = data.get('reviewCount', 0) violation_count = data.get('violationCount', 0) error_percentage = data.get('errorPercentage', 0) response_time_avg = data.get('averageResponseTime', 0) review_percentage = data.get('reviewPercentage', 0) domain_json = { "id": domain.id, "name": domain.name, "url": domain.url, "pageCount": page_count, "reviewCount": review_count, "violationCount": violation_count, "reviewPercentage": review_percentage, "is_active": domain.is_active, "errorPercentage": error_percentage, "averageResponseTime": response_time_avg, "homepageId": "", "homepageReviewId": "", } homepage = domain.get_homepage(self.db) if homepage: if homepage.uuid: domain_json["homepageId"] = str(homepage.uuid) if homepage.last_review_uuid: domain_json["homepageReviewId"] = str( homepage.last_review_uuid) self.write_json(domain_json)
def get(self, domain_name): domain = Domain.get_domain_by_name(domain_name, self.db) if not domain: self.set_status(404, 'Domain %s not found' % domain_name) return violations_per_day = domain.get_violations_per_day(self.db) domain_json = { "id": domain.id, "name": domain.name, "url": domain.url, "violations": violations_per_day } self.write_json(domain_json)
def get(self, domain_name): domain = Domain.get_domain_by_name(domain_name, self.db) if not domain: self.set_status(404, self._('Domain %s not found') % domain_name) return violations_per_day = domain.get_violations_per_day(self.db) domain_json = { "id": domain.id, "name": domain.name, "url": domain.url, "violations": violations_per_day } self.write_json(domain_json)
def increment_count(self, key, domain_name, get_default_method, increment=1): key = '%s-%s' % (self.get_domain_name(domain_name), key) has_key = self.has_key(key) domain = domain_name if domain and not isinstance(domain, Domain): domain = Domain.get_domain_by_name(domain_name, self.db) if has_key: self.redis.incrby(key, increment) else: if domain is None: value = Page.get_page_count(self.db) + increment - 1 else: value = get_default_method(domain) + increment - 1 self.redis.set(key, value)
def get(self, domain_name): domain = Domain.get_domain_by_name(domain_name, self.db) if not domain: self.set_status(404, 'Domain %s not found' % domain_name) return page_count = yield self.cache.get_page_count(domain) review_count = yield self.cache.get_active_review_count(domain) violation_count = yield self.cache.get_violation_count(domain) bad_request_count = yield self.cache.get_bad_request_count(domain) good_request_count = yield self.cache.get_good_request_count(domain) total_request_count = good_request_count + bad_request_count if total_request_count > 0: error_percentage = round(float(bad_request_count) / total_request_count * 100, 2) else: error_percentage = 0 response_time_avg = yield self.cache.get_response_time_avg(domain) status_code_info = Request.get_status_code_info(domain_name, self.db) if page_count > 0: review_percentage = round(float(review_count) / page_count * 100, 2) else: review_percentage = 0 domain_json = { "id": domain.id, "name": domain.name, "url": domain.url, "pageCount": page_count, "reviewCount": review_count, "violationCount": violation_count, "reviewPercentage": review_percentage, "is_active": domain.is_active, "statusCodeInfo": status_code_info, "errorPercentage": error_percentage, "averageResponseTime": response_time_avg, } self.write_json(domain_json)
def get(self, key_name): current_page = int(self.get_argument("current_page", 1)) page_size = int(self.get_argument("page_size", 10)) domain_filter = self.get_argument("domain_filter", None) page_filter = self.get_argument("page_filter", None) domain = None if domain_filter is not None: domain = Domain.get_domain_by_name(domain_filter, self.db) if not domain: self.set_status(404, self._("Domain %s not found") % domain_filter) self.finish() return violations = self.application.violation_definitions if key_name not in violations: self.set_status(404, self._("Invalid violation key %s") % key_name) self.finish() return violation_title = violations[key_name]["title"] key_id = violations[key_name]["key"].id violation = yield self.application.search_provider.get_by_violation_key_name( key_id=key_id, current_page=current_page, page_size=page_size, domain=domain, page_filter=page_filter ) if "error" in violation: self.set_status(violation["error"]["status_code"], violation["error"]["reason"]) self.finish() return if "reviewsCount" not in violation: if not domain and not page_filter: violation["reviewsCount"] = Review.count_by_violation_key_name(self.db, key_id) else: violation["reviewsCount"] = None violation["title"] = violation_title self.write_json(violation) self.finish()
def post(self, domain_name): if not self.validate_superuser(): return domain = Domain.get_domain_by_name(domain_name, self.db) if not domain: self.set_status(404, self._('Domain %s not found') % domain_name) return post_data = loads(self.request.body) DomainsViolationsPrefs.update_by_domain( self.db, self.cache, domain, post_data ) self.write_json({ 'reason': self._('Preferences successfully saved!'), 'description': self._('Preferences successfully saved!') })
def get(self, domain_name, key_category_id): domain = Domain.get_domain_by_name(domain_name, self.db) if not domain: self.set_status(404, self._('Domain %s not found') % domain_name) return key_category = KeysCategory.get_by_id(self.db, key_category_id) if not key_category: self.set_status( 404, self._('Key category %s not found') % key_category_id) return violation_defs = self.application.violation_definitions top_violations = self.girl.get( 'top_violations_in_category_for_domains') or {} violations = [] for top_violation in top_violations.get(domain_name, {}).get(key_category.id, []): violations.append({ 'title': self._(violation_defs[top_violation['key_name']]['title']), 'count': top_violation['count'], 'key': top_violation['key_name'], }) result = { "domainId": domain.id, 'domainName': domain.name, 'domainURL': domain.url, 'categoryId': key_category_id, 'violations': violations } self.write_json(result)
def get_count(self, key, domain_name, expiration, get_count_method): cache_key = '%s-%s' % (self.get_domain_name(domain_name), key) count = self.redis.get(cache_key) if count is not None: return int(count) domain = domain_name if domain and not isinstance(domain, Domain): domain = Domain.get_domain_by_name(domain_name, self.db) if domain is None: count = Page.get_page_count(self.db) else: count = get_count_method(domain) cache_key = '%s-%s' % (self.get_domain_name(domain), key) self.redis.setex(cache_key, expiration, value=int(count)) return int(count)
def get(self, domain_name): term = self.get_argument('term', None) current_page = int(self.get_argument('current_page', 1)) page_size = int(self.get_argument('page_size', 10)) domain = Domain.get_domain_by_name(domain_name, self.db) if not domain: self.set_status(404, 'Domain %s not found' % domain_name) return reviews = domain.get_active_reviews( self.db, url_starts_with=term, current_page=current_page, page_size=page_size ) if term: review_count = len(reviews) else: review_count = yield self.cache.get_active_review_count(domain) result = { 'reviewCount': review_count, 'pages': [], } for page in reviews: result['pages'].append({ "url": page.url, "uuid": str(page.uuid), "violationCount": page.violations_count, "completedAt": page.last_review_date, "reviewId": str(page.last_review_uuid) }) self.write_json(result)
def get(self, domain_name): domain = Domain.get_domain_by_name(domain_name, self.db) if not domain: self.set_status(404, self._('Domain %s not found') % domain_name) return violation_defs = self.application.violation_definitions grouped_violations = self.girl.get( 'violation_count_by_category_for_domains') or {} total = 0 violations = [] for item in grouped_violations.get(domain.id, []): key_name, key_category_id, count = item['key_name'], item[ 'category_id'], item['violation_count'] violations.append({ 'categoryId': key_category_id, 'categoryName': self._(violation_defs[key_name]['category']), 'count': count }) total += count result = { "domainId": domain.id, 'domainName': domain.name, 'domainURL': domain.url, 'total': total, 'violations': violations } self.write_json(result)
def get_domain(self, domain_name): domain = domain_name if domain and not isinstance(domain, Domain): domain = Domain.get_domain_by_name(domain_name, self.db) return domain
def test_can_get_domain_by_name(self): domain = DomainFactory.create() loaded_domain = Domain.get_domain_by_name(domain.name, self.db) expect(loaded_domain.id).to_equal(domain.id)
def test_invalid_domain_returns_None(self): domain_name = 'domain-details.com' domain = Domain.get_domain_by_name(domain_name, self.db) expect(domain).to_be_null()