dev #1
@@ -29,11 +29,18 @@
|
|||||||
</div>
|
</div>
|
||||||
<script>
|
<script>
|
||||||
document.addEventListener("DOMContentLoaded", () => {
|
document.addEventListener("DOMContentLoaded", () => {
|
||||||
const intervalMinutes = {{ interval | int }};
|
const intervalMinutes = {{ interval | int }}; // aus DB
|
||||||
const intervalMilliseconds = intervalMinutes * 60 * 1000;
|
const intervalMilliseconds = intervalMinutes * 60 * 1000;
|
||||||
let lastUpdateTime = Date.now(); // optional: vom letzten Refresh / Log-Eintrag
|
let lastUpdateTime = Date.now();
|
||||||
|
|
||||||
function updateDashboardTimer() {
|
function parseLogTimestamp(ts) {
|
||||||
|
const parts = ts.match(/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/);
|
||||||
|
if (!parts) return Date.now();
|
||||||
|
const [, year, month, day, hour, minute, second] = parts.map(Number);
|
||||||
|
return new Date(year, month - 1, day, hour, minute, second).getTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateTimer() {
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
const elapsed = now - lastUpdateTime;
|
const elapsed = now - lastUpdateTime;
|
||||||
const remainingMs = intervalMilliseconds - (elapsed % intervalMilliseconds);
|
const remainingMs = intervalMilliseconds - (elapsed % intervalMilliseconds);
|
||||||
@@ -41,13 +48,33 @@ document.addEventListener("DOMContentLoaded", () => {
|
|||||||
document.getElementById("dashboard-timer").innerText = remainingSec + " s";
|
document.getElementById("dashboard-timer").innerText = remainingSec + " s";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Optional: reset timer beim manuellen Refresh
|
function fetchLastLog() {
|
||||||
// lastUpdateTime = Date.now();
|
fetch("{{ url_for('get_log') }}")
|
||||||
|
.then(response => response.text())
|
||||||
|
.then(data => {
|
||||||
|
const lines = data.split("\n").filter(line => !line.includes("ist erreichbar!"));
|
||||||
|
let lastSepIndex = -1;
|
||||||
|
for (let i = lines.length - 1; i >= 0; i--) {
|
||||||
|
if (lines[i].startsWith("--------------------------------------------------------------------")) {
|
||||||
|
lastSepIndex = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (lastSepIndex >= 0 && lastSepIndex + 1 < lines.length) {
|
||||||
|
const firstLine = lines[lastSepIndex + 1];
|
||||||
|
const match = firstLine.match(/^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})/);
|
||||||
|
if (match) lastUpdateTime = parseLogTimestamp(match[1]);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(err => console.error("Fehler beim Laden der Logs:", err));
|
||||||
|
}
|
||||||
|
|
||||||
setInterval(updateDashboardTimer, 1000);
|
// Timer aktualisieren jede Sekunde
|
||||||
updateDashboardTimer(); // direkt beim Laden
|
setInterval(updateTimer, 1000);
|
||||||
|
|
||||||
|
// einmal beim Laden die letzte Log-Zeit setzen
|
||||||
|
fetchLastLog();
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|||||||
Reference in New Issue
Block a user