def sample_status_per_plate(session):
    """This function queries the lims database for sample information and aggregate at the plate name level"""
    match = json.loads(request.args.get('match', '{}'))
    project_name = match.get('project_id')
    all_plates = defaultdict(Container)
    sample_to_container = {}
    for result in get_sample_info(session, project_name, udfs=['Prep Workflow', 'Species']):
        (pjct_name, sample_name, container, wellx, welly, udf_name, udf_value) = result
        all_plates[container].name = container
        all_plates[container].project_id = pjct_name
        if udf_name == 'Prep Workflow':
            all_plates[container].samples[sanitize_user_id(sample_name)].planned_library = udf_value
        if udf_name == 'Species':
            all_plates[container].samples[sanitize_user_id(sample_name)].species = udf_value
        sample_to_container[sample_name] = container

    for result in get_samples_and_processes(
            session,
            project_name,
            workstatus='COMPLETE',
            list_process=list(status_cfg.step_completed_to_status) \
                       + list(status_cfg.additional_step_completed) \
                       + list(status_cfg.library_type_step_completed)
    ):
        (pjct_name, sample_name, process_name, process_status, date_run) = result
        container = sample_to_container.get(sample_name)
        all_plates[container].samples[sanitize_user_id(sample_name)].add_completed_process(process_name, date_run)

    for result in non_QC_queues(session, project_name, list_process=status_cfg.step_queued_to_status):
        pjct_name, sample_name, process_name, queued_date = result
        container = sample_to_container.get(sample_name)
        all_plates[container].samples[sanitize_user_id(sample_name)].add_queue_location(process_name, queued_date)

    return [p.to_json() for p in all_plates.values()]
def sample_status_per_project(session):
    """This function queries the lims database for sample information and aggregate at the project name level"""
    match = json.loads(request.args.get('match', '{}'))
    project_name = match.get('project_id')
    all_projects = defaultdict(Project)
    for project_info in get_project_info(session, project_name, udfs=['Number of Quoted Samples']):
        pjct_name, open_date, firstname, lastname, udf_name , nb_quoted_samples = project_info
        all_projects[pjct_name].name = pjct_name
        all_projects[pjct_name].open_date = open_date.isoformat() + 'Z'
        all_projects[pjct_name].researcher_name = '%s %s' % (firstname, lastname)
        all_projects[pjct_name].nb_quoted_samples = nb_quoted_samples

    for result in get_samples_and_processes(
            session,
            project_name,
            workstatus='COMPLETE',
            list_process=list(status_cfg.step_completed_to_status) \
                       + list(status_cfg.additional_step_completed) \
                       + list(status_cfg.library_type_step_completed)
    ):
        (pjct_name, sample_name, process_name, process_status, date_run) = result
        all_projects[pjct_name].samples[sanitize_user_id(sample_name)].add_completed_process(process_name, date_run)

    for result in non_QC_queues(session, project_name, list_process=status_cfg.step_queued_to_status):
        pjct_name, sample_name, process_name, queued_date = result
        all_projects[pjct_name].samples[sanitize_user_id(sample_name)].add_queue_location(process_name, queued_date)

    for result in get_sample_info(session, project_name, udfs=['Prep Workflow', 'Species']):
        (pjct_name, sample_name, container, wellx, welly, udf_name, udf_value) = result
        if udf_name == 'Prep Workflow':
            all_projects[pjct_name].samples[sanitize_user_id(sample_name)].planned_library = udf_value
        if udf_name == 'Species':
            all_projects[pjct_name].samples[sanitize_user_id(sample_name)].species = udf_value

    return [p.to_json() for p in all_projects.values()]