Repository: Significant-Gravitas/Auto-GPT
File: autogpt_platform/backend/backend/data/graph.py (Python)

Code before:
    graphs = await AgentGraph.prisma().find_many(
        where=where_clause,
        distinct=["id"],
        order={"version": "desc"},
        include=AGENT_GRAPH_INCLUDE,
    )

    graph_models = []
    for graph in graphs:
        try:
            graph_models.append(GraphModel.from_db(graph))
        except Exception as e:
            logger.error(f"Error processing graph {graph.id}: {e}")
            continue

    return graph_models


async def get_graphs_executions(user_id: str) -> list[GraphExecutionMeta]:
    executions = await AgentGraphExecution.prisma().find_many(
        where={"userId": user_id},
        order={"createdAt": "desc"},
    )
    return [
        GraphExecutionMeta.from_db(
            execution, await get_graph_execution_cost(execution.id)
        )
        for execution in executions
    ]


async def get_graph_executions(graph_id: str, user_id: str) -> list[GraphExecutionMeta]:
    executions = await AgentGraphExecution.prisma().find_many(
        where={"agentGraphId": graph_id, "userId": user_id},
        order={"createdAt": "desc"},
    )
    return [
        GraphExecutionMeta.from_db(
            execution, await get_graph_execution_cost(execution.id)
        )
        for execution in executions
    ]


async def get_execution_meta(
    user_id: str, execution_id: str
) -> GraphExecutionMeta | None:
    execution = await AgentGraphExecution.prisma().find_first(
        where={"id": execution_id, "userId": user_id}
    )
    return (

Review comment (performance):
I'm not sure how much extra % of time/resources it costs to do this on every `GraphExecutionMeta` fetch. It would probably be a lot more efficient to do this through a `JOIN`, but we chose against that earlier. cc @majdyz

Code after:
        where=where_clause,
        distinct=["id"],
        order={"version": "desc"},
        include=AGENT_GRAPH_INCLUDE,
    )

    graph_models = []
    for graph in graphs:
        try:
            graph_models.append(GraphModel.from_db(graph))
        except Exception as e:
            logger.error(f"Error processing graph {graph.id}: {e}")
            continue

    return graph_models


async def get_graphs_executions(user_id: str) -> list[GraphExecutionMeta]:
    executions = await AgentGraphExecution.prisma().find_many(
        where={"userId": user_id},
        order={"createdAt": "desc"},
    )
    return [GraphExecutionMeta.from_db(execution) for execution in executions]


async def get_graph_executions(graph_id: str, user_id: str) -> list[GraphExecutionMeta]:
    executions = await AgentGraphExecution.prisma().find_many(
        where={"agentGraphId": graph_id, "userId": user_id},
        order={"createdAt": "desc"},
    )
    return [GraphExecutionMeta.from_db(execution) for execution in executions]


async def get_execution_meta(
    user_id: str, execution_id: str
) -> GraphExecutionMeta | None:
    execution = await AgentGraphExecution.prisma().find_first(
        where={"id": execution_id, "userId": user_id}
    )
    return GraphExecutionMeta.from_db(execution) if execution else None


async def get_execution(user_id: str, execution_id: str) -> GraphExecution | None:
    execution = await AgentGraphExecution.prisma().find_first(
        where={"id": execution_id, "userId": user_id},
        include={
            "AgentNodeExecutions": {
                "include": {"AgentNode": True, "Input": True, "Output": True},
                "order_by": [
                    {"queuedTime": "asc"},
                    {  # Fallback: Incomplete execs has no queuedTime.