Skip to content
This repository has been archived by the owner on Jun 12, 2018. It is now read-only.

Commit

Permalink
Use index queries for recent message list.
Browse files Browse the repository at this point in the history
  • Loading branch information
jerith committed Jul 29, 2015
1 parent 43d53d0 commit 1883e33
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
6 changes: 4 additions & 2 deletions go/conversation/view_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,15 @@ def get_sent_messages(start, stop):
return [add_event_status(m)
for m in conversation.sent_messages_in_cache(start, stop)]

# FIXME: This is horrible and inefficient.
# https://github.com/praekelt/vumi-go/issues/1321
# Paginator starts counting at 1 so 0 would also be invalid
inbound_message_paginator = Paginator(PagedMessageCache(
conversation.count_inbound_messages(),
min(conversation.count_inbound_messages(), 2000),
lambda start, stop: conversation.received_messages_in_cache(
start, stop)), 20)
outbound_message_paginator = Paginator(PagedMessageCache(
conversation.count_outbound_messages(),
min(conversation.count_outbound_messages(), 2000),
lambda start, stop: get_sent_messages(start, stop)), 20)

tag_context = {
Expand Down
42 changes: 20 additions & 22 deletions go/vumitools/conversation/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# -*- test-case-name: go.vumitools.conversation.tests.test_utils -*-
# -*- coding: utf-8 -*-

import warnings

from twisted.internet.defer import returnValue

from vumi.persist.model import Manager
Expand Down Expand Up @@ -258,21 +256,16 @@ def filter_and_scrub_messages(self, messages, include_sensitive, scrubber):
collection.append(scrubbed_msg)
return collection

def received_messages(self, start=0, limit=100, include_sensitive=False,
scrubber=None):
warnings.warn('received_messages() is deprecated. Please use '
'received_messages_in_cache() instead.',
category=DeprecationWarning)
return self.received_messages_in_cache(start, limit, include_sensitive,
scrubber)

@Manager.calls_manager
def received_messages_in_cache(self, start=0, limit=100,
include_sensitive=False, scrubber=None):
"""
Get a list of replies from the message store. The keys come from
the message store's cache.
FIXME: The name of this method and many of its parameters are lies.
See https://github.com/praekelt/vumi-go/issues/1321
:param int start:
Where to start in the result set.
:param int limit:
Expand All @@ -290,8 +283,13 @@ def received_messages_in_cache(self, start=0, limit=100,
# FIXME: limit actually means end, apparently.
scrubber = scrubber or (lambda msg: msg)

keys = yield self.qms.list_batch_recent_inbound_keys(self.batch.key)
keys = keys[start:limit]
# If we're not actually being asked for anything, return nothing.
if limit <= 0:
returnValue([])

page = yield self.qms.list_batch_inbound_messages(
self.batch.key, page_size=limit)
keys = [key for key, _timestamp, _addr in list(page)[start:limit]]

replies = yield self.collect_messages(
keys, self.qms.get_inbound_message, include_sensitive, scrubber)
Expand All @@ -301,21 +299,16 @@ def received_messages_in_cache(self, start=0, limit=100,
sorted(replies, key=lambda msg: msg['timestamp'],
reverse=True))

def sent_messages(self, start=0, limit=100, include_sensitive=False,
scrubber=None):
warnings.warn('sent_messages() is deprecated. Please use '
'sent_messages_in_cache() instead.',
category=DeprecationWarning)
return self.sent_messages_in_cache(start, limit, include_sensitive,
scrubber)

@Manager.calls_manager
def sent_messages_in_cache(self, start=0, limit=100,
include_sensitive=False, scrubber=None):
"""
Get a list of sent_messages from the message store. The keys come from
the message store's cache.
FIXME: The name of this method and many of its parameters are lies.
See https://github.com/praekelt/vumi-go/issues/1321
:param int start:
Where to start
:param int limit:
Expand All @@ -333,8 +326,13 @@ def sent_messages_in_cache(self, start=0, limit=100,
# FIXME: limit actually means end, apparently.
scrubber = scrubber or (lambda msg: msg)

keys = yield self.qms.list_batch_recent_outbound_keys(self.batch.key)
keys = keys[start:limit]
# If we're not actually being asked for anything, return nothing.
if limit <= 0:
returnValue([])

page = yield self.qms.list_batch_outbound_messages(
self.batch.key, page_size=limit)
keys = [key for key, _timestamp, _addr in list(page)[start:limit]]

sent_messages = yield self.collect_messages(
keys, self.qms.get_outbound_message, include_sensitive, scrubber)
Expand Down

0 comments on commit 1883e33

Please sign in to comment.