Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | export const dynamic = "force-dynamic"; /** * Dev Dashboard Metrics API * GET /api/dev/metrics/dashboard - Get dashboard statistics */ import { NextRequest, NextResponse } from 'next/server'; import { } from 'next-auth'; import { withAdmin, withErrorHandling, successResponse, ApiSuccessResponse, ApiErrorResponse } from '@/lib/api'; import { } from '@/lib/api/middleware'; import { prisma } from '@/lib/prisma'; async function handleGet( request: NextRequest ): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> { const { searchParams } = new URL(request.url); const projectId = searchParams.get('projectId'); const now = new Date(); const weekAgo = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000); const monthAgo = new Date(now.getTime() - 30 * 24 * 60 * 60 * 1000); // Build where clause for project filtering const ticketWhere = projectId ? { projectId } : {}; // Execute all queries in parallel const [ // Overall counts totalTickets, openTickets, inProgressTickets, inReviewTickets, testingTickets, blockedTickets, completedThisWeek, createdThisWeek, overdueTickets, unassignedTickets, // Group by queries statusCounts, typeCounts, priorityCounts, severityCounts, // Project & Sprint stats projectStats, activeSprintsCount, // Recent activity recentlyCompleted, recentlyCreated, // Time tracking hoursLoggedThisWeek, // Assignee workload assigneeWorkload, ] = await Promise.all([ // Total tickets prisma.devTicket.count({ where: ticketWhere }), // Open tickets prisma.devTicket.count({ where: { ...ticketWhere, status: 'OPEN' } }), // In progress prisma.devTicket.count({ where: { ...ticketWhere, status: 'IN_PROGRESS' } }), // In review prisma.devTicket.count({ where: { ...ticketWhere, status: 'IN_REVIEW' } }), // Testing prisma.devTicket.count({ where: { ...ticketWhere, status: 'TESTING' } }), // Blocked prisma.devTicket.count({ where: { ...ticketWhere, status: 'BLOCKED' } }), // Completed this week prisma.devTicket.count({ where: { ...ticketWhere, status: 'COMPLETED', completedAt: { gte: weekAgo } } }), // Created this week prisma.devTicket.count({ where: { ...ticketWhere, createdAt: { gte: weekAgo } } }), // Overdue prisma.devTicket.count({ where: { ...ticketWhere, dueDate: { lt: now }, status: { notIn: ['COMPLETED', 'CANCELLED', 'WONT_FIX'] } } }), // Unassigned (active) prisma.devTicket.count({ where: { ...ticketWhere, assigneeId: null, status: { in: ['OPEN', 'IN_PROGRESS'] } } }), // Status breakdown prisma.devTicket.groupBy({ by: ['status'], where: ticketWhere, _count: true }), // Type breakdown prisma.devTicket.groupBy({ by: ['type'], where: ticketWhere, _count: true }), // Priority breakdown prisma.devTicket.groupBy({ by: ['priority'], where: ticketWhere, _count: true }), // Severity breakdown (bugs only) prisma.devTicket.groupBy({ by: ['severity'], where: { ...ticketWhere, type: 'BUG', severity: { not: null } }, _count: true }), // Project ticket counts prisma.devProject.findMany({ where: { isActive: true }, select: { id: true, name: true, key: true, color: true, _count: { select: { tickets: true } } } }), // Active sprints prisma.devSprint.count({ where: { status: 'ACTIVE' } }), // Recently completed tickets prisma.devTicket.findMany({ where: { ...ticketWhere, status: 'COMPLETED', completedAt: { gte: monthAgo } }, select: { id: true, ticketNumber: true, title: true, type: true, completedAt: true, assignee: { select: { id: true, name: true, image: true } } }, orderBy: { completedAt: 'desc' }, take: 10 }), // Recently created tickets prisma.devTicket.findMany({ where: { ...ticketWhere, createdAt: { gte: weekAgo } }, select: { id: true, ticketNumber: true, title: true, type: true, priority: true, createdAt: true, reporter: { select: { id: true, name: true, image: true } } }, orderBy: { createdAt: 'desc' }, take: 10 }), // Hours logged this week prisma.devTimeEntry.aggregate({ where: { date: { gte: weekAgo }, ...(projectId ? { ticket: { projectId } } : {}) }, _sum: { hours: true } }), // Assignee workload (tickets per person) prisma.devTicket.groupBy({ by: ['assigneeId'], where: { ...ticketWhere, assigneeId: { not: null }, status: { in: ['OPEN', 'IN_PROGRESS', 'IN_REVIEW', 'TESTING', 'BLOCKED'] } }, _count: true, _sum: { storyPoints: true } }), ]); // Get assignee details for workload const assigneeIds = assigneeWorkload .map((w) => w.assigneeId) .filter((id): id is number => id !== null); const assignees = assigneeIds.length > 0 ? await prisma.user.findMany({ where: { id: { in: assigneeIds } }, select: { id: true, name: true, email: true, image: true } }) : []; const assigneeMap = new Map(assignees.map((a) => [a.id, a])); // Transform results const byStatus = Object.fromEntries(statusCounts.map((s) => [s.status, s._count])); const byType = Object.fromEntries(typeCounts.map((t) => [t.type, t._count])); const byPriority = Object.fromEntries(priorityCounts.map((p) => [p.priority, p._count])); const bySeverity = Object.fromEntries( severityCounts.map((s) => [s.severity, s._count]) ); const workloadByAssignee = assigneeWorkload.map((w) => ({ assignee: w.assigneeId ? assigneeMap.get(w.assigneeId) : null, ticketCount: w._count, storyPoints: w._sum.storyPoints || 0 })); const projectTicketCounts = projectStats.map((p) => ({ id: p.id, name: p.name, key: p.key, color: p.color, ticketCount: p._count.tickets })); return successResponse({ overview: { totalTickets, openTickets, inProgressTickets, inReviewTickets, testingTickets, blockedTickets, completedThisWeek, createdThisWeek, overdueTickets, unassignedTickets, activeSprintsCount, hoursLoggedThisWeek: hoursLoggedThisWeek._sum.hours || 0 }, breakdown: { byStatus, byType, byPriority, bySeverity }, projects: projectTicketCounts, workload: workloadByAssignee, recentActivity: { completed: recentlyCompleted, created: recentlyCreated } }); } export const GET = withErrorHandling(withAdmin(handleGet)); |