Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
44 lines (34 sloc) 1.26 KB
import json
import zlib
import redis
from django.conf import settings
from django.http import HttpResponseBadRequest, HttpResponse
from django.conf.urls.defaults import patterns
from django.core.management import execute_from_command_line
from pymongo import Connection
from pymongo.objectid import ObjectId
if not settings.configured:
settings.configure(
DEBUG = True,
ROOT_URLCONF = 'web',
TEMPLATE_DIRS = ['.'],
MONGO_SERVERS = [('192.168.1.94', 27017), ('192.168.1.95', 27017)],
REDIS_SERVERS = ['192.168.1.94', '192.168.1.95']
)
urlpatterns = patterns('',
(r'^$', 'web.search'),
)
def search(request):
request_id = request.GET.get('request_id')
if not request_id:
return HttpResponseBadRequest()
shard = zlib.crc32(request_id) % len(settings.MONGO_SERVERS)
messages = Connection(*settings.MONGO_SERVERS[shard]).logging.messages
index = redis.Redis(settings.REDIS_SERVERS[shard])
messages_ids = index.lrange(request_id, 0, -1)
msgs_items = list(messages.find({'_id': { '$in' : map(ObjectId, messages_ids) }}))
for m in msgs_items:
del m['_id']
return HttpResponse(json.dumps(msgs_items), mimetype='application/json')
if __name__ == '__main__':
execute_from_command_line()