Example #1
0
def process_igc(task_id: int, par_id: int, tracklog):
    from pilot.track import create_igc_filename, igc_parsing_config_from_yaml
    from calcUtils import epoch_to_date
    from airspace import AirspaceCheck
    from igc_lib import Flight
    from task import Task
    from pilot.flightresult import FlightResult, save_track

    pilot = FlightResult.read(par_id, task_id)
    if pilot.name:
        task = Task.read(task_id)
        fullname = create_igc_filename(task.file_path, task.date, pilot.name)
        tracklog.save(fullname)
        pilot.track_file = Path(fullname).name
    else:
        return None, None
    """import track"""
    # track = Track(track_file=fullname, par_id=pilot.par_id)
    FlightParsingConfig = igc_parsing_config_from_yaml(task.igc_config_file)
    flight = Flight.create_from_file(fullname,
                                     config_class=FlightParsingConfig)
    """check result"""
    if not flight:
        error = f"for {pilot.name} - Track is not a valid track file"
        return None, error
    elif not epoch_to_date(flight.date_timestamp) == task.date:
        error = f"for {pilot.name} - Track has a different date from task date"
        return None, error
    else:
        print(
            f"pilot {pilot.par_id} associated with track {pilot.track_file} \n"
        )
        """checking track against task"""
        if task.airspace_check:
            airspace = AirspaceCheck.from_task(task)
        else:
            airspace = None
        pilot.check_flight(flight, task, airspace_obj=airspace)
        print(f"track verified with task {task.task_id}\n")
        '''create map file'''
        pilot.save_tracklog_map_file(task, flight)
        """adding track to db"""
        # pilot.to_db()
        save_track(pilot, task.id)
        time = ''
        data = {'par_id': pilot.par_id, 'track_id': pilot.track_id}
        if pilot.goal_time:
            time = sec_to_time(pilot.ss_time)
        if pilot.result_type == 'goal':
            data['Result'] = f'Goal {time}'
        elif pilot.result_type == 'lo':
            data['Result'] = f"LO {round(pilot.distance / 1000, 2)}"
        if pilot.track_id:  # if there is a track, make the result a link to the map
            # trackid = data['track_id']
            parid = data['par_id']
            result = data['Result']
            data[
                'Result'] = f'<a href="/map/{parid}-{task.task_id}">{result}</a>'
    return data, None
Example #2
0
def save_igc_background(task_id: int,
                        par_id: int,
                        tracklog,
                        user,
                        check_g_record=False):
    from task import Task
    from pilot.track import create_igc_filename, validate_G_record
    from pilot.flightresult import FlightResult

    pilot = FlightResult.read(par_id, task_id)
    print = partial(print_to_sse, id=par_id, channel=user)
    if pilot.name:
        task = Task.read(task_id)
        fullname = create_igc_filename(task.file_path, task.date, pilot.name)
        tracklog.save(fullname)
        print('|open_modal')
        print('***************START*******************')
        if check_g_record:
            print('Checking G-Record...')
            validation = validate_G_record(fullname)
            if validation == 'FAILED':
                print('G-Record not valid')
                data = {
                    'par_id': pilot.par_id,
                    'track_id': pilot.track_id,
                    'Result': ''
                }
                print(json.dumps(data) + '|g_record_fail')
                return None, None
            if validation == 'ERROR':
                print('Error trying to validate G-Record')
                return None, None
            if validation == 'PASSED':
                print('G-Record is valid')
        print(f'IGC file saved: {fullname.name}')
    else:
        return None, None
    return fullname
Example #3
0
def assign_and_import_tracks(files,
                             task,
                             track_source=None,
                             user=None,
                             check_g_record=False,
                             print=print):
    """Find pilots to associate with tracks"""
    from compUtils import get_registration
    from pilot.track import Track, validate_G_record, igc_parsing_config_from_yaml
    from functools import partial
    from frontendUtils import print_to_sse
    import json
    import importlib

    pilot_list = []
    task_id = task.id
    comp_id = task.comp_id
    """checking if comp requires a regisration.
    Then we create a list of registered pilots to check against tracks filename.
    This should be much faster than checking against all pilots in database through a query"""
    registration = get_registration(comp_id)
    if registration:
        """We add tracks for the registered pilots not yet scored"""
        print(
            "Comp with registration: files will be checked against registered pilots not yet scored"
        )
        pilot_list = get_unscored_pilots(task_id, track_source)
        if len(pilot_list) == 0:
            print(f"Pilot list is empty")
            return
        print(
            f"We have {len(pilot_list)} pilots to find tracks for, and {len(files)} tracks"
        )
    else:
        print(
            f"No registration required, we have {len(files)} tracks to associate"
        )

    task_date = task.date
    track_counter = 0
    track_path = task.file_path
    FlightParsingConfig = igc_parsing_config_from_yaml(task.igc_config_file)

    # print("found {} tracks \n".format(len(files)))
    for file in files:
        mytrack = None
        filename = file.name
        print(f'filename {filename}, {type(filename)}')
        if registration:
            # print(f"checking {filename} against {len(pilot_list)} pilots...")
            """check filenames to find pilots"""
            if track_source:
                # print(f'Source: {track_source}')
                ''' Use Live server filename format to get pilot '''
                lib = importlib.import_module('.'.join(
                    ['sources', track_source]))
                pilot = lib.get_pilot_from_list(filename, pilot_list)
            else:
                pilot = get_pilot_from_list(filename, pilot_list)

            if pilot:
                """found a pilot for the track file.
                dropping pilot from list and creating track obj"""
                # print(f"Found a pilot to associate with file. dropping {pilot.name} from non scored list")
                pilot_list[:] = [
                    d for d in pilot_list if d.par_id != pilot.par_id
                ]
                mytrack = Track.read_file(filename=file,
                                          config=FlightParsingConfig,
                                          print=print)
        else:
            """We add track if we find a pilot in database
            that has not yet been scored"""
            mytrack = Track.read_file(filename=file,
                                      config=FlightParsingConfig,
                                      print=print)
            if get_pil_track(mytrack.par_id, task_id):
                """pilot has already been scored"""
                print(
                    f"Pilot with ID {mytrack.par_id} has already a valid track for task with ID {task_id}"
                )
                continue
            pilot = FlightResult.read(par_id=mytrack.par_id, task_id=task_id)
        """check result"""
        if not mytrack:
            print(
                f"Track {filename} is not a valid track file, pilot not found in competition or pilot "
                f"already has a track")
            continue
        elif not mytrack.date == task_date:
            print(f"track {filename} has a different date from task")
            continue
        """pilot is registered and has no valid track yet
        moving file to correct folder and adding to the list of valid tracks"""
        track_counter += 1
        print(f"Track {track_counter}|counter")
        mytrack.task_id = task_id
        filename_and_path = mytrack.copy_track_file(task_path=track_path,
                                                    pname=pilot.name)
        # print(f"pilot {mytrack.par_id} associated with track {mytrack.filename}")
        pilot.track_file = filename_and_path.name
        print(f"processing {pilot.ID} {pilot.name}:")
        if user:
            new_print = partial(print_to_sse, id=mytrack.par_id, channel=user)
            print('***************START*******************')
        else:
            new_print = print
        if check_g_record:
            print('Checking G-Record...')
            validation = validate_G_record(filename_and_path)
            if validation == 'FAILED':
                print('G-Record not valid')
                data = {
                    'par_id': pilot.par_id,
                    'track_id': pilot.track_id,
                    'Result': ''
                }
                print(json.dumps(data) + '|g_record_fail')
                continue
            if validation == 'ERROR':
                print('Error trying to validate G-Record')
                continue
            if validation == 'PASSED':
                print('G-Record is valid')
        verify_and_import_track(pilot, mytrack, task, print=new_print)
    print("*******************processed all tracks**********************")
Example #4
0
def process_igc_background(task_id: int, par_id: int, file: Path, user: str):
    from pilot.track import igc_parsing_config_from_yaml
    from calcUtils import epoch_to_date
    from pilot.flightresult import FlightResult, save_track
    from airspace import AirspaceCheck
    from igc_lib import Flight
    from task import Task
    import json
    pilot = FlightResult.read(par_id, task_id)
    task = Task.read(task_id)
    print = partial(print_to_sse, id=par_id, channel=user)
    """import track"""
    # pilot.track = Track(track_file=filename, par_id=pilot.par_id)
    FlightParsingConfig = igc_parsing_config_from_yaml(task.igc_config_file)
    flight = Flight.create_from_file(file, config_class=FlightParsingConfig)
    data = {
        'par_id': pilot.par_id,
        'track_id': pilot.track_id,
        'Result': 'Not Yet Processed'
    }
    """check result"""
    if not flight:
        print(f"for {pilot.name} - Track is not a valid track file")
        print(json.dumps(data) + '|result')
        return None
    if not flight.valid:
        print(
            f'IGC does not meet quality standard set by igc parsing config. Notes:{pilot.flight.notes}'
        )
        print(json.dumps(data) + '|result')
        return None
    elif not epoch_to_date(flight.date_timestamp) == task.date:
        print(f"for {pilot.name} - Track has a different date from task date")
        print(json.dumps(data) + '|result')
        return None
    else:
        print(f"pilot {pilot.par_id} associated with track {file.name} \n")
        pilot.track_file = file.name
        """checking track against task"""
        if task.airspace_check:
            airspace = AirspaceCheck.from_task(task)
        else:
            airspace = None
        pilot.check_flight(flight, task, airspace_obj=airspace, print=print)
        print(f"track verified with task {task.task_id}\n")
        '''create map file'''
        pilot.save_tracklog_map_file(task, flight)
        """adding track to db"""
        # pilot.to_db()
        save_track(pilot, task.id)
        data['track_id'] = pilot.track_id
        time = ''

        if pilot.goal_time:
            time = sec_to_time(pilot.ESS_time - pilot.SSS_time)
        if pilot.result_type == 'goal':
            data['Result'] = f'Goal {time}'
        elif pilot.result_type == 'lo':
            data['Result'] = f"LO {round(pilot.distance / 1000, 2)}"
        if pilot.track_id:  # if there is a track, make the result a link to the map
            # trackid = data['track_id']
            parid = data['par_id']
            result = data['Result']
            data[
                'Result'] = f'<a href="/map/{parid}-{task.task_id}">{result}</a>'
        print(data['Result'])
        print(json.dumps(data) + '|result')
        print('***************END****************')
    return None