This repository has been archived by the owner on Jun 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into feature/issue-774-metrics-mw-expire-keys
- Loading branch information
Showing
17 changed files
with
410 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,23 @@ | ||
from go.vumitools.conversation.definition import ConversationDefinitionBase | ||
from go.apps.subscription.metrics import SubscribedMetric, UnsubscribedMetric | ||
|
||
|
||
class ConversationDefinition(ConversationDefinitionBase): | ||
conversation_type = u'subscription' | ||
|
||
def get_metrics(self): | ||
metrics = super(ConversationDefinition, self).get_metrics() | ||
|
||
campaign_names = sorted(set([ | ||
h['campaign_name'] | ||
for h in self.conv.get_config().get('handlers', [])])) | ||
|
||
metrics.extend([ | ||
SubscribedMetric(self.conv, campaign_name) | ||
for campaign_name in campaign_names]) | ||
|
||
metrics.extend([ | ||
UnsubscribedMetric(self.conv, campaign_name) | ||
for campaign_name in campaign_names]) | ||
|
||
return metrics |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from go.vumitools.metrics import ConversationMetric | ||
|
||
|
||
class SubscriptionMetric(ConversationMetric): | ||
CONTACT_LOOKUP_KEY = None | ||
|
||
def __init__(self, conv, campaign_name): | ||
self.campaign_name = campaign_name | ||
metric_name = self.make_metric_name(campaign_name) | ||
super(SubscriptionMetric, self).__init__(conv, metric_name) | ||
|
||
@classmethod | ||
def make_metric_name(cls, campaign_name): | ||
return "%s.%s" % (campaign_name, cls.METRIC_NAME) | ||
|
||
def get_value(self, user_api): | ||
contacts = user_api.contact_store.contacts | ||
search = contacts.raw_search( | ||
"subscription-%s:%s" % | ||
(self.campaign_name, self.CONTACT_LOOKUP_KEY)) | ||
|
||
return search.get_count() | ||
|
||
|
||
class SubscribedMetric(SubscriptionMetric): | ||
METRIC_NAME = 'subscribed' | ||
CONTACT_LOOKUP_KEY = 'subscribed' | ||
|
||
|
||
class UnsubscribedMetric(SubscriptionMetric): | ||
METRIC_NAME = 'unsubscribed' | ||
CONTACT_LOOKUP_KEY = 'unsubscribed' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from go.base.tests.utils import VumiGoDjangoTestCase | ||
from go.vumitools.metrics import MessagesReceivedMetric, MessagesSentMetric | ||
from go.apps.subscription.definition import ConversationDefinition | ||
from go.apps.subscription.metrics import SubscribedMetric, UnsubscribedMetric | ||
|
||
|
||
class TestSubscriptionConversationDefinition(VumiGoDjangoTestCase): | ||
def setUp(self): | ||
super(TestSubscriptionConversationDefinition, self).setUp() | ||
|
||
self.setup_user_api() | ||
self.conv = self.create_conversation( | ||
conversation_type=u'subscription', | ||
config={ | ||
'handlers': [ | ||
{'campaign_name': 'campaign-1'}, | ||
{'campaign_name': 'campaign-2'}] | ||
}) | ||
self.conv_def = ConversationDefinition(self.conv) | ||
|
||
def test_metrics_retrieval(self): | ||
[m1, m2, m3, m4, m5, m6] = self.conv_def.get_metrics() | ||
|
||
metric_name_prefix = ( | ||
"go.campaigns.%s.conversations.%s" | ||
% (self.conv.user_account.key, self.conv.key)) | ||
|
||
self.assertEqual( | ||
m1.get_full_name(), | ||
'%s.messages_sent' % metric_name_prefix) | ||
self.assertTrue(isinstance(m1, MessagesSentMetric)) | ||
|
||
self.assertEqual( | ||
m2.get_full_name(), | ||
'%s.messages_received' % metric_name_prefix) | ||
self.assertTrue(isinstance(m2, MessagesReceivedMetric)) | ||
|
||
self.assertEqual( | ||
m3.get_full_name(), | ||
'%s.campaign-1.subscribed' % metric_name_prefix) | ||
self.assertTrue(isinstance(m3, SubscribedMetric)) | ||
|
||
self.assertEqual( | ||
m4.get_full_name(), | ||
'%s.campaign-2.subscribed' % metric_name_prefix) | ||
self.assertTrue(isinstance(m4, SubscribedMetric)) | ||
|
||
self.assertEqual( | ||
m5.get_full_name(), | ||
'%s.campaign-1.unsubscribed' % metric_name_prefix) | ||
self.assertTrue(isinstance(m5, UnsubscribedMetric)) | ||
|
||
self.assertEqual( | ||
m6.get_full_name(), | ||
'%s.campaign-2.unsubscribed' % metric_name_prefix) | ||
self.assertTrue(isinstance(m6, UnsubscribedMetric)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from twisted.internet.defer import inlineCallbacks | ||
from go.apps.subscription.metrics import SubscriptionMetric | ||
from go.vumitools.tests.utils import TxMetricTestBase | ||
from go.vumitools.contact import ContactStore | ||
|
||
|
||
class ToySubscriptionMetric(SubscriptionMetric): | ||
METRIC_NAME = 'toy-subscription-metric' | ||
CONTACT_LOOKUP_KEY = 'toy-subscription' | ||
|
||
|
||
class TestSubscriptionMetric(TxMetricTestBase): | ||
@inlineCallbacks | ||
def setUp(self): | ||
yield super(TestSubscriptionMetric, self).setUp() | ||
|
||
self.conv = yield self.create_conversation( | ||
conversation_type=u'some_conversation') | ||
|
||
self.contact_store = ContactStore.from_user_account(self.user) | ||
yield self.contact_store.contacts.enable_search() | ||
|
||
self.contact1 = yield self.contact_store.new_contact( | ||
name=u'contact-1', | ||
msisdn=u'+27831234567') | ||
|
||
self.contact2 = yield self.contact_store.new_contact( | ||
name=u'contact-2', | ||
msisdn=u'+27831234568') | ||
|
||
yield self.contact1.save() | ||
yield self.contact2.save() | ||
|
||
self.metric = ToySubscriptionMetric(self.conv, 'campaign-1') | ||
|
||
def test_name_construction(self): | ||
self.assertEqual( | ||
self.metric.get_full_name(), | ||
"go.campaigns.test-0-user.conversations.%s." | ||
"campaign-1.toy-subscription-metric" % self.conv.key) | ||
|
||
@inlineCallbacks | ||
def test_value_retrieval(self): | ||
self.assertEqual( | ||
(yield self.metric.get_value(self.user_api)), 0) | ||
|
||
self.contact1.subscription['campaign-1'] = u'toy-subscription' | ||
self.contact2.subscription['campaign-1'] = u'toy-subscription' | ||
|
||
yield self.contact1.save() | ||
yield self.contact2.save() | ||
|
||
self.assertEqual( | ||
(yield self.metric.get_value(self.user_api)), 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.