File size: 12,147 Bytes
6a0a429
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import Dict


def get_color(value: float, thresholds: tuple = (50, 25)) -> str:
    high, low = thresholds
    if value >= high:
        return "#4ade80"
    elif value >= low:
        return "#fb923c"
    else:
        return "#f87171"


def get_diff_color(value: float) -> str:
    return "#4ade80" if value >= 0 else "#f87171"


# Card Components
def metric_card(title: str, value: str, color: str, subtitle: str = "") -> str:
    subtitle_html = (
        f'<div style="font-size: 11px; color: #9ca3af; margin-top: 4px;">{subtitle}</div>'
        if subtitle
        else ""
    )
    return f"""
        <div style="background: #1e293b; padding: 20px; border-radius: 12px; border-left: 4px solid {color};">
            <div style="font-size: 12px; color: #9ca3af; text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 8px;">{title}</div>
            <div style="font-size: 28px; font-weight: 700; color: {color};">{value}</div>
            {subtitle_html}
        </div>
    """


def info_row(label: str, value: str, color: str = "#fff", border: bool = True) -> str:
    border_style = "border-bottom: 1px solid #334155;" if border else ""
    return f"""
        <div style="display: flex; justify-content: space-between; padding: 12px 0; {border_style}">
            <span style="color: #9ca3af;">{label}</span>
            <span style="font-weight: 600; color: {color};">{value}</span>
        </div>
    """


def info_card(title: str, rows: list) -> str:
    rows_html = "".join(rows)
    return f"""
        <div style="background: #1e293b; padding: 20px; border-radius: 12px;">
            <h4 style="margin: 0 0 16px 0; color: #fff; font-size: 14px; font-weight: 600;">{title}</h4>
            {rows_html}
        </div>
    """


# Summary Templates
def build_validation_summary(data: Dict) -> str:
    """Build summary HTML for validation mode (when actual data exists)."""
    year = data["year"]
    semester_name = data["semester_name"]
    class_capacity = data["class_capacity"]
    data_source = data.get("data_source", "kalkulasi")

    # Metrics
    class_accuracy_pct = data.get("class_accuracy_pct", 0)
    class_within_one_pct = data.get("class_within_one_pct", 0)
    total_classes = data.get("total_classes", 0)
    comparison_mae = data.get("comparison_mae", 0)
    comparison_rmse = data.get("comparison_rmse", 0)
    total_for_class_accuracy = data.get("total_for_class_accuracy", 0)

    # Enrollment metrics
    total_actual = data.get("total_actual", 0)
    total_predicted = data.get("total_predicted", 0)
    accuracy_pct = data.get("accuracy_pct", 0)
    class_matches = data.get("class_matches", 0)
    class_within_one = data.get("class_within_one", 0)

    # Colors
    class_accuracy_color = get_color(class_accuracy_pct)
    diff_color = get_diff_color(total_predicted - total_actual)

    return f"""
<div style="padding: 24px;">
    <div style="margin-bottom: 24px;">
        <h2 style="margin: 0 0 8px 0; color: #fff; font-size: 24px; font-weight: 600;">{
        year
    } Semester {semester_name}</h2>
        <p style="color: #9ca3af; margin: 0; font-size: 14px;">Validasi prediksi terhadap data aktual | Kapasitas per kelas: {
        class_capacity
    } mahasiswa | Sumber kelas aktual: {data_source}</p>
    </div>

    <div style="display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin-bottom: 24px;">
        {
        metric_card(
            "Akurasi Kelas",
            f"{class_accuracy_pct:.1f}%",
            class_accuracy_color,
            f"±1 kelas: {class_within_one_pct:.1f}%",
        )
    }
        {metric_card("Total Kelas Prediksi", str(total_classes), "#60a5fa")}
        {
        metric_card(
            "MAE / RMSE", f"{comparison_mae:.1f} / {comparison_rmse:.1f}", "#a78bfa"
        )
    }
        {metric_card("MK Divalidasi", str(total_for_class_accuracy), "#fb923c")}
    </div>

    <div style="display: grid; grid-template-columns: repeat(2, 1fr); gap: 16px;">
        {
        info_card(
            "Ringkasan Enrollment",
            [
                info_row("Total Aktual", str(int(total_actual))),
                info_row("Total Prediksi", str(int(total_predicted))),
                info_row(
                    "Selisih",
                    f"{int(total_predicted - total_actual):+d}",
                    diff_color,
                    border=False,
                ),
            ],
        )
    }
        {
        info_card(
            f"Akurasi Prediksi Kelas (dari {data_source})",
            [
                info_row(
                    "Kelas Tepat",
                    f"{class_matches}/{total_for_class_accuracy}",
                    "#4ade80",
                ),
                info_row(
                    "Selisih ±1 Kelas",
                    f"{class_within_one}/{total_for_class_accuracy}",
                    "#60a5fa",
                ),
                info_row("Akurasi Enrollment", f"{accuracy_pct:.1f}%", border=False),
            ],
        )
    }
    </div>
</div>
"""


def build_no_match_summary(data: Dict) -> str:
    year = data["year"]
    semester_name = data["semester_name"]
    metrics = data.get("metrics", {"mae": 0, "rmse": 0})
    total_to_open = data.get("total_to_open", 0)
    total_classes = data.get("total_classes", 0)

    return f"""
<div style="padding: 24px;">
    <div style="margin-bottom: 24px;">
        <h2 style="margin: 0 0 8px 0; color: #fff; font-size: 24px; font-weight: 600;">{year} Semester {semester_name}</h2>
        <p style="color: #9ca3af; margin: 0; font-size: 14px;">Data semester ada, tetapi tidak ditemukan MK pilihan yang cocok</p>
    </div>

    <div style="display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px;">
        {metric_card("MAE (Backtest)", f"{metrics['mae']:.2f}", "#60a5fa")}
        {metric_card("RMSE (Backtest)", f"{metrics['rmse']:.2f}", "#a78bfa")}
        {metric_card("MK Dibuka", str(total_to_open), "#4ade80")}
        {metric_card("Total Kelas", str(total_classes), "#fb923c")}
    </div>
</div>
"""


def build_future_prediction_summary(data: Dict) -> str:
    year = data["year"]
    semester_name = data["semester_name"]
    class_capacity = data["class_capacity"]
    metrics = data.get("metrics", {"mae": 0, "rmse": 0})

    total_to_open = data.get("total_to_open", 0)
    total_classes = data.get("total_classes", 0)
    total_predicted_students = data.get("total_predicted_students", 0)
    total_capacity = data.get("total_capacity", 0)

    avg_utilization = (
        (total_predicted_students / total_capacity * 100) if total_capacity > 0 else 0
    )

    return f"""
<div style="padding: 24px;">
    <div style="margin-bottom: 24px;">
        <h2 style="margin: 0 0 8px 0; color: #fff; font-size: 24px; font-weight: 600;">{
        year
    } Semester {semester_name}</h2>
        <p style="color: #9ca3af; margin: 0; font-size: 14px;">Prediksi masa depan berdasarkan tren historis | Kapasitas per kelas: {
        class_capacity
    } mahasiswa</p>
    </div>

    <div style="display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin-bottom: 24px;">
        {metric_card("MK Dibuka", str(total_to_open), "#4ade80")}
        {metric_card("Total Kelas Dibuka", str(total_classes), "#60a5fa")}
        {metric_card("Prediksi Mahasiswa", str(total_predicted_students), "#a78bfa")}
        {metric_card("Total Kuota", str(total_capacity), "#fb923c")}
    </div>

    <div style="display: grid; grid-template-columns: repeat(2, 1fr); gap: 16px;">
        {
        info_card(
            "Backtest Metrics",
            [
                info_row("MAE", f"{metrics['mae']:.2f}"),
                info_row("RMSE", f"{metrics['rmse']:.2f}", border=False),
            ],
        )
    }
        {
        info_card(
            "Kapasitas Info",
            [
                info_row("Kapasitas/Kelas", f"{class_capacity} mhs"),
                info_row("Avg Utilization", f"{avg_utilization:.1f}%", border=False),
            ],
        )
    }
    </div>
</div>
"""


def build_prediction_summary(data: Dict) -> str:
    has_actual_data = data.get("has_actual_data", False)

    if has_actual_data:
        if "comparison_mae" in data:
            return build_validation_summary(data)
        else:
            return build_no_match_summary(data)
    else:
        return build_future_prediction_summary(data)


def build_multi_year_summary(data: Dict) -> str:
    year = data["year"]
    years_ahead = data["years_ahead"]
    semester_name = data["semester_name"]
    class_capacity = data["class_capacity"]

    first_year_classes = data["first_year_classes"]
    last_year_classes = data["last_year_classes"]
    growth_classes = data["growth_classes"]
    growth_students = data["growth_students"]

    growth_class_color = get_diff_color(growth_classes)
    growth_student_color = get_diff_color(growth_students)

    return f"""
<div style="padding: 24px;">
    <div style="margin-bottom: 24px;">
        <h2 style="margin: 0 0 8px 0; color: #fff; font-size: 24px; font-weight: 600;">Proyeksi {years_ahead} Tahun ke Depan - Semester {semester_name}</h2>
        <p style="color: #9ca3af; margin: 0; font-size: 14px;">Forecasting kebutuhan kelas {year} - {year + years_ahead} | Kapasitas per kelas: {class_capacity} mahasiswa</p>
    </div>

    <div style="display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin-bottom: 24px;">
        {metric_card(f"Kelas ({year})", str(first_year_classes), "#4ade80")}
        {metric_card(f"Kelas ({year + years_ahead})", str(last_year_classes), "#60a5fa")}
        {metric_card("Pertumbuhan Kelas", f"{growth_classes:+d}", growth_class_color)}
        {metric_card("Pertumbuhan Mhs", f"{growth_students:+d}", growth_student_color)}
    </div>
</div>
"""


# Placeholder Templates
def placeholder_card(title: str, subtitle: str) -> str:
    return f"""
        <div style="padding: 60px 40px; text-align: center; background: #1e293b; border-radius: 12px;">
            <h3 style="color: #fff; margin: 0 0 8px 0; font-size: 18px; font-weight: 600;">{title}</h3>
            <p style="color: #9ca3af; margin: 0; font-size: 14px;">{subtitle}</p>
        </div>
    """


def get_prediction_placeholder() -> str:
    return placeholder_card(
        "Pilih tahun dan semester",
        "Klik Generate Predictions untuk melihat rekomendasi jumlah kelas",
    )


def get_forecast_placeholder() -> str:
    return placeholder_card(
        "Proyeksi Multi-Tahun", "Lihat tren kebutuhan kelas beberapa tahun ke depan"
    )


# Data Info Component
def build_data_info(data: Dict) -> str:
    if "error" in data:
        return f"<p style='color: #f87171;'>{data['error']}</p>"

    return f"""
<div style="padding: 8px 0;">
    <div style="display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px;">
        <div style="background: #1e293b; padding: 16px; border-radius: 8px; text-align: center;">
            <div style="font-size: 11px; color: #9ca3af; margin-bottom: 6px;">Total MK</div>
            <div style="font-size: 20px; font-weight: 700; color: #fff;">{data["total_courses"]}</div>
        </div>
        <div style="background: #1e293b; padding: 16px; border-radius: 8px; text-align: center;">
            <div style="font-size: 11px; color: #9ca3af; margin-bottom: 6px;">MK Pilihan</div>
            <div style="font-size: 20px; font-weight: 700; color: #4ade80;">{data["elective_courses"]}</div>
        </div>
        <div style="background: #1e293b; padding: 16px; border-radius: 8px; text-align: center;">
            <div style="font-size: 11px; color: #9ca3af; margin-bottom: 6px;">Kapasitas/Kelas</div>
            <div style="font-size: 20px; font-weight: 700; color: #60a5fa;">{data["class_capacity"]}</div>
        </div>
        <div style="background: #1e293b; padding: 16px; border-radius: 8px; text-align: center;">
            <div style="font-size: 11px; color: #9ca3af; margin-bottom: 6px;">Tahun Data</div>
            <div style="font-size: 20px; font-weight: 700; color: #fb923c;">{data["year_min"]}-{data["year_max"]}</div>
        </div>
    </div>
</div>
"""