Reduce Convex report memory footprint
This commit is contained in:
parent
87f729b80f
commit
c3ee23f967
1 changed files with 332 additions and 254 deletions
|
|
@ -169,6 +169,120 @@ function resolveScopedCompanyId(
|
||||||
return companyId;
|
return companyId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type TicketProcessor = (ticket: Doc<"tickets">) => void | Promise<void>;
|
||||||
|
|
||||||
|
async function forEachScopedTicket(
|
||||||
|
ctx: QueryCtx,
|
||||||
|
tenantId: string,
|
||||||
|
viewer: Awaited<ReturnType<typeof requireStaff>>,
|
||||||
|
processor: TicketProcessor,
|
||||||
|
) {
|
||||||
|
const scopedCompanyId = resolveScopedCompanyId(viewer);
|
||||||
|
await paginateTickets(
|
||||||
|
() =>
|
||||||
|
scopedCompanyId
|
||||||
|
? ctx.db
|
||||||
|
.query("tickets")
|
||||||
|
.withIndex("by_tenant_company", (q) => q.eq("tenantId", tenantId).eq("companyId", scopedCompanyId))
|
||||||
|
.order("desc")
|
||||||
|
: ctx.db.query("tickets").withIndex("by_tenant", (q) => q.eq("tenantId", tenantId)).order("desc"),
|
||||||
|
async (ticket) => {
|
||||||
|
await processor(ticket);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function forEachTenantTicket(ctx: QueryCtx, tenantId: string, processor: TicketProcessor) {
|
||||||
|
await paginateTickets(
|
||||||
|
() => ctx.db.query("tickets").withIndex("by_tenant", (q) => q.eq("tenantId", tenantId)).order("desc"),
|
||||||
|
async (ticket) => {
|
||||||
|
await processor(ticket);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function forEachScopedTicketByCreatedRange(
|
||||||
|
ctx: QueryCtx,
|
||||||
|
tenantId: string,
|
||||||
|
viewer: Awaited<ReturnType<typeof requireStaff>>,
|
||||||
|
startMs: number,
|
||||||
|
endMs: number,
|
||||||
|
companyId: Id<"companies"> | undefined,
|
||||||
|
processor: TicketProcessor,
|
||||||
|
) {
|
||||||
|
const scopedCompanyId = resolveScopedCompanyId(viewer, companyId);
|
||||||
|
const query = scopedCompanyId
|
||||||
|
? ctx.db
|
||||||
|
.query("tickets")
|
||||||
|
.withIndex("by_tenant_company_created", (q) => {
|
||||||
|
let range = q.eq("tenantId", tenantId).eq("companyId", scopedCompanyId);
|
||||||
|
range = withLowerBound(range, "createdAt", startMs);
|
||||||
|
range = withUpperBound(range, "createdAt", endMs);
|
||||||
|
return range;
|
||||||
|
})
|
||||||
|
.order("desc")
|
||||||
|
: ctx.db
|
||||||
|
.query("tickets")
|
||||||
|
.withIndex("by_tenant_created", (q) => {
|
||||||
|
let range = q.eq("tenantId", tenantId);
|
||||||
|
range = withLowerBound(range, "createdAt", startMs);
|
||||||
|
range = withUpperBound(range, "createdAt", endMs);
|
||||||
|
return range;
|
||||||
|
})
|
||||||
|
.order("desc");
|
||||||
|
|
||||||
|
await paginateTickets(
|
||||||
|
() => query,
|
||||||
|
async (ticket) => {
|
||||||
|
const createdAt = typeof ticket.createdAt === "number" ? ticket.createdAt : null;
|
||||||
|
if (createdAt === null) return;
|
||||||
|
if (createdAt < startMs || createdAt >= endMs) return;
|
||||||
|
await processor(ticket);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function forEachScopedTicketByResolvedRange(
|
||||||
|
ctx: QueryCtx,
|
||||||
|
tenantId: string,
|
||||||
|
viewer: Awaited<ReturnType<typeof requireStaff>>,
|
||||||
|
startMs: number,
|
||||||
|
endMs: number,
|
||||||
|
companyId: Id<"companies"> | undefined,
|
||||||
|
processor: TicketProcessor,
|
||||||
|
) {
|
||||||
|
const scopedCompanyId = resolveScopedCompanyId(viewer, companyId);
|
||||||
|
const query = scopedCompanyId
|
||||||
|
? ctx.db
|
||||||
|
.query("tickets")
|
||||||
|
.withIndex("by_tenant_company_resolved", (q) => {
|
||||||
|
let range = q.eq("tenantId", tenantId).eq("companyId", scopedCompanyId);
|
||||||
|
range = withLowerBound(range, "resolvedAt", startMs);
|
||||||
|
range = withUpperBound(range, "resolvedAt", endMs);
|
||||||
|
return range;
|
||||||
|
})
|
||||||
|
.order("desc")
|
||||||
|
: ctx.db
|
||||||
|
.query("tickets")
|
||||||
|
.withIndex("by_tenant_resolved", (q) => {
|
||||||
|
let range = q.eq("tenantId", tenantId);
|
||||||
|
range = withLowerBound(range, "resolvedAt", startMs);
|
||||||
|
range = withUpperBound(range, "resolvedAt", endMs);
|
||||||
|
return range;
|
||||||
|
})
|
||||||
|
.order("desc");
|
||||||
|
|
||||||
|
await paginateTickets(
|
||||||
|
() => query,
|
||||||
|
async (ticket) => {
|
||||||
|
const resolvedAt = typeof ticket.resolvedAt === "number" ? ticket.resolvedAt : null;
|
||||||
|
if (resolvedAt === null) return;
|
||||||
|
if (resolvedAt < startMs || resolvedAt >= endMs) return;
|
||||||
|
await processor(ticket);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export async function fetchOpenScopedTickets(
|
export async function fetchOpenScopedTickets(
|
||||||
ctx: QueryCtx,
|
ctx: QueryCtx,
|
||||||
tenantId: string,
|
tenantId: string,
|
||||||
|
|
@ -255,12 +369,9 @@ function isNotNull<T>(value: T | null): value is T {
|
||||||
|
|
||||||
export async function fetchTickets(ctx: QueryCtx, tenantId: string) {
|
export async function fetchTickets(ctx: QueryCtx, tenantId: string) {
|
||||||
const results: Doc<"tickets">[] = [];
|
const results: Doc<"tickets">[] = [];
|
||||||
await paginateTickets(
|
await forEachTenantTicket(ctx, tenantId, (ticket) => {
|
||||||
() => ctx.db.query("tickets").withIndex("by_tenant", (q) => q.eq("tenantId", tenantId)).order("desc"),
|
results.push(ticket);
|
||||||
(ticket) => {
|
});
|
||||||
results.push(ticket);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -284,20 +395,9 @@ export async function fetchScopedTickets(
|
||||||
const scopedCompanyId = resolveScopedCompanyId(viewer);
|
const scopedCompanyId = resolveScopedCompanyId(viewer);
|
||||||
const results: Doc<"tickets">[] = [];
|
const results: Doc<"tickets">[] = [];
|
||||||
|
|
||||||
await paginateTickets(
|
await forEachScopedTicket(ctx, tenantId, viewer, (ticket) => {
|
||||||
() => {
|
results.push(ticket);
|
||||||
if (scopedCompanyId) {
|
});
|
||||||
return ctx.db
|
|
||||||
.query("tickets")
|
|
||||||
.withIndex("by_tenant_company", (q) => q.eq("tenantId", tenantId).eq("companyId", scopedCompanyId))
|
|
||||||
.order("desc");
|
|
||||||
}
|
|
||||||
return ctx.db.query("tickets").withIndex("by_tenant", (q) => q.eq("tenantId", tenantId)).order("desc");
|
|
||||||
},
|
|
||||||
(ticket) => {
|
|
||||||
results.push(ticket);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
@ -310,36 +410,10 @@ export async function fetchScopedTicketsByCreatedRange(
|
||||||
endMs: number,
|
endMs: number,
|
||||||
companyId?: Id<"companies">,
|
companyId?: Id<"companies">,
|
||||||
) {
|
) {
|
||||||
const scopedCompanyId = resolveScopedCompanyId(viewer, companyId);
|
|
||||||
const results: Doc<"tickets">[] = [];
|
const results: Doc<"tickets">[] = [];
|
||||||
|
await forEachScopedTicketByCreatedRange(ctx, tenantId, viewer, startMs, endMs, companyId, (ticket) => {
|
||||||
const query = scopedCompanyId
|
|
||||||
? ctx.db
|
|
||||||
.query("tickets")
|
|
||||||
.withIndex("by_tenant_company_created", (q) => {
|
|
||||||
let range = q.eq("tenantId", tenantId).eq("companyId", scopedCompanyId);
|
|
||||||
range = withLowerBound(range, "createdAt", startMs);
|
|
||||||
range = withUpperBound(range, "createdAt", endMs);
|
|
||||||
return range;
|
|
||||||
})
|
|
||||||
.order("desc")
|
|
||||||
: ctx.db
|
|
||||||
.query("tickets")
|
|
||||||
.withIndex("by_tenant_created", (q) => {
|
|
||||||
let range = q.eq("tenantId", tenantId);
|
|
||||||
range = withLowerBound(range, "createdAt", startMs);
|
|
||||||
range = withUpperBound(range, "createdAt", endMs);
|
|
||||||
return range;
|
|
||||||
})
|
|
||||||
.order("desc");
|
|
||||||
|
|
||||||
const snapshot = await query.collect();
|
|
||||||
for (const ticket of snapshot) {
|
|
||||||
const createdAt = typeof ticket.createdAt === "number" ? ticket.createdAt : null;
|
|
||||||
if (createdAt === null) continue;
|
|
||||||
if (createdAt < startMs || createdAt >= endMs) continue;
|
|
||||||
results.push(ticket);
|
results.push(ticket);
|
||||||
}
|
});
|
||||||
|
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
@ -352,36 +426,10 @@ export async function fetchScopedTicketsByResolvedRange(
|
||||||
endMs: number,
|
endMs: number,
|
||||||
companyId?: Id<"companies">,
|
companyId?: Id<"companies">,
|
||||||
) {
|
) {
|
||||||
const scopedCompanyId = resolveScopedCompanyId(viewer, companyId);
|
|
||||||
const results: Doc<"tickets">[] = [];
|
const results: Doc<"tickets">[] = [];
|
||||||
|
await forEachScopedTicketByResolvedRange(ctx, tenantId, viewer, startMs, endMs, companyId, (ticket) => {
|
||||||
const query = scopedCompanyId
|
|
||||||
? ctx.db
|
|
||||||
.query("tickets")
|
|
||||||
.withIndex("by_tenant_company_resolved", (q) => {
|
|
||||||
let range = q.eq("tenantId", tenantId).eq("companyId", scopedCompanyId);
|
|
||||||
range = withLowerBound(range, "resolvedAt", startMs);
|
|
||||||
range = withUpperBound(range, "resolvedAt", endMs);
|
|
||||||
return range;
|
|
||||||
})
|
|
||||||
.order("desc")
|
|
||||||
: ctx.db
|
|
||||||
.query("tickets")
|
|
||||||
.withIndex("by_tenant_resolved", (q) => {
|
|
||||||
let range = q.eq("tenantId", tenantId);
|
|
||||||
range = withLowerBound(range, "resolvedAt", startMs);
|
|
||||||
range = withUpperBound(range, "resolvedAt", endMs);
|
|
||||||
return range;
|
|
||||||
})
|
|
||||||
.order("desc");
|
|
||||||
|
|
||||||
const snapshot = await query.collect();
|
|
||||||
for (const ticket of snapshot) {
|
|
||||||
const resolvedAt = typeof ticket.resolvedAt === "number" ? ticket.resolvedAt : null;
|
|
||||||
if (resolvedAt === null) continue;
|
|
||||||
if (resolvedAt < startMs || resolvedAt >= endMs) continue;
|
|
||||||
results.push(ticket);
|
results.push(ticket);
|
||||||
}
|
});
|
||||||
|
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
@ -393,6 +441,30 @@ async function fetchQueues(ctx: QueryCtx, tenantId: string) {
|
||||||
.collect();
|
.collect();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type CompanySummary = {
|
||||||
|
name: string;
|
||||||
|
isAvulso: boolean;
|
||||||
|
contractedHoursPerMonth: number | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
async function getCompanySummary(
|
||||||
|
ctx: QueryCtx,
|
||||||
|
companyId: Id<"companies">,
|
||||||
|
cache: Map<string, CompanySummary>,
|
||||||
|
): Promise<CompanySummary> {
|
||||||
|
const key = String(companyId);
|
||||||
|
const cached = cache.get(key);
|
||||||
|
if (cached) return cached;
|
||||||
|
const company = await ctx.db.get(companyId);
|
||||||
|
const summary: CompanySummary = {
|
||||||
|
name: company?.name ?? "Sem empresa",
|
||||||
|
isAvulso: Boolean(company?.isAvulso ?? false),
|
||||||
|
contractedHoursPerMonth: company?.contractedHoursPerMonth ?? null,
|
||||||
|
};
|
||||||
|
cache.set(key, summary);
|
||||||
|
return summary;
|
||||||
|
}
|
||||||
|
|
||||||
function deriveMachineStatus(machine: Doc<"machines">, now: number) {
|
function deriveMachineStatus(machine: Doc<"machines">, now: number) {
|
||||||
if (machine.isActive === false) {
|
if (machine.isActive === false) {
|
||||||
return "deactivated";
|
return "deactivated";
|
||||||
|
|
@ -433,79 +505,6 @@ type CsatSurvey = {
|
||||||
assigneeName: string | null;
|
assigneeName: string | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
async function collectCsatSurveys(ctx: QueryCtx, tickets: Doc<"tickets">[]): Promise<CsatSurvey[]> {
|
|
||||||
const perTicket = await Promise.all(
|
|
||||||
tickets.map(async (ticket) => {
|
|
||||||
if (typeof ticket.csatScore === "number") {
|
|
||||||
const snapshot = (ticket.csatAssigneeSnapshot ?? null) as {
|
|
||||||
name?: string | null
|
|
||||||
email?: string | null
|
|
||||||
} | null
|
|
||||||
const assigneeId =
|
|
||||||
ticket.csatAssigneeId && typeof ticket.csatAssigneeId === "string"
|
|
||||||
? ticket.csatAssigneeId
|
|
||||||
: ticket.csatAssigneeId
|
|
||||||
? String(ticket.csatAssigneeId)
|
|
||||||
: ticket.assigneeId
|
|
||||||
? String(ticket.assigneeId)
|
|
||||||
: null
|
|
||||||
const assigneeName = snapshot?.name?.trim?.() || snapshot?.email?.trim?.() || null
|
|
||||||
const maxScore =
|
|
||||||
typeof ticket.csatMaxScore === "number" && Number.isFinite(ticket.csatMaxScore)
|
|
||||||
? (ticket.csatMaxScore as number)
|
|
||||||
: 5
|
|
||||||
const receivedAtRaw =
|
|
||||||
typeof ticket.csatRatedAt === "number"
|
|
||||||
? ticket.csatRatedAt
|
|
||||||
: typeof ticket.resolvedAt === "number"
|
|
||||||
? ticket.resolvedAt
|
|
||||||
: ticket.updatedAt ?? ticket.createdAt
|
|
||||||
if (typeof receivedAtRaw !== "number") {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
return [
|
|
||||||
{
|
|
||||||
ticketId: ticket._id,
|
|
||||||
reference: ticket.reference,
|
|
||||||
score: ticket.csatScore,
|
|
||||||
maxScore,
|
|
||||||
comment:
|
|
||||||
typeof ticket.csatComment === "string" && ticket.csatComment.trim().length > 0
|
|
||||||
? ticket.csatComment.trim()
|
|
||||||
: null,
|
|
||||||
receivedAt: receivedAtRaw,
|
|
||||||
assigneeId,
|
|
||||||
assigneeName,
|
|
||||||
} satisfies CsatSurvey,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
const events = await ctx.db
|
|
||||||
.query("ticketEvents")
|
|
||||||
.withIndex("by_ticket", (q) => q.eq("ticketId", ticket._id))
|
|
||||||
.collect();
|
|
||||||
return events
|
|
||||||
.filter((event) => event.type === "CSAT_RECEIVED" || event.type === "CSAT_RATED")
|
|
||||||
.map((event) => {
|
|
||||||
const score = extractScore(event.payload);
|
|
||||||
if (score === null) return null;
|
|
||||||
const assignee = extractAssignee(event.payload);
|
|
||||||
return {
|
|
||||||
ticketId: ticket._id,
|
|
||||||
reference: ticket.reference,
|
|
||||||
score,
|
|
||||||
maxScore: extractMaxScore(event.payload) ?? 5,
|
|
||||||
comment: extractComment(event.payload),
|
|
||||||
receivedAt: event.createdAt,
|
|
||||||
assigneeId: assignee.id,
|
|
||||||
assigneeName: assignee.name,
|
|
||||||
} as CsatSurvey;
|
|
||||||
})
|
|
||||||
.filter(isNotNull);
|
|
||||||
})
|
|
||||||
);
|
|
||||||
return perTicket.flat();
|
|
||||||
}
|
|
||||||
|
|
||||||
function formatDateKey(timestamp: number) {
|
function formatDateKey(timestamp: number) {
|
||||||
const date = new Date(timestamp);
|
const date = new Date(timestamp);
|
||||||
const year = date.getUTCFullYear();
|
const year = date.getUTCFullYear();
|
||||||
|
|
@ -534,33 +533,16 @@ export async function slaOverviewHandler(
|
||||||
) {
|
) {
|
||||||
const viewer = await requireStaff(ctx, viewerId, tenantId);
|
const viewer = await requireStaff(ctx, viewerId, tenantId);
|
||||||
const { startMs, endMs, days } = resolveRangeWindow(range, dateFrom, dateTo, 90);
|
const { startMs, endMs, days } = resolveRangeWindow(range, dateFrom, dateTo, 90);
|
||||||
const inRange = await fetchScopedTicketsByCreatedRange(ctx, tenantId, viewer, startMs, endMs, companyId);
|
|
||||||
const queues = await fetchQueues(ctx, tenantId);
|
const queues = await fetchQueues(ctx, tenantId);
|
||||||
const categoriesMap = await fetchCategoryMap(ctx, tenantId);
|
const categoriesMap = await fetchCategoryMap(ctx, tenantId);
|
||||||
|
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
const openTickets = inRange.filter((ticket) => OPEN_STATUSES.has(normalizeStatus(ticket.status)));
|
const totals = { total: 0, open: 0, resolved: 0, overdue: 0 };
|
||||||
const resolvedTickets = inRange.filter((ticket) => {
|
const queueOpenCounts = new Map<string, number>();
|
||||||
const status = normalizeStatus(ticket.status);
|
let firstResponseSum = 0;
|
||||||
return status === "RESOLVED";
|
let firstResponseCount = 0;
|
||||||
});
|
let resolutionSum = 0;
|
||||||
const overdueTickets = openTickets.filter((ticket) => ticket.dueAt && ticket.dueAt < now);
|
let resolutionCount = 0;
|
||||||
|
|
||||||
const firstResponseTimes = inRange
|
|
||||||
.filter((ticket) => ticket.firstResponseAt)
|
|
||||||
.map((ticket) => (ticket.firstResponseAt! - ticket.createdAt) / 60000);
|
|
||||||
const resolutionTimes = resolvedTickets
|
|
||||||
.filter((ticket) => ticket.resolvedAt)
|
|
||||||
.map((ticket) => (ticket.resolvedAt! - ticket.createdAt) / 60000);
|
|
||||||
|
|
||||||
const queueBreakdown = queues.map((queue) => {
|
|
||||||
const count = openTickets.filter((ticket) => ticket.queueId === queue._id).length;
|
|
||||||
return {
|
|
||||||
id: queue._id,
|
|
||||||
name: queue.name,
|
|
||||||
open: count,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
const categoryStats = new Map<
|
const categoryStats = new Map<
|
||||||
string,
|
string,
|
||||||
|
|
@ -574,13 +556,37 @@ export async function slaOverviewHandler(
|
||||||
}
|
}
|
||||||
>()
|
>()
|
||||||
|
|
||||||
for (const ticket of inRange) {
|
await forEachScopedTicketByCreatedRange(ctx, tenantId, viewer, startMs, endMs, companyId, (ticket) => {
|
||||||
const snapshot = (ticket.slaSnapshot ?? null) as { categoryId?: Id<"ticketCategories">; categoryName?: string; priority?: string } | null
|
totals.total += 1;
|
||||||
const rawCategoryId = ticket.categoryId ? String(ticket.categoryId) : snapshot?.categoryId ? String(snapshot.categoryId) : null
|
const status = normalizeStatus(ticket.status);
|
||||||
const categoryName = resolveCategoryName(rawCategoryId, snapshot, categoriesMap)
|
if (OPEN_STATUSES.has(status)) {
|
||||||
const priority = (snapshot?.priority ?? ticket.priority ?? "MEDIUM").toUpperCase()
|
totals.open += 1;
|
||||||
const key = `${rawCategoryId ?? "uncategorized"}::${priority}`
|
if (ticket.dueAt && ticket.dueAt < now) {
|
||||||
let stat = categoryStats.get(key)
|
totals.overdue += 1;
|
||||||
|
}
|
||||||
|
if (ticket.queueId) {
|
||||||
|
const key = String(ticket.queueId);
|
||||||
|
queueOpenCounts.set(key, (queueOpenCounts.get(key) ?? 0) + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (status === "RESOLVED") {
|
||||||
|
totals.resolved += 1;
|
||||||
|
if (typeof ticket.resolvedAt === "number") {
|
||||||
|
resolutionSum += (ticket.resolvedAt - ticket.createdAt) / 60000;
|
||||||
|
resolutionCount += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (typeof ticket.firstResponseAt === "number") {
|
||||||
|
firstResponseSum += (ticket.firstResponseAt - ticket.createdAt) / 60000;
|
||||||
|
firstResponseCount += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const snapshot = (ticket.slaSnapshot ?? null) as { categoryId?: Id<"ticketCategories">; categoryName?: string; priority?: string } | null;
|
||||||
|
const rawCategoryId = ticket.categoryId ? String(ticket.categoryId) : snapshot?.categoryId ? String(snapshot.categoryId) : null;
|
||||||
|
const categoryName = resolveCategoryName(rawCategoryId, snapshot, categoriesMap);
|
||||||
|
const priority = (snapshot?.priority ?? ticket.priority ?? "MEDIUM").toUpperCase();
|
||||||
|
const key = `${rawCategoryId ?? "uncategorized"}::${priority}`;
|
||||||
|
let stat = categoryStats.get(key);
|
||||||
if (!stat) {
|
if (!stat) {
|
||||||
stat = {
|
stat = {
|
||||||
categoryId: rawCategoryId,
|
categoryId: rawCategoryId,
|
||||||
|
|
@ -589,17 +595,23 @@ export async function slaOverviewHandler(
|
||||||
total: 0,
|
total: 0,
|
||||||
responseMet: 0,
|
responseMet: 0,
|
||||||
solutionMet: 0,
|
solutionMet: 0,
|
||||||
}
|
};
|
||||||
categoryStats.set(key, stat)
|
categoryStats.set(key, stat);
|
||||||
}
|
}
|
||||||
stat.total += 1
|
stat.total += 1;
|
||||||
if (ticket.slaResponseStatus === "met") {
|
if (ticket.slaResponseStatus === "met") {
|
||||||
stat.responseMet += 1
|
stat.responseMet += 1;
|
||||||
}
|
}
|
||||||
if (ticket.slaSolutionStatus === "met") {
|
if (ticket.slaSolutionStatus === "met") {
|
||||||
stat.solutionMet += 1
|
stat.solutionMet += 1;
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
|
const queueBreakdown = queues.map((queue) => ({
|
||||||
|
id: queue._id,
|
||||||
|
name: queue.name,
|
||||||
|
open: queueOpenCounts.get(String(queue._id)) ?? 0,
|
||||||
|
}));
|
||||||
|
|
||||||
const categoryBreakdown = Array.from(categoryStats.values())
|
const categoryBreakdown = Array.from(categoryStats.values())
|
||||||
.map((entry) => ({
|
.map((entry) => ({
|
||||||
|
|
@ -611,18 +623,19 @@ export async function slaOverviewHandler(
|
||||||
|
|
||||||
return {
|
return {
|
||||||
totals: {
|
totals: {
|
||||||
total: inRange.length,
|
total: totals.total,
|
||||||
open: openTickets.length,
|
open: totals.open,
|
||||||
resolved: resolvedTickets.length,
|
resolved: totals.resolved,
|
||||||
overdue: overdueTickets.length,
|
overdue: totals.overdue,
|
||||||
},
|
},
|
||||||
response: {
|
response: {
|
||||||
averageFirstResponseMinutes: average(firstResponseTimes),
|
averageFirstResponseMinutes:
|
||||||
responsesRegistered: firstResponseTimes.length,
|
firstResponseCount > 0 ? firstResponseSum / firstResponseCount : null,
|
||||||
|
responsesRegistered: firstResponseCount,
|
||||||
},
|
},
|
||||||
resolution: {
|
resolution: {
|
||||||
averageResolutionMinutes: average(resolutionTimes),
|
averageResolutionMinutes: resolutionCount > 0 ? resolutionSum / resolutionCount : null,
|
||||||
resolvedCount: resolutionTimes.length,
|
resolvedCount: resolutionCount,
|
||||||
},
|
},
|
||||||
queueBreakdown,
|
queueBreakdown,
|
||||||
categoryBreakdown,
|
categoryBreakdown,
|
||||||
|
|
@ -689,9 +702,80 @@ export async function csatOverviewHandler(
|
||||||
}: { tenantId: string; viewerId: Id<"users">; range?: string; companyId?: Id<"companies">; dateFrom?: string; dateTo?: string }
|
}: { tenantId: string; viewerId: Id<"users">; range?: string; companyId?: Id<"companies">; dateFrom?: string; dateTo?: string }
|
||||||
) {
|
) {
|
||||||
const viewer = await requireStaff(ctx, viewerId, tenantId);
|
const viewer = await requireStaff(ctx, viewerId, tenantId);
|
||||||
const { startMs, endMs } = resolveRangeWindow(range, dateFrom, dateTo, 90);
|
const { startMs, endMs, days } = resolveRangeWindow(range, dateFrom, dateTo, 90);
|
||||||
const tickets = await fetchScopedTicketsByCreatedRange(ctx, tenantId, viewer, startMs, endMs, companyId);
|
const surveys: CsatSurvey[] = [];
|
||||||
const surveys = (await collectCsatSurveys(ctx, tickets)).filter((s) => s.receivedAt >= startMs && s.receivedAt < endMs);
|
|
||||||
|
await forEachScopedTicketByCreatedRange(ctx, tenantId, viewer, startMs, endMs, companyId, async (ticket) => {
|
||||||
|
if (typeof ticket.csatScore === "number") {
|
||||||
|
const snapshot = (ticket.csatAssigneeSnapshot ?? null) as {
|
||||||
|
name?: string | null;
|
||||||
|
email?: string | null;
|
||||||
|
} | null;
|
||||||
|
const assigneeId =
|
||||||
|
ticket.csatAssigneeId && typeof ticket.csatAssigneeId === "string"
|
||||||
|
? ticket.csatAssigneeId
|
||||||
|
: ticket.csatAssigneeId
|
||||||
|
? String(ticket.csatAssigneeId)
|
||||||
|
: ticket.assigneeId
|
||||||
|
? String(ticket.assigneeId)
|
||||||
|
: null;
|
||||||
|
const assigneeName = snapshot?.name?.trim?.() || snapshot?.email?.trim?.() || null;
|
||||||
|
const maxScore =
|
||||||
|
typeof ticket.csatMaxScore === "number" && Number.isFinite(ticket.csatMaxScore)
|
||||||
|
? (ticket.csatMaxScore as number)
|
||||||
|
: 5;
|
||||||
|
const receivedAtRaw =
|
||||||
|
typeof ticket.csatRatedAt === "number"
|
||||||
|
? ticket.csatRatedAt
|
||||||
|
: typeof ticket.resolvedAt === "number"
|
||||||
|
? ticket.resolvedAt
|
||||||
|
: ticket.updatedAt ?? ticket.createdAt;
|
||||||
|
if (typeof receivedAtRaw !== "number") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (receivedAtRaw < startMs || receivedAtRaw >= endMs) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
surveys.push({
|
||||||
|
ticketId: ticket._id,
|
||||||
|
reference: ticket.reference,
|
||||||
|
score: ticket.csatScore,
|
||||||
|
maxScore,
|
||||||
|
comment:
|
||||||
|
typeof ticket.csatComment === "string" && ticket.csatComment.trim().length > 0
|
||||||
|
? ticket.csatComment.trim()
|
||||||
|
: null,
|
||||||
|
receivedAt: receivedAtRaw,
|
||||||
|
assigneeId,
|
||||||
|
assigneeName,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const events = await ctx.db
|
||||||
|
.query("ticketEvents")
|
||||||
|
.withIndex("by_ticket", (q) => q.eq("ticketId", ticket._id))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
for (const event of events) {
|
||||||
|
if (event.type !== "CSAT_RECEIVED" && event.type !== "CSAT_RATED") continue;
|
||||||
|
const score = extractScore(event.payload);
|
||||||
|
if (score === null) continue;
|
||||||
|
const assignee = extractAssignee(event.payload);
|
||||||
|
const receivedAt = event.createdAt;
|
||||||
|
if (receivedAt < startMs || receivedAt >= endMs) continue;
|
||||||
|
surveys.push({
|
||||||
|
ticketId: ticket._id,
|
||||||
|
reference: ticket.reference,
|
||||||
|
score,
|
||||||
|
maxScore: extractMaxScore(event.payload) ?? 5,
|
||||||
|
comment: extractComment(event.payload),
|
||||||
|
receivedAt,
|
||||||
|
assigneeId: assignee.id,
|
||||||
|
assigneeName: assignee.name,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const normalizeToFive = (value: CsatSurvey) => {
|
const normalizeToFive = (value: CsatSurvey) => {
|
||||||
if (!value.maxScore || value.maxScore <= 0) return value.score;
|
if (!value.maxScore || value.maxScore <= 0) return value.score;
|
||||||
|
|
@ -766,7 +850,7 @@ export async function csatOverviewHandler(
|
||||||
assigneeId: item.assigneeId,
|
assigneeId: item.assigneeId,
|
||||||
assigneeName: item.assigneeName,
|
assigneeName: item.assigneeName,
|
||||||
})),
|
})),
|
||||||
rangeDays: Math.max(1, Math.round((endMs - startMs) / ONE_DAY_MS)),
|
rangeDays: days,
|
||||||
positiveRate,
|
positiveRate,
|
||||||
byAgent,
|
byAgent,
|
||||||
};
|
};
|
||||||
|
|
@ -861,28 +945,26 @@ export async function backlogOverviewHandler(
|
||||||
) {
|
) {
|
||||||
const viewer = await requireStaff(ctx, viewerId, tenantId);
|
const viewer = await requireStaff(ctx, viewerId, tenantId);
|
||||||
const { startMs, endMs, days } = resolveRangeWindow(range, dateFrom, dateTo, 90);
|
const { startMs, endMs, days } = resolveRangeWindow(range, dateFrom, dateTo, 90);
|
||||||
const inRange = await fetchScopedTicketsByCreatedRange(ctx, tenantId, viewer, startMs, endMs, companyId);
|
const statusCounts = {} as Record<TicketStatusNormalized, number>;
|
||||||
|
const priorityCounts: Record<string, number> = {};
|
||||||
const statusCounts = inRange.reduce<Record<TicketStatusNormalized, number>>((acc, ticket) => {
|
|
||||||
const status = normalizeStatus(ticket.status);
|
|
||||||
acc[status] = (acc[status] ?? 0) + 1;
|
|
||||||
return acc;
|
|
||||||
}, {} as Record<TicketStatusNormalized, number>);
|
|
||||||
|
|
||||||
const priorityCounts = inRange.reduce<Record<string, number>>((acc, ticket) => {
|
|
||||||
acc[ticket.priority] = (acc[ticket.priority] ?? 0) + 1;
|
|
||||||
return acc;
|
|
||||||
}, {});
|
|
||||||
|
|
||||||
const openTickets = inRange.filter((ticket) => OPEN_STATUSES.has(normalizeStatus(ticket.status)));
|
|
||||||
|
|
||||||
const queueMap = new Map<string, { name: string; count: number }>();
|
const queueMap = new Map<string, { name: string; count: number }>();
|
||||||
for (const ticket of openTickets) {
|
let totalOpen = 0;
|
||||||
const queueId = ticket.queueId ? ticket.queueId : "sem-fila";
|
|
||||||
const current = queueMap.get(queueId) ?? { name: queueId === "sem-fila" ? "Sem fila" : "", count: 0 };
|
await forEachScopedTicketByCreatedRange(ctx, tenantId, viewer, startMs, endMs, companyId, (ticket) => {
|
||||||
|
const status = normalizeStatus(ticket.status);
|
||||||
|
statusCounts[status] = (statusCounts[status] ?? 0) + 1;
|
||||||
|
priorityCounts[ticket.priority] = (priorityCounts[ticket.priority] ?? 0) + 1;
|
||||||
|
|
||||||
|
if (!OPEN_STATUSES.has(status)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
totalOpen += 1;
|
||||||
|
const queueKey = ticket.queueId ? String(ticket.queueId) : "sem-fila";
|
||||||
|
const current = queueMap.get(queueKey) ?? { name: queueKey === "sem-fila" ? "Sem fila" : "", count: 0 };
|
||||||
current.count += 1;
|
current.count += 1;
|
||||||
queueMap.set(queueId, current);
|
queueMap.set(queueKey, current);
|
||||||
}
|
});
|
||||||
|
|
||||||
const queues = await fetchQueues(ctx, tenantId);
|
const queues = await fetchQueues(ctx, tenantId);
|
||||||
|
|
||||||
|
|
@ -901,7 +983,7 @@ export async function backlogOverviewHandler(
|
||||||
name: data.name,
|
name: data.name,
|
||||||
total: data.count,
|
total: data.count,
|
||||||
})),
|
})),
|
||||||
totalOpen: openTickets.length,
|
totalOpen,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1361,7 +1443,6 @@ export async function ticketsByChannelHandler(
|
||||||
end.setUTCHours(0, 0, 0, 0);
|
end.setUTCHours(0, 0, 0, 0);
|
||||||
const endMs = end.getTime() + ONE_DAY_MS;
|
const endMs = end.getTime() + ONE_DAY_MS;
|
||||||
const startMs = endMs - days * ONE_DAY_MS;
|
const startMs = endMs - days * ONE_DAY_MS;
|
||||||
const tickets = await fetchScopedTicketsByCreatedRange(ctx, tenantId, viewer, startMs, endMs, companyId);
|
|
||||||
|
|
||||||
const timeline = new Map<string, Map<string, number>>();
|
const timeline = new Map<string, Map<string, number>>();
|
||||||
for (let ts = startMs; ts < endMs; ts += ONE_DAY_MS) {
|
for (let ts = startMs; ts < endMs; ts += ONE_DAY_MS) {
|
||||||
|
|
@ -1370,15 +1451,14 @@ export async function ticketsByChannelHandler(
|
||||||
|
|
||||||
const channels = new Set<string>();
|
const channels = new Set<string>();
|
||||||
|
|
||||||
for (const ticket of tickets) {
|
await forEachScopedTicketByCreatedRange(ctx, tenantId, viewer, startMs, endMs, companyId, (ticket) => {
|
||||||
if (ticket.createdAt < startMs || ticket.createdAt >= endMs) continue;
|
|
||||||
const dateKey = formatDateKey(ticket.createdAt);
|
const dateKey = formatDateKey(ticket.createdAt);
|
||||||
const channelKey = ticket.channel ?? "OUTRO";
|
const channelKey = ticket.channel ?? "OUTRO";
|
||||||
channels.add(channelKey);
|
channels.add(channelKey);
|
||||||
const dayMap = timeline.get(dateKey) ?? new Map<string, number>();
|
const dayMap = timeline.get(dateKey) ?? new Map<string, number>();
|
||||||
dayMap.set(channelKey, (dayMap.get(channelKey) ?? 0) + 1);
|
dayMap.set(channelKey, (dayMap.get(channelKey) ?? 0) + 1);
|
||||||
timeline.set(dateKey, dayMap);
|
timeline.set(dateKey, dayMap);
|
||||||
}
|
});
|
||||||
|
|
||||||
const sortedChannels = Array.from(channels).sort();
|
const sortedChannels = Array.from(channels).sort();
|
||||||
|
|
||||||
|
|
@ -1713,9 +1793,8 @@ export async function hoursByClientHandler(
|
||||||
{ tenantId, viewerId, range, dateFrom, dateTo }: { tenantId: string; viewerId: Id<"users">; range?: string; dateFrom?: string; dateTo?: string }
|
{ tenantId, viewerId, range, dateFrom, dateTo }: { tenantId: string; viewerId: Id<"users">; range?: string; dateFrom?: string; dateTo?: string }
|
||||||
) {
|
) {
|
||||||
const viewer = await requireStaff(ctx, viewerId, tenantId)
|
const viewer = await requireStaff(ctx, viewerId, tenantId)
|
||||||
const tickets = await fetchScopedTickets(ctx, tenantId, viewer)
|
|
||||||
|
|
||||||
const { startMs, endMs, days } = resolveRangeWindow(range, dateFrom, dateTo, 90)
|
const { startMs, endMs, days } = resolveRangeWindow(range, dateFrom, dateTo, 90)
|
||||||
|
const companyCache = new Map<string, CompanySummary>()
|
||||||
|
|
||||||
type Acc = {
|
type Acc = {
|
||||||
companyId: Id<"companies">
|
companyId: Id<"companies">
|
||||||
|
|
@ -1728,31 +1807,31 @@ export async function hoursByClientHandler(
|
||||||
}
|
}
|
||||||
const map = new Map<string, Acc>()
|
const map = new Map<string, Acc>()
|
||||||
|
|
||||||
for (const t of tickets) {
|
await forEachScopedTicket(ctx, tenantId, viewer, async (ticket) => {
|
||||||
if (t.updatedAt < startMs || t.updatedAt >= endMs) continue
|
if (ticket.updatedAt < startMs || ticket.updatedAt >= endMs) return
|
||||||
const companyId = t.companyId ?? null
|
const companyId = ticket.companyId ?? null
|
||||||
if (!companyId) continue
|
if (!companyId) return
|
||||||
|
const key = String(companyId)
|
||||||
let acc = map.get(companyId)
|
let acc = map.get(key)
|
||||||
if (!acc) {
|
if (!acc) {
|
||||||
const company = await ctx.db.get(companyId)
|
const company = await getCompanySummary(ctx, companyId, companyCache)
|
||||||
acc = {
|
acc = {
|
||||||
companyId,
|
companyId,
|
||||||
name: company?.name ?? "Sem empresa",
|
name: company.name,
|
||||||
isAvulso: Boolean(company?.isAvulso ?? false),
|
isAvulso: company.isAvulso,
|
||||||
internalMs: 0,
|
internalMs: 0,
|
||||||
externalMs: 0,
|
externalMs: 0,
|
||||||
totalMs: 0,
|
totalMs: 0,
|
||||||
contractedHoursPerMonth: company?.contractedHoursPerMonth ?? null,
|
contractedHoursPerMonth: company.contractedHoursPerMonth,
|
||||||
}
|
}
|
||||||
map.set(companyId, acc)
|
map.set(key, acc)
|
||||||
}
|
}
|
||||||
const internal = t.internalWorkedMs ?? 0
|
const internal = ticket.internalWorkedMs ?? 0
|
||||||
const external = t.externalWorkedMs ?? 0
|
const external = ticket.externalWorkedMs ?? 0
|
||||||
acc.internalMs += internal
|
acc.internalMs += internal
|
||||||
acc.externalMs += external
|
acc.externalMs += external
|
||||||
acc.totalMs += internal + external
|
acc.totalMs += internal + external
|
||||||
}
|
})
|
||||||
|
|
||||||
const items = Array.from(map.values()).sort((a, b) => b.totalMs - a.totalMs)
|
const items = Array.from(map.values()).sort((a, b) => b.totalMs - a.totalMs)
|
||||||
return {
|
return {
|
||||||
|
|
@ -1785,9 +1864,8 @@ export async function hoursByClientInternalHandler(
|
||||||
ctx: QueryCtx,
|
ctx: QueryCtx,
|
||||||
{ tenantId, range, dateFrom, dateTo }: { tenantId: string; range?: string; dateFrom?: string; dateTo?: string }
|
{ tenantId, range, dateFrom, dateTo }: { tenantId: string; range?: string; dateFrom?: string; dateTo?: string }
|
||||||
) {
|
) {
|
||||||
const tickets = await fetchTickets(ctx, tenantId)
|
|
||||||
|
|
||||||
const { startMs, endMs, days } = resolveRangeWindow(range, dateFrom, dateTo, 90)
|
const { startMs, endMs, days } = resolveRangeWindow(range, dateFrom, dateTo, 90)
|
||||||
|
const companyCache = new Map<string, CompanySummary>()
|
||||||
|
|
||||||
type Acc = {
|
type Acc = {
|
||||||
companyId: Id<"companies">
|
companyId: Id<"companies">
|
||||||
|
|
@ -1800,31 +1878,31 @@ export async function hoursByClientInternalHandler(
|
||||||
}
|
}
|
||||||
const map = new Map<string, Acc>()
|
const map = new Map<string, Acc>()
|
||||||
|
|
||||||
for (const t of tickets) {
|
await forEachTenantTicket(ctx, tenantId, async (ticket) => {
|
||||||
if (t.updatedAt < startMs || t.updatedAt >= endMs) continue
|
if (ticket.updatedAt < startMs || ticket.updatedAt >= endMs) return
|
||||||
const companyId = t.companyId ?? null
|
const companyId = ticket.companyId ?? null
|
||||||
if (!companyId) continue
|
if (!companyId) return
|
||||||
|
const key = String(companyId)
|
||||||
let acc = map.get(companyId)
|
let acc = map.get(key)
|
||||||
if (!acc) {
|
if (!acc) {
|
||||||
const company = await ctx.db.get(companyId)
|
const company = await getCompanySummary(ctx, companyId, companyCache)
|
||||||
acc = {
|
acc = {
|
||||||
companyId,
|
companyId,
|
||||||
name: company?.name ?? "Sem empresa",
|
name: company.name,
|
||||||
isAvulso: Boolean(company?.isAvulso ?? false),
|
isAvulso: company.isAvulso,
|
||||||
internalMs: 0,
|
internalMs: 0,
|
||||||
externalMs: 0,
|
externalMs: 0,
|
||||||
totalMs: 0,
|
totalMs: 0,
|
||||||
contractedHoursPerMonth: company?.contractedHoursPerMonth ?? null,
|
contractedHoursPerMonth: company.contractedHoursPerMonth,
|
||||||
}
|
}
|
||||||
map.set(companyId, acc)
|
map.set(key, acc)
|
||||||
}
|
}
|
||||||
const internal = t.internalWorkedMs ?? 0
|
const internal = ticket.internalWorkedMs ?? 0
|
||||||
const external = t.externalWorkedMs ?? 0
|
const external = ticket.externalWorkedMs ?? 0
|
||||||
acc.internalMs += internal
|
acc.internalMs += internal
|
||||||
acc.externalMs += external
|
acc.externalMs += external
|
||||||
acc.totalMs += internal + external
|
acc.totalMs += internal + external
|
||||||
}
|
})
|
||||||
|
|
||||||
const items = Array.from(map.values()).sort((a, b) => b.totalMs - a.totalMs)
|
const items = Array.from(map.values()).sort((a, b) => b.totalMs - a.totalMs)
|
||||||
return {
|
return {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue