Skip to content

Commit

Permalink
Add download counts for top agents
Browse files Browse the repository at this point in the history
  • Loading branch information
Swiftyos committed Aug 2, 2024
1 parent 98714a9 commit b29a02c
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 28 deletions.
3 changes: 3 additions & 0 deletions rnd/autogpt_builder/src/app/marketplace/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ const AgentCard: React.FC<{ agent: Agent; featured?: boolean }> = ({ agent, feat
<div className="text-xs text-gray-400">
Updated {new Date(agent.updatedAt).toLocaleDateString()}
</div>
<div className="text-xs text-gray-400">
Downloads {agent.downloads}
</div>
{'rank' in agent && (
<div className="text-xs text-indigo-600">
Rank: {agent.rank.toFixed(2)}
Expand Down
2 changes: 2 additions & 0 deletions rnd/autogpt_builder/src/lib/marketplace-api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export type Agent = {
version: number;
createdAt: string; // ISO8601 datetime string
updatedAt: string; // ISO8601 datetime string
views: number;
downloads: number;
};

export type AgentList = {
Expand Down
40 changes: 31 additions & 9 deletions rnd/market/market/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import prisma.errors
import prisma.models
import prisma.types
import pydantic

import market.model
import market.utils.extension_types


Expand All @@ -14,6 +16,25 @@ class AgentQueryError(Exception):
pass


class TopAgentsDBResponse(pydantic.BaseModel):
"""
Represents a response containing a list of top agents.
Attributes:
analytics (list[AgentResponse]): The list of top agents.
total_count (int): The total count of agents.
page (int): The current page number.
page_size (int): The number of agents per page.
total_pages (int): The total number of pages.
"""

analytics: list[prisma.models.AnalyticsTracker]
total_count: int
page: int
page_size: int
total_pages: int


async def create_agent_entry(
name: str,
description: str,
Expand Down Expand Up @@ -267,7 +288,9 @@ async def search_db(
raise AgentQueryError(f"Unexpected error occurred: {str(e)}")


async def get_top_agents_by_downloads(page: int = 1, page_size: int = 10):
async def get_top_agents_by_downloads(
page: int = 1, page_size: int = 10
) -> TopAgentsDBResponse:
"""Retrieve the top agents by download count.
Args:
Expand Down Expand Up @@ -296,14 +319,13 @@ async def get_top_agents_by_downloads(page: int = 1, page_size: int = 10):
# Get total count for pagination info
total_count = len(analytics)

return {
# should this be analytics as the top object?
"agents": [agent.agent for agent in analytics],
"total_count": total_count,
"page": page,
"page_size": page_size,
"total_pages": (total_count + page_size - 1) // page_size, # floor division
}
return TopAgentsDBResponse(
analytics=analytics,
total_count=total_count,
page=page,
page_size=page_size,
total_pages=(total_count + page_size - 1) // page_size,
)

except AgentQueryError as e:
# Log the error or handle it as needed
Expand Down
2 changes: 2 additions & 0 deletions rnd/market/market/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class AgentResponse(pydantic.BaseModel):
version: int
createdAt: datetime.datetime
updatedAt: datetime.datetime
views: int = 0
downloads: int = 0


class AgentListResponse(pydantic.BaseModel):
Expand Down
34 changes: 24 additions & 10 deletions rnd/market/market/routes/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,18 +230,32 @@ async def top_agents_by_downloads(
page_size=page_size,
)

agents = [
market.model.AgentResponse(**agent.dict()) for agent in result["agents"]
]

return market.model.AgentListResponse(
agents=agents,
total_count=result["total_count"],
page=result["page"],
page_size=result["page_size"],
total_pages=result["total_pages"],
ret = market.model.AgentListResponse(
total_count=result.total_count,
page=result.page,
page_size=result.page_size,
total_pages=result.total_pages,
agents=[
market.model.AgentResponse(
id=item.agent.id,
name=item.agent.name,
description=item.agent.description,
author=item.agent.author,
keywords=item.agent.keywords,
categories=item.agent.categories,
version=item.agent.version,
createdAt=item.agent.createdAt,
updatedAt=item.agent.updatedAt,
views=item.views,
downloads=item.downloads,
)
for item in result.analytics
if item.agent is not None
],
)

return ret

except market.db.AgentQueryError as e:
raise fastapi.HTTPException(status_code=400, detail=str(e)) from e
except Exception as e:
Expand Down
25 changes: 16 additions & 9 deletions rnd/market/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,36 @@ def lint():
print("Lint failed, try running `poetry run format` to fix the issues: ", e)
raise e


def populate_database():
import requests
import pathlib
import glob
import json
import pathlib

import requests

import market.model

templates = pathlib.Path(__file__).parent.parent / "autogpt_server" / "graph_templates"


templates = (
pathlib.Path(__file__).parent.parent / "autogpt_server" / "graph_templates"
)

all_files = glob.glob(str(templates / "*.json"))

for file in all_files:
with open(file, "r") as f:
data = f.read()
req = market.model.AddAgentRequest(
graph=json.loads(data),
author="Populate DB",
categories=["Pre-Populated"],
keywords=["test"]
keywords=["test"],
)
response = requests.post(
"http://localhost:8001/market/admin/agent", json=req.model_dump()
)
response = requests.post("http://localhost:8001/market/admin/agent", json=req.model_dump())
print(response.text)


def format():
run("ruff", "check", "--fix", ".")
Expand Down

0 comments on commit b29a02c

Please sign in to comment.