기능구현중
This commit is contained in:
153
server/api/dashboard/stats.get.ts
Normal file
153
server/api/dashboard/stats.get.ts
Normal file
@@ -0,0 +1,153 @@
|
||||
import { query } from '../../utils/db'
|
||||
import { requireAuth } from '../../utils/session'
|
||||
|
||||
/**
|
||||
* 대시보드 통계 API
|
||||
* GET /api/dashboard/stats
|
||||
*
|
||||
* - 인원별 이번 주 실적/차주 계획 시간
|
||||
* - 프로젝트별 투입 인원/시간
|
||||
* - 제출 현황
|
||||
*/
|
||||
export default defineEventHandler(async (event) => {
|
||||
const userId = await requireAuth(event)
|
||||
|
||||
const q = getQuery(event)
|
||||
const year = parseInt(q.year as string) || new Date().getFullYear()
|
||||
const week = parseInt(q.week as string) || getISOWeek(new Date())
|
||||
|
||||
// 1. 인원별 실적/계획 현황
|
||||
const employeeStats = await query<any>(`
|
||||
SELECT
|
||||
e.employee_id,
|
||||
e.employee_name,
|
||||
e.company,
|
||||
r.report_id,
|
||||
r.report_status,
|
||||
COALESCE(SUM(CASE WHEN t.task_type = 'WORK' THEN t.task_hours ELSE 0 END), 0) as work_hours,
|
||||
COALESCE(SUM(CASE WHEN t.task_type = 'PLAN' THEN t.task_hours ELSE 0 END), 0) as plan_hours,
|
||||
COUNT(DISTINCT CASE WHEN t.task_type = 'WORK' THEN t.project_id END) as work_project_count,
|
||||
COUNT(DISTINCT CASE WHEN t.task_type = 'PLAN' THEN t.project_id END) as plan_project_count
|
||||
FROM wr_employee_info e
|
||||
LEFT JOIN wr_weekly_report r ON e.employee_id = r.author_id
|
||||
AND r.report_year = $1 AND r.report_week = $2
|
||||
LEFT JOIN wr_weekly_report_task t ON r.report_id = t.report_id
|
||||
WHERE e.is_active = true
|
||||
GROUP BY e.employee_id, e.employee_name, e.company, r.report_id, r.report_status
|
||||
ORDER BY work_hours DESC, e.employee_name
|
||||
`, [year, week])
|
||||
|
||||
// 2. 프로젝트별 투입 현황
|
||||
const projectStats = await query<any>(`
|
||||
SELECT
|
||||
p.project_id,
|
||||
p.project_code,
|
||||
p.project_name,
|
||||
COUNT(DISTINCT r.author_id) as member_count,
|
||||
COALESCE(SUM(CASE WHEN t.task_type = 'WORK' THEN t.task_hours ELSE 0 END), 0) as work_hours,
|
||||
COALESCE(SUM(CASE WHEN t.task_type = 'PLAN' THEN t.task_hours ELSE 0 END), 0) as plan_hours,
|
||||
ARRAY_AGG(DISTINCT e.employee_name) as members
|
||||
FROM wr_project_info p
|
||||
JOIN wr_weekly_report_task t ON p.project_id = t.project_id
|
||||
JOIN wr_weekly_report r ON t.report_id = r.report_id
|
||||
AND r.report_year = $1 AND r.report_week = $2
|
||||
JOIN wr_employee_info e ON r.author_id = e.employee_id
|
||||
GROUP BY p.project_id, p.project_code, p.project_name
|
||||
ORDER BY work_hours DESC
|
||||
`, [year, week])
|
||||
|
||||
// 3. 전체 요약
|
||||
const activeEmployees = employeeStats.length
|
||||
const submittedCount = employeeStats.filter((e: any) =>
|
||||
e.report_status === 'SUBMITTED' || e.report_status === 'AGGREGATED'
|
||||
).length
|
||||
const totalWorkHours = employeeStats.reduce((sum: number, e: any) => sum + parseFloat(e.work_hours || 0), 0)
|
||||
const totalPlanHours = employeeStats.reduce((sum: number, e: any) => sum + parseFloat(e.plan_hours || 0), 0)
|
||||
|
||||
// 4. TODO 현황
|
||||
const todoStats = await query<any>(`
|
||||
SELECT
|
||||
COUNT(*) FILTER (WHERE status = 'PENDING') as pending,
|
||||
COUNT(*) FILTER (WHERE status = 'IN_PROGRESS') as in_progress,
|
||||
COUNT(*) FILTER (WHERE status = 'COMPLETED' AND completed_at >= NOW() - INTERVAL '7 days') as completed_this_week,
|
||||
COUNT(*) FILTER (WHERE due_date < CURRENT_DATE AND status NOT IN ('COMPLETED', 'CANCELLED')) as overdue
|
||||
FROM wr_todo
|
||||
`, [])
|
||||
|
||||
// 5. 유지보수 현황
|
||||
const maintenanceStats = await query<any>(`
|
||||
SELECT
|
||||
COUNT(*) FILTER (WHERE status = 'PENDING') as pending,
|
||||
COUNT(*) FILTER (WHERE status = 'IN_PROGRESS') as in_progress,
|
||||
COUNT(*) FILTER (WHERE status = 'COMPLETED' AND updated_at >= NOW() - INTERVAL '7 days') as completed_this_week
|
||||
FROM wr_maintenance_task
|
||||
`, [])
|
||||
|
||||
// 6. 최근 회의 현황
|
||||
const meetingStats = await query<any>(`
|
||||
SELECT
|
||||
COUNT(*) FILTER (WHERE meeting_date >= CURRENT_DATE - INTERVAL '7 days') as this_week,
|
||||
COUNT(*) FILTER (WHERE meeting_date >= CURRENT_DATE - INTERVAL '30 days') as this_month
|
||||
FROM wr_meeting
|
||||
`, [])
|
||||
|
||||
return {
|
||||
year,
|
||||
week,
|
||||
summary: {
|
||||
activeEmployees,
|
||||
submittedCount,
|
||||
notSubmittedCount: activeEmployees - submittedCount,
|
||||
totalWorkHours,
|
||||
totalPlanHours,
|
||||
projectCount: projectStats.length
|
||||
},
|
||||
todo: {
|
||||
pending: parseInt(todoStats[0]?.pending) || 0,
|
||||
inProgress: parseInt(todoStats[0]?.in_progress) || 0,
|
||||
completedThisWeek: parseInt(todoStats[0]?.completed_this_week) || 0,
|
||||
overdue: parseInt(todoStats[0]?.overdue) || 0
|
||||
},
|
||||
maintenance: {
|
||||
pending: parseInt(maintenanceStats[0]?.pending) || 0,
|
||||
inProgress: parseInt(maintenanceStats[0]?.in_progress) || 0,
|
||||
completedThisWeek: parseInt(maintenanceStats[0]?.completed_this_week) || 0
|
||||
},
|
||||
meeting: {
|
||||
thisWeek: parseInt(meetingStats[0]?.this_week) || 0,
|
||||
thisMonth: parseInt(meetingStats[0]?.this_month) || 0
|
||||
},
|
||||
employees: employeeStats.map((e: any) => ({
|
||||
employeeId: e.employee_id,
|
||||
employeeName: e.employee_name,
|
||||
company: e.company,
|
||||
reportId: e.report_id,
|
||||
reportStatus: e.report_status,
|
||||
workHours: parseFloat(e.work_hours) || 0,
|
||||
planHours: parseFloat(e.plan_hours) || 0,
|
||||
workProjectCount: parseInt(e.work_project_count) || 0,
|
||||
planProjectCount: parseInt(e.plan_project_count) || 0,
|
||||
isSubmitted: e.report_status === 'SUBMITTED' || e.report_status === 'AGGREGATED'
|
||||
})),
|
||||
projects: projectStats.map((p: any) => ({
|
||||
projectId: p.project_id,
|
||||
projectCode: p.project_code,
|
||||
projectName: p.project_name,
|
||||
memberCount: parseInt(p.member_count) || 0,
|
||||
workHours: parseFloat(p.work_hours) || 0,
|
||||
planHours: parseFloat(p.plan_hours) || 0,
|
||||
members: p.members || []
|
||||
}))
|
||||
}
|
||||
})
|
||||
|
||||
function getISOWeek(date: Date): number {
|
||||
const target = new Date(date)
|
||||
target.setHours(0, 0, 0, 0)
|
||||
const thursday = new Date(target)
|
||||
thursday.setDate(target.getDate() - ((target.getDay() + 6) % 7) + 3)
|
||||
const year = thursday.getFullYear()
|
||||
const firstThursday = new Date(year, 0, 4)
|
||||
firstThursday.setDate(firstThursday.getDate() - ((firstThursday.getDay() + 6) % 7) + 3)
|
||||
return Math.ceil((thursday.getTime() - firstThursday.getTime()) / (7 * 24 * 60 * 60 * 1000)) + 1
|
||||
}
|
||||
Reference in New Issue
Block a user