Commit ยท
ee2b6ed
1
Parent(s): a5f60d9
Fix infinite generation loop and improve progress visibility
Browse files- web_app.py: reset stuck 'generating' status on startup (survives hot-reload/crash)
- db/database.py: set SQLite timeout=30s and enable WAL mode to prevent write-lock failures
- optimizer/solver.py: fix _solve_best_effort ignoring always_available flags; add phase/score tracking to progress callback; notify phase-2 start when falling back to best-effort
- routes/generate.py: pass period_id to solve(); expose phase/obj fields in status endpoint
- waiting.html: rAF-driven smooth bar (moves from page load, not first callback); phase labels (step 1/2 vs 2/2); objective score display with delta; 90s client-side timeout
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- db/database.py +2 -1
- optimizer/solver.py +29 -3
- routes/generate.py +7 -3
- templates/generate/waiting.html +180 -50
- web_app.py +12 -0
db/database.py
CHANGED
|
@@ -72,9 +72,10 @@ class Connection:
|
|
| 72 |
else:
|
| 73 |
import sqlite3
|
| 74 |
_DB_PATH.parent.mkdir(parents=True, exist_ok=True)
|
| 75 |
-
self._conn = sqlite3.connect(str(_DB_PATH))
|
| 76 |
self._conn.row_factory = sqlite3.Row
|
| 77 |
self._conn.execute("PRAGMA foreign_keys = ON")
|
|
|
|
| 78 |
self._pool = None
|
| 79 |
self.backend = "sqlite"
|
| 80 |
self.lastrowid = None
|
|
|
|
| 72 |
else:
|
| 73 |
import sqlite3
|
| 74 |
_DB_PATH.parent.mkdir(parents=True, exist_ok=True)
|
| 75 |
+
self._conn = sqlite3.connect(str(_DB_PATH), timeout=30)
|
| 76 |
self._conn.row_factory = sqlite3.Row
|
| 77 |
self._conn.execute("PRAGMA foreign_keys = ON")
|
| 78 |
+
self._conn.execute("PRAGMA journal_mode = WAL")
|
| 79 |
self._pool = None
|
| 80 |
self.backend = "sqlite"
|
| 81 |
self.lastrowid = None
|
optimizer/solver.py
CHANGED
|
@@ -51,11 +51,15 @@ class SolveProgressCallback(cp_model.CpSolverSolutionCallback):
|
|
| 51 |
return
|
| 52 |
self._last_update = elapsed
|
| 53 |
pct = min(90, int(elapsed / self._max_time * 100))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
try:
|
| 55 |
from db import repositories as repo
|
| 56 |
repo.update_period_gen_status(
|
| 57 |
self._period_id, "generating",
|
| 58 |
-
f"progress:{pct},solutions:{self._n},elapsed:{elapsed:.1f}"
|
| 59 |
)
|
| 60 |
except Exception:
|
| 61 |
pass
|
|
@@ -67,6 +71,7 @@ def solve(
|
|
| 67 |
requests: list[ShiftRequest],
|
| 68 |
config: SolverConfig | None = None,
|
| 69 |
progress_callback: SolveProgressCallback | None = None,
|
|
|
|
| 70 |
) -> SolveResult:
|
| 71 |
"""
|
| 72 |
ใทใใใๆ้ฉๅใใฆ SolveResult ใ่ฟใใ
|
|
@@ -434,6 +439,9 @@ def solve(
|
|
| 434 |
# ๅฎ่กไธๅฏ่ฝ โ ๅถ็ด้ๅใฎ่จบๆญใกใใปใผใธใๅ้
|
| 435 |
errs = _diagnose_infeasible(active_employees, date_strs, req_map)
|
| 436 |
|
|
|
|
|
|
|
|
|
|
| 437 |
# ใในใใจใใฉใผใ็ๆ: ไบบๆฐใปใชใผใใผๅถ็ดใใฝใใๅใใฆๅๅฎ่ก
|
| 438 |
best_assignments, best_warnings = _solve_best_effort(
|
| 439 |
active_employees, date_strs, slots, positions,
|
|
@@ -450,6 +458,19 @@ def solve(
|
|
| 450 |
)
|
| 451 |
|
| 452 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 453 |
def _extract_assignments(solver, assign, active_employees, date_strs, slots, positions) -> list[ShiftAssignment]:
|
| 454 |
result = []
|
| 455 |
for emp in active_employees:
|
|
@@ -502,7 +523,12 @@ def _solve_best_effort(
|
|
| 502 |
for emp in active_employees:
|
| 503 |
for ds in date_strs:
|
| 504 |
for slot in slots:
|
| 505 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 506 |
for pos in positions:
|
| 507 |
if not can_work:
|
| 508 |
model.add(assign[emp.id][ds][slot.value][pos.value] == 0)
|
|
@@ -631,7 +657,7 @@ def _diagnose_infeasible(
|
|
| 631 |
|
| 632 |
for ds in date_strs:
|
| 633 |
d = date.fromisoformat(ds)
|
| 634 |
-
dow_labels = ["ๆ", "็ซ", "ๆฐด", "ๆจ", "้", "
|
| 635 |
label = f"{d.month}/{d.day}({dow_labels[d.weekday()]})"
|
| 636 |
|
| 637 |
for slot in TimeSlot:
|
|
|
|
| 51 |
return
|
| 52 |
self._last_update = elapsed
|
| 53 |
pct = min(90, int(elapsed / self._max_time * 100))
|
| 54 |
+
try:
|
| 55 |
+
obj = int(self.ObjectiveValue())
|
| 56 |
+
except Exception:
|
| 57 |
+
obj = -1
|
| 58 |
try:
|
| 59 |
from db import repositories as repo
|
| 60 |
repo.update_period_gen_status(
|
| 61 |
self._period_id, "generating",
|
| 62 |
+
f"phase:1,progress:{pct},solutions:{self._n},elapsed:{elapsed:.1f},obj:{obj}"
|
| 63 |
)
|
| 64 |
except Exception:
|
| 65 |
pass
|
|
|
|
| 71 |
requests: list[ShiftRequest],
|
| 72 |
config: SolverConfig | None = None,
|
| 73 |
progress_callback: SolveProgressCallback | None = None,
|
| 74 |
+
period_id: int | None = None,
|
| 75 |
) -> SolveResult:
|
| 76 |
"""
|
| 77 |
ใทใใใๆ้ฉๅใใฆ SolveResult ใ่ฟใใ
|
|
|
|
| 439 |
# ๅฎ่กไธๅฏ่ฝ โ ๅถ็ด้ๅใฎ่จบๆญใกใใปใผใธใๅ้
|
| 440 |
errs = _diagnose_infeasible(active_employees, date_strs, req_map)
|
| 441 |
|
| 442 |
+
# ใใงใผใบ2้ๅงใ้็ฅ
|
| 443 |
+
_notify_phase2(period_id or (progress_callback._period_id if progress_callback else None))
|
| 444 |
+
|
| 445 |
# ใในใใจใใฉใผใ็ๆ: ไบบๆฐใปใชใผใใผๅถ็ดใใฝใใๅใใฆๅๅฎ่ก
|
| 446 |
best_assignments, best_warnings = _solve_best_effort(
|
| 447 |
active_employees, date_strs, slots, positions,
|
|
|
|
| 458 |
)
|
| 459 |
|
| 460 |
|
| 461 |
+
def _notify_phase2(period_id):
|
| 462 |
+
if period_id is None:
|
| 463 |
+
return
|
| 464 |
+
try:
|
| 465 |
+
from db import repositories as repo
|
| 466 |
+
repo.update_period_gen_status(
|
| 467 |
+
period_id, "generating",
|
| 468 |
+
"phase:2,progress:0,solutions:0,elapsed:0,obj:-1"
|
| 469 |
+
)
|
| 470 |
+
except Exception:
|
| 471 |
+
pass
|
| 472 |
+
|
| 473 |
+
|
| 474 |
def _extract_assignments(solver, assign, active_employees, date_strs, slots, positions) -> list[ShiftAssignment]:
|
| 475 |
result = []
|
| 476 |
for emp in active_employees:
|
|
|
|
| 523 |
for emp in active_employees:
|
| 524 |
for ds in date_strs:
|
| 525 |
for slot in slots:
|
| 526 |
+
if slot == TimeSlot.BREAKFAST and emp.always_available_breakfast:
|
| 527 |
+
can_work = ds not in emp.fixed_unavailable_dates
|
| 528 |
+
elif slot == TimeSlot.DINNER and emp.always_available_dinner:
|
| 529 |
+
can_work = ds not in emp.fixed_unavailable_dates
|
| 530 |
+
else:
|
| 531 |
+
can_work = req_map.get((emp.id, ds, slot.value), False)
|
| 532 |
for pos in positions:
|
| 533 |
if not can_work:
|
| 534 |
model.add(assign[emp.id][ds][slot.value][pos.value] == 0)
|
|
|
|
| 657 |
|
| 658 |
for ds in date_strs:
|
| 659 |
d = date.fromisoformat(ds)
|
| 660 |
+
dow_labels = ["ๆ", "็ซ", "ๆฐด", "ๆจ", "้", "ๅ", "ๆฅ"]
|
| 661 |
label = f"{d.month}/{d.day}({dow_labels[d.weekday()]})"
|
| 662 |
|
| 663 |
for slot in TimeSlot:
|
routes/generate.py
CHANGED
|
@@ -53,7 +53,7 @@ def run():
|
|
| 53 |
with app.app_context():
|
| 54 |
try:
|
| 55 |
cb = SolveProgressCallback(period_id, max_time=25.0)
|
| 56 |
-
result = solve(period, employees, requests_list, config, progress_callback=cb)
|
| 57 |
if result.status in ("optimal", "feasible"):
|
| 58 |
repo.save_assignments(period_id, result.assignments)
|
| 59 |
msg = f"{result.status},{result.solve_time_sec:.1f}"
|
|
@@ -85,8 +85,12 @@ def status(period_id):
|
|
| 85 |
msg = gen.get("message", "")
|
| 86 |
elapsed = 0.0
|
| 87 |
solutions = 0
|
| 88 |
-
|
|
|
|
|
|
|
| 89 |
parts = dict(kv.split(":") for kv in msg.split(",") if ":" in kv)
|
| 90 |
elapsed = float(parts.get("elapsed", 0))
|
| 91 |
solutions = int(parts.get("solutions", 0))
|
| 92 |
-
|
|
|
|
|
|
|
|
|
| 53 |
with app.app_context():
|
| 54 |
try:
|
| 55 |
cb = SolveProgressCallback(period_id, max_time=25.0)
|
| 56 |
+
result = solve(period, employees, requests_list, config, progress_callback=cb, period_id=period_id)
|
| 57 |
if result.status in ("optimal", "feasible"):
|
| 58 |
repo.save_assignments(period_id, result.assignments)
|
| 59 |
msg = f"{result.status},{result.solve_time_sec:.1f}"
|
|
|
|
| 85 |
msg = gen.get("message", "")
|
| 86 |
elapsed = 0.0
|
| 87 |
solutions = 0
|
| 88 |
+
phase = 1
|
| 89 |
+
obj = -1
|
| 90 |
+
if gen["status"] == "generating" and ("progress:" in msg or "phase:" in msg):
|
| 91 |
parts = dict(kv.split(":") for kv in msg.split(",") if ":" in kv)
|
| 92 |
elapsed = float(parts.get("elapsed", 0))
|
| 93 |
solutions = int(parts.get("solutions", 0))
|
| 94 |
+
phase = int(parts.get("phase", 1))
|
| 95 |
+
obj = int(parts.get("obj", -1))
|
| 96 |
+
return jsonify({**gen, "elapsed": elapsed, "solutions": solutions, "phase": phase, "obj": obj})
|
templates/generate/waiting.html
CHANGED
|
@@ -6,85 +6,215 @@
|
|
| 6 |
</div>
|
| 7 |
|
| 8 |
<div class="content">
|
| 9 |
-
<div class="card mx-auto" style="max-width:
|
| 10 |
<div class="card-body py-4 text-center">
|
| 11 |
<div class="mb-3">
|
| 12 |
-
<i class="bi bi-cpu fs-1 text-primary"></i>
|
| 13 |
</div>
|
| 14 |
<h6 class="mb-1">{{ period.start_date }} ใ {{ period.end_date }}</h6>
|
| 15 |
-
<p class="
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
<!--
|
| 18 |
-
<div class="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
<div id="prog-bar"
|
| 20 |
class="progress-bar progress-bar-striped progress-bar-animated bg-primary"
|
| 21 |
role="progressbar"
|
| 22 |
-
style="width:0%
|
| 23 |
</div>
|
| 24 |
</div>
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
<span id="elapsed-label">ๅพ
ๆฉไธญโฆ</span>
|
| 27 |
<span id="solution-label"></span>
|
| 28 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
</div>
|
| 30 |
</div>
|
| 31 |
</div>
|
| 32 |
|
| 33 |
<script>
|
| 34 |
(function () {
|
| 35 |
-
|
| 36 |
-
const
|
|
|
|
|
|
|
| 37 |
const scheduleUrl = "{{ url_for('schedule.index', period_id=period.id) }}";
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
const
|
| 41 |
-
const
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
}
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
function poll() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
fetch("{{ url_for('generate.status', period_id=period.id) }}")
|
| 50 |
.then(r => r.json())
|
| 51 |
.then(data => {
|
| 52 |
if (data.status === 'done') {
|
| 53 |
-
|
| 54 |
-
bar.classList.remove('progress-bar-animated', 'bg-primary');
|
| 55 |
-
bar.classList.add('bg-success');
|
| 56 |
-
elapsedLabel.textContent = 'ๅฎไบ';
|
| 57 |
-
statusMsg.textContent = '็ๆๅฎไบ๏ผใทใใ่กจใซ็งปๅใใพใโฆ';
|
| 58 |
-
setTimeout(() => { window.location.href = scheduleUrl; }, 800);
|
| 59 |
-
|
| 60 |
} else if (data.status === 'failed') {
|
| 61 |
-
|
| 62 |
-
bar.classList.remove('progress-bar-animated', 'bg-primary');
|
| 63 |
-
bar.classList.add('bg-danger');
|
| 64 |
-
elapsedLabel.textContent = 'ใจใฉใผ';
|
| 65 |
-
statusMsg.textContent = 'ใจใฉใผ: ' + (data.message || '็ๆใซๅคฑๆใใพใใ');
|
| 66 |
-
|
| 67 |
} else {
|
| 68 |
-
|
| 69 |
-
const elapsed = data.elapsed || 0;
|
| 70 |
-
if (elapsed > 0) {
|
| 71 |
-
const pct = Math.min(95, (elapsed / MAX_SEC) * 100);
|
| 72 |
-
setBar(pct, true);
|
| 73 |
-
elapsedLabel.textContent = elapsed.toFixed(1) + '็ง็ต้ / ๆๅคง็ด' + MAX_SEC + '็ง';
|
| 74 |
-
} else {
|
| 75 |
-
// ใพใ ใณใผใซใใใฏๆช็็ซ๏ผๅฏ่ฝ่งฃใใ่ฆใคใใฃใฆใใชใ
|
| 76 |
-
elapsedLabel.textContent = '่งฃใๆข็ดขไธญโฆ';
|
| 77 |
-
}
|
| 78 |
-
|
| 79 |
-
if (data.solutions > 0) {
|
| 80 |
-
solutionLabel.textContent = '่งฃ ' + data.solutions + ' ๅ็บ่ฆ';
|
| 81 |
-
if (data.solutions === 1) {
|
| 82 |
-
statusMsg.textContent = 'ๅๆ่งฃใ็บ่ฆใใใ่ฏใ่งฃใๆข็ดขไธญโฆ';
|
| 83 |
-
} else {
|
| 84 |
-
statusMsg.textContent = '่งฃใๆนๅไธญโฆ๏ผ' + data.solutions + ' ๅๆดๆฐ๏ผ';
|
| 85 |
-
}
|
| 86 |
-
}
|
| 87 |
-
|
| 88 |
setTimeout(poll, 2000);
|
| 89 |
}
|
| 90 |
})
|
|
|
|
| 6 |
</div>
|
| 7 |
|
| 8 |
<div class="content">
|
| 9 |
+
<div class="card mx-auto" style="max-width:520px">
|
| 10 |
<div class="card-body py-4 text-center">
|
| 11 |
<div class="mb-3">
|
| 12 |
+
<i class="bi bi-cpu fs-1 text-primary" id="status-icon"></i>
|
| 13 |
</div>
|
| 14 |
<h6 class="mb-1">{{ period.start_date }} ใ {{ period.end_date }}</h6>
|
| 15 |
+
<p class="mb-3" id="status-msg" style="min-height:1.4em">
|
| 16 |
+
<span class="text-muted small">ใขใใซใๆง็ฏใใฆใใพใโฆ</span>
|
| 17 |
+
</p>
|
| 18 |
|
| 19 |
+
<!-- ใใงใผใบ่กจ็คบ -->
|
| 20 |
+
<div class="d-flex justify-content-between align-items-center small mb-1">
|
| 21 |
+
<span id="phase-label" class="fw-semibold text-primary">
|
| 22 |
+
<i class="bi bi-search me-1"></i>ในใใใ 1/2: ๆ้ฉ่งฃใๆข็ดขไธญ
|
| 23 |
+
</span>
|
| 24 |
+
<span id="pct-label" class="text-muted">0%</span>
|
| 25 |
+
</div>
|
| 26 |
+
|
| 27 |
+
<!-- ใใญใฐใฌในใใผ -->
|
| 28 |
+
<div class="progress mb-2" style="height:14px;border-radius:7px">
|
| 29 |
<div id="prog-bar"
|
| 30 |
class="progress-bar progress-bar-striped progress-bar-animated bg-primary"
|
| 31 |
role="progressbar"
|
| 32 |
+
style="width:0%">
|
| 33 |
</div>
|
| 34 |
</div>
|
| 35 |
+
|
| 36 |
+
<!-- ็ต้ๆ้ & ็บ่ฆๆฐ -->
|
| 37 |
+
<div class="d-flex justify-content-between small text-muted mb-2">
|
| 38 |
<span id="elapsed-label">ๅพ
ๆฉไธญโฆ</span>
|
| 39 |
<span id="solution-label"></span>
|
| 40 |
</div>
|
| 41 |
+
|
| 42 |
+
<!-- ในใณใข่ก๏ผ่งฃใ่ฆใคใใฃใใจใใ ใ่กจ็คบ๏ผ -->
|
| 43 |
+
<div id="score-row" class="small p-2 rounded" style="display:none;background:var(--bs-light,#f8f9fa)">
|
| 44 |
+
<i class="bi bi-graph-down-arrow me-1 text-success"></i>
|
| 45 |
+
ในใณใขๆๅฐๅไธญ: <strong id="score-label">โ</strong>
|
| 46 |
+
<span id="score-hint" class="text-muted ms-1"></span>
|
| 47 |
+
</div>
|
| 48 |
</div>
|
| 49 |
</div>
|
| 50 |
</div>
|
| 51 |
|
| 52 |
<script>
|
| 53 |
(function () {
|
| 54 |
+
// โโ ๅฎๆฐ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 55 |
+
const MAX_SEC_P1 = 25.0; // ใใงใผใบ1ใฎๆๅคง็งๆฐ
|
| 56 |
+
const MAX_SEC_P2 = 15.0; // ใใงใผใบ2ใฎๆๅคง็งๆฐ
|
| 57 |
+
const TIMEOUT_SEC = 90; // ใใใ่ถ
ใใใ็ฐๅธธใจใฟใชใ
|
| 58 |
const scheduleUrl = "{{ url_for('schedule.index', period_id=period.id) }}";
|
| 59 |
+
|
| 60 |
+
// โโ DOM โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 61 |
+
const bar = document.getElementById('prog-bar');
|
| 62 |
+
const pctLabel = document.getElementById('pct-label');
|
| 63 |
+
const phaseLabel = document.getElementById('phase-label');
|
| 64 |
+
const elapsedLabel= document.getElementById('elapsed-label');
|
| 65 |
+
const solutionLabel=document.getElementById('solution-label');
|
| 66 |
+
const statusMsg = document.getElementById('status-msg');
|
| 67 |
+
const scoreRow = document.getElementById('score-row');
|
| 68 |
+
const scoreLabel = document.getElementById('score-label');
|
| 69 |
+
const scoreHint = document.getElementById('score-hint');
|
| 70 |
+
|
| 71 |
+
// โโ ็ถๆ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 72 |
+
const pageStart = Date.now(); // ใใผใธ่ชญใฟ่พผใฟๅบ็น
|
| 73 |
+
let phase2Start = null; // ใใงใผใบ2ใซๅ
ฅใฃใ wall-clock
|
| 74 |
+
let lastServerElapsed = 0; // ๆๅพใซใตใผใใผใใๅใๅใฃใ elapsed
|
| 75 |
+
let lastServerAt = Date.now(); // ใใฎใจใใฎใฏใฉใคใขใณใๆๅป
|
| 76 |
+
let currentPhase = 1;
|
| 77 |
+
let prevObj = null;
|
| 78 |
+
let rafId = null;
|
| 79 |
+
|
| 80 |
+
// โโ ใใผๆ็ป โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 81 |
+
function setBar(pct) {
|
| 82 |
+
const p = Math.min(100, Math.max(0, pct));
|
| 83 |
+
bar.style.width = p + '%';
|
| 84 |
+
pctLabel.textContent = Math.round(p) + '%';
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
// โโ ใฏใฉใคใขใณใๅดใขใใกใผใทใงใณ๏ผ60fps๏ผ โโโโโโ๏ฟฝ๏ฟฝ๏ฟฝโโโโโโโโโโโโโโโโโโโโโโโ
|
| 88 |
+
function tick() {
|
| 89 |
+
const nowMs = Date.now();
|
| 90 |
+
|
| 91 |
+
if (currentPhase === 1) {
|
| 92 |
+
// ใตใผใใผ elapsed + ใฏใฉใคใขใณใ่ฃ้
|
| 93 |
+
const clientDelta = (nowMs - lastServerAt) / 1000;
|
| 94 |
+
const estimated = lastServerElapsed + clientDelta;
|
| 95 |
+
setBar(Math.min(95, (estimated / MAX_SEC_P1) * 100));
|
| 96 |
+
elapsedLabel.textContent = estimated.toFixed(1) + '็ง็ต้ / ๆๅคง' + MAX_SEC_P1 + '็ง';
|
| 97 |
+
} else {
|
| 98 |
+
// ใใงใผใบ2: phase2Start ใใใฎ็ต้
|
| 99 |
+
if (phase2Start !== null) {
|
| 100 |
+
const p2elapsed = (nowMs - phase2Start) / 1000;
|
| 101 |
+
setBar(Math.min(95, (p2elapsed / MAX_SEC_P2) * 100));
|
| 102 |
+
elapsedLabel.textContent = p2elapsed.toFixed(1) + '็ง็ต้ / ๆๅคง' + MAX_SEC_P2 + '็ง';
|
| 103 |
+
}
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
rafId = requestAnimationFrame(tick);
|
| 107 |
+
}
|
| 108 |
+
rafId = requestAnimationFrame(tick);
|
| 109 |
+
|
| 110 |
+
// โโ ใตใผใใผๆดๆฐใๅๆ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 111 |
+
function applyGenerating(data) {
|
| 112 |
+
const newPhase = data.phase || 1;
|
| 113 |
+
|
| 114 |
+
if (newPhase === 2 && currentPhase === 1) {
|
| 115 |
+
// ใใงใผใบๅใๆฟใ
|
| 116 |
+
currentPhase = 2;
|
| 117 |
+
phase2Start = Date.now();
|
| 118 |
+
phaseLabel.innerHTML =
|
| 119 |
+
'<i class="bi bi-arrow-repeat me-1"></i>ในใใใ 2/2: ใในใใจใใฉใผใใงๅๆง็ฏไธญ';
|
| 120 |
+
phaseLabel.classList.replace('text-primary', 'text-warning');
|
| 121 |
+
bar.classList.replace('bg-primary', 'bg-warning');
|
| 122 |
+
statusMsg.innerHTML =
|
| 123 |
+
'<span class="text-warning small">ๅถ็ดใ็ทฉๅใใฆๅฎ่กๅฏ่ฝใชใทใใใ็ๆใใฆใใพใโฆ</span>';
|
| 124 |
+
solutionLabel.textContent = '';
|
| 125 |
+
scoreRow.style.display = 'none';
|
| 126 |
+
return;
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
if (newPhase === 1) {
|
| 130 |
+
lastServerElapsed = data.elapsed || 0;
|
| 131 |
+
lastServerAt = Date.now();
|
| 132 |
+
|
| 133 |
+
const n = data.solutions || 0;
|
| 134 |
+
if (n > 0) {
|
| 135 |
+
solutionLabel.textContent = '่งฃ ' + n + ' ไปถ็บ่ฆ';
|
| 136 |
+
if (n === 1) {
|
| 137 |
+
statusMsg.innerHTML =
|
| 138 |
+
'<span class="text-primary small fw-semibold">ๅๆ่งฃใ็บ่ฆ โ ใใ่ฏใ่งฃใๆข็ดขไธญโฆ</span>';
|
| 139 |
+
} else {
|
| 140 |
+
statusMsg.innerHTML =
|
| 141 |
+
'<span class="text-primary small fw-semibold">่งฃใๆนๅไธญ๏ผ' + n + ' ๅๆดๆฐ๏ผ</span>';
|
| 142 |
+
}
|
| 143 |
+
} else {
|
| 144 |
+
statusMsg.innerHTML =
|
| 145 |
+
'<span class="text-muted small">ๅถ็ดใๆบใใๅๆ่งฃใๆข็ดขไธญโฆ</span>';
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
// ในใณใข่กจ็คบ
|
| 149 |
+
const obj = data.obj;
|
| 150 |
+
if (obj !== undefined && obj >= 0) {
|
| 151 |
+
scoreRow.style.display = '';
|
| 152 |
+
scoreLabel.textContent = obj.toLocaleString();
|
| 153 |
+
if (prevObj !== null && obj < prevObj) {
|
| 154 |
+
const diff = prevObj - obj;
|
| 155 |
+
scoreHint.textContent = '(โผ' + diff.toLocaleString() + ' ๆนๅ)';
|
| 156 |
+
} else {
|
| 157 |
+
scoreHint.textContent = '';
|
| 158 |
+
}
|
| 159 |
+
prevObj = obj;
|
| 160 |
+
}
|
| 161 |
+
}
|
| 162 |
}
|
| 163 |
|
| 164 |
+
// โโ ๅฎไบใปใจใฉใผๅฆ็ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 165 |
+
function finish(ok, msg) {
|
| 166 |
+
cancelAnimationFrame(rafId);
|
| 167 |
+
setBar(100);
|
| 168 |
+
bar.classList.remove('progress-bar-animated', 'progress-bar-striped',
|
| 169 |
+
'bg-primary', 'bg-warning');
|
| 170 |
+
bar.style.transition = 'width 0.5s ease-out';
|
| 171 |
+
if (ok) {
|
| 172 |
+
bar.classList.add('bg-success');
|
| 173 |
+
phaseLabel.innerHTML = '<i class="bi bi-check-circle me-1"></i>็ๆๅฎไบ';
|
| 174 |
+
phaseLabel.classList.remove('text-primary', 'text-warning');
|
| 175 |
+
phaseLabel.classList.add('text-success');
|
| 176 |
+
elapsedLabel.textContent = 'ๅฎไบ';
|
| 177 |
+
statusMsg.innerHTML =
|
| 178 |
+
'<span class="text-success fw-semibold">ใทใใ่กจใซ็งปๅใใพใโฆ</span>';
|
| 179 |
+
setTimeout(() => { window.location.href = scheduleUrl; }, 800);
|
| 180 |
+
} else {
|
| 181 |
+
bar.classList.add('bg-danger');
|
| 182 |
+
phaseLabel.innerHTML = '<i class="bi bi-exclamation-triangle me-1"></i>็ๆๅคฑๆ';
|
| 183 |
+
phaseLabel.classList.remove('text-primary', 'text-warning');
|
| 184 |
+
phaseLabel.classList.add('text-danger');
|
| 185 |
+
elapsedLabel.textContent = 'ใจใฉใผ';
|
| 186 |
+
statusMsg.innerHTML =
|
| 187 |
+
'<span class="text-danger small">' + (msg || '็ๆใซๅคฑๆใใพใใ') + '</span>';
|
| 188 |
+
}
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
+
function showTimeout() {
|
| 192 |
+
cancelAnimationFrame(rafId);
|
| 193 |
+
bar.classList.remove('progress-bar-animated', 'bg-primary', 'bg-warning');
|
| 194 |
+
bar.classList.add('bg-secondary');
|
| 195 |
+
phaseLabel.innerHTML = '<i class="bi bi-clock-history me-1"></i>ๅฟ็ญใชใ';
|
| 196 |
+
phaseLabel.className = 'fw-semibold text-secondary';
|
| 197 |
+
elapsedLabel.textContent = 'ใฟใคใ ใขใฆใ';
|
| 198 |
+
statusMsg.innerHTML =
|
| 199 |
+
'<span class="text-muted small">ใใผใธใๅ่ชญใฟ่พผใฟใใฆๅๅบฆใ่ฉฆใใใ ใใใ</span>';
|
| 200 |
+
}
|
| 201 |
+
|
| 202 |
+
// โโ ใใผใชใณใฐ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ๏ฟฝ๏ฟฝ๏ฟฝโโโโโโโโโโโโโโโโโโโโโ
|
| 203 |
function poll() {
|
| 204 |
+
if ((Date.now() - pageStart) / 1000 > TIMEOUT_SEC) {
|
| 205 |
+
showTimeout();
|
| 206 |
+
return;
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
fetch("{{ url_for('generate.status', period_id=period.id) }}")
|
| 210 |
.then(r => r.json())
|
| 211 |
.then(data => {
|
| 212 |
if (data.status === 'done') {
|
| 213 |
+
finish(true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
} else if (data.status === 'failed') {
|
| 215 |
+
finish(false, data.message);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
} else {
|
| 217 |
+
applyGenerating(data);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
setTimeout(poll, 2000);
|
| 219 |
}
|
| 220 |
})
|
web_app.py
CHANGED
|
@@ -17,6 +17,18 @@ def create_app():
|
|
| 17 |
|
| 18 |
initialize_db()
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
from routes.employees import bp as emp_bp
|
| 21 |
from routes.shifts import bp as shifts_bp
|
| 22 |
from routes.generate import bp as gen_bp
|
|
|
|
| 17 |
|
| 18 |
initialize_db()
|
| 19 |
|
| 20 |
+
# ใฏใฉใใทใฅ/ใชใญใผใๅพใซ "generating" ใฎใพใพในใฟใใฏใใ็ถๆ
ใใชใปใใ
|
| 21 |
+
try:
|
| 22 |
+
from db.database import Connection as _Conn
|
| 23 |
+
_c = _Conn()
|
| 24 |
+
_c.execute(
|
| 25 |
+
"UPDATE schedule_periods SET gen_status='idle', gen_message='' WHERE gen_status='generating'"
|
| 26 |
+
)
|
| 27 |
+
_c.commit()
|
| 28 |
+
_c.close()
|
| 29 |
+
except Exception:
|
| 30 |
+
pass
|
| 31 |
+
|
| 32 |
from routes.employees import bp as emp_bp
|
| 33 |
from routes.shifts import bp as shifts_bp
|
| 34 |
from routes.generate import bp as gen_bp
|