src/server/server.py
def on_save_laps():
    '''Save current laps data to the database.'''
    global FULL_RESULTS_CACHE_VALID
    FULL_RESULTS_CACHE_VALID = False
    race_format = getCurrentRaceFormat()
    heat = Database.Heat.query.get(RACE.current_heat)
    # Get the last saved round for the current heat
    max_round = DB.session.query(DB.func.max(Database.SavedRaceMeta.round_id)) \
            .filter_by(heat_id=RACE.current_heat).scalar()
    if max_round is None:
        max_round = 0
    # Loop through laps to copy to saved races
    profile = getCurrentProfile()
    profile_freqs = json.loads(profile.frequencies)

    new_race = Database.SavedRaceMeta( \
        round_id=max_round+1, \
        heat_id=RACE.current_heat, \
        class_id=heat.class_id, \
        format_id=Options.get('currentFormat'), \
        start_time = RACE.start_time_monotonic, \
        start_time_formatted = RACE.start_time.strftime("%Y-%m-%d %H:%M:%S"), \
        cacheStatus=Results.CacheStatus.INVALID
    )
    DB.session.add(new_race)
    DB.session.flush()
    DB.session.refresh(new_race)

    for node_index in range(RACE.num_nodes):
        if profile_freqs["f"][node_index] != RHUtils.FREQUENCY_ID_NONE:
            pilot_id = Database.HeatNode.query.filter_by( \
                heat_id=RACE.current_heat, node_index=node_index).one().pilot_id

            new_pilotrace = Database.SavedPilotRace( \
                race_id=new_race.id, \
                node_index=node_index, \
                pilot_id=pilot_id, \
                history_values=json.dumps(INTERFACE.nodes[node_index].history_values), \
                history_times=json.dumps(INTERFACE.nodes[node_index].history_times), \
                penalty_time=0, \
                enter_at=INTERFACE.nodes[node_index].enter_at_level, \
                exit_at=INTERFACE.nodes[node_index].exit_at_level
            )
            DB.session.add(new_pilotrace)
            DB.session.flush()
            DB.session.refresh(new_pilotrace)

            for lap in RACE.node_laps[node_index]:
                DB.session.add(Database.SavedRaceLap( \
                    race_id=new_race.id, \
                    pilotrace_id=new_pilotrace.id, \
                    node_index=node_index, \
                    pilot_id=pilot_id, \
                    lap_time_stamp=lap['lap_time_stamp'], \
                    lap_time=lap['lap_time'], \
                    lap_time_formatted=lap['lap_time_formatted'], \
                    source = lap['source'], \
                    deleted = lap['deleted']
                ))

    DB.session.commit()

    # spawn thread for updating results caches
    params = {
        'race_id': new_race.id,
        'heat_id': RACE.current_heat,
        'round_id': new_race.round_id,
    }
    gevent.spawn(Results.build_race_results_caches, DB, params)

    Events.trigger(Evt.LAPS_SAVE, {
        'race_id': new_race.id,
        })

    logger.info('Current laps saved: Heat {0} Round {1}'.format(RACE.current_heat, max_round+1))
    on_discard_laps(saved=True) # Also clear the current laps
    emit_round_data_notify()
Save current laps data to the database.
Save current laps data to the database.
Save current laps data to the database .
def on_save_laps():
    global FULL_RESULTS_CACHE_VALID
    FULL_RESULTS_CACHE_VALID = False
    race_format = getCurrentRaceFormat()
    heat = Database.Heat.query.get(RACE.current_heat)
    max_round = DB.session.query(DB.func.max(Database.SavedRaceMeta.round_id)) \
            .filter_by(heat_id=RACE.current_heat).scalar()
    if max_round is None:
        max_round = 0
    profile = getCurrentProfile()
    profile_freqs = json.loads(profile.frequencies)
    new_race = Database.SavedRaceMeta( \
        round_id=max_round+1, \
        heat_id=RACE.current_heat, \
        class_id=heat.class_id, \
        format_id=Options.get('currentFormat'), \
        start_time = RACE.start_time_monotonic, \
        start_time_formatted = RACE.start_time.strftime("%Y-%m-%d %H:%M:%S"), \
        cacheStatus=Results.CacheStatus.INVALID
    )
    DB.session.add(new_race)
    DB.session.flush()
    DB.session.refresh(new_race)
    for node_index in range(RACE.num_nodes):
        if profile_freqs["f"][node_index] != RHUtils.FREQUENCY_ID_NONE:
            pilot_id = Database.HeatNode.query.filter_by( \
                heat_id=RACE.current_heat, node_index=node_index).one().pilot_id
            new_pilotrace = Database.SavedPilotRace( \
                race_id=new_race.id, \
                node_index=node_index, \
                pilot_id=pilot_id, \
                history_values=json.dumps(INTERFACE.nodes[node_index].history_values), \
                history_times=json.dumps(INTERFACE.nodes[node_index].history_times), \
                penalty_time=0, \
                enter_at=INTERFACE.nodes[node_index].enter_at_level, \
                exit_at=INTERFACE.nodes[node_index].exit_at_level
            )
            DB.session.add(new_pilotrace)
            DB.session.flush()
            DB.session.refresh(new_pilotrace)
            for lap in RACE.node_laps[node_index]:
                DB.session.add(Database.SavedRaceLap( \
                    race_id=new_race.id, \
                    pilotrace_id=new_pilotrace.id, \
                    node_index=node_index, \
                    pilot_id=pilot_id, \
                    lap_time_stamp=lap['lap_time_stamp'], \
                    lap_time=lap['lap_time'], \
                    lap_time_formatted=lap['lap_time_formatted'], \
                    source = lap['source'], \
                    deleted = lap['deleted']
                ))
    DB.session.commit()
    params = {
        'race_id': new_race.id,
        'heat_id': RACE.current_heat,
        'round_id': new_race.round_id,
    }
    gevent.spawn(Results.build_race_results_caches, DB, params)
    Events.trigger(Evt.LAPS_SAVE, {
        'race_id': new_race.id,
        })
    logger.info('Current laps saved: Heat {0} Round {1}'.format(RACE.current_heat, max_round+1))
    on_discard_laps(saved=True) 
    emit_round_data_notify()
def on_save_laps ( ) : global FULL_RESULTS_CACHE_VALID FULL_RESULTS_CACHE_VALID = False race_format = getCurrentRaceFormat ( ) heat = Database . Heat . query . get ( RACE . current_heat ) max_round = DB . session . query ( DB . func . max ( Database . SavedRaceMeta . round_id ) ) . filter_by ( heat_id = RACE . current_heat ) . scalar ( ) if max_round is None : max_round = 0 profile = getCurrentProfile ( ) profile_freqs = json . loads ( profile . frequencies ) new_race = Database . SavedRaceMeta ( round_id = max_round + 1 , heat_id = RACE . current_heat , class_id = heat . class_id , format_id = Options . get ( 'currentFormat' ) , start_time = RACE . start_time_monotonic , start_time_formatted = RACE . start_time . strftime ( "%Y-%m-%d %H:%M:%S" ) , cacheStatus = Results . CacheStatus . INVALID ) DB . session . add ( new_race ) DB . session . flush ( ) DB . session . refresh ( new_race ) for node_index in range ( RACE . num_nodes ) : if profile_freqs [ "f" ] [ node_index ] != RHUtils . FREQUENCY_ID_NONE : pilot_id = Database . HeatNode . query . filter_by ( heat_id = RACE . current_heat , node_index = node_index ) . one ( ) . pilot_id new_pilotrace = Database . SavedPilotRace ( race_id = new_race . id , node_index = node_index , pilot_id = pilot_id , history_values = json . dumps ( INTERFACE . nodes [ node_index ] . history_values ) , history_times = json . dumps ( INTERFACE . nodes [ node_index ] . history_times ) , penalty_time = 0 , enter_at = INTERFACE . nodes [ node_index ] . enter_at_level , exit_at = INTERFACE . nodes [ node_index ] . exit_at_level ) DB . session . add ( new_pilotrace ) DB . session . flush ( ) DB . session . refresh ( new_pilotrace ) for lap in RACE . node_laps [ node_index ] : DB . session . add ( Database . SavedRaceLap ( race_id = new_race . id , pilotrace_id = new_pilotrace . id , node_index = node_index , pilot_id = pilot_id , lap_time_stamp = lap [ 'lap_time_stamp' ] , lap_time = lap [ 'lap_time' ] , lap_time_formatted = lap [ 'lap_time_formatted' ] , source = lap [ 'source' ] , deleted = lap [ 'deleted' ] ) ) DB . session . commit ( ) params = { 'race_id' : new_race . id , 'heat_id' : RACE . current_heat , 'round_id' : new_race . round_id , } gevent . spawn ( Results . build_race_results_caches , DB , params ) Events . trigger ( Evt . LAPS_SAVE , { 'race_id' : new_race . id , } ) logger . info ( 'Current laps saved: Heat {0} Round {1}' . format ( RACE . current_heat , max_round + 1 ) ) on_discard_laps ( saved = True ) emit_round_data_notify ( )
Save current laps data to the database.
Save current laps data to the database .
'''Save current laps data to the database.''' # Get the last saved round for the current heat # Loop through laps to copy to saved races # spawn thread for updating results caches # Also clear the current laps