From f1a2c4d653a643f4beefc8dea83db3f1806bcdb1 Mon Sep 17 00:00:00 2001 From: michael Date: Tue, 21 Oct 2025 07:36:11 -0500 Subject: [PATCH] added usage stats for agents --- export_shared_agents.js | 98 ++++++++++++++++++++++++++++++++++++++++- register_agents.py | 11 +++++ 2 files changed, 108 insertions(+), 1 deletion(-) diff --git a/export_shared_agents.js b/export_shared_agents.js index 4d1a0f3..2c47e55 100755 --- a/export_shared_agents.js +++ b/export_shared_agents.js @@ -24,10 +24,106 @@ const pipeline = [ }, { $unwind: { path: "$authorDetails", preserveNullAndEmptyArrays: true } }, { $set: { "agentDetails.author": "$authorDetails.email" } }, + + // NEW: Get usage timeline - daily message counts + { + $lookup: { + from: "conversations", + let: { agentId: "$agentDetails.id" }, + pipeline: [ + { + $match: { + $expr: { $eq: ["$agent_id", "$$agentId"] }, + endpoint: "agents" + } + }, + // Unwind messages array to get individual message ObjectIds + { $unwind: "$messages" }, + // Lookup actual message documents + { + $lookup: { + from: "messages", + localField: "messages", + foreignField: "_id", + as: "messageDoc" + } + }, + { $unwind: "$messageDoc" }, + // Group by date to count messages per day + { + $group: { + _id: { + $dateToString: { + format: "%Y-%m-%d", + date: "$messageDoc.createdAt" + } + }, + messageCount: { $sum: 1 } + } + }, + // Sort by date + { $sort: { _id: 1 } }, + // Reshape to nice format + { + $project: { + _id: 0, + date: "$_id", + message_count: "$messageCount" + } + } + ], + as: "usageTimeline" + } + }, + + // NEW: Get usage summary stats + { + $lookup: { + from: "conversations", + let: { agentId: "$agentDetails.id" }, + pipeline: [ + { + $match: { + $expr: { $eq: ["$agent_id", "$$agentId"] }, + endpoint: "agents" + } + }, + { + $group: { + _id: null, + conversationCount: { $sum: 1 }, + uniqueUsers: { $addToSet: "$user" }, + totalMessages: { $sum: { $size: "$messages" } }, + firstUsed: { $min: "$createdAt" }, + lastUsed: { $max: "$updatedAt" } + } + } + ], + as: "usageSummary" + } + }, + { $unwind: { path: "$usageSummary", preserveNullAndEmptyArrays: true } }, + + // Add usage data to agentDetails + { + $set: { + "agentDetails.usage_timeline": "$usageTimeline", + "agentDetails.usage_summary": { + conversation_count: { $ifNull: ["$usageSummary.conversationCount", 0] }, + unique_users: { $ifNull: [{ $size: "$usageSummary.uniqueUsers" }, 0] }, + total_messages: { $ifNull: ["$usageSummary.totalMessages", 0] }, + first_used: "$usageSummary.firstUsed", + last_used: "$usageSummary.lastUsed" + } + } + }, + { $project: { "agentDetails.versions": 0, "agentDetails.instructions": 0, - "authorDetails": 0 + "authorDetails": 0, + "usageTimeline": 0, + "usageSummary": 0 } } ]; diff --git a/register_agents.py b/register_agents.py index 6871430..972e3aa 100755 --- a/register_agents.py +++ b/register_agents.py @@ -135,6 +135,10 @@ def build_payload(agent: Dict[str, Any]) -> Dict[str, Any]: # Prune empty metadata metadata = {k: v for k, v in metadata.items() if v not in (None, "", [], {})} + # Extract usage data + usage_timeline = agent.get("usage_timeline", []) + usage_summary = agent.get("usage_summary", {}) + payload: Dict[str, Any] = { "name": name, "description": description, @@ -149,6 +153,13 @@ def build_payload(agent: Dict[str, Any]) -> Dict[str, Any]: "contact_person": contact_person, "tags": tags or None, "metadata": metadata or None, + # Usage data: + "usage_timeline": usage_timeline or None, + "conversation_count": usage_summary.get("conversation_count", 0), + "unique_users": usage_summary.get("unique_users", 0), + "total_messages": usage_summary.get("total_messages", 0), + "first_used": parse_iso(usage_summary.get("first_used")), + "last_used": parse_iso(usage_summary.get("last_used")), } # Final prune of empties so the API only sees meaningful data for k in list(payload.keys()):