<commit_before>import datetime

from flask import jsonify, abort, current_app, request
from sqlalchemy.exc import IntegrityError
from sqlalchemy import asc, desc
from app import search_api_client
from dmapiclient.audit import AuditTypes
from dmutils.config import convert_to_boolean

from .. import main
from ... import db
from ...models import User, AuditEvent, ArchivedService, Outcome
from ...models.direct_award import DirectAwardProject, DirectAwardSearch
from ...utils import (
    drop_all_other_fields,
    get_int_or_400,
    get_json_from_request,
    get_valid_page_or_1,
    json_has_required_keys,
    paginated_result_response,
    pagination_links,
    single_result_response,
    validate_and_return_updater_request,
)


def get_project_by_id_or_404(project_id: int):
    return DirectAwardProject.query.filter(DirectAwardProject.external_id == project_id).first_or_404()


@main.route('/direct-award/projects', methods=['GET'])
def list_projects():
    page = get_valid_page_or_1()

    projects = DirectAwardProject.query.options(db.joinedload(DirectAwardProject.outcome))

    includes = request.args.get('include', '').split(',')

    with_users = 'users' in includes
    if with_users:
        projects = projects.options(db.joinedload('users').lazyload('supplier'))

    user_id = get_int_or_400(request.args, 'user-id')
    if user_id:
        projects = projects.filter(DirectAwardProject.users.any(id=user_id))

    if 'latest-first' in request.args:
        if convert_to_boolean(request.args.get('latest-first')):
            projects = projects.order_by(desc(DirectAwardProject.created_at), desc(DirectAwardProject.id))
        else:
            projects = projects.order_by(asc(DirectAwardProject.created_at), asc(DirectAwardProject.id))
    else:
        projects = projects.order_by(asc(DirectAwardProject.id))<commit_msg>list_projects: be smarter about how much data we load related to outcomes<commit_after>import datetime

from flask import jsonify, abort, current_app, request
from sqlalchemy.exc import IntegrityError
from sqlalchemy import asc, desc
from app import search_api_client
from dmapiclient.audit import AuditTypes
from dmutils.config import convert_to_boolean

from .. import main
from ... import db
from ...models import User, AuditEvent, ArchivedService, Outcome
from ...models.direct_award import DirectAwardProject, DirectAwardSearch
from ...utils import (
    drop_all_other_fields,
    get_int_or_400,
    get_json_from_request,
    get_valid_page_or_1,
    json_has_required_keys,
    paginated_result_response,
    pagination_links,
    single_result_response,
    validate_and_return_updater_request,
)


def get_project_by_id_or_404(project_id: int):
    return DirectAwardProject.query.filter(DirectAwardProject.external_id == project_id).first_or_404()


@main.route('/direct-award/projects', methods=['GET'])
def list_projects():
    page = get_valid_page_or_1()

    projects = DirectAwardProject.query.options(
        db.joinedload(
            DirectAwardProject.outcome
        ).joinedload(
            Outcome.direct_award_archived_service
        ).load_only(
            "service_id"
        )
    )

    includes = request.args.get('include', '').split(',')

    with_users = 'users' in includes
    if with_users:
        projects = projects.options(db.joinedload('users').lazyload('supplier'))

    user_id = get_int_or_400(request.args, 'user-id')
    if user_id:
        projects = projects.filter(DirectAwardProject.users.any(id=user_id))

    if 'latest-first' in request.args:
        if convert_to_boolean(request.args.get('latest-first')):
            projects = projects.order_by(desc(DirectAwardProject.created_at), desc(DirectAwardProject.id))
        else:
            projects = projects.order_by(asc(DirectAwardProject.created_at), asc(DirectAwardProject.id))
    else:
        projects = projects.order_by(asc(DirectAwardProject.id))
{'fraction_non_alphanumeric': 0.09540816326530613, 'fraction_numerical': 0.007142857142857143, 'mean_word_length': 4.2349799732977305, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2464488', 'n_tokens_mistral': 1244, 'n_tokens_neox': 1212, 'n_words': 232}