# All files / models WorkAnalysis.js

0% Statements 0/67
0% Branches 0/129
0% Functions 0/21
0% Lines 0/58

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
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432

     // models/WorkAnalysis.js - 향상된 버전

    class WorkAnalysis {
        constructor(db) {
            this.db = db;
        }

        // 기본 통계 조회
        async getBasicStats(startDate, endDate) {
            const query = `
                SELECT
                    COALESCE(SUM(work_hours), 0) as total_hours,
                    COUNT(*) as total_reports,
                    COUNT(DISTINCT project_id) as active_projects,
                    COUNT(DISTINCT worker_id) as active_workers,
                    SUM(CASE WHEN work_status_id = 2 THEN 1 ELSE 0 END) as error_reports,
                    ROUND(AVG(work_hours), 2) as avg_hours_per_report
                FROM daily_work_reports
                WHERE report_date BETWEEN ? AND ?
            `;

            try {
                const [results] = await this.db.execute(query, [startDate, endDate]);
                const stats = results[0];

                const errorRate = stats.total_reports > 0
                    ? (stats.error_reports / stats.total_reports) * 100
                    : 0;

                return {
                    totalHours: parseFloat(stats.total_hours) || 0,
                    totalReports: parseInt(stats.total_reports) || 0,
                    activeProjects: parseInt(stats.active_projects) || 0,
                    activeworkers: parseInt(stats.active_workers) || 0,
                    errorRate: parseFloat(errorRate.toFixed(2)) || 0,
                    avgHoursPerReport: parseFloat(stats.avg_hours_per_report) || 0
                };
            } catch (error) {
                throw new Error(`기본 통계 조회 실패: ${error.message}`);
            }
        }

        // 일별 작업시간 추이
        async getDailyTrend(startDate, endDate) {
            const query = `
                SELECT
                    report_date as date,
                    SUM(work_hours) as hours,
                    COUNT(*) as reports,
                    COUNT(DISTINCT worker_id) as workers,
                    SUM(CASE WHEN work_status_id = 2 THEN 1 ELSE 0 END) as errors
                FROM daily_work_reports
                WHERE report_date BETWEEN ? AND ?
                GROUP BY report_date
                ORDER BY report_date
            `;

            try {
                const [results] = await this.db.execute(query, [startDate, endDate]);
                return results.map(row => ({
                    date: row.date,
                    hours: parseFloat(row.hours) || 0,
                    reports: parseInt(row.reports) || 0,
                    workers: parseInt(row.workers) || 0,
                    errors: parseInt(row.errors) || 0
                }));
            } catch (error) {
                throw new Error(`일별 추이 조회 실패: ${error.message}`);
            }
        }

        // 작업자별 통계
        async getWorkerStats(startDate, endDate) {
            const query = `
                SELECT
                    dwr.worker_id,
                    w.worker_name,
                    SUM(dwr.work_hours) as totalHours,
                    COUNT(*) as totalReports,
                    ROUND(AVG(dwr.work_hours), 2) as avgHours,
                    COUNT(DISTINCT dwr.project_id) as projectCount,
                    SUM(CASE WHEN dwr.work_status_id = 2 THEN 1 ELSE 0 END) as errorCount,
                    COUNT(DISTINCT dwr.report_date) as workingDays
                FROM daily_work_reports dwr
                LEFT JOIN workers w ON dwr.worker_id = w.worker_id
                WHERE dwr.report_date BETWEEN ? AND ?
                GROUP BY dwr.worker_id, w.worker_name
                ORDER BY totalHours DESC
            `;

            try {
                const [results] = await this.db.execute(query, [startDate, endDate]);
                return results.map(row => ({
                    worker_id: row.worker_id,
                    worker_name: row.worker_name || `작업자 ${row.worker_id}`,
                    totalHours: parseFloat(row.totalHours) || 0,
                    totalReports: parseInt(row.totalReports) || 0,
                    avgHours: parseFloat(row.avgHours) || 0,
                    projectCount: parseInt(row.projectCount) || 0,
                    errorCount: parseInt(row.errorCount) || 0,
                    workingDays: parseInt(row.workingDays) || 0,
                    errorRate: row.totalReports > 0 ? parseFloat(((row.errorCount / row.totalReports) * 100).toFixed(2)) : 0
                }));
            } catch (error) {
                throw new Error(`작업자별 통계 조회 실패: ${error.message}`);
            }
        }

        // 프로젝트별 통계
        async getProjectStats(startDate, endDate) {
            const query = `
                SELECT
                    dwr.project_id,
                    p.project_name,
                    SUM(dwr.work_hours) as totalHours,
                    COUNT(*) as totalReports,
                    COUNT(DISTINCT dwr.worker_id) as workerCount,
                    ROUND(AVG(dwr.work_hours), 2) as avgHours,
                    SUM(CASE WHEN dwr.work_status_id = 2 THEN 1 ELSE 0 END) as errorCount,
                    COUNT(DISTINCT dwr.report_date) as activeDays
                FROM daily_work_reports dwr
                LEFT JOIN projects p ON dwr.project_id = p.project_id
                WHERE dwr.report_date BETWEEN ? AND ?
                GROUP BY dwr.project_id, p.project_name
                ORDER BY totalHours DESC
            `;

            try {
                const [results] = await this.db.execute(query, [startDate, endDate]);
                return results.map(row => ({
                    project_id: row.project_id,
                    project_name: row.project_name || `프로젝트 ${row.project_id}`,
                    totalHours: parseFloat(row.totalHours) || 0,
                    totalReports: parseInt(row.totalReports) || 0,
                    workerCount: parseInt(row.workerCount) || 0,
                    avgHours: parseFloat(row.avgHours) || 0,
                    errorCount: parseInt(row.errorCount) || 0,
                    activeDays: parseInt(row.activeDays) || 0,
                    errorRate: row.totalReports > 0 ? parseFloat(((row.errorCount / row.totalReports) * 100).toFixed(2)) : 0
                }));
            } catch (error) {
                throw new Error(`프로젝트별 통계 조회 실패: ${error.message}`);
            }
        }

        // 작업유형별 통계
        async getWorkTypeStats(startDate, endDate) {
            const query = `
                SELECT
                    dwr.work_type_id,
                    wt.name as work_type_name,
                    SUM(dwr.work_hours) as totalHours,
                    COUNT(*) as totalReports,
                    ROUND(AVG(dwr.work_hours), 2) as avgHours,
                    COUNT(DISTINCT dwr.worker_id) as workerCount,
                    SUM(CASE WHEN dwr.work_status_id = 2 THEN 1 ELSE 0 END) as errorCount,
                    COUNT(DISTINCT dwr.project_id) as projectCount
                FROM daily_work_reports dwr
                LEFT JOIN work_types wt ON dwr.work_type_id = wt.id
                WHERE dwr.report_date BETWEEN ? AND ?
                GROUP BY dwr.work_type_id, wt.name
                ORDER BY totalHours DESC
            `;

            try {
                const [results] = await this.db.execute(query, [startDate, endDate]);
                return results.map(row => ({
                    work_type_id: row.work_type_id,
                    work_type_name: row.work_type_name || `작업유형 ${row.work_type_id}`,
                    totalHours: parseFloat(row.totalHours) || 0,
                    totalReports: parseInt(row.totalReports) || 0,
                    avgHours: parseFloat(row.avgHours) || 0,
                    workerCount: parseInt(row.workerCount) || 0,
                    errorCount: parseInt(row.errorCount) || 0,
                    projectCount: parseInt(row.projectCount) || 0,
                    errorRate: row.totalReports > 0 ? parseFloat(((row.errorCount / row.totalReports) * 100).toFixed(2)) : 0
                }));
            } catch (error) {
                throw new Error(`작업유형별 통계 조회 실패: ${error.message}`);
            }
        }

        // 최근 작업 현황
        async getRecentWork(startDate, endDate, limit = 50) {
            const query = `
                SELECT
                    dwr.id,
                    dwr.report_date,
                    dwr.worker_id,
                    w.worker_name,
                    dwr.project_id,
                    p.project_name,
                    p.job_no,
                    dwr.work_type_id,
                    wt.name as work_type_name,
                    dwr.work_status_id,
                    wst.name as work_status_name,
                    dwr.error_type_id,
                    et.name as error_type_name,
                    dwr.work_hours,
                    dwr.created_by,
                    u.name as created_by_name,
                    dwr.created_at
                FROM daily_work_reports dwr
                LEFT JOIN workers w ON dwr.worker_id = w.worker_id
                LEFT JOIN projects p ON dwr.project_id = p.project_id
                LEFT JOIN work_types wt ON dwr.work_type_id = wt.id
                LEFT JOIN work_status_types wst ON dwr.work_status_id = wst.id
                LEFT JOIN error_types et ON dwr.error_type_id = et.id
                LEFT JOIN users u ON dwr.created_by = u.user_id
                WHERE dwr.report_date BETWEEN ? AND ?
                ORDER BY dwr.created_at DESC
                LIMIT ?
            `;

            try {
                const [results] = await this.db.execute(query, [startDate, endDate, parseInt(limit)]);
                return results.map(row => ({
                    id: row.id,
                    report_date: row.report_date,
                    worker_id: row.worker_id,
                    worker_name: row.worker_name || `작업자 ${row.worker_id}`,
                    project_id: row.project_id,
                    project_name: row.project_name || `프로젝트 ${row.project_id}`,
                    job_no: row.job_no || 'N/A',
                    work_type_id: row.work_type_id,
                    work_type_name: row.work_type_name || `작업유형 ${row.work_type_id}`,
                    work_status_id: row.work_status_id,
                    work_status_name: row.work_status_name || '정상',
                    error_type_id: row.error_type_id,
                    error_type_name: row.error_type_name || null,
                    work_hours: parseFloat(row.work_hours) || 0,
                    created_by: row.created_by,
                    created_by_name: row.created_by_name || '미지정',
                    created_at: row.created_at
                }));
            } catch (error) {
                throw new Error(`최근 작업 현황 조회 실패: ${error.message}`);
            }
        }

        // 요일별 패턴 분석
        async getWeekdayPattern(startDate, endDate) {
            const query = `
                SELECT
                    DAYOFWEEK(report_date) as day_of_week,
                    CASE DAYOFWEEK(report_date)
                        WHEN 1 THEN '일요일'
                        WHEN 2 THEN '월요일'
                        WHEN 3 THEN '화요일'
                        WHEN 4 THEN '수요일'
                        WHEN 5 THEN '목요일'
                        WHEN 6 THEN '금요일'
                        WHEN 7 THEN '토요일'
                    END as day_name,
                    SUM(work_hours) as total_hours,
                    COUNT(*) as total_reports,
                    ROUND(AVG(work_hours), 2) as avg_hours,
                    COUNT(DISTINCT worker_id) as active_workers
                FROM daily_work_reports
                WHERE report_date BETWEEN ? AND ?
                GROUP BY DAYOFWEEK(report_date)
                ORDER BY day_of_week
            `;

            try {
                const [results] = await this.db.execute(query, [startDate, endDate]);
                return results.map(row => ({
                    dayOfWeek: row.day_of_week,
                    dayName: row.day_name,
                    totalHours: parseFloat(row.total_hours) || 0,
                    totalReports: parseInt(row.total_reports) || 0,
                    avgHours: parseFloat(row.avg_hours) || 0,
                    activeworkers: parseInt(row.active_workers) || 0
                }));
            } catch (error) {
                throw new Error(`요일별 패턴 분석 실패: ${error.message}`);
            }
        }

        // 에러 분석
        async getErrorAnalysis(startDate, endDate) {
            const query = `
                SELECT
                    dwr.error_type_id,
                    et.name as error_type_name,
                    COUNT(*) as error_count,
                    SUM(dwr.work_hours) as total_hours,
                    COUNT(DISTINCT dwr.worker_id) as affected_workers,
                    COUNT(DISTINCT dwr.project_id) as affected_projects
                FROM daily_work_reports dwr
                LEFT JOIN error_types et ON dwr.error_type_id = et.id
                WHERE dwr.report_date BETWEEN ? AND ?
                AND dwr.work_status_id = 2
                GROUP BY dwr.error_type_id, et.name
                ORDER BY error_count DESC
            `;

            try {
                const [results] = await this.db.execute(query, [startDate, endDate]);
                return results.map(row => ({
                    error_type_id: row.error_type_id,
                    error_type_name: row.error_type_name || `에러유형 ${row.error_type_id}`,
                    errorCount: parseInt(row.error_count) || 0,
                    totalHours: parseFloat(row.total_hours) || 0,
                    affectedworkers: parseInt(row.affected_workers) || 0,
                    affectedProjects: parseInt(row.affected_projects) || 0
                }));
            } catch (error) {
                throw new Error(`에러 분석 실패: ${error.message}`);
            }
        }

        // 월별 비교 분석
        async getMonthlyComparison(year) {
            const query = `
                SELECT
                    MONTH(report_date) as month,
                    MONTHNAME(report_date) as month_name,
                    SUM(work_hours) as total_hours,
                    COUNT(*) as total_reports,
                    COUNT(DISTINCT worker_id) as active_workers,
                    COUNT(DISTINCT project_id) as active_projects,
                    SUM(CASE WHEN work_status_id = 2 THEN 1 ELSE 0 END) as error_count
                FROM daily_work_reports
                WHERE YEAR(report_date) = ?
                GROUP BY MONTH(report_date), MONTHNAME(report_date)
                ORDER BY month
            `;

            try {
                const [results] = await this.db.execute(query, [year]);
                return results.map(row => ({
                    month: row.month,
                    monthName: row.month_name,
                    totalHours: parseFloat(row.total_hours) || 0,
                    totalReports: parseInt(row.total_reports) || 0,
                    activeworkers: parseInt(row.active_workers) || 0,
                    activeProjects: parseInt(row.active_projects) || 0,
                    errorCount: parseInt(row.error_count) || 0,
                    errorRate: row.total_reports > 0 ? parseFloat(((row.error_count / row.total_reports) * 100).toFixed(2)) : 0
                }));
            } catch (error) {
                throw new Error(`월별 비교 분석 실패: ${error.message}`);
            }
        }

        // 작업자별 전문분야 분석
        async getWorkerSpecialization(startDate, endDate) {
            const query = `
                SELECT
                    dwr.worker_id,
                    w.worker_name,
                    dwr.work_type_id,
                    wt.name as work_type_name,
                    dwr.project_id,
                    p.project_name,
                    SUM(dwr.work_hours) as totalHours,
                    COUNT(*) as totalReports,
                    ROUND((SUM(dwr.work_hours) / (
                        SELECT SUM(work_hours)
                        FROM daily_work_reports
                        WHERE worker_id = dwr.worker_id
                        AND report_date BETWEEN ? AND ?
                    )) * 100, 2) as percentage
                FROM daily_work_reports dwr
                LEFT JOIN workers w ON dwr.worker_id = w.worker_id
                LEFT JOIN work_types wt ON dwr.work_type_id = wt.id
                LEFT JOIN projects p ON dwr.project_id = p.project_id
                WHERE dwr.report_date BETWEEN ? AND ?
                GROUP BY dwr.worker_id, w.worker_name, dwr.work_type_id, wt.name, dwr.project_id, p.project_name
                HAVING totalHours > 0
                ORDER BY dwr.worker_id, totalHours DESC
            `;

            try {
                const [results] = await this.db.execute(query, [startDate, endDate, startDate, endDate]);
                return results.map(row => ({
                    worker_id: row.worker_id,
                    worker_name: row.worker_name || `작업자 ${row.worker_id}`,
                    work_type_id: row.work_type_id,
                    work_type_name: row.work_type_name || `작업유형 ${row.work_type_id}`,
                    project_id: row.project_id,
                    project_name: row.project_name || `프로젝트 ${row.project_id}`,
                    totalHours: parseFloat(row.totalHours) || 0,
                    totalReports: parseInt(row.totalReports) || 0,
                    percentage: parseFloat(row.percentage) || 0
                }));
            } catch (error) {
                throw new Error(`작업자별 전문분야 분석 실패: ${error.message}`);
            }
        }

        // 대시보드용 종합 데이터
        async getDashboardData(startDate, endDate) {
            try {
                // 병렬로 모든 데이터 조회
                const [
                    stats,
                    dailyTrend,
                    workerStats,
                    projectStats,
                    workTypeStats,
                    recentWork
                ] = await Promise.all([
                    this.getBasicStats(startDate, endDate),
                    this.getDailyTrend(startDate, endDate),
                    this.getWorkerStats(startDate, endDate),
                    this.getProjectStats(startDate, endDate),
                    this.getWorkTypeStats(startDate, endDate),
                    this.getRecentWork(startDate, endDate, 20)
                ]);

                return {
                    stats,
                    dailyTrend,
                    workerStats,
                    projectStats,
                    workTypeStats,
                    recentWork,
                    metadata: {
                        period: `${startDate} ~ ${endDate}`,
                        timestamp: new Date().toISOString()
                    }
                };
            } catch (error) {
                throw new Error(`대시보드 데이터 조회 실패: ${error.message}`);
            }
        }
    }

    module.exports = WorkAnalysis;