Commit ·
2d01f48
1
Parent(s): 39db4c7
waiting: ログ表示をSSEストリーミングに変更
Browse filessetInterval(3秒) → EventSource(/generate/log/stream) に切り替え。
サーバー側で0.25秒ごとにログファイルの差分を読み取り、
新行をSSEで即座にプッシュ。クライアントは1行ずつ追記して自動スクロール。
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- routes/generate.py +45 -1
- templates/generate/waiting.html +20 -20
routes/generate.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
import logging
|
|
|
|
| 2 |
import threading
|
| 3 |
-
|
|
|
|
| 4 |
from db import repositories as repo
|
| 5 |
from optimizer.solver import solve, SolverConfig, PRIORITY_SCALE, SolveProgressCallback
|
| 6 |
from utils.solver_logger import logger, log_path
|
|
@@ -169,6 +171,48 @@ def view_log():
|
|
| 169 |
return jsonify({"error": str(e)}), 500
|
| 170 |
|
| 171 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
@bp.get("/diag/<int:period_id>")
|
| 173 |
def diag(period_id):
|
| 174 |
"""シフト入力データの診断(DBの実データを確認)"""
|
|
|
|
| 1 |
import logging
|
| 2 |
+
import os
|
| 3 |
import threading
|
| 4 |
+
import time
|
| 5 |
+
from flask import Blueprint, render_template, request, redirect, url_for, flash, current_app, jsonify, make_response, Response, stream_with_context
|
| 6 |
from db import repositories as repo
|
| 7 |
from optimizer.solver import solve, SolverConfig, PRIORITY_SCALE, SolveProgressCallback
|
| 8 |
from utils.solver_logger import logger, log_path
|
|
|
|
| 171 |
return jsonify({"error": str(e)}), 500
|
| 172 |
|
| 173 |
|
| 174 |
+
@bp.get("/log/stream")
|
| 175 |
+
def log_stream():
|
| 176 |
+
"""ソルバーログをSSEでリアルタイムストリーミング(生成中画面用)"""
|
| 177 |
+
def generate():
|
| 178 |
+
path = log_path()
|
| 179 |
+
last_pos = 0
|
| 180 |
+
|
| 181 |
+
# まず既存ログ全行を送信
|
| 182 |
+
if os.path.exists(path):
|
| 183 |
+
with open(path, encoding="utf-8", errors="replace") as f:
|
| 184 |
+
content = f.read()
|
| 185 |
+
last_pos = f.tell()
|
| 186 |
+
for line in content.splitlines():
|
| 187 |
+
if line.strip():
|
| 188 |
+
yield f"data: {line}\n\n"
|
| 189 |
+
|
| 190 |
+
# 以降は差分のみ(最大480回 × 0.25秒 ≒ 2分)
|
| 191 |
+
for _ in range(480):
|
| 192 |
+
time.sleep(0.25)
|
| 193 |
+
try:
|
| 194 |
+
cur_size = os.path.getsize(path)
|
| 195 |
+
except OSError:
|
| 196 |
+
yield ": hb\n\n"
|
| 197 |
+
continue
|
| 198 |
+
if cur_size > last_pos:
|
| 199 |
+
with open(path, encoding="utf-8", errors="replace") as f:
|
| 200 |
+
f.seek(last_pos)
|
| 201 |
+
chunk = f.read()
|
| 202 |
+
last_pos = f.tell()
|
| 203 |
+
for line in chunk.splitlines():
|
| 204 |
+
if line.strip():
|
| 205 |
+
yield f"data: {line}\n\n"
|
| 206 |
+
else:
|
| 207 |
+
yield ": hb\n\n"
|
| 208 |
+
|
| 209 |
+
return Response(
|
| 210 |
+
stream_with_context(generate()),
|
| 211 |
+
mimetype="text/event-stream",
|
| 212 |
+
headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"},
|
| 213 |
+
)
|
| 214 |
+
|
| 215 |
+
|
| 216 |
@bp.get("/diag/<int:period_id>")
|
| 217 |
def diag(period_id):
|
| 218 |
"""シフト入力データの診断(DBの実データを確認)"""
|
templates/generate/waiting.html
CHANGED
|
@@ -89,7 +89,7 @@
|
|
| 89 |
const scoreLabel = document.getElementById('score-label');
|
| 90 |
const scoreHint = document.getElementById('score-hint');
|
| 91 |
|
| 92 |
-
const logUrl = "{{ url_for('generate.
|
| 93 |
|
| 94 |
// ── 状態 ──────────────────────────────────────────────────────────────
|
| 95 |
const pageStart = Date.now(); // ページ読み込み基点
|
|
@@ -100,7 +100,6 @@
|
|
| 100 |
let prevObj = null;
|
| 101 |
let rafId = null;
|
| 102 |
let logVisible = true;
|
| 103 |
-
let logTimer = null;
|
| 104 |
|
| 105 |
// ── バー描画 ──────────────────────────────────────────────────────────
|
| 106 |
function setBar(pct) {
|
|
@@ -190,7 +189,6 @@
|
|
| 190 |
// ok=true のとき warnings は文字列配列、ok=false のとき errMsg はエラー文字列
|
| 191 |
function finish(ok, warnOrErr) {
|
| 192 |
stopLogPolling();
|
| 193 |
-
fetchLog(); // 完了時に最終ログを取得
|
| 194 |
cancelAnimationFrame(rafId);
|
| 195 |
setBar(100);
|
| 196 |
bar.classList.remove('progress-bar-animated', 'progress-bar-striped',
|
|
@@ -230,26 +228,28 @@
|
|
| 230 |
}
|
| 231 |
}
|
| 232 |
|
| 233 |
-
// ── ログパネル ───────────────────────────────────
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
pre.textContent = data.log || '(ログなし)';
|
| 241 |
-
pre.scrollTop = pre.scrollHeight;
|
| 242 |
-
})
|
| 243 |
-
.catch(() => {});
|
| 244 |
-
}
|
| 245 |
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
}
|
| 250 |
|
| 251 |
function stopLogPolling() {
|
| 252 |
-
if (
|
| 253 |
}
|
| 254 |
|
| 255 |
function toggleLog() {
|
|
@@ -294,7 +294,7 @@
|
|
| 294 |
.catch(() => setTimeout(poll, 3000));
|
| 295 |
}
|
| 296 |
|
| 297 |
-
|
| 298 |
setTimeout(poll, 1500);
|
| 299 |
})();
|
| 300 |
</script>
|
|
|
|
| 89 |
const scoreLabel = document.getElementById('score-label');
|
| 90 |
const scoreHint = document.getElementById('score-hint');
|
| 91 |
|
| 92 |
+
const logUrl = "{{ url_for('generate.log_stream') }}";
|
| 93 |
|
| 94 |
// ── 状態 ──────────────────────────────────────────────────────────────
|
| 95 |
const pageStart = Date.now(); // ページ読み込み基点
|
|
|
|
| 100 |
let prevObj = null;
|
| 101 |
let rafId = null;
|
| 102 |
let logVisible = true;
|
|
|
|
| 103 |
|
| 104 |
// ── バー描画 ──────────────────────────────────────────────────────────
|
| 105 |
function setBar(pct) {
|
|
|
|
| 189 |
// ok=true のとき warnings は文字列配列、ok=false のとき errMsg はエラー文字列
|
| 190 |
function finish(ok, warnOrErr) {
|
| 191 |
stopLogPolling();
|
|
|
|
| 192 |
cancelAnimationFrame(rafId);
|
| 193 |
setBar(100);
|
| 194 |
bar.classList.remove('progress-bar-animated', 'progress-bar-striped',
|
|
|
|
| 228 |
}
|
| 229 |
}
|
| 230 |
|
| 231 |
+
// ── ログパネル(SSEストリーミング) ───────────────────────────────────
|
| 232 |
+
let evtSource = null;
|
| 233 |
+
|
| 234 |
+
function startLogStream() {
|
| 235 |
+
const pre = document.getElementById('log-pre');
|
| 236 |
+
if (!pre) return;
|
| 237 |
+
pre.textContent = '';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
|
| 239 |
+
evtSource = new EventSource(logUrl);
|
| 240 |
+
evtSource.onmessage = function(e) {
|
| 241 |
+
const line = document.createTextNode(e.data + '\n');
|
| 242 |
+
pre.appendChild(line);
|
| 243 |
+
pre.scrollTop = pre.scrollHeight;
|
| 244 |
+
};
|
| 245 |
+
evtSource.onerror = function() {
|
| 246 |
+
// SSE切断時は静かに閉じる
|
| 247 |
+
if (evtSource) { evtSource.close(); evtSource = null; }
|
| 248 |
+
};
|
| 249 |
}
|
| 250 |
|
| 251 |
function stopLogPolling() {
|
| 252 |
+
if (evtSource) { evtSource.close(); evtSource = null; }
|
| 253 |
}
|
| 254 |
|
| 255 |
function toggleLog() {
|
|
|
|
| 294 |
.catch(() => setTimeout(poll, 3000));
|
| 295 |
}
|
| 296 |
|
| 297 |
+
startLogStream();
|
| 298 |
setTimeout(poll, 1500);
|
| 299 |
})();
|
| 300 |
</script>
|