Ejemplo n.º 1
0
def get_payments(pay_request):
    """
    Fetch the payments from the database.

    May raise a PaymentsNotCalculatedException if payments are not ready yet.

    :param pay_request: Payment request.
    :return: List of payments.
    """
    logger = logging.getLogger(__name__)
    yield logger.info("Checking for payments for {0}.".format(
        pay_request.timestamp))

    # Check if payments calculation is done
    round_doc = yield db_utils.get_payment_round(pay_request.timestamp)
    if not round_doc:
        yield logger.error("Payments not calculated yet.")
        raise PaymentsNotCalculatedException()

    # Collect events and theirs payment and respond to request
    events_payments = []

    _iter = yield db_utils.get_payments_iter(pay_request.timestamp)
    while True:
        payment_doc = yield _iter.next()
        if not payment_doc:
            break

        events_payments.append(
            iface_proto.SinglePaymentResponse(event_id=payment_doc['event_id'],
                                              amount=payment_doc['payment'],
                                              reason=payment_doc['reason']))

    yield logger.debug(events_payments)
    defer.returnValue(iface_proto.PaymentsResponse(payments=events_payments))
Ejemplo n.º 2
0
    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'])
Ejemplo n.º 3
0
    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'])
Ejemplo n.º 4
0
def step_impl(context, number, timestamp, event_id):
    class QueryAnalyzer:
        def __init__(self, dfr):
            self.finished = False
            self.length = 0
            dfr.addCallback(self.test_query)

        def test_query(self, query):
            assert query is not None
            assert isinstance(query, utils.QueryIterator)

            while not self.finished:
                payment_doc = yield query.next()
                if not payment_doc:
                    self.finished = True
                self.length += 1

    timestamp = timestamp2hour(timestamp)
    qa = QueryAnalyzer(utils.get_payments_iter(timestamp))
    assert qa.length == int(number)
Ejemplo n.º 5
0
    def test_payment(self):
        for i in range(200):
            yield db_utils.update_event_payment(
                    campaign_id="campaign_id",
                    timestamp=0,
                    event_id=i,
                    payment=i * 20,
                    reason=0
                    )

        counter = 0
        payments_iter = yield db_utils.get_payments_iter(0)
        while True:
            payment_doc = yield payments_iter.next()
            if not payment_doc:
                break

            self.assertEqual(payment_doc['campaign_id'], "campaign_id")
            self.assertEqual(payment_doc['payment'], payment_doc['event_id'] * 20)
            counter += 1
        self.assertEqual(counter, 200)
Ejemplo n.º 6
0
    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)
Ejemplo n.º 7
0
    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)