def play_fake_results(update_interval=60): """ Play back faked results, poll closing time by poll closing time """ import models from peewee import fn from app_utils import eastern_now print "Playing back results, ctrl-c to stop" ct_query = models.Race.select(fn.Distinct( models.Race.poll_closing_time)).order_by(models.Race.poll_closing_time) closing_times = [ct.poll_closing_time for ct in ct_query] closing_times = closing_times * 2 closing_times.sort() try: for i, ct in enumerate(closing_times): races = models.Race.select().where( models.Race.poll_closing_time == ct) if i % 2 == 0: print "Polls close at %s, precincts reporting" % ct for race in races: race.precincts_total = random.randint(2000, 4000) race.precincts_reporting = random.randint( 200, race.precincts_total - 200) _fake_results(race) race.last_updated = eastern_now() race.save() else: print "Races are called!" for race in races: race.ap_called = True race.ap_called_time = datetime.utcnow() race.precincts_reporting = random.randint( race.precincts_total - 500, race.precincts_total) _fake_results(race) race.last_updated = eastern_now() race.save() #if app_config.DEPLOYMENT_TARGET: #execute('liveblog.update') #execute('deploy_liveblog') #execute('deploy_bop') #execute('deploy_big_boards') #execute('deploy_results') sleep(float(update_interval)) print "All done, resetting results" reset_results() play_fake_results() except KeyboardInterrupt: print "ctrl-c pressed, resetting results" reset_results()
def play_fake_results(update_interval=60): """ Play back faked results, poll closing time by poll closing time """ import models from peewee import fn from app_utils import eastern_now print "Playing back results, ctrl-c to stop" ct_query = models.Race.select(fn.Distinct(models.Race.poll_closing_time)).order_by(models.Race.poll_closing_time) closing_times = [ct.poll_closing_time for ct in ct_query] closing_times = closing_times * 2 closing_times.sort() try: for i, ct in enumerate(closing_times): races = models.Race.select().where(models.Race.poll_closing_time == ct) if i % 2 == 0: print "Polls close at %s, precincts reporting" % ct for race in races: race.precincts_total = random.randint(2000, 4000) race.precincts_reporting = random.randint(200, race.precincts_total - 200) _fake_results(race) race.last_updated = eastern_now() race.save() else: print "Races are called!" for race in races: race.ap_called = True race.ap_called_time = datetime.utcnow() race.precincts_reporting = random.randint(race.precincts_total - 500, race.precincts_total) _fake_results(race) race.last_updated = eastern_now() race.save() #if app_config.DEPLOYMENT_TARGET: #execute('liveblog.update') #execute('deploy_liveblog') #execute('deploy_bop') #execute('deploy_big_boards') #execute('deploy_results') sleep(float(update_interval)) print "All done, resetting results" reset_results() play_fake_results() except KeyboardInterrupt: print "ctrl-c pressed, resetting results" reset_results()
def load_races(path): """ Load AP race data from intermediary JSON into the database. """ import models print 'Loading race data from AP init data on disk' with open(path) as f: races = json.load(f) now = eastern_now() with models.db.transaction(): for race in races: models.Race.create( state_postal=race['state_postal'], office_id=race['office_id'], office_name=race['office_name'], seat_name=race['seat_name'], seat_number=race['seat_number'], race_id=race['race_id'], race_type=race['race_type'], last_updated=now, ) print 'Loaded %i races' % len(races)
def load_races(path): """ Load AP race data from intermediary JSON into the database. """ import models print 'Loading race data from AP init data on disk' with open(path) as f: races = json.load(f) now = eastern_now() with models.db.transaction(): for race in races: models.Race.create( state_postal = race['state_postal'], office_id = race['office_id'], office_name = race['office_name'], seat_name = race['seat_name'], seat_number = race['seat_number'], race_id = race['race_id'], race_type = race['race_type'], last_updated = now, ) print 'Loaded %i races' % len(races)
def load_updates(path): """ Update the latest results from the AP intermediary files. """ import models races_updated = 0 candidates_updated = 0 now = eastern_now() print 'Loading latest results from AP update data on disk' with open(path) as f: races = json.load(f) for race in races: changed = False race_model = models.Race.get(models.Race.race_id == race['race_id']) for candidate in race['candidates']: # Select candidate by candidate_id AND race_id, since they can appear in multiple races candidate_model = models.Candidate.get( models.Candidate.candidate_id == candidate['candidate_id'], models.Candidate.race == race_model) if candidate.get('vote_count'): if candidate_model.vote_count != candidate['vote_count']: changed = True candidate_model.vote_count = candidate['vote_count'] candidate_model.save() candidates_updated += 1 if race_model.precincts_reporting != race['precincts_reporting']: changed = True if changed: race_model.last_updated = now races_updated += 1 race_model.precincts_reporting = race['precincts_reporting'] race_model.precincts_total = race['precincts_total'] race_model.save() print 'Updated %i races' % races_updated print 'Updated %i candidates' % candidates_updated
def load_updates(path): """ Update the latest results from the AP intermediary files. """ import models races_updated = 0 candidates_updated = 0 now = eastern_now() print 'Loading latest results from AP update data on disk' with open(path) as f: races = json.load(f) for race in races: changed = False race_model = models.Race.get(models.Race.race_id == race['race_id']) for candidate in race['candidates']: # Select candidate by candidate_id AND race_id, since they can appear in multiple races candidate_model = models.Candidate.get(models.Candidate.candidate_id == candidate['candidate_id'], models.Candidate.race == race_model) if candidate.get('vote_count'): if candidate_model.vote_count != candidate['vote_count']: changed = True candidate_model.vote_count = candidate['vote_count'] candidate_model.save() candidates_updated += 1 if race_model.precincts_reporting != race['precincts_reporting']: changed = True if changed: race_model.last_updated = now races_updated += 1 race_model.precincts_reporting = race['precincts_reporting'] race_model.precincts_total = race['precincts_total'] race_model.save() print 'Updated %i races' % races_updated print 'Updated %i candidates' % candidates_updated