From b55b25d07b8d1c48a93fb48f537ffcba8362cc48 Mon Sep 17 00:00:00 2001 From: admtracksteel Date: Mon, 22 Jun 2026 19:15:36 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=20Auto-deploy:=20BrainKP=20atualiz?= =?UTF-8?q?ado=20em=2022/06/2026=2019:15:36?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 118 ++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 92 insertions(+), 26 deletions(-) diff --git a/app.py b/app.py index 09697ec..08cd93a 100644 --- a/app.py +++ b/app.py @@ -40,37 +40,103 @@ def get_backup_log(): if not os.path.exists(LOG_FILE): return {"last_run": None, "entries": []} - with open(LOG_FILE, "r") as f: - lines = f.readlines() + try: + with open(LOG_FILE, "r") as f: + lines = f.readlines() + except Exception: + return {"last_run": None, "entries": []} - entries = [] - current = {} + import re + from datetime import datetime - for line in lines[-500:]: # last 500 lines - if "==========" in line: - if current: - entries.append(current) - ts = line.strip().split("==========")[1].strip() - current = {"timestamp": ts, "scripts": []} - elif "[Backup" in line and "started" in line: - script = line.strip().split("] ")[1].split(" started")[0] - current["scripts"].append({"name": script, "status": "running"}) - elif "[Backup" in line and "finished" in line: - script = line.strip().split("] ")[1].split(" finished")[0] - for s in current.get("scripts", []): - if s["name"] == script: - s["status"] = "ok" - elif "error" in line.lower() or "failed" in line.lower(): - current["has_error"] = True - current["error"] = line.strip() + pattern = re.compile(r"^\[(.*?)\]\s+(Backup\s+[\w\s-]+?)\s+(started|finished)$") + events = [] + active_runs = {} - if current: - entries.append(current) + for line in lines[-1000:]: + line_str = line.strip() + match = pattern.match(line_str) + if match: + ts, name, action = match.groups() + events.append({ + "ts": ts, + "name": name, + "action": action, + "has_error": False, + "error_msg": None + }) + idx = len(events) - 1 + if action == "started": + active_runs[name] = idx + elif action == "finished": + if name in active_runs: + del active_runs[name] + else: + if "error" in line_str.lower() or "failed" in line_str.lower(): + for name, idx in list(active_runs.items()): + events[idx]["has_error"] = True + events[idx]["error_msg"] = line_str + + if not events: + return {"last_run": None, "entries": []} + + sessions = [] + current_session = None + + def parse_ts(ts_str): + try: + cleaned = ts_str.replace("UTC ", "") + cleaned = " ".join(cleaned.split()) + return datetime.strptime(cleaned, "%a %b %d %H:%M:%S %Y") + except: + return None + + for ev in events: + ev_time = parse_ts(ev["ts"]) + is_new_session = False + if not current_session: + is_new_session = True + else: + sess_time = parse_ts(current_session["timestamp"]) + if ev_time and sess_time: + diff = (ev_time - sess_time).total_seconds() + if diff > 900: + is_new_session = True + else: + is_new_session = True + + if is_new_session: + if current_session: + sessions.append(current_session) + current_session = { + "timestamp": ev["ts"], + "scripts": [], + "has_error": False + } + + script_entry = next((s for s in current_session["scripts"] if s["name"] == ev["name"]), None) + if not script_entry: + script_entry = {"name": ev["name"], "status": "running"} + current_session["scripts"].append(script_entry) + + if ev["action"] == "finished": + script_entry["status"] = "ok" + elif ev["action"] == "started": + script_entry["status"] = "running" + + if ev["has_error"]: + script_entry["status"] = "error" + current_session["has_error"] = True + current_session["error"] = ev["error_msg"] + + if current_session: + sessions.append(current_session) + + sessions.reverse() - entries.reverse() return { - "last_run": entries[0] if entries else None, - "entries": entries[:10] + "last_run": sessions[0] if sessions else None, + "entries": sessions[:10] } def list_rclone_contents(remote, path="", depth=1):