Skip to content
This repository has been archived by the owner on Mar 27, 2023. It is now read-only.

Commit

Permalink
Materialize number of requests per statys in the last day (re #36)
Browse files Browse the repository at this point in the history
  • Loading branch information
scorphus committed Apr 28, 2014
1 parent 5b3737e commit cdc59c1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
2 changes: 2 additions & 0 deletions holmes/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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')

Expand Down
11 changes: 5 additions & 6 deletions holmes/handlers/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
14 changes: 14 additions & 0 deletions holmes/material.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import sys
import inspect
import datetime
from uuid import uuid4
from functools import partial
from collections import defaultdict
Expand Down Expand Up @@ -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)
Expand All @@ -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):
Expand Down

0 comments on commit cdc59c1

Please sign in to comment.