From cdc59c138fc7cd8a3e77c4ec03510cb0013b6d4d Mon Sep 17 00:00:00 2001 From: Pablo Aguiar Date: Mon, 28 Apr 2014 19:44:50 -0300 Subject: [PATCH] Materialize number of requests per statys in the last day (re #36) --- holmes/config/__init__.py | 2 ++ holmes/handlers/request.py | 11 +++++------ holmes/material.py | 14 ++++++++++++++ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/holmes/config/__init__.py b/holmes/config/__init__.py index ac350eb..8f6aca6 100644 --- a/holmes/config/__init__.py +++ b/holmes/config/__init__.py @@ -122,6 +122,7 @@ 'blacklist_domain_count': 10 * MINUTE + 1, 'most_common_violations': HOUR + 7, 'old_requests': (lambda config: config.get('DAYS_TO_KEEP_REQUESTS') * DAY - 1), + 'requests_in_last_day_count': HOUR + 13, } Config.define('MATERIALS_EXPIRATION_IN_SECONDS', materials_expiration_in_seconds, 'Expire times for materials', 'material') @@ -132,6 +133,7 @@ 'blacklist_domain_count': 2 * materials_expiration_in_seconds['blacklist_domain_count'], 'most_common_violations': 2 * materials_expiration_in_seconds['most_common_violations'], 'old_requests': (lambda config: 2 * materials_expiration_in_seconds['old_requests'](config)), + 'requests_in_last_day_count': 2 * materials_expiration_in_seconds['requests_in_last_day_count'], } Config.define('MATERIALS_GRACE_PERIOD_IN_SECONDS', materials_grace_period_in_seconds, 'Grace period times for materials', 'material') diff --git a/holmes/handlers/request.py b/holmes/handlers/request.py index 31caa01..880f02b 100644 --- a/holmes/handlers/request.py +++ b/holmes/handlers/request.py @@ -85,15 +85,14 @@ def get(self): class RequestsInLastDayHandler(BaseHandler): @coroutine def get(self): - from_date = datetime.datetime.utcnow() - datetime.timedelta(days=1) - requests = Request.get_requests_count_by_status_in_period_of_days(self.db, from_date=from_date) + requests = self.girl.get('requests_in_last_day') result = [] - for request in requests: + for status_code, count in requests: result.append({ - 'statusCode': request.status_code, - 'statusCodeTitle': get_status_code_title(request.status_code), # FIXME: is it code or title?? - 'count': request.count, + 'statusCode': status_code, + 'statusCodeTitle': get_status_code_title(status_code), # FIXME: is it code or title?? + 'count': count, }) self.write_json(result) diff --git a/holmes/material.py b/holmes/material.py index 4793d2b..2192435 100644 --- a/holmes/material.py +++ b/holmes/material.py @@ -3,6 +3,7 @@ import sys import inspect +import datetime from uuid import uuid4 from functools import partial from collections import defaultdict @@ -62,6 +63,14 @@ def configure_materials(girl, db, config): get_func_or_value(config.MATERIALS_GRACE_PERIOD_IN_SECONDS['old_requests'], config) ) + girl.add_material( + 'requests_in_last_day', + partial(MaterialConveyor.get_requests_in_last_day, db), + config.MATERIALS_EXPIRATION_IN_SECONDS['requests_in_last_day_count'], + config.MATERIALS_GRACE_PERIOD_IN_SECONDS['requests_in_last_day_count'] + ) + + def get_func_or_value(value, config): if inspect.ismethod(value) or inspect.isfunction(value): return value(config) @@ -80,6 +89,11 @@ def get_blacklist_domain_count(cls, db): blacklist = sorted(ungrouped.items(), key=lambda xz: -xz[1]) return [dict(zip(('domain', 'count'), x)) for x in blacklist] + @classmethod + def get_requests_in_last_day(cls, db): + from_date = datetime.datetime.utcnow() - datetime.timedelta(days=1) + return Request.get_requests_count_by_status_in_period_of_days(db, from_date=from_date) + class MaterialWorker(BaseCLI): def initialize(self):