added usage stats for agents
This commit is contained in:
parent
a0d7f6ed26
commit
f1a2c4d653
2 changed files with 108 additions and 1 deletions
|
|
@ -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
|
||||
}
|
||||
}
|
||||
];
|
||||
|
|
|
|||
|
|
@ -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()):
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue