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 | export const dynamic = "force-dynamic"; /** * Dev Velocity Metrics API * GET /api/dev/metrics/velocity - Get sprint velocity data */ 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'; interface SprintVelocity { sprintId: string; sprintName: string; startDate: Date; endDate: Date; status: string; committedPoints: number; completedPoints: number; committedTickets: number; completedTickets: number; velocityPercentage: number; } async function handleGet( request: NextRequest ): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> { const { searchParams } = new URL(request.url); const projectId = searchParams.get('projectId'); const limit = parseInt(searchParams.get('limit') || '10', 10); // Build where clause const where: Record<string, unknown> = { status: { in: ['COMPLETED', 'ACTIVE'] } }; if (projectId) { where.projectId = projectId; } // Get completed and active sprints const sprints = await prisma.devSprint.findMany({ where, orderBy: { endDate: 'desc' }, take: limit, include: { project: { select: { id: true, name: true, key: true, color: true } }, tickets: { select: { id: true, status: true, storyPoints: true } } } }); // Calculate velocity for each sprint const velocityData: SprintVelocity[] = sprints.map((sprint) => { const tickets = sprint.tickets; const committedPoints = tickets.reduce((sum, t) => sum + (t.storyPoints || 0), 0); const completedPoints = tickets .filter((t) => t.status === 'COMPLETED') .reduce((sum, t) => sum + (t.storyPoints || 0), 0); const committedTickets = tickets.length; const completedTickets = tickets.filter((t) => ['COMPLETED', 'CANCELLED', 'WONT_FIX'].includes(t.status) ).length; return { sprintId: sprint.id, sprintName: sprint.name, startDate: sprint.startDate, endDate: sprint.endDate, status: sprint.status, committedPoints, completedPoints, committedTickets, completedTickets, velocityPercentage: committedPoints > 0 ? Math.round((completedPoints / committedPoints) * 100) : 0 }; }); // Calculate average velocity (from completed sprints only) const completedSprints = velocityData.filter((s) => s.status === 'COMPLETED'); const averageVelocity = completedSprints.length > 0 ? Math.round( completedSprints.reduce((sum, s) => sum + s.completedPoints, 0) / completedSprints.length ) : 0; // Calculate trend (last 3 vs previous 3) let trend: 'up' | 'down' | 'stable' = 'stable'; if (completedSprints.length >= 6) { const recent3 = completedSprints.slice(0, 3); const previous3 = completedSprints.slice(3, 6); const recentAvg = recent3.reduce((sum, s) => sum + s.completedPoints, 0) / recent3.length; const previousAvg = previous3.reduce((sum, s) => sum + s.completedPoints, 0) / previous3.length; const diff = recentAvg - previousAvg; const threshold = previousAvg * 0.1; // 10% threshold if (diff > threshold) { trend = 'up'; } else if (diff < -threshold) { trend = 'down'; } } // Get project breakdown if no specific project filter let projectVelocity: Array<{ projectId: string; projectName: string; projectKey: string; averageVelocity: number; sprintCount: number; }> = []; if (!projectId) { const projects = await prisma.devProject.findMany({ where: { isActive: true }, select: { id: true, name: true, key: true, sprints: { where: { status: 'COMPLETED' }, take: 5, orderBy: { endDate: 'desc' }, include: { tickets: { where: { status: 'COMPLETED' }, select: { storyPoints: true } } } } } }); projectVelocity = projects .filter((p) => p.sprints.length > 0) .map((p) => { const totalPoints = p.sprints.reduce( (sum, s) => sum + s.tickets.reduce((tSum, t) => tSum + (t.storyPoints || 0), 0), 0 ); return { projectId: p.id, projectName: p.name, projectKey: p.key, averageVelocity: Math.round(totalPoints / p.sprints.length), sprintCount: p.sprints.length }; }) .sort((a, b) => b.averageVelocity - a.averageVelocity); } return successResponse({ sprints: velocityData, summary: { averageVelocity, trend, completedSprintsCount: completedSprints.length, totalPointsDelivered: completedSprints.reduce( (sum, s) => sum + s.completedPoints, 0 ) }, byProject: projectVelocity }); } export const GET = withErrorHandling(withAdmin(handleGet)); |