🚀 Auto-deploy: BrainKP atualizado em 22/06/2026 19:15:36
This commit is contained in:
@@ -40,37 +40,103 @@ def get_backup_log():
|
|||||||
if not os.path.exists(LOG_FILE):
|
if not os.path.exists(LOG_FILE):
|
||||||
return {"last_run": None, "entries": []}
|
return {"last_run": None, "entries": []}
|
||||||
|
|
||||||
with open(LOG_FILE, "r") as f:
|
try:
|
||||||
lines = f.readlines()
|
with open(LOG_FILE, "r") as f:
|
||||||
|
lines = f.readlines()
|
||||||
|
except Exception:
|
||||||
|
return {"last_run": None, "entries": []}
|
||||||
|
|
||||||
entries = []
|
import re
|
||||||
current = {}
|
from datetime import datetime
|
||||||
|
|
||||||
for line in lines[-500:]: # last 500 lines
|
pattern = re.compile(r"^\[(.*?)\]\s+(Backup\s+[\w\s-]+?)\s+(started|finished)$")
|
||||||
if "==========" in line:
|
events = []
|
||||||
if current:
|
active_runs = {}
|
||||||
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()
|
|
||||||
|
|
||||||
if current:
|
for line in lines[-1000:]:
|
||||||
entries.append(current)
|
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 {
|
return {
|
||||||
"last_run": entries[0] if entries else None,
|
"last_run": sessions[0] if sessions else None,
|
||||||
"entries": entries[:10]
|
"entries": sessions[:10]
|
||||||
}
|
}
|
||||||
|
|
||||||
def list_rclone_contents(remote, path="", depth=1):
|
def list_rclone_contents(remote, path="", depth=1):
|
||||||
|
|||||||
Reference in New Issue
Block a user