<commit_before>import asyncio
import os

import aiojobs.aiohttp

import virtool.db.history
import virtool.db.processes
import virtool.db.refs
import virtool.db.utils
import virtool.http.routes
import virtool.kinds
import virtool.utils
from virtool.api.utils import compose_regex_query, json_response, not_found, paginate

routes = virtool.http.routes.Routes()


@routes.get("/api/refs")
async def find(req):
    db = req.app["db"]

    term = req.query.get("find", None)

    db_query = dict()

    if term:
        db_query.update(compose_regex_query(term, ["name", "data_type"]))

    data = await paginate(db.refs, db_query, req.query, sort="name")

    for d in data["documents"]:
        contributors, internal_control, latest_build = await asyncio.gather(
            virtool.db.refs.get_contributors(db, d["id"]),
            virtool.db.refs.get_internal_control(db, d["id"]),
            virtool.db.refs.get_latest_build(db, d["id"])
        )

        d["contributors"] = contributors
        d["internal_control"] = internal_control
        d["latest_build"] = latest_build

    return json_response(data)


@routes.get("/api/refs/{ref_id}")
async def get(req):
    """
    Get the complete representation of a specfic reference.

    """
    db = req.app["db"]

    ref_id = req.match_info["ref_id"]

    document = await db.refs.find_one(ref_id)

    if not document:
        return not_found()

    contributors, internal_control, latest_build = await asyncio.gather(
        virtool.db.refs.get_contributors(db, ref_id),
        virtool.db.refs.get_internal_control(db, ref_id),
        virtool.db.refs.get_latest_build(db, ref_id)
    )

    document["contributors"] = contributors
    document["internal_control"] = internal_control
    document["latest_build"] = latest_build

    return json_response(document)


@routes.post("/api/refs", permission="create_ref", schema={
    "name": {
        "type": "string",
        "required": True
    },
    "description": {
        "type": "string",
        "default": ""
    },
    "data_type": {
        "type": "string",
        "allowed": ["genome", "barcode"],
        "default": "genome"
    },
    "import_from": {
        "type": "string"
    },
    "organism": {
        "type": "string"
    },
    "public": {
        "type": "boolean",
        "default": False
    }

})<commit_msg>Add find kinds to refs api<commit_after>import asyncio
import os

import aiojobs.aiohttp

import virtool.db.history
import virtool.db.processes
import virtool.db.refs
import virtool.db.utils
import virtool.http.routes
import virtool.kinds
import virtool.utils
from virtool.api.utils import compose_regex_query, json_response, not_found, paginate

routes = virtool.http.routes.Routes()


@routes.get("/api/refs")
async def find(req):
    db = req.app["db"]

    term = req.query.get("find", None)

    db_query = dict()

    if term:
        db_query.update(compose_regex_query(term, ["name", "data_type"]))

    data = await paginate(db.refs, db_query, req.query, sort="name")

    for d in data["documents"]:
        contributors, internal_control, latest_build = await asyncio.gather(
            virtool.db.refs.get_contributors(db, d["id"]),
            virtool.db.refs.get_internal_control(db, d["id"]),
            virtool.db.refs.get_latest_build(db, d["id"])
        )

        d["contributors"] = contributors
        d["internal_control"] = internal_control
        d["latest_build"] = latest_build

    return json_response(data)


@routes.get("/api/refs/{ref_id}")
async def get(req):
    """
    Get the complete representation of a specfic reference.

    """
    db = req.app["db"]

    ref_id = req.match_info["ref_id"]

    document = await db.refs.find_one(ref_id)

    if not document:
        return not_found()

    contributors, internal_control, latest_build = await asyncio.gather(
        virtool.db.refs.get_contributors(db, ref_id),
        virtool.db.refs.get_internal_control(db, ref_id),
        virtool.db.refs.get_latest_build(db, ref_id)
    )

    document["contributors"] = contributors
    document["internal_control"] = internal_control
    document["latest_build"] = latest_build

    return json_response(document)


@routes.get("/api/refs/{ref_id}/kinds")
async def find_kinds(req):
    db = req.app["db"]

    ref_id = req.match_info["ref_id"]

    term = req.query.get("find", None)
    verified = req.query.get("verified", None)
    names = req.query.get("names", False)

    db_query = dict()

    if term:
        db_query.update(compose_regex_query(term, ["name", "abbreviation"]))

    if verified is not None:
        db_query["verified"] = virtool.utils.to_bool(verified)

    if names in [True, "true"]:
        data = await db.kinds.find(db_query, ["name"], sort=[("name", 1)]).to_list(None)
        data = [virtool.utils.base_processor(d) for d in data]
    else:
        data = await paginate(
            db.kinds,
            db_query,
            req.query,
            sort="name", projection=virtool.kinds.LIST_PROJECTION)
        data["modified_count"] = len(await db.history.find({"index.id": "unbuilt"}, ["kind"]).distinct("kind.name"))

    return json_response(data)


@routes.post("/api/refs", permission="create_ref", schema={
    "name": {
        "type": "string",
        "required": True
    },
    "description": {
        "type": "string",
        "default": ""
    },
    "data_type": {
        "type": "string",
        "allowed": ["genome", "barcode"],
        "default": "genome"
    },
    "import_from": {
        "type": "string"
    },
    "organism": {
        "type": "string"
    },
    "public": {
        "type": "boolean",
        "default": False
    }

})
{'fraction_non_alphanumeric': 0.14626865671641792, 'fraction_numerical': 0.0001755926251097454, 'mean_word_length': 2.922865013774105, 'pattern_counts': {'":': 35, '<': 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': '726123', 'n_tokens_mistral': 1921, 'n_tokens_neox': 1859, 'n_words': 365}