def test_campaign_payments_more(self): payment_percentage_cutoff = 0.5 cpv, cpc = 10, 20 timestamp = 3600 # Add campaign cmp_doc = {"campaign_id": "campaign_id", "time_start": 0, "time_end": timestamp + 100, "max_cpc": cpc, "max_cpm": cpv, "budget": 1000, "filters": {'require': {}, 'exclude': {}}} yield db_utils.update_campaign(cmp_doc) # Add 3 banners for this campaign for i in range(3): yield db_utils.update_banner({'banner_id': 'banner_id' + str(i), 'campaign_id': cmp_doc['campaign_id']}) # Add events for users yield db_utils.update_event({ 'campaign_id': cmp_doc['campaign_id'], "event_id": "event2_user_id1", "event_type": stats_consts.EVENT_TYPE_VIEW, "timestamp": timestamp + 1, "user_id": 'user_id1', "banner_id": 'banner_id1', "event_value": 0.2, "our_keywords": {}, "their_keywords": {}, "human_score": 1.0}) yield db_utils.update_event({ 'campaign_id': cmp_doc['campaign_id'], "event_id": "event2_user_id2", "event_type": stats_consts.EVENT_TYPE_CONVERSION, "timestamp": timestamp + 2, "user_id": 'user_id2', "banner_id": 'banner_id1', "event_value": 0.5, "our_keywords": {}, "their_keywords": {}, "human_score": 1.0}) yield stats_legacy.calculate_events_payments(cmp_doc, timestamp, payment_percentage_cutoff=payment_percentage_cutoff) # Check user values user2_value_doc = yield db_utils.get_user_value_in_campaign(cmp_doc['campaign_id'], "user_id2") self.assertEqual(user2_value_doc['payment'], 10) self.assertEqual(user2_value_doc['human_score'], 1) # User scores should be empty. _iter = yield db_utils.get_sorted_user_score_iter(cmp_doc['campaign_id'], timestamp, limit=1) user_score_doc = yield _iter.next() self.assertIsNone(user_score_doc)
def test_non_payable_events(self): """ Test for non payable events. """ timestamp = 3600 # Add campaign with one banner cmp_doc = { "campaign_id": "campaign_id", "time_start": 0, "time_end": 3600 + 100, "max_cpc": 5, "max_cpm": 5000, "budget": 10, "filters": { 'require': {}, 'exclude': {} } } yield db_utils.update_campaign(cmp_doc) yield db_utils.update_banner({ 'banner_id': 'banner_id', 'campaign_id': 'campaign_id' }) # Add 5 events event = { "event_id": "event_id", # This will be modified "event_type": "click", # This will be modified "timestamp": timestamp + 2, "user_id": 'test_user', "banner_id": 'banner_id', "campaign_id": "campaign_id", "event_value": 5, "our_keywords": {}, "their_keywords": {}, "human_score": 1.0 } for i in range(5): event['event_id'] = 'event_id_' + str(i) event['event_type'] = str(random.randint(1000, 1001)) yield db_utils.update_event(event) # Calculate payments yield stats_default.calculate_events_payments(cmp_doc, timestamp) # Check payments _iter = yield db_utils.get_payments_iter(timestamp) while True: payment_doc = yield _iter.next() if not payment_doc: break # Check payment reason is accepted self.assertEqual(0, payment_doc['reason']) # But payment is 0 self.assertEqual(0, payment_doc['payment'])
def test_budget(self): """ Test for hourly budget constraint. Total payments can't be higher than the campaign budget. """ timestamp = 3600 # Add campaign with one banner cmp_doc = { "campaign_id": "campaign_id", "time_start": 0, "time_end": 3600 + 100, "max_cpc": 5, "max_cpm": 5000, "budget": 10, "filters": { 'require': {}, 'exclude': {} } } yield db_utils.update_campaign(cmp_doc) yield db_utils.update_banner({ 'banner_id': 'banner_id', 'campaign_id': 'campaign_id' }) # Add all kind of paid events event = { "event_id": "event_id", # This will be modified "event_type": "click", # This will be modified "timestamp": timestamp + 2, "user_id": 'test_user', "banner_id": 'banner_id', "campaign_id": "campaign_id", "event_value": 5, "our_keywords": {}, "their_keywords": {}, "human_score": 1.0 } for i, event_type in enumerate(stats_consts.PAID_EVENT_TYPES): event['event_id'] = 'event_id_' + str(i) event['event_type'] = event_type yield db_utils.update_event(event) # Calculate payments yield stats_default.calculate_events_payments(cmp_doc, timestamp) # Check payments total_payments = 0 _iter = yield db_utils.get_payments_iter(timestamp) while True: payment_doc = yield _iter.next() if not payment_doc: break total_payments += payment_doc['payment'] self.assertLessEqual(total_payments, cmp_doc['budget'])
def test_event(self): # Test event add. for i in range(300): yield db_utils.update_event({ "event_id": str(i), "event_type": stats_consts.EVENT_TYPE_CLICK, "timestamp": 0, "user_id": str(i % 20), "banner_id": '1', "campaign_id": "campaign_id", "event_value": 10, "our_keywords": {}, "human_score": 1 }) # Test event select. counter = 0 event_iter = yield db_utils.get_events_per_user_iter( campaign_id="campaign_id", timestamp=0, uid=u'0') while True: event_doc = yield event_iter.next() if not event_doc: break self.assertEqual(event_doc['user_id'], u'0') self.assertEqual(event_doc['timestamp'], 0) self.assertEqual(event_doc['campaign_id'], "campaign_id") counter += 1 self.assertEqual(counter, 15) unique_uids = yield db_utils.get_distinct_users_from_events( "campaign_id", timestamp=0) self.assertEqual(set(unique_uids), set([str(x) for x in range(20)])) # Test event deletion. for i in range(300): yield db_utils.delete_event(str(i)) counter = 0 event_iter = yield db_utils.get_events_per_user_iter("campaign_id", timestamp=0, uid=u'0') while True: event_doc = yield event_iter.next() if not event_doc: break counter += 1 self.assertEqual(counter, 0)
def add_event(eventobj): """ Insert (create or update) event object into the database. Update keywords and view statistics (for user value method) :param eventobj: Event object :return: """ logger = logging.getLogger(__name__) event_doc = eventobj.to_json() event_doc['campaign_id'] = 'not_found' banner_doc = yield db_utils.get_banner(eventobj.banner_id) if not banner_doc: yield logger.warning("Event update: No banner found.") else: campaign_doc = yield db_utils.get_campaign(banner_doc['campaign_id']) if not campaign_doc: yield logger.warning("Event update: No campaign found.") else: event_doc['campaign_id'] = campaign_doc['campaign_id'] inserted = yield db_utils.update_event(event_doc) if stats_consts.CALCULATION_METHOD == 'user_value': # Update global keywords cache and user keyword stats. view_view_keywords = [] for user_keyword, user_val in eventobj.our_keywords.items(): view_view_keywords.append( common_utils.genkey(user_keyword, user_val)) yield stats_legacy.add_view_keywords(eventobj.user_id, view_view_keywords) defer.returnValue(inserted)
def test_campaign_payments(self): payment_percentage_cutoff = 0.5 cpv, cpc = 10, 20 timestamp = 3600 cmp_doc = {"campaign_id": "campaign_id", "time_start": 0, "time_end": 3600 + 100, "max_cpc": cpc, "max_cpm": cpv, "budget": 100000, "filters": {'require': {}, 'exclude': {}}} yield db_utils.update_campaign(cmp_doc) # Add 3 banners for this campaign for i in range(3): yield db_utils.update_banner({'banner_id': 'banner_id' + str(i), 'campaign_id': cmp_doc['campaign_id']}) # Add events for users yield db_utils.update_event({ 'campaign_id': cmp_doc['campaign_id'], "event_id": "event1_user_id1", "event_type": stats_consts.EVENT_TYPE_CLICK, "timestamp": timestamp + 2, "user_id": 'user_id1', "banner_id": 'banner_id1', "event_value": 0.1, "our_keywords": {}, "their_keywords": {}, "human_score": 1}) yield stats_legacy.calculate_events_payments(cmp_doc, timestamp, payment_percentage_cutoff=payment_percentage_cutoff) # Check user values user_value_doc = yield db_utils.get_user_value_in_campaign(cmp_doc['campaign_id'], "user_id1") self.assertEqual(user_value_doc['payment'], 20) self.assertEqual(user_value_doc['human_score'], 1) yield db_utils.update_event({ 'campaign_id': cmp_doc['campaign_id'], "event_id": "event2_user_id1", "event_type": stats_consts.EVENT_TYPE_VIEW, "timestamp": timestamp + 2, "user_id": 'user_id1', "banner_id": 'banner_id1', "event_value": 0.2, "our_keywords": {}, "their_keywords": {}, "human_score": 1}) yield db_utils.update_event({ 'campaign_id': cmp_doc['campaign_id'], "event_id": "event2_user_id2", "event_type": stats_consts.EVENT_TYPE_CONVERSION, "timestamp": timestamp + 2, "user_id": 'user_id2', "banner_id": 'banner_id1', "event_value": 0.5, "our_keywords": {}, "their_keywords": {}, "human_score": 1}) yield stats_legacy.calculate_events_payments(cmp_doc, 3600, payment_percentage_cutoff=payment_percentage_cutoff) # Check user values user2_value_doc = yield db_utils.get_user_value_in_campaign("campaign_id", "user_id2") self.assertEqual(user2_value_doc['payment'], 10) self.assertEqual(user2_value_doc['human_score'], 1) # Mock total use score yield db_utils.update_event({ 'campaign_id': cmp_doc['campaign_id'], "event_id": "event2_user_id3", "event_type": stats_consts.EVENT_TYPE_CONVERSION, "timestamp": timestamp + 2, "user_id": 'user_id3', "banner_id": 'banner_id1', "event_value": 0.5, "our_keywords": {}, "their_keywords": {}, "human_score": 1.0}) with patch('adpay.stats.legacy.get_total_user_score', MagicMock(return_value=0)): yield stats_legacy.calculate_events_payments(cmp_doc, 3600, payment_percentage_cutoff=payment_percentage_cutoff) # Check user values user2_value_doc = yield db_utils.get_user_value_in_campaign("campaign_id", "user_id3") self.assertEqual(user2_value_doc['payment'], 10) self.assertEqual(user2_value_doc['human_score'], 1)
def test_campaign_payments_more(self): cpv, cpc = 10, 20 timestamp = 3600 cmp_doc = { "campaign_id": "campaign_id", "time_start": 1234, "time_end": 3456, "max_cpc": cpc, "max_cpm": cpv, "budget": 1000, "filters": { 'require': {}, 'exclude': {} } } yield db_utils.update_campaign(cmp_doc) yield db_utils.update_banner({ 'banner_id': 'banner_id1', 'campaign_id': 'campaign_id' }) yield db_utils.update_banner({ 'banner_id': 'banner_id2', 'campaign_id': 'campaign_id' }) yield db_utils.update_banner({ 'banner_id': 'banner_id3', 'campaign_id': 'campaign_id' }) # Add events for users yield db_utils.update_event({ "event_id": "event2_user_id1", "event_type": stats_consts.EVENT_TYPE_VIEW, "timestamp": 3600, "user_id": 'user_id1', "banner_id": 'banner_id1', "campaign_id": "campaign_id", "event_value": 0.2, "our_keywords": {}, "their_keywords": {}, "human_score": 1 }) yield db_utils.update_event({ "event_id": "event2_user_id2", "event_type": stats_consts.EVENT_TYPE_CONVERSION, "timestamp": 3602, "user_id": 'user_id2', "banner_id": 'banner_id1', "campaign_id": "campaign_id", "event_value": 0.5, "our_keywords": {}, "their_keywords": {}, "human_score": 1 }) yield stats_default.calculate_events_payments(cmp_doc, timestamp) _iter = yield db_utils.get_payments_iter(timestamp) while True: payment_doc = yield _iter.next() if not payment_doc: break if payment_doc['event_id'] == "event2_user_id1": self.assertLess(payment_doc['payment'], 10) if payment_doc['event_id'] == "event2_user_id2": self.assertLess(payment_doc['payment'], 0.025) # User scores should be empty. _iter = yield db_utils.get_sorted_user_score_iter("campaign_id", 3600, limit=1) user_score_doc = yield _iter.next() self.assertIsNone(user_score_doc)
def test_campaign_payments(self): cpv, cpc = 10, 20 cmp_doc = { "campaign_id": "campaign_id", "time_start": 0, "time_end": 3600 + 100, "max_cpc": cpc, "max_cpm": cpv, "budget": 1000, "filters": { 'require': {}, 'exclude': {} } } yield db_utils.update_campaign(cmp_doc) yield db_utils.update_banner({ 'banner_id': 'banner_id1', 'campaign_id': 'campaign_id' }) # Add events for users yield db_utils.update_event({ "event_id": "event1_user_id1", "event_type": stats_consts.EVENT_TYPE_CLICK, "timestamp": 3601, "user_id": 'user_id1', "banner_id": 'banner_id1', "campaign_id": "campaign_id", "event_value": 0.1, "our_keywords": {}, "their_keywords": {}, "human_score": 1 }) timestamp = 3600 yield stats_default.calculate_events_payments(cmp_doc, timestamp) _iter = yield db_utils.get_payments_iter(timestamp) while True: payment_doc = yield _iter.next() if not payment_doc: break self.assertEqual(payment_doc['payment'], 20) yield db_utils.update_event({ "event_id": "event2_user_id1", "event_type": stats_consts.EVENT_TYPE_VIEW, "timestamp": 3600, "user_id": 'user_id1', "banner_id": 'banner_id1', "campaign_id": "campaign_id", "event_value": 0.2, "our_keywords": {}, "their_keywords": {}, "human_score": 1 }) yield db_utils.update_event({ "event_id": "event2_user_id2", "event_type": stats_consts.EVENT_TYPE_CONVERSION, "timestamp": 3602, "user_id": 'user_id2', "banner_id": 'banner_id1', "campaign_id": "campaign_id", "event_value": 0.5, "our_keywords": {}, "their_keywords": {}, "human_score": 1 }) yield stats_default.calculate_events_payments(cmp_doc, timestamp) _iter = yield db_utils.get_payments_iter(timestamp) while True: payment_doc = yield _iter.next() if not payment_doc: break if payment_doc['event_id'] == "event2_user_id1": self.assertLess(payment_doc['payment'], 10) if payment_doc['event_id'] == "event2_user_id2": self.assertLess(payment_doc['payment'], 0.01)