Ejemplo n.º 1
0
def read(project_id: str, model_id: str) -> List[Dict[str, Any]]:
    """
    Get a list with every story name
    :return: list with the names
    """
    pipeline = [
        {
            "$lookup": {
                "from": "interactions",
                "localField": "_id",
                "foreignField": "story_id",
                "as": "interactions",
            }
        },
        {
            "$match": {
                "project_id": ObjectId(project_id),
            }
        },
        {
            "$project": {
                "name": 1,
                "interactions_length": {
                    "$size": "$interactions"
                }
            }
        },
    ]

    return json.objectid_to_id(
        json.loads(json_mongo.dumps(STORIES_COLL.aggregate(pipeline))))
Ejemplo n.º 2
0
def read() -> List[Dict[str, Any]]:
    """
    Read project data from database
    :return: project data
    """
    pipeline = [
        {
            "$lookup": {
                "from": "models",
                "localField": "_id",
                "foreignField": "project_id",
                "as": "models",
            }
        },
        {
            "$project": {
                "name": 1,
                "models_length": {
                    "$size": "$models"
                }
            }
        },
    ]

    return json.objectid_to_id(
        json.loads(json_mongo.dumps(PROJECTS_COLL.aggregate(pipeline))))
Ejemplo n.º 3
0
def read(project_id: str, model_id: str) -> List[Dict[str, Any]]:
    """
    Get the information of every stored intent
    :return: a dictionary with every intent and its related data
    """
    pipeline = [
        {
            "$lookup": {
                "from": "templates",
                "localField": "_id",
                "foreignField": "intent_id",
                "as": "templates",
            }
        },
        {
            "$match": {
                "project_id": ObjectId(project_id),
                "model_id": ObjectId(model_id),
            }
        },
        {
            "$project": {
                "name": 1,
                "templates_length": {
                    "$size": "$templates"
                }
            }
        },
    ]

    return json.objectid_to_id(
        json.loads(json_mongo.dumps(INTENTS_COLL.aggregate(pipeline))))
def read(project_id: str, model_id: str) -> List[Dict[str, Any]]:
    """
    Return information from entities
    :return: data of the entities
    """
    pipeline = [
        {
            "$lookup": {
                "from": "values",
                "localField": "_id",
                "foreignField": "entity_id",
                "as": "values",
            }
        },
        {
            "$match": {
                "project_id": ObjectId(project_id),
            }
        },
        {
            "$project": {
                "name": 1,
                "color": 1,
                "intents": 1,
                "text_color": 1,
                "intents_length": {"$size": "$intents"},
                "values_length": {"$size": "$values"},
            }
        },
    ]

    data = list(ENTITIES_COLL.aggregate(pipeline))

    for doc in data:
        intent_list = doc["intents"]
        entity = doc["name"]
        regx = re.compile(r"\${" + entity + "}", re.IGNORECASE)
        for intent in intent_list:
            intent["templates_length"] = TEMPLATES_COLL.count_documents(
                {"intent_id": intent["id"], "name": {"$regex": regx}}
            )

    return json.objectid_to_id(json.loads(json_mongo.dumps(data)))
def read_values(project_id: str, entity_id: str) -> List[Dict[str, Any]]:
    """
    Return data from an entity with id [entity_id]
    :return: entity data
    """

    return json.objectid_to_id(
        json.loads(
            json_mongo.dumps(
                VALUES_COLL.find(
                    {
                        "project_id": ObjectId(project_id),
                        "entity_id": ObjectId(entity_id),
                    },
                    {"project_id": 0, "model_id": 0, "entity_id": 0},
                )
            )
        )
    )
Ejemplo n.º 6
0
def read_models(project_id: str) -> List[Dict[str, Any]]:
    """
    Read data of every model stored related to a project with id [project_id]
    :param project_id: id of the project
    :return: a list with the data of the models
    """
    pipeline = [
        {
            "$lookup": {
                "from": "stories",
                "localField": "_id",
                "foreignField": "model_id",
                "as": "stories",
            }
        },
        {
            "$match": {
                "project_id": ObjectId(project_id)
            }
        },
        {
            "$project": {
                "name": 1,
                "stories_length": {
                    "$size": "$stories"
                }
            }
        },
    ]

    data = list(MODELS_COLL.aggregate(pipeline))
    project_name = read_project_name_from_id(project_id)
    for doc in data:
        model_name = read_model_name_from_id(doc["_id"])
        if os.path.exists(os.path.join(MODEL_PATH, project_name, model_name)):
            doc["last_trained_timestamp"] = datetime.fromtimestamp(
                os.path.getctime(
                    get_latest_model(
                        os.path.join(MODEL_PATH, project_name,
                                     model_name)))).isoformat()

    return json.objectid_to_id(json.loads(json_mongo.dumps(data)))
Ejemplo n.º 7
0
def read_templates(project_id: str,
                   model_id: str,
                   intent_id: str = None) -> List[Dict[str, Any]]:
    """
    Get templates of every intent or an specific one with id [intent_id]
    :param intent_id: id of the intent
    :return:
    """
    data = {"project_id": ObjectId(project_id), "model_id": ObjectId(model_id)}

    if intent_id:
        data["intent_id"] = ObjectId(intent_id)

    return json.objectid_to_id(
        json.loads(
            json_mongo.dumps(
                TEMPLATES_COLL.find(data, {
                    "project_id": 0,
                    "model_id": 0
                }))))
Ejemplo n.º 8
0
def read_interactions(project_id: str, story_id: str) -> List[Dict[str, Any]]:
    """
    Gets every interaction associated to the story with id [story_id]
    :param project_id:
    :param story_id:
    :return:
    """

    return json.objectid_to_id(
        json.loads(
            json_mongo.dumps(
                INTERACTIONS_COLL.find(
                    {
                        "project_id": ObjectId(project_id),
                        "story_id": ObjectId(story_id),
                    },
                    {
                        "project_id": 0,
                        "model_id": 0,
                        "story_id": 0
                    },
                ))))