def add_dutch_winner(self, bid): with utils.update_auction_document(self): LOGGER.info( '---------------- Adding dutch winner ----------------', extra={ "JOURNAL_REQUEST_ID": self.request_id, "MESSAGE_ID": AUCTION_WORKER_SERVICE_END_FIRST_PAUSE } ) try: bid['bidder_name'] = self.mapping.get(bid['bidder_id'], False) current_stage = bid['current_stage'] del bid['current_stage'] if current_stage != self.auction_document['current_stage']: raise Exception( u"Your bid is not submitted since the previous " "step has already ended.") bid = self.approve_dutch_winner(bid) if bid: result = utils.prepare_results_stage(**bid) self.auction_document['stages'][current_stage].update( result ) self.auction_document['results'].append( result ) LOGGER.info('Approved dutch winner') self.end_dutch() return True except Exception as e: LOGGER.fatal( "Exception during initialization dutch winner. " "Error: {}".format(e) ) return e
def end_sealedbid(self, stage): def get_amount(bid): return bid['amount'] with utils.update_auction_document(self): self._end_sealedbid.set() while not self.bids_queue.empty(): LOGGER.info( "Waiting for bids to process" ) sleep(0.1) LOGGER.info("Done processing bids queue") self.auction_document['results'] = utils.prepare_auction_results(self, self._bids_data) if len([bid for bid in self.auction_document['results'] if str(bid['amount']) != '-1']) < 2: LOGGER.info("No bids on sealedbid phase. End auction now!") self.end_auction() return # find sealedbid winner in auction_document max_bid = max(self.auction_document['results'], key=get_amount) LOGGER.info("Approved sealedbid winner {bidder_id} with amount {amount}".format( **max_bid )) if not max_bid.get('dutch_winner', False): max_bid['sealedbid_winner'] = True self.auction_document['stages'][self.auction_document['current_stage']].update( max_bid ) self.approve_audit_info_on_sealedbid(utils.update_stage(self)) self.auction_document['current_phase'] = PREBESTBID
def end_sealedbid(self, stage): with utils.update_auction_document(self): self._end_sealedbid.set() while not self.bids_queue.empty(): LOGGER.info("Waiting for bids to process") sleep(0.1) LOGGER.info("Done processing bids queue") if len(self._bids_data.keys()) < 2: LOGGER.info("No bids on sealedbid phase. end auction") self.end_auction() return all_bids = deepcopy(self._bids_data) minimal_bids = [] max_bid = {'amount': 0} # init sealedbid winner bid for bid_id in all_bids.keys(): bid = get_latest_bid_for_bidder(all_bids[bid_id], bid_id) bid['bidder_name'] = self.mapping[bid['bidder_id']] minimal_bids.append(utils.prepare_results_stage(**bid)) # find a winner max_bid = max([max_bid, bid], key=lambda bid: bid['amount']) minimal_bids = sorting_by_amount(minimal_bids) self.auction_document['results'] = minimal_bids # save winner to stages in auction_document max_bid['sealedbid_winner'] = True self.auction_document['stages'][ self.auction_document['current_stage']].update( utils.prepare_results_stage(**max_bid)) run_time = utils.update_stage(self) self.approve_audit_info_on_sealedbid(run_time) self.auction_document['current_phase'] = PREBESTBID
def next_stage(self, stage): with utils.lock_bids(self), utils.update_auction_document(self): run_time = utils.update_stage(self) stage_index = self.auction_document['current_stage'] self.auction_document['stages'][stage_index - 1].update({ 'passed': True }) if stage['type'].startswith(DUTCH): LOGGER.info( '---------------- SWITCH DUTCH VALUE ----------------' ) self.auction_document['stages'][stage_index]['time']\ = run_time if stage_index == 1: self.auction_document['current_phase'] = DUTCH self.audit['timeline'][DUTCH]['timeline']['start']\ = run_time old = self.auction_document['stages'][stage_index - 1].get( 'amount', '' ) or self.auction_document['initial_value'] LOGGER.info('Switched dutch phase value from {} to {}'.format( old, stage['amount']) ) turn = 'turn_{}'.format(stage_index) self.audit['timeline'][DUTCH][turn] = { 'amount': stage['amount'], 'time': run_time, } else: self.end_dutch()
def switch_to_sealedbid(self, stage): with utils.lock_bids(self), utils.update_auction_document(self): self._end_sealedbid = Event() run_time = utils.update_stage(self) self.auction_document['current_phase'] = SEALEDBID self.audit['timeline'][SEALEDBID]['timeline']['start'] =\ run_time spawn(self.add_bid) LOGGER.info("Swithed auction to {} phase".format(SEALEDBID))
def end_bestbid(self, stage): with simple.update_auction_document(self): run_time = simple.update_stage(self) self.auction_document['current_phase'] = END all_bids = deepcopy(self._bids_data) self.auction_document['results'] = sorting_by_amount( all_bids.values()) if not self.debug: # TODO: post results data pass self.approve_audit_info_on_bestbid(run_time) self.end_auction()
def end_sealedbid(self, stage): with simple.update_auction_document(self): run_time = simple.update_stage(self) self._end_sealedbid.set() while not self.bids_queue.empty(): LOGGER.info("Waiting for bids to process") sleep(0.1) LOGGER.info("Done processing bids queue") self.auction_document['current_phase'] = PREBESTBID all_bids = deepcopy(self._bids_data) self.auction_document['results'] = sorting_by_amount( all_bids.values()) self.approve_audit_info_on_sealedbid(run_time)
def start_auction(self): self.generate_request_id() self.audit['timeline']['auction_start']['time']\ = datetime.now(tzlocal()).isoformat() LOGGER.info('---------------- Start auction ----------------', extra={ "JOURNAL_REQUEST_ID": self.request_id, "MESSAGE_ID": AUCTION_WORKER_SERVICE_END_FIRST_PAUSE }) self.get_auction_info() with lock_bids(self), update_auction_document(self): self.auction_document["current_stage"] = 0 LOGGER.info("Switched current stage to {}".format( self.auction_document['current_stage']))
def end_bestbid(self, stage): with utils.update_auction_document(self): all_bids = deepcopy(self._bids_data) minimal_bids = [] for bid_id in all_bids.keys(): bid = get_latest_bid_for_bidder(all_bids[bid_id], bid_id) bid['bidder_name'] = self.mapping[bid['bidder_id']] minimal_bids.append(utils.prepare_results_stage(**bid)) minimal_bids = sorting_by_amount(minimal_bids) self.auction_document['results'] = minimal_bids run_time = utils.update_stage(self) self.approve_audit_info_on_bestbid(run_time) self.end_auction()
def add_dutch_winner(self, bid): with simple.update_auction_document(self): LOGGER.info( '---------------- Adding dutch winner ----------------', extra={ "JOURNAL_REQUEST_ID": self.request_id, "MESSAGE_ID": AUCTION_WORKER_SERVICE_END_FIRST_PAUSE }) try: if self.approve_dutch_winner(bid): LOGGER.info('Approved dutch winner') self.end_dutch() if not self.debug: simple.post_dutch_results(self) return True except Exception as e: LOGGER.fatal("Exception during initialization dutch winner. " "Error: {}".format(e)) return e
def add_dutch_winner(self, bid): with utils.update_auction_document(self): LOGGER.info( '---------------- Adding dutch winner ----------------', extra={ "JOURNAL_REQUEST_ID": self.request_id, "MESSAGE_ID": AUCTION_WORKER_SERVICE_END_FIRST_PAUSE }) try: bid = self.approve_dutch_winner(bid) if bid: bid['bidder_name'] = self.mapping[bid['bidder_id']] result = utils.prepare_results_stage(**bid) self.auction_document['stages'][ self.auction_document['current_stage']].update(result) self.auction_document['results'].append(result) LOGGER.info('Approved dutch winner') self.end_dutch() return True except Exception as e: LOGGER.fatal("Exception during initialization dutch winner. " "Error: {}".format(e)) return e
def schedule_auction(self): self.generate_request_id() with update_auction_document(self): if self.debug: LOGGER.info("Get _auction_data from auction_document") self._auction_data = self.auction_document.get( 'test_auction_data', {}) self.get_auction_info() self.audit = prepare_audit(self) round_number = 0 SCHEDULER.add_job(self.start_auction, 'date', run_date=self.convert_datetime( self.auction_document['stages'][0]['start']), name="Start of Auction", id="auction:start") round_number += 1 for index, stage in enumerate(self.auction_document['stages'][1:], 1): if stage['type'].startswith(DUTCH): name = 'End of dutch stage: [{} -> {}]'.format( index - 1, index) id = 'auction:{}-{}'.format(DUTCH, index) func = self.next_stage elif stage['type'] == PRESEALEDBID: name = 'End of dutch phase' id = 'auction:{}'.format(PRESEALEDBID) func = self.end_dutch elif stage['type'] == SEALEDBID: name = "Sealedbid phase" func = self.switch_to_sealedbid id = "auction:{}".format(SEALEDBID) elif stage['type'] == PREBESTBID: id = 'auction:{}'.format(PREBESTBID) name = "End of sealedbid phase" func = self.end_sealedbid elif stage['type'] == BESTBID: id = 'auction:{}'.format(BESTBID) name = 'BestBid phase' func = self.switch_to_bestbid elif stage['type'] == END: id = 'auction:{}'.format(END) name = 'End of bestbid phase' func = self.end_bestbid SCHEDULER.add_job( func, 'date', args=(stage, ), run_date=self.convert_datetime( self.auction_document['stages'][index]['start']), name=name, id=id) round_number += 1 LOGGER.info("Prepare server ...", extra={ "JOURNAL_REQUEST_ID": self.request_id, "MESSAGE_ID": AUCTION_WORKER_SERVICE_PREPARE_SERVER }) self.server = run_server( self, self.convert_datetime( self.auction_document['stages'][-2]['start']), LOGGER)
def end_bestbid(self, stage): with utils.update_auction_document(self): self.auction_document['results'] = utils.prepare_auction_results(self, self._bids_data) self.approve_audit_info_on_bestbid(utils.update_stage(self)) self.end_auction()
def switch_to_bestbid(self, stage): with utils.lock_bids(self), utils.update_auction_document(self): self.auction_document['current_phase'] = BESTBID self.audit['timeline'][BESTBID]['timeline']['start'] = utils.update_stage(self)
def post_announce(self): self.generate_request_id() with utils.update_auction_document(self): utils.announce_results_data(self, None)
def switch_to_bestbid(self, stage): with simple.lock_bids(self), simple.update_auction_document(self): run_time = simple.update_stage(self) self.auction_document['current_phase'] = BESTBID self.audit['timeline'][BESTBID]['timeline']['start'] = run_time