fix: #536 #538 performance metrics — filter CDR by agentId, add team call counts

#536: Performance endpoint now accepts agentId query param and filters
CDR to that agent only. Previously returned all agents' calls as one
agent's total. Fixed 'Unanswered' → 'NotAnswered' status filter.

#538: Team performance now includes per-agent call metrics (total,
inbound, outbound, answered, missed) from CDR data + teamTotals
aggregate. Previously only returned Ozonetel time breakdown without
any call counts.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-10 19:33:59 +05:30
parent be505b8d1f
commit 0248c4cad1
2 changed files with 53 additions and 17 deletions

View File

@@ -186,20 +186,52 @@ export class SupervisorService implements OnModuleInit {
);
const agents = agentData?.agents?.edges?.map((e: any) => e.node) ?? [];
// Fetch Ozonetel time summary per agent
// Fetch CDR for the entire account for this date (one call, not per-agent)
let allCdr: any[] = [];
try {
allCdr = await this.ozonetel.fetchCDR({ date });
} catch (err) {
this.logger.warn(`Failed to fetch CDR for ${date}: ${err}`);
}
// Fetch Ozonetel time summary per agent + compute call metrics from CDR
const summaries = await Promise.all(
agents.map(async (agent: any) => {
if (!agent.ozonetelAgentId) return { ...agent, timeBreakdown: null };
if (!agent.ozonetelAgentId) return { ...agent, timeBreakdown: null, calls: null };
try {
const summary = await this.ozonetel.getAgentSummary(agent.ozonetelAgentId, date);
return { ...agent, timeBreakdown: summary };
// Filter CDR to this agent
const agentCdr = allCdr.filter(
(c: any) => c.AgentID === agent.ozonetelAgentId || c.AgentName === agent.ozonetelAgentId,
);
const totalCalls = agentCdr.length;
const inbound = agentCdr.filter((c: any) => c.Type === 'InBound').length;
const outbound = agentCdr.filter((c: any) => c.Type === 'Manual' || c.Type === 'Progressive').length;
const answered = agentCdr.filter((c: any) => c.Status === 'Answered').length;
const missed = agentCdr.filter((c: any) => c.Status === 'NotAnswered').length;
return {
...agent,
timeBreakdown: summary,
calls: { total: totalCalls, inbound, outbound, answered, missed },
};
} catch (err) {
this.logger.warn(`Failed to get summary for ${agent.ozonetelAgentId}: ${err}`);
return { ...agent, timeBreakdown: null };
return { ...agent, timeBreakdown: null, calls: null };
}
}),
);
return { date, agents: summaries };
// Aggregate team totals
const teamTotals = {
totalCalls: summaries.reduce((sum, a) => sum + (a.calls?.total ?? 0), 0),
inbound: summaries.reduce((sum, a) => sum + (a.calls?.inbound ?? 0), 0),
outbound: summaries.reduce((sum, a) => sum + (a.calls?.outbound ?? 0), 0),
answered: summaries.reduce((sum, a) => sum + (a.calls?.answered ?? 0), 0),
missed: summaries.reduce((sum, a) => sum + (a.calls?.missed ?? 0), 0),
};
return { date, agents: summaries, teamTotals };
}
}