Esempio n. 1
0
 def getWinningCampaign(self, country_code, sort_key):
     if self.setFilter(country_code):
         camps_sorted_by_price = self.getSorted(sort_key)
         return camps_sorted_by_price[0]
     else:
         logger.info('No winning campaign')
         return None
Esempio n. 2
0
def main():
    app = connexion.App(__name__, specification_dir='swagger/')
    app.add_api('bluebananabidder.yaml', resolver=RestyResolver('bbidder.api'))
    application = app.app

    # run our standalone server
    logger.info('Starting BlueBanana\'s Bidder ...')
    app.run(port=5000, server='gevent')
Esempio n. 3
0
    def getSorted(self, sort_key):

        if self.filtered_campaigns:
            return sorted(self.filtered_campaigns,
                          key=lambda k: getattr(k, sort_key),
                          reverse=True)
        else:
            # log and raise exception
            logger.info('No campaigns after filtering')
            return None
Esempio n. 4
0
    def setFilter(self, country_code):
        def filter_func(camp):
            return country_code in [c for c in camp.targetedCountries]

        if self.campaigns:
            self.filtered_campaigns = list(filter(filter_func, self.campaigns))
            return self.filtered_campaigns
        else:
            # log and raise exception
            logger.info('No campaigns in pool')
            return None
Esempio n. 5
0
    def get(self):
        mock_data = Config.getMockedCampaigns()
        if not mock_data:
            response = requests.get(url=self.host_url, headers=self.headers)
            camp_data = response.json()
            if response.status_code is not 200:
                logger.info('Campaign request failed with code:{0}'.format(
                    response.status_code))
                raise BidderException(
                    'Campaign request failed with code:{0}'.format(
                        response.status_code))
        else:
            camp_data = mock_data

        self.campaigns = self.buildCampaigns(camp_data)
        return self.campaigns
Esempio n. 6
0
def main():
    parser = argparse.ArgumentParser(description='Pacing Algorithm Deamon')
    parser.add_argument('--limit',
                        default=Config.getParam('pacing', 'defaultlimit'))
    parser.add_argument('--reset-time',
                        default=Config.getParam('pacing', 'defaultresettime'))
    args = parser.parse_args()
    pc = PacingController(args.limit, args.reset_time)

    # Start zmq socket
    context = zmq.Context(1)
    server = context.socket(zmq.REP)
    bind_address = Config.getParam('pacing', 'bindaddrserver')
    logger.info('Binding to %s' % bind_address)
    server.bind(bind_address)

    # Populate rpc methods dict
    methods = {'pa_push_bid': pc.pa_push_bid, 'pa_reset': pc.reset}
    stime = time.time()
    # Enter main loop
    try:
        logger.info('Entering main loop')
        while True:
            # reset campaigns bids count
            # if reset time has been reached
            ctime = time.time()
            if ctime - stime > 1000:
                stime = ctime
                pc.reset()
                logger.info('pacing reset performed')

            request = server.recv_json()
            logger.info('main loop req {0}'.format(request))
            response = dispatch(methods, request)
            server.send_json(response)
    finally:
        logger.info('Cleaning up')
        server.close()
        context.term()
Esempio n. 7
0
 def postBidHandler(self, bid):
     bid_id = bid[self.ID_REQ_KEY]
     bid_country = bid[self.DEVICE_REQ_KEY][self.GEO_REQ_KEY][self.COUNTRY_REQ_KEY] 
     try:
         wincamp = CampaignsFact().getWinningCampaign(bid_country, CampaignsFact.PRICE_KEY)
     except  BidderException as error:
         logger.info('Campaign request error: {0}'.format(error))
            
     if wincamp:
         pa_resp = {'campaignid':wincamp.id}
         pa_status = rpcClient('pa_push_bid', pa_resp).call()
         if pa_status:
             return {
                         self.ID_RESP_KEY: bid_id,
                         self.BID_RESP_KEY: {
                         self.CAMPAIGNID_RESP_KEY: wincamp.id,
                         self.PRICE_RESP_KEY: wincamp.price,
                         self.ADM_RESP_KEY: wincamp.adm
                         }
                     }
         else:
             return NoContent, 204
     else:
         return NoContent, 204
Esempio n. 8
0
    methods = {'pa_push_bid': pc.pa_push_bid, 'pa_reset': pc.reset}
    stime = time.time()
    # Enter main loop
    try:
        logger.info('Entering main loop')
        while True:
            # reset campaigns bids count
            # if reset time has been reached
            ctime = time.time()
            if ctime - stime > 1000:
                stime = ctime
                pc.reset()
                logger.info('pacing reset performed')

            request = server.recv_json()
            logger.info('main loop req {0}'.format(request))
            response = dispatch(methods, request)
            server.send_json(response)
    finally:
        logger.info('Cleaning up')
        server.close()
        context.term()


if __name__ == '__main__':
    try:
        main()
    except Exception as e:
        logger.info('Encoutered unhandled exception %s' % e)
        raise